From fe9c552365e2736bf1f0cbd3cb2c18c98a167bc4 Mon Sep 17 00:00:00 2001 From: Up Date: Sun, 3 Nov 2024 22:18:49 +0100 Subject: [PATCH] fix armor rendering and shortcut on empty stacks --- .../client/event/ArmorRendererRegistry.java | 9 ++++-- .../client/SparkweaveTestmodClient.java | 4 +-- .../client/renderers/MageRobesRenderer.java | 28 +++++++------------ .../mixin/client/HumanoidArmorLayerMixin.java | 8 +++--- .../mixin/client/HumanoidArmorLayerMixin.java | 7 +++-- 5 files changed, 27 insertions(+), 29 deletions(-) diff --git a/Common/src/main/java/dev/upcraft/sparkweave/client/event/ArmorRendererRegistry.java b/Common/src/main/java/dev/upcraft/sparkweave/client/event/ArmorRendererRegistry.java index 744667d..5284e77 100644 --- a/Common/src/main/java/dev/upcraft/sparkweave/client/event/ArmorRendererRegistry.java +++ b/Common/src/main/java/dev/upcraft/sparkweave/client/event/ArmorRendererRegistry.java @@ -11,6 +11,7 @@ import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.ItemLike; import org.jetbrains.annotations.ApiStatus; @@ -37,8 +38,12 @@ public static > void register(C } @SuppressWarnings({"unchecked", "rawtypes"}) - public static > Optional> get(RenderLayerParent renderer, E entity, Item item) { - return (Optional>) RENDERERS.computeIfAbsent(Pair.of(entity.getClass(), item), key -> { + public static > Optional> get(RenderLayerParent renderer, E entity, ItemStack stack) { + if(stack.isEmpty()) { + return Optional.empty(); + } + + return (Optional>) RENDERERS.computeIfAbsent(Pair.of(entity.getClass(), stack.getItem()), key -> { var factory = FACTORIES.get(key.getSecond()); if(factory == null) { return Optional.empty(); diff --git a/Common/src/testmod/java/dev/upcraft/sparkweave/testmod/client/SparkweaveTestmodClient.java b/Common/src/testmod/java/dev/upcraft/sparkweave/testmod/client/SparkweaveTestmodClient.java index fb4e8ab..c1a2b16 100644 --- a/Common/src/testmod/java/dev/upcraft/sparkweave/testmod/client/SparkweaveTestmodClient.java +++ b/Common/src/testmod/java/dev/upcraft/sparkweave/testmod/client/SparkweaveTestmodClient.java @@ -1,7 +1,7 @@ package dev.upcraft.sparkweave.testmod.client; import dev.upcraft.sparkweave.api.client.Debug; -import dev.upcraft.sparkweave.client.event.ArmorRendererRegistry; +import dev.upcraft.sparkweave.api.client.event.RegisterCustomArmorRenderersEvent; import dev.upcraft.sparkweave.api.client.event.RegisterLayerDefinitionsEvent; import dev.upcraft.sparkweave.api.entrypoint.ClientEntryPoint; import dev.upcraft.sparkweave.api.platform.ModContainer; @@ -18,7 +18,7 @@ public class SparkweaveTestmodClient implements ClientEntryPoint { @Override public void onInitializeClient(ModContainer mod) { RegisterLayerDefinitionsEvent.EVENT.register(event -> event.registerModelLayers(MageRobesModel.MODEL_LAYER, MageRobesModel::createBodyLayer)); - ArmorRendererRegistry.register((entity, context, renderer) -> new MageRobesRenderer(context), Items.CHAINMAIL_HELMET, Items.CHAINMAIL_CHESTPLATE, Items.CHAINMAIL_LEGGINGS, Items.CHAINMAIL_BOOTS); + RegisterCustomArmorRenderersEvent.EVENT.register(event -> event.register((entity, context, renderer) -> new MageRobesRenderer(context), Items.CHAINMAIL_HELMET, Items.CHAINMAIL_CHESTPLATE, Items.CHAINMAIL_LEGGINGS, Items.CHAINMAIL_BOOTS)); } public static void onClientTickStart(Minecraft client) { diff --git a/Common/src/testmod/java/dev/upcraft/sparkweave/testmod/client/renderers/MageRobesRenderer.java b/Common/src/testmod/java/dev/upcraft/sparkweave/testmod/client/renderers/MageRobesRenderer.java index a0cadba..7a0fdef 100644 --- a/Common/src/testmod/java/dev/upcraft/sparkweave/testmod/client/renderers/MageRobesRenderer.java +++ b/Common/src/testmod/java/dev/upcraft/sparkweave/testmod/client/renderers/MageRobesRenderer.java @@ -21,24 +21,16 @@ public MageRobesRenderer(EntityRendererProvider.Context context) { @Override protected void setPartVisibility(MageRobesModel model, LivingEntity entity, ItemStack stack, EquipmentSlot slot) { - switch (slot) { - case HEAD -> { - model.openHood.visible = true; - model.cloak.visible = true; - } - case CHEST -> { - model.garb.visible = true; - model.leftSleeve.visible = true; - model.rightSleeve.visible = true; - } - case LEGS -> { - model.belt.visible = true; - } - case FEET -> { - model.leftShoe.visible = true; - model.rightShoe.visible = true; - } - } + model.setAllVisible(true); + model.closedHood.visible = false; + model.openHood.visible = slot == EquipmentSlot.HEAD; + model.cloak.visible = slot == EquipmentSlot.HEAD; + model.garb.visible = slot == EquipmentSlot.CHEST; + model.leftSleeve.visible = slot == EquipmentSlot.CHEST; + model.rightSleeve.visible = slot == EquipmentSlot.CHEST; + model.belt.visible = slot == EquipmentSlot.LEGS; + model.leftShoe.visible = slot == EquipmentSlot.FEET; + model.rightShoe.visible = slot == EquipmentSlot.FEET; } @Override diff --git a/Fabric/src/main/java/dev/upcraft/sparkweave/fabric/mixin/client/HumanoidArmorLayerMixin.java b/Fabric/src/main/java/dev/upcraft/sparkweave/fabric/mixin/client/HumanoidArmorLayerMixin.java index a940e22..5ed1c3e 100644 --- a/Fabric/src/main/java/dev/upcraft/sparkweave/fabric/mixin/client/HumanoidArmorLayerMixin.java +++ b/Fabric/src/main/java/dev/upcraft/sparkweave/fabric/mixin/client/HumanoidArmorLayerMixin.java @@ -2,8 +2,8 @@ import com.llamalad7.mixinextras.sugar.Local; import com.mojang.blaze3d.vertex.PoseStack; -import dev.upcraft.sparkweave.client.event.ArmorRendererRegistry; import dev.upcraft.sparkweave.api.client.render.RenderLayerExtensions; +import dev.upcraft.sparkweave.client.event.ArmorRendererRegistry; import net.minecraft.client.model.HumanoidModel; import net.minecraft.client.renderer.MultiBufferSource; import net.minecraft.client.renderer.entity.RenderLayerParent; @@ -11,7 +11,7 @@ import net.minecraft.client.renderer.entity.layers.RenderLayer; import net.minecraft.world.entity.EquipmentSlot; import net.minecraft.world.entity.LivingEntity; -import net.minecraft.world.item.ArmorItem; +import net.minecraft.world.item.Equipable; import net.minecraft.world.item.ItemStack; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; @@ -29,8 +29,8 @@ private HumanoidArmorLayerMixin(RenderLayerParent parent) { @SuppressWarnings("unchecked") @Inject(method = "renderArmorPiece", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/item/ItemStack;getItem()Lnet/minecraft/world/item/Item;"), cancellable = true) private void disableDefaultArmorRendererForCustomArmor(PoseStack poseStack, MultiBufferSource bufferSource, T entity, EquipmentSlot slot, int light, A model, CallbackInfo info, @Local ItemStack armorStack) { - if (!(armorStack.getItem() instanceof ArmorItem armorItem) || armorItem.getEquipmentSlot() == slot) { - ArmorRendererRegistry.get(((RenderLayerExtensions) this).sparkweave$getParent(), entity, armorStack.getItem()).ifPresent(renderer -> { + if (!(armorStack.getItem() instanceof Equipable equipable) || equipable.getEquipmentSlot() == slot) { + ArmorRendererRegistry.get(((RenderLayerExtensions) this).sparkweave$getParent(), entity, armorStack).ifPresent(renderer -> { renderer.render(poseStack, bufferSource, armorStack, entity, slot, light, this.getParentModel()); info.cancel(); }); diff --git a/NeoForge/src/main/java/dev/upcraft/sparkweave/neoforge/mixin/client/HumanoidArmorLayerMixin.java b/NeoForge/src/main/java/dev/upcraft/sparkweave/neoforge/mixin/client/HumanoidArmorLayerMixin.java index b10c911..0fe946e 100644 --- a/NeoForge/src/main/java/dev/upcraft/sparkweave/neoforge/mixin/client/HumanoidArmorLayerMixin.java +++ b/NeoForge/src/main/java/dev/upcraft/sparkweave/neoforge/mixin/client/HumanoidArmorLayerMixin.java @@ -11,7 +11,7 @@ import net.minecraft.client.renderer.entity.layers.RenderLayer; import net.minecraft.world.entity.EquipmentSlot; import net.minecraft.world.entity.LivingEntity; -import net.minecraft.world.item.ArmorItem; +import net.minecraft.world.item.Equipable; import net.minecraft.world.item.ItemStack; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; @@ -29,8 +29,9 @@ private HumanoidArmorLayerMixin(RenderLayerParent parent) { @SuppressWarnings("unchecked") @Inject(method = "renderArmorPiece(Lcom/mojang/blaze3d/vertex/PoseStack;Lnet/minecraft/client/renderer/MultiBufferSource;Lnet/minecraft/world/entity/LivingEntity;Lnet/minecraft/world/entity/EquipmentSlot;ILnet/minecraft/client/model/HumanoidModel;FFFFFF)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/item/ItemStack;getItem()Lnet/minecraft/world/item/Item;"), cancellable = true) private void disableDefaultArmorRendererForCustomArmor(PoseStack poseStack, MultiBufferSource bufferSource, T entity, EquipmentSlot slot, int packedLight, A p_model, float limbSwing, float limbSwingAmount, float partialTick, float ageInTicks, float netHeadYaw, float headPitch, CallbackInfo ci, @Local ItemStack armorStack) { - if (!(armorStack.getItem() instanceof ArmorItem armorItem) || armorItem.getEquipmentSlot(armorStack) == slot) { - ArmorRendererRegistry.get(((RenderLayerExtensions) this).sparkweave$getParent(), entity, armorStack.getItem()).ifPresent(renderer -> { + EquipmentSlot itemSlot = armorStack.getEquipmentSlot(); + if (itemSlot == slot || (itemSlot == null && (!(armorStack.getItem() instanceof Equipable equipable) || equipable.getEquipmentSlot() == slot))) { + ArmorRendererRegistry.get(((RenderLayerExtensions) this).sparkweave$getParent(), entity, armorStack).ifPresent(renderer -> { renderer.render(poseStack, bufferSource, armorStack, entity, slot, packedLight, this.getParentModel()); ci.cancel(); });