diff --git a/src/main/java/com/gtnewhorizons/angelica/mixins/Mixins.java b/src/main/java/com/gtnewhorizons/angelica/mixins/Mixins.java index a47a02fe7..8901b6c55 100644 --- a/src/main/java/com/gtnewhorizons/angelica/mixins/Mixins.java +++ b/src/main/java/com/gtnewhorizons/angelica/mixins/Mixins.java @@ -252,6 +252,7 @@ public enum Mixins implements IMixins { , "shaders.MixinRenderEntityFlame" , "shaders.MixinRendererLivingEntity" , "shaders.MixinRenderGlobal" + , "shaders.AccessorEntityHorse" , "shaders.MixinRenderHorse" , "shaders.MixinRenderItem" , "shaders.MixinRenderNameTag" diff --git a/src/main/java/com/gtnewhorizons/angelica/rendering/ModelHorseArmor.java b/src/main/java/com/gtnewhorizons/angelica/rendering/ModelHorseArmor.java new file mode 100644 index 000000000..77f8f0b2f --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/rendering/ModelHorseArmor.java @@ -0,0 +1,31 @@ +package com.gtnewhorizons.angelica.rendering; + +import net.minecraft.client.model.ModelBox; +import net.minecraft.client.model.ModelHorse; +import net.minecraft.client.model.ModelRenderer; + +import java.util.ArrayList; +import java.util.List; + +/** + * Horse model with all boxes inflated outward, used as a separate armor layer. + * Extends ModelHorse to inherit all animation and render logic. The constructor + * builds the default geometry, then we rebuild every box with inflate applied. + * This mirrors how modern MC uses a separate ModelLayers.HORSE_ARMOR. + */ +public class ModelHorseArmor extends ModelHorse { + + public ModelHorseArmor(float inflate) { + super(); + for (ModelRenderer renderer : this.boxList) { + List originalBoxes = new ArrayList<>(renderer.cubeList); + renderer.cubeList.clear(); + for (ModelBox box : originalBoxes) { + int width = Math.round(box.posX2 - box.posX1); + int height = Math.round(box.posY2 - box.posY1); + int depth = Math.round(box.posZ2 - box.posZ1); + renderer.addBox(box.posX1, box.posY1, box.posZ1, width, height, depth, inflate); + } + } + } +} diff --git a/src/main/java/net/coderbot/iris/uniforms/ItemMaterialHelper.java b/src/main/java/net/coderbot/iris/uniforms/ItemMaterialHelper.java index 37c16eba0..74c064bb4 100644 --- a/src/main/java/net/coderbot/iris/uniforms/ItemMaterialHelper.java +++ b/src/main/java/net/coderbot/iris/uniforms/ItemMaterialHelper.java @@ -2,8 +2,6 @@ package net.coderbot.iris.uniforms; import it.unimi.dsi.fastutil.ints.Int2IntMap; import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap; -import it.unimi.dsi.fastutil.ints.IntOpenHashSet; -import it.unimi.dsi.fastutil.ints.IntSet; import it.unimi.dsi.fastutil.objects.Object2IntFunction; import it.unimi.dsi.fastutil.objects.Reference2ObjectMap; import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap; @@ -22,8 +20,6 @@ import net.minecraft.util.ResourceLocation; public class ItemMaterialHelper { private static final Reference2ObjectMap MATERIAL_CACHE = new Reference2ObjectOpenHashMap<>(); private static final int CACHE_MISS_SENTINEL = Integer.MIN_VALUE; - public static final IntSet WARNED_UNKNOWN_ARMOR_INDICES = new IntOpenHashSet(); - /** * Get the material ID for an ItemStack. * For ItemBlock: checks block.properties first, then item.properties. @@ -114,6 +110,5 @@ public class ItemMaterialHelper { */ public static void clearCache() { MATERIAL_CACHE.clear(); - WARNED_UNKNOWN_ARMOR_INDICES.clear(); } } diff --git a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/shaders/AccessorEntityHorse.java b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/shaders/AccessorEntityHorse.java new file mode 100644 index 000000000..188bc80cb --- /dev/null +++ b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/shaders/AccessorEntityHorse.java @@ -0,0 +1,13 @@ +package com.gtnewhorizons.angelica.mixins.early.shaders; + +import net.minecraft.entity.passive.EntityHorse; +import net.minecraft.item.ItemStack; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Invoker; + +@Mixin(EntityHorse.class) +public interface AccessorEntityHorse { + + @Invoker("getHorseArmorIndex") + int invokeGetHorseArmorIndex(ItemStack stack); +} diff --git a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/shaders/MixinRenderHorse.java b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/shaders/MixinRenderHorse.java index 6c64e7263..a53dfcfd5 100644 --- a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/shaders/MixinRenderHorse.java +++ b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/shaders/MixinRenderHorse.java @@ -1,84 +1,136 @@ package com.gtnewhorizons.angelica.mixins.early.shaders; +import com.gtnewhorizons.angelica.rendering.ModelHorseArmor; import com.llamalad7.mixinextras.injector.wrapoperation.Operation; import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; import net.coderbot.iris.uniforms.CapturedRenderingState; import net.coderbot.iris.uniforms.ItemIdManager; import net.coderbot.iris.uniforms.ItemMaterialHelper; +import net.minecraft.client.Minecraft; import net.minecraft.client.model.ModelBase; +import net.minecraft.client.model.ModelRenderer; import net.minecraft.client.renderer.entity.RenderHorse; import net.minecraft.entity.passive.EntityHorse; -import net.minecraft.init.Items; import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.util.ResourceLocation; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; +import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; + +import java.util.HashMap; +import java.util.Map; + /** - * Mixin to set the currentRenderedItem ID when rendering horse armor. - * This allows shaders to identify and apply different materials to horse armor. + * Mixin to set the currentRenderedItem ID on the armor portion of horse rendering. */ @Mixin(RenderHorse.class) public abstract class MixinRenderHorse { + @Unique private static final Logger LOGGER = LogManager.getLogger("Angelica"); + @Unique + private static final float ARMOR_INFLATE = 0.125F; + + @Unique + private static ModelHorseArmor angelica$armorModel; + /** - * Set the item ID when rendering the horse model if it has armor. - * Wraps the mainModel.render() call to set the material ID only around the actual render. + * Reverse mapping from armor index to Item, built lazily by scanning the item registry + * for items recognized by EntityHorse.func_146085_a (isHorseArmor) and invoking + * EntityHorse.getHorseArmorIndex to determine their index. + * Automatically picks up modded armor that hooks into the vanilla's system. (I hope) + */ + @Unique + private static Int2ObjectOpenHashMap angelica$armorIndexToItem; + + @Unique + private static final Map angelica$armorTextureCache = new HashMap<>(); + + /** + * Two-pass rendering with a separate inflated armor model: + * Pass 1: Render the base horse with the composite texture, no item ID. + * Pass 2: Render the inflated armor model with the armor-only texture and item ID set. + * The inflate makes armor geometry sit above the base. + * Alpha test discards transparent (non-armor) pixels. */ @WrapOperation( method = "renderModel(Lnet/minecraft/entity/passive/EntityHorse;FFFFFF)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/model/ModelBase;render(Lnet/minecraft/entity/Entity;FFFFFF)V") ) private void iris$setHorseArmorItemId(ModelBase model, net.minecraft.entity.Entity entity, float p1, float p2, float p3, float p4, float p5, float p6, Operation original, EntityHorse horse) { - // Only set ID if horse has armor equipped - boolean hasArmor = false; - - // Check if horse type == 0 (regular horse) - if (horse.func_110259_cr()) { - // Get armor index from DataWatcher (synced from server) - int armorIndex = horse.func_110241_cb(); - - if (armorIndex > 0) { - // Map armor index to vanilla items - Item armorItem = null; - switch (armorIndex) { - case 1: - armorItem = Items.iron_horse_armor; - break; - case 2: - armorItem = Items.golden_horse_armor; - break; - case 3: - armorItem = Items.diamond_horse_armor; - break; - default: - // Unknown armor index - likely from a mod - // Warn once per unique index to avoid spam - if (!ItemMaterialHelper.WARNED_UNKNOWN_ARMOR_INDICES.contains(armorIndex)) { - ItemMaterialHelper.WARNED_UNKNOWN_ARMOR_INDICES.add(armorIndex); - LOGGER.warn("Unknown horse armor index detected: {}. " + - "This is likely from a mod. Vanilla horse armor uses indices 1-3 (iron/gold/diamond). " + - "Modded horse armor material IDs are not currently supported.", armorIndex); - } - break; - } - - if (armorItem != null) { - int id = ItemMaterialHelper.getMaterialId(armorItem, 0); - CapturedRenderingState.INSTANCE.setCurrentRenderedItem(id); - hasArmor = true; - } - } - } - - // Call original render + // Pass 1: Render the base horse normally original.call(model, entity, p1, p2, p3, p4, p5, p6); - // Reset only if we set it - if (hasArmor) { - ItemIdManager.resetItemId(); + if (!horse.func_110259_cr()) return; + + int armorIndex = horse.func_110241_cb(); + if (armorIndex <= 0) return; + + String armorTexturePath = horse.getVariantTexturePaths()[2]; + if (armorTexturePath == null) return; + + // Pass 2: Render inflated armor model with item ID + if (angelica$armorModel == null) { + angelica$armorModel = new ModelHorseArmor(ARMOR_INFLATE); + } + + Item armorItem = angelica$getArmorItem(horse, armorIndex); + if (armorItem != null) { + int id = ItemMaterialHelper.getMaterialId(armorItem, 0); + CapturedRenderingState.INSTANCE.setCurrentRenderedItem(id); + } + + // Copy animation state from the base model to the armor model + angelica$copyRotations(model, angelica$armorModel); + + ResourceLocation armorTexture = angelica$armorTextureCache.computeIfAbsent(armorTexturePath, ResourceLocation::new); + Minecraft.getMinecraft().getTextureManager().bindTexture(armorTexture); + + angelica$armorModel.render(horse, p1, p2, p3, p4, p5, p6); + + ItemIdManager.resetItemId(); + } + + // Copy rotation angles and positions from one model's renderers to another's + @Unique + private static void angelica$copyRotations(ModelBase from, ModelBase to) { + for (int i = 0; i < from.boxList.size() && i < to.boxList.size(); i++) { + ModelRenderer src = from.boxList.get(i); + ModelRenderer dst = to.boxList.get(i); + dst.rotateAngleX = src.rotateAngleX; + dst.rotateAngleY = src.rotateAngleY; + dst.rotateAngleZ = src.rotateAngleZ; + dst.rotationPointX = src.rotationPointX; + dst.rotationPointY = src.rotationPointY; + dst.rotationPointZ = src.rotationPointZ; } } + + /** + * Look up the item for a given armor index. On first call, builds a reverse mapping + * by scanning the item registry for items that pass EntityHorse.func_146085_a (isHorseArmor) + * and invoking the getHorseArmorIndex to determine their index. + */ + @Unique + private static Item angelica$getArmorItem(EntityHorse horse, int armorIndex) { + if (angelica$armorIndexToItem == null) { + angelica$armorIndexToItem = new Int2ObjectOpenHashMap<>(); + AccessorEntityHorse accessor = (AccessorEntityHorse) horse; + for (Object obj : Item.itemRegistry) { + Item item = (Item) obj; + if (!EntityHorse.func_146085_a(item)) continue; + int idx = accessor.invokeGetHorseArmorIndex(new ItemStack(item)); + if (idx > 0) { + angelica$armorIndexToItem.put(idx, item); + } + } + LOGGER.debug("Built horse armor index map: {} entries", angelica$armorIndexToItem.size()); + } + return angelica$armorIndexToItem.get(armorIndex); + } }