Skip to content

Commit

Permalink
add trim effect tooltips (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bawnorton committed Aug 13, 2023
1 parent 5384972 commit fc0804a
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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<Text> tooltip, CallbackInfo ci, @Local ArmorTrim trim) {
ArmorTrimEffects.forEachEffect(stack, effect -> tooltip.add(ScreenTexts.space().append(effect.getTooltip().copy().fillStyle(trim.getMaterial().value().description().getStyle()))));
}
}
1 change: 1 addition & 0 deletions src/client/resources/bettertrims.client.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
"defaultRequire": 1
},
"mixins": [
"ArmorTrimMixin"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -47,6 +50,10 @@ public void apply(Iterable<ItemStack> armour, Effect effect) {
}
}

public Text getTooltip() {
return tooltip;
}

@FunctionalInterface
public interface Effect {
void applyEffect(ItemStack stack);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ArmorTrimEffect> 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);
Expand All @@ -28,6 +35,20 @@ private static ArmorTrimEffect of(RegistryKey<ArmorTrimMaterial> 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<ArmorTrimEffect> effectConsumer) {
for(ArmorTrimEffect effect : EFFECTS) {
if(effect.appliesTo(stack)) {
effectConsumer.accept(effect);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Pattern> compiledPatterns = new HashMap<>();

public boolean matches(@Nullable Identifier other) {
if (other == null) return false;
return matches(other.getNamespace(), other.getPath());
Expand All @@ -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();
}
}
14 changes: 14 additions & 0 deletions src/main/resources/assets/bettertrims/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit fc0804a

Please sign in to comment.