diff --git a/gradle.properties b/gradle.properties index a490159..33e9afe 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,7 +6,7 @@ minecraft_version=1.21.4 yarn_mappings=1.21.4+build.8 loader_version=0.16.10 # Mod Properties -mod_version=1.14.514.003 +mod_version=1.14.514.005 maven_group=org.branulf archives_base_name=Show_Enchants_from_fabrication # Dependencies diff --git a/src/main/java/org/branulf/show_enchants/mixin/DrawContextMixin.java b/src/main/java/org/branulf/show_enchants/mixin/DrawContextMixin.java index adb9acd..09eb6a7 100644 --- a/src/main/java/org/branulf/show_enchants/mixin/DrawContextMixin.java +++ b/src/main/java/org/branulf/show_enchants/mixin/DrawContextMixin.java @@ -6,12 +6,14 @@ import net.minecraft.client.util.math.MatrixStack; import net.minecraft.component.DataComponentTypes; import net.minecraft.component.type.ItemEnchantmentsComponent; import net.minecraft.enchantment.Enchantment; +import net.minecraft.enchantment.Enchantments; import net.minecraft.item.ItemStack; import net.minecraft.item.Items; import net.minecraft.registry.entry.RegistryEntry; import net.minecraft.registry.tag.EnchantmentTags; import net.minecraft.text.Text; import net.minecraft.registry.RegistryKey; +import net.minecraft.util.Identifier; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -40,8 +42,19 @@ public abstract class DrawContextMixin { at = @At("TAIL") ) private void onRenderItemOverlay(TextRenderer renderer, ItemStack stack, int x, int y, String countLabel, CallbackInfo ci) { - if (stack == null || stack.getItem() != Items.ENCHANTED_BOOK) return; + if (stack == null) return; + // 处理附魔书 + if (stack.getItem() == Items.ENCHANTED_BOOK) { + handleEnchantedBook(stack, renderer, x, y); + return; + } + + // 处理武器和工具 + handleWeaponsAndTools(stack, renderer, x, y); + } + + private void handleEnchantedBook(ItemStack stack, TextRenderer renderer, int x, int y) { ItemEnchantmentsComponent enchants = stack.get(DataComponentTypes.STORED_ENCHANTMENTS); if (enchants == null || enchants.isEmpty()) return; @@ -70,6 +83,79 @@ public abstract class DrawContextMixin { } } + private void handleWeaponsAndTools(ItemStack stack, TextRenderer renderer, int x, int y) { + ItemEnchantmentsComponent enchants = stack.get(DataComponentTypes.ENCHANTMENTS); + if (enchants == null || enchants.isEmpty()) return; + + List> enchantments = Lists.newArrayList(enchants.getEnchantments()); + if (enchantments.isEmpty()) return; + + // 优先显示武器的主要附魔 + RegistryEntry primaryEnchant = findPrimaryEnchantment(enchantments); + if (primaryEnchant == null) return; + + String name = primaryEnchant.value().description().getString(); + String firstChar = getFirstVisibleChar(name); + + if (firstChar.isEmpty()) { + firstChar = getFallbackChar(primaryEnchant); + } + + if (!firstChar.isEmpty()) { + matrices.push(); + matrices.translate(0, 0, 200); + + int color = primaryEnchant.isIn(EnchantmentTags.CURSE) ? 0xFFFF5555 : + primaryEnchant.isIn(EnchantmentTags.TREASURE) ? 0xFF55FFFF : 0xFFFF55FF; + + this.drawText(renderer, Text.literal(firstChar), x + 2, y + 9, color, true); + matrices.pop(); + } + } + + private RegistryEntry findPrimaryEnchantment(List> enchantments) { + // 武器 + for (RegistryEntry entry : enchantments) { + Optional> key = entry.getKey(); + if (key.isPresent()) { + Identifier id = key.get().getValue(); + if (id.equals(Enchantments.SHARPNESS.getValue()) || + id.equals(Enchantments.SMITE.getValue()) || + id.equals(Enchantments.BANE_OF_ARTHROPODS.getValue())) { + return entry; + } + } + } + + // 工具 + for (RegistryEntry entry : enchantments) { + Optional> key = entry.getKey(); + if (key.isPresent()) { + Identifier id = key.get().getValue(); + if (id.equals(Enchantments.SILK_TOUCH.getValue()) || + id.equals(Enchantments.FORTUNE.getValue())) { + return entry; + } + } + } + + // 杂七杂八 + for (RegistryEntry entry : enchantments) { + Optional> key = entry.getKey(); + if (key.isPresent()) { + Identifier id = key.get().getValue(); + if (id.equals(Enchantments.RIPTIDE.getValue()) || + id.equals(Enchantments.LOYALTY.getValue()) || + id.equals(Enchantments.CHANNELING.getValue())) { + return entry; + } + } + } + + return null; + } + + private String getFirstVisibleChar(String str) { if (str == null || str.isEmpty()) { return ""; @@ -109,8 +195,4 @@ public abstract class DrawContextMixin { } return "?"; } - - public void debug(String a) { - System.out.println("Show E DEBUG: " + a); - } }