From 7b7cb619ed23b0a91533f90bb16ed848e1fc89b4 Mon Sep 17 00:00:00 2001 From: Patbox <39821509+Patbox@users.noreply.github.com> Date: Mon, 9 Dec 2024 06:35:17 +0100 Subject: [PATCH] Use Item Model component ids instead of raw item ids for uniform id lookup (#2558) --- .../iris/api/v0/item/IrisItemLightProvider.java | 1 - .../ItemStackStateLayerMixin.java | 6 +++--- .../entity_render_context/ItemStackStateMixin.java | 11 +++++++++-- .../MixinEquipmentLayerRenderer.java | 5 ++++- .../entity_render_context/MixinHorseArmorLayer.java | 7 +++++-- .../entity_render_context/MixinItemRenderer.java | 7 ++++++- .../iris/mixinterface/ItemContextState.java | 4 +++- .../net/irisshaders/iris/uniforms/IdMapUniforms.java | 6 +++++- 8 files changed, 35 insertions(+), 12 deletions(-) diff --git a/common/src/api/java/net/irisshaders/iris/api/v0/item/IrisItemLightProvider.java b/common/src/api/java/net/irisshaders/iris/api/v0/item/IrisItemLightProvider.java index c9fe1416eb..abdaca2e85 100644 --- a/common/src/api/java/net/irisshaders/iris/api/v0/item/IrisItemLightProvider.java +++ b/common/src/api/java/net/irisshaders/iris/api/v0/item/IrisItemLightProvider.java @@ -11,7 +11,6 @@ public interface IrisItemLightProvider { default int getLightEmission(Player player, ItemStack stack) { if (stack.getItem() instanceof BlockItem item) { - return item.getBlock().defaultBlockState().getLightEmission(); } diff --git a/common/src/main/java/net/irisshaders/iris/mixin/entity_render_context/ItemStackStateLayerMixin.java b/common/src/main/java/net/irisshaders/iris/mixin/entity_render_context/ItemStackStateLayerMixin.java index c01b3da5ee..c72b30564c 100644 --- a/common/src/main/java/net/irisshaders/iris/mixin/entity_render_context/ItemStackStateLayerMixin.java +++ b/common/src/main/java/net/irisshaders/iris/mixin/entity_render_context/ItemStackStateLayerMixin.java @@ -34,7 +34,7 @@ public class ItemStackStateLayerMixin { @Inject(method = "render", at = @At("HEAD")) private void onRender(PoseStack poseStack, MultiBufferSource multiBufferSource, int i, int j, CallbackInfo ci, @Share("lastBState") LocalIntRef ref) { ref.set(CapturedRenderingState.INSTANCE.getCurrentRenderedBlockEntity()); - iris$setupId(((ItemContextState) parentState).getDisplayItem()); + iris$setupId(((ItemContextState) parentState).getDisplayItem(), ((ItemContextState) parentState).getDisplayItemModel()); } @Inject(method = "render", at = @At("TAIL")) @@ -44,7 +44,7 @@ private void onRenderEnd(PoseStack poseStack, MultiBufferSource multiBufferSourc } @Unique - private void iris$setupId(Item item) { + private void iris$setupId(Item item, ResourceLocation modelId) { if (WorldRenderingSettings.INSTANCE.getItemIds() == null) return; if (item instanceof BlockItem blockItem && !(item instanceof SolidBucketItem)) { @@ -55,7 +55,7 @@ private void onRenderEnd(PoseStack poseStack, MultiBufferSource multiBufferSourc //System.out.println(WorldRenderingSettings.INSTANCE.getBlockStateIds().getInt(blockItem.getBlock().defaultBlockState())); CapturedRenderingState.INSTANCE.setCurrentRenderedItem(WorldRenderingSettings.INSTANCE.getBlockStateIds().getOrDefault(blockItem.getBlock().defaultBlockState(), 0)); } else { - ResourceLocation location = BuiltInRegistries.ITEM.getKey(item); + ResourceLocation location = modelId != null ? modelId : BuiltInRegistries.ITEM.getKey(item); CapturedRenderingState.INSTANCE.setCurrentRenderedItem(WorldRenderingSettings.INSTANCE.getItemIds().applyAsInt(new NamespacedId(location.getNamespace(), location.getPath()))); } diff --git a/common/src/main/java/net/irisshaders/iris/mixin/entity_render_context/ItemStackStateMixin.java b/common/src/main/java/net/irisshaders/iris/mixin/entity_render_context/ItemStackStateMixin.java index 514cb04749..a1c50a9e59 100644 --- a/common/src/main/java/net/irisshaders/iris/mixin/entity_render_context/ItemStackStateMixin.java +++ b/common/src/main/java/net/irisshaders/iris/mixin/entity_render_context/ItemStackStateMixin.java @@ -2,8 +2,8 @@ import net.irisshaders.iris.mixinterface.ItemContextState; import net.minecraft.client.renderer.item.ItemStackRenderState; +import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.Item; -import net.minecraft.world.item.ItemStack; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; @@ -14,19 +14,26 @@ public class ItemStackStateMixin implements ItemContextState { @Unique private Item iris_displayStack; + @Unique + private ResourceLocation iris_displayModelId; @Override - public void setDisplayItem(Item itemStack) { + public void setDisplayItem(Item itemStack, ResourceLocation modelId) { this.iris_displayStack = itemStack; + this.iris_displayModelId = modelId; } @Override public Item getDisplayItem() { return iris_displayStack; } + public ResourceLocation getDisplayItemModel() { + return iris_displayModelId; + } @Inject(method = "clear", at = @At("HEAD")) private void clearDisplayStack(CallbackInfo ci) { this.iris_displayStack = null; + this.iris_displayModelId = null; } } diff --git a/common/src/main/java/net/irisshaders/iris/mixin/entity_render_context/MixinEquipmentLayerRenderer.java b/common/src/main/java/net/irisshaders/iris/mixin/entity_render_context/MixinEquipmentLayerRenderer.java index 6c480164b3..4c5c7c01b0 100644 --- a/common/src/main/java/net/irisshaders/iris/mixin/entity_render_context/MixinEquipmentLayerRenderer.java +++ b/common/src/main/java/net/irisshaders/iris/mixin/entity_render_context/MixinEquipmentLayerRenderer.java @@ -36,7 +36,10 @@ public abstract class MixinEquipmentLayerRenderer { private void changeId(CallbackInfo ci, @Local(argsOnly = true) ItemStack itemStack) { if (WorldRenderingSettings.INSTANCE.getItemIds() == null) return; - ResourceLocation location = BuiltInRegistries.ITEM.getKey(itemStack.getItem()); + ResourceLocation location = itemStack.get(DataComponents.ITEM_MODEL); + if (location == null) + location = BuiltInRegistries.ITEM.getKey(itemStack.getItem()); + CapturedRenderingState.INSTANCE.setCurrentRenderedItem(WorldRenderingSettings.INSTANCE.getItemIds().applyAsInt(new NamespacedId(location.getNamespace(), location.getPath()))); } diff --git a/common/src/main/java/net/irisshaders/iris/mixin/entity_render_context/MixinHorseArmorLayer.java b/common/src/main/java/net/irisshaders/iris/mixin/entity_render_context/MixinHorseArmorLayer.java index 468e1378a4..783da800fa 100644 --- a/common/src/main/java/net/irisshaders/iris/mixin/entity_render_context/MixinHorseArmorLayer.java +++ b/common/src/main/java/net/irisshaders/iris/mixin/entity_render_context/MixinHorseArmorLayer.java @@ -7,9 +7,9 @@ import net.minecraft.client.renderer.MultiBufferSource; import net.minecraft.client.renderer.entity.layers.HorseArmorLayer; import net.minecraft.client.renderer.entity.state.HorseRenderState; +import net.minecraft.core.component.DataComponents; import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.resources.ResourceLocation; -import net.minecraft.world.entity.animal.horse.Horse; import net.minecraft.world.item.AnimalArmorItem; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; @@ -23,7 +23,10 @@ private void changeId(PoseStack poseStack, MultiBufferSource multiBufferSource, if (WorldRenderingSettings.INSTANCE.getItemIds() == null || !(horseRenderState.bodyArmorItem.getItem() instanceof AnimalArmorItem)) return; - ResourceLocation location = BuiltInRegistries.ITEM.getKey((horseRenderState.bodyArmorItem.getItem())); + ResourceLocation location = horseRenderState.bodyArmorItem.get(DataComponents.ITEM_MODEL); + if (location == null) + location = BuiltInRegistries.ITEM.getKey((horseRenderState.bodyArmorItem.getItem())); + CapturedRenderingState.INSTANCE.setCurrentRenderedItem(WorldRenderingSettings.INSTANCE.getItemIds().applyAsInt(new NamespacedId(location.getNamespace(), location.getPath()))); } diff --git a/common/src/main/java/net/irisshaders/iris/mixin/entity_render_context/MixinItemRenderer.java b/common/src/main/java/net/irisshaders/iris/mixin/entity_render_context/MixinItemRenderer.java index 4a97f7eda0..a3280dd046 100644 --- a/common/src/main/java/net/irisshaders/iris/mixin/entity_render_context/MixinItemRenderer.java +++ b/common/src/main/java/net/irisshaders/iris/mixin/entity_render_context/MixinItemRenderer.java @@ -3,6 +3,7 @@ import net.irisshaders.iris.mixinterface.ItemContextState; import net.minecraft.client.renderer.item.ItemModelResolver; import net.minecraft.client.renderer.item.ItemStackRenderState; +import net.minecraft.core.component.DataComponents; import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.item.ItemDisplayContext; import net.minecraft.world.item.ItemStack; @@ -20,6 +21,10 @@ public abstract class MixinItemRenderer { @Inject(method = "appendItemLayers", at = @At(value = "HEAD")) private void changeId(ItemStackRenderState itemStackRenderState, ItemStack itemStack, ItemDisplayContext itemDisplayContext, Level level, LivingEntity livingEntity, int i, CallbackInfo ci) { - ((ItemContextState) itemStackRenderState).setDisplayItem(itemStack != null ? itemStack.getItem() : null); + if (itemStack != null) { + ((ItemContextState) itemStackRenderState).setDisplayItem(itemStack.getItem(), itemStack.get(DataComponents.ITEM_MODEL)); + } else { + ((ItemContextState) itemStackRenderState).setDisplayItem(null, null); + } } } diff --git a/common/src/main/java/net/irisshaders/iris/mixinterface/ItemContextState.java b/common/src/main/java/net/irisshaders/iris/mixinterface/ItemContextState.java index 5b0fd4d651..b8323983a8 100644 --- a/common/src/main/java/net/irisshaders/iris/mixinterface/ItemContextState.java +++ b/common/src/main/java/net/irisshaders/iris/mixinterface/ItemContextState.java @@ -1,10 +1,12 @@ package net.irisshaders.iris.mixinterface; +import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; public interface ItemContextState { - void setDisplayItem(Item itemStack); + void setDisplayItem(Item itemStack, ResourceLocation location); Item getDisplayItem(); + ResourceLocation getDisplayItemModel(); } diff --git a/common/src/main/java/net/irisshaders/iris/uniforms/IdMapUniforms.java b/common/src/main/java/net/irisshaders/iris/uniforms/IdMapUniforms.java index 606ac2bfbe..6a35dd156b 100644 --- a/common/src/main/java/net/irisshaders/iris/uniforms/IdMapUniforms.java +++ b/common/src/main/java/net/irisshaders/iris/uniforms/IdMapUniforms.java @@ -8,6 +8,7 @@ import net.irisshaders.iris.shaderpack.materialmap.NamespacedId; import net.minecraft.client.Minecraft; import net.minecraft.client.player.LocalPlayer; +import net.minecraft.core.component.DataComponents; import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.InteractionHand; @@ -85,7 +86,10 @@ public void update() { return; } - ResourceLocation heldItemId = BuiltInRegistries.ITEM.getKey(heldItem); + ResourceLocation heldItemId = heldStack.get(DataComponents.ITEM_MODEL); + if (heldItemId == null) { + heldItemId = BuiltInRegistries.ITEM.getKey(heldItem); + } intID = itemIdMap.applyAsInt(new NamespacedId(heldItemId.getNamespace(), heldItemId.getPath())); IrisItemLightProvider lightProvider = (IrisItemLightProvider) heldItem;