diff --git a/src/client/java/com/bawnorton/bettertrims/mixin/client/ArmorTrimMixin.java b/src/client/java/com/bawnorton/bettertrims/mixin/client/ArmorTrimMixin.java new file mode 100644 index 0000000..16d3794 --- /dev/null +++ b/src/client/java/com/bawnorton/bettertrims/mixin/client/ArmorTrimMixin.java @@ -0,0 +1,23 @@ +package com.bawnorton.bettertrims.mixin.client; + +import com.bawnorton.bettertrims.effect.ArmorTrimEffects; +import com.llamalad7.mixinextras.sugar.Local; +import net.minecraft.item.ItemStack; +import net.minecraft.item.trim.ArmorTrim; +import net.minecraft.registry.DynamicRegistryManager; +import net.minecraft.screen.ScreenTexts; +import net.minecraft.text.Text; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import java.util.List; + +@Mixin(ArmorTrim.class) +public abstract class ArmorTrimMixin { + @Inject(method = "appendTooltip", at = @At(value = "INVOKE", target = "Ljava/util/List;add(Ljava/lang/Object;)Z", ordinal = 2, shift = At.Shift.AFTER)) + private static void addEffectTooltip(ItemStack stack, DynamicRegistryManager registryManager, List tooltip, CallbackInfo ci, @Local ArmorTrim trim) { + ArmorTrimEffects.forEachEffect(stack, effect -> tooltip.add(ScreenTexts.space().append(effect.getTooltip().copy().fillStyle(trim.getMaterial().value().description().getStyle())))); + } +} diff --git a/src/client/resources/bettertrims.client.mixins.json b/src/client/resources/bettertrims.client.mixins.json index 4bfc0f3..6d09b1a 100644 --- a/src/client/resources/bettertrims.client.mixins.json +++ b/src/client/resources/bettertrims.client.mixins.json @@ -10,5 +10,6 @@ "defaultRequire": 1 }, "mixins": [ + "ArmorTrimMixin" ] } \ No newline at end of file diff --git a/src/main/java/com/bawnorton/bettertrims/effect/ArmorTrimEffect.java b/src/main/java/com/bawnorton/bettertrims/effect/ArmorTrimEffect.java index c855a00..4f149a9 100644 --- a/src/main/java/com/bawnorton/bettertrims/effect/ArmorTrimEffect.java +++ b/src/main/java/com/bawnorton/bettertrims/effect/ArmorTrimEffect.java @@ -6,15 +6,18 @@ import net.minecraft.item.ItemStack; import net.minecraft.nbt.NbtCompound; import net.minecraft.nbt.NbtElement; +import net.minecraft.text.Text; import net.minecraft.util.Identifier; import java.util.List; public class ArmorTrimEffect { private final RegexIdentifier material; + private final Text tooltip; - public ArmorTrimEffect(RegexIdentifier matieral) { + public ArmorTrimEffect(RegexIdentifier matieral, Text tooltip) { this.material = matieral; + this.tooltip = tooltip; } private Identifier getTrimMaterial(ItemStack stack) { @@ -47,6 +50,10 @@ public void apply(Iterable armour, Effect effect) { } } + public Text getTooltip() { + return tooltip; + } + @FunctionalInterface public interface Effect { void applyEffect(ItemStack stack); diff --git a/src/main/java/com/bawnorton/bettertrims/effect/ArmorTrimEffects.java b/src/main/java/com/bawnorton/bettertrims/effect/ArmorTrimEffects.java index a0c84b5..cddc5a8 100644 --- a/src/main/java/com/bawnorton/bettertrims/effect/ArmorTrimEffects.java +++ b/src/main/java/com/bawnorton/bettertrims/effect/ArmorTrimEffects.java @@ -1,13 +1,20 @@ package com.bawnorton.bettertrims.effect; import com.bawnorton.bettertrims.util.RegexIdentifier; +import net.minecraft.item.ItemStack; import net.minecraft.item.trim.ArmorTrimMaterial; import net.minecraft.item.trim.ArmorTrimMaterials; import net.minecraft.registry.RegistryKey; -import net.minecraft.util.Identifier; +import net.minecraft.text.Text; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; public abstract class ArmorTrimEffects { + private static final List EFFECTS = new ArrayList<>(); + public static final ArmorTrimEffect QUARTZ = of(ArmorTrimMaterials.QUARTZ); public static final ArmorTrimEffect IRON = of(ArmorTrimMaterials.IRON); public static final ArmorTrimEffect NETHERITE = of(ArmorTrimMaterials.NETHERITE); @@ -28,6 +35,20 @@ private static ArmorTrimEffect of(RegistryKey material) { } private static ArmorTrimEffect of(RegexIdentifier material) { - return new ArmorTrimEffect(material); + ArmorTrimEffect effect = new ArmorTrimEffect(material, getTooltip(material.path())); + EFFECTS.add(effect); + return effect; + } + + private static Text getTooltip(String path) { + return Text.translatable("effect.bettertrims." + path + ".tooltip"); + } + + public static void forEachEffect(ItemStack stack, Consumer effectConsumer) { + for(ArmorTrimEffect effect : EFFECTS) { + if(effect.appliesTo(stack)) { + effectConsumer.accept(effect); + } + } } } diff --git a/src/main/java/com/bawnorton/bettertrims/util/RegexIdentifier.java b/src/main/java/com/bawnorton/bettertrims/util/RegexIdentifier.java index 14f3fbb..62266a9 100644 --- a/src/main/java/com/bawnorton/bettertrims/util/RegexIdentifier.java +++ b/src/main/java/com/bawnorton/bettertrims/util/RegexIdentifier.java @@ -3,9 +3,13 @@ import net.minecraft.util.Identifier; import org.jetbrains.annotations.Nullable; +import java.util.HashMap; +import java.util.Map; import java.util.regex.Pattern; public record RegexIdentifier(String namespace, String path) { + private static final Map compiledPatterns = new HashMap<>(); + public boolean matches(@Nullable Identifier other) { if (other == null) return false; return matches(other.getNamespace(), other.getPath()); @@ -23,12 +27,14 @@ private boolean matches(String namespace, String path) { } private boolean matchesNamespace(String namespace) { - Pattern pattern = Pattern.compile(this.namespace()); + compiledPatterns.putIfAbsent(this.namespace(), Pattern.compile(this.namespace())); + Pattern pattern = compiledPatterns.get(this.namespace()); return pattern.matcher(namespace).matches(); } private boolean matchesPath(String path) { - Pattern pattern = Pattern.compile(this.path()); + compiledPatterns.putIfAbsent(this.path(), Pattern.compile(this.path())); + Pattern pattern = compiledPatterns.get(this.path()); return pattern.matcher(path).matches(); } } diff --git a/src/main/resources/assets/bettertrims/lang/en_us.json b/src/main/resources/assets/bettertrims/lang/en_us.json new file mode 100644 index 0000000..a145425 --- /dev/null +++ b/src/main/resources/assets/bettertrims/lang/en_us.json @@ -0,0 +1,14 @@ +{ + "effect.bettertrims.quartz.tooltip": "Gain More Experience", + "effect.bettertrims.iron.tooltip": "Mine Faster", + "effect.bettertrims.netherite.tooltip": "Fire Resistant", + "effect.bettertrims.redstone.tooltip": "Move Faster", + "effect.bettertrims.copper.tooltip": "Swim Faster", + "effect.bettertrims.gold.tooltip": "Piglins Ignore You", + "effect.bettertrims.emerald.tooltip": "Villagers Like You", + "effect.bettertrims.diamond.tooltip": "Harder to Kill", + "effect.bettertrims.lapis.tooltip": "More Enchantable", + "effect.bettertrims.amethyst.tooltip": "Stronger Potion Effects", + "effect.bettertrims.platinum.tooltip": "Illagers Ignore You", + "effect.bettertrims.silver.tooltip": "Stronger at Night" +} \ No newline at end of file