Skip to content

Commit

Permalink
fix armor rendering and shortcut on empty stacks
Browse files Browse the repository at this point in the history
  • Loading branch information
UpcraftLP committed Nov 3, 2024
1 parent 1cbb02a commit fe9c552
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -37,8 +38,12 @@ public static <E extends LivingEntity, M extends EntityModel<E>> void register(C
}

@SuppressWarnings({"unchecked", "rawtypes"})
public static <E extends LivingEntity, M extends EntityModel<E>> Optional<CustomArmorRenderer<E, M>> get(RenderLayerParent<E, M> renderer, E entity, Item item) {
return (Optional<CustomArmorRenderer<E,M>>) RENDERERS.computeIfAbsent(Pair.of(entity.getClass(), item), key -> {
public static <E extends LivingEntity, M extends EntityModel<E>> Optional<CustomArmorRenderer<E, M>> get(RenderLayerParent<E, M> renderer, E entity, ItemStack stack) {
if(stack.isEmpty()) {
return Optional.empty();
}

return (Optional<CustomArmorRenderer<E,M>>) RENDERERS.computeIfAbsent(Pair.of(entity.getClass(), stack.getItem()), key -> {
var factory = FACTORIES.get(key.getSecond());
if(factory == null) {
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,16 @@ public MageRobesRenderer(EntityRendererProvider.Context context) {

@Override
protected void setPartVisibility(MageRobesModel<LivingEntity> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

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;
import net.minecraft.client.renderer.entity.layers.HumanoidArmorLayer;
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;
Expand All @@ -29,8 +29,8 @@ private HumanoidArmorLayerMixin(RenderLayerParent<T, M> 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<T, M>) this).sparkweave$getParent(), entity, armorStack.getItem()).ifPresent(renderer -> {
if (!(armorStack.getItem() instanceof Equipable equipable) || equipable.getEquipmentSlot() == slot) {
ArmorRendererRegistry.get(((RenderLayerExtensions<T, M>) this).sparkweave$getParent(), entity, armorStack).ifPresent(renderer -> {
renderer.render(poseStack, bufferSource, armorStack, entity, slot, light, this.getParentModel());
info.cancel();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,8 +29,9 @@ private HumanoidArmorLayerMixin(RenderLayerParent<T, M> 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<T, M>) 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<T, M>) this).sparkweave$getParent(), entity, armorStack).ifPresent(renderer -> {
renderer.render(poseStack, bufferSource, armorStack, entity, slot, packedLight, this.getParentModel());
ci.cancel();
});
Expand Down

0 comments on commit fe9c552

Please sign in to comment.