From a1f84872f29d833da44c86775746f3e3dd217d69 Mon Sep 17 00:00:00 2001 From: Sollace Date: Wed, 21 Sep 2022 18:02:45 +0200 Subject: [PATCH] Fixed gear not rendering on ponies --- .../client/minelittlepony/AmuletGear.java | 6 ++-- .../client/minelittlepony/BangleGear.java | 27 ++++++++-------- .../unicopia/client/minelittlepony/Main.java | 4 ++- .../unicopia/item/WearableItem.java | 31 +++++++++++++++++-- .../unicopia/trinkets/TrinketsDelegate.java | 4 +++ 5 files changed, 51 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/client/minelittlepony/AmuletGear.java b/src/main/java/com/minelittlepony/unicopia/client/minelittlepony/AmuletGear.java index 9ddeceb10..43c803f63 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/minelittlepony/AmuletGear.java +++ b/src/main/java/com/minelittlepony/unicopia/client/minelittlepony/AmuletGear.java @@ -15,7 +15,6 @@ import net.minecraft.client.render.entity.model.BipedEntityModel; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.entity.Entity; -import net.minecraft.entity.EquipmentSlot; import net.minecraft.entity.LivingEntity; import net.minecraft.util.Identifier; import net.minecraft.util.registry.Registry; @@ -32,8 +31,7 @@ public AmuletGear() { @Override public boolean canRender(IModel model, Entity entity) { - return entity instanceof LivingEntity living - && living.getEquippedStack(EquipmentSlot.CHEST).getItem() instanceof AmuletItem; + return entity instanceof LivingEntity living && !AmuletItem.getForEntity(living).isEmpty(); } @Override @@ -43,7 +41,7 @@ public BodyPart getGearLocation() { @Override public Identifier getTexture(T entity, Context context) { - return textures.computeIfAbsent(Registry.ITEM.getId(((LivingEntity)entity).getEquippedStack(EquipmentSlot.CHEST).getItem()), id -> new Identifier(id.getNamespace(), "textures/models/armor/" + id.getPath() + ".png")); + return textures.computeIfAbsent(Registry.ITEM.getId(AmuletItem.getForEntity((LivingEntity)entity).getItem()), id -> new Identifier(id.getNamespace(), "textures/models/armor/" + id.getPath() + ".png")); } @Override diff --git a/src/main/java/com/minelittlepony/unicopia/client/minelittlepony/BangleGear.java b/src/main/java/com/minelittlepony/unicopia/client/minelittlepony/BangleGear.java index 2eb1d810d..7d48ee222 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/minelittlepony/BangleGear.java +++ b/src/main/java/com/minelittlepony/unicopia/client/minelittlepony/BangleGear.java @@ -10,6 +10,7 @@ import com.minelittlepony.unicopia.client.render.BraceletFeatureRenderer.BraceletModel; import com.minelittlepony.unicopia.item.FriendshipBraceletItem; import com.minelittlepony.unicopia.item.GlowableItem; +import com.minelittlepony.unicopia.trinkets.TrinketsDelegate; import net.minecraft.client.model.Dilation; import net.minecraft.client.network.ClientPlayerEntity; @@ -17,10 +18,9 @@ import net.minecraft.client.render.entity.model.BipedEntityModel; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.entity.Entity; -import net.minecraft.entity.EquipmentSlot; import net.minecraft.entity.LivingEntity; import net.minecraft.item.DyeableItem; -import net.minecraft.item.ItemStack; +import net.minecraft.util.Arm; import net.minecraft.util.Identifier; class BangleGear implements IGear { @@ -33,7 +33,10 @@ class BangleGear implements IGear { private IModel model; - public BangleGear() { + private final Identifier slot; + + public BangleGear(Identifier slot) { + this.slot = slot; Dilation dilation = new Dilation(0.3F); steveModel = new BraceletModel(BraceletModel.getData(dilation, false, -1, 4, 0).createModel()); alexModel = new BraceletModel(BraceletModel.getData(dilation, true, -1, 4, 0).createModel()); @@ -41,8 +44,7 @@ public BangleGear() { @Override public boolean canRender(IModel model, Entity entity) { - return entity instanceof LivingEntity living - && living.getEquippedStack(EquipmentSlot.CHEST).getItem() instanceof FriendshipBraceletItem; + return entity instanceof LivingEntity living && FriendshipBraceletItem.getWornBangles(living, slot).findFirst().isPresent(); } @Override @@ -57,19 +59,19 @@ public Identifier getTexture(T entity, Context context) @Override public void setModelAttributes(IModel model, Entity entity) { - ItemStack item = ((LivingEntity)entity).getEquippedStack(EquipmentSlot.CHEST); - - color = ((DyeableItem)item.getItem()).getColor(item); - glowing = ((GlowableItem)item.getItem()).isGlowing(item); - alex = entity instanceof ClientPlayerEntity && ((ClientPlayerEntity)entity).getModel().startsWith("slim"); this.model = model; - + alex = entity instanceof ClientPlayerEntity && ((ClientPlayerEntity)entity).getModel().startsWith("slim"); + FriendshipBraceletItem.getWornBangles((LivingEntity)entity, slot).findFirst().ifPresent(bracelet -> { + color = ((DyeableItem)bracelet.getItem()).getColor(bracelet); + glowing = ((GlowableItem)bracelet.getItem()).isGlowing(bracelet); + }); BraceletModel m = alex ? alexModel : steveModel; if (model instanceof BipedEntityModel biped) { m.setAngles(biped); } - m.setVisible(((LivingEntity)entity).getMainArm()); + Arm mainArm = ((LivingEntity)entity).getMainArm(); + m.setVisible(slot == TrinketsDelegate.MAINHAND ? mainArm : mainArm.getOpposite()); } @Override @@ -77,7 +79,6 @@ public void render(MatrixStack stack, VertexConsumer consumer, int light, int ov popAndApply(model, BodyPart.LEGS, stack); BraceletModel m = alex ? alexModel : steveModel; - m.render(stack, consumer, glowing ? 0x0F00F0 : light, overlay, Color.r(color), Color.g(color), Color.b(color), 1); } diff --git a/src/main/java/com/minelittlepony/unicopia/client/minelittlepony/Main.java b/src/main/java/com/minelittlepony/unicopia/client/minelittlepony/Main.java index 9244e02ee..1a686b231 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/minelittlepony/Main.java +++ b/src/main/java/com/minelittlepony/unicopia/client/minelittlepony/Main.java @@ -11,6 +11,7 @@ import com.minelittlepony.unicopia.*; import com.minelittlepony.unicopia.client.render.PlayerPoser.Animation; import com.minelittlepony.unicopia.entity.player.Pony; +import com.minelittlepony.unicopia.trinkets.TrinketsDelegate; import com.minelittlepony.unicopia.util.AnimationUtil; import net.fabricmc.api.ClientModInitializer; @@ -29,7 +30,8 @@ public class Main extends MineLPDelegate implements ClientModInitializer { public void onInitializeClient() { INSTANCE = this; PonyModelPrepareCallback.EVENT.register(this::onPonyModelPrepared); - IGear.register(BangleGear::new); + IGear.register(() -> new BangleGear(TrinketsDelegate.MAINHAND)); + IGear.register(() -> new BangleGear(TrinketsDelegate.OFFHAND)); IGear.register(AmuletGear::new); } diff --git a/src/main/java/com/minelittlepony/unicopia/item/WearableItem.java b/src/main/java/com/minelittlepony/unicopia/item/WearableItem.java index 3a3e062f6..bb9ae738d 100644 --- a/src/main/java/com/minelittlepony/unicopia/item/WearableItem.java +++ b/src/main/java/com/minelittlepony/unicopia/item/WearableItem.java @@ -4,23 +4,26 @@ import net.fabricmc.fabric.api.item.v1.FabricItemSettings; import net.minecraft.block.DispenserBlock; -import net.minecraft.entity.EquipmentSlot; +import net.minecraft.block.dispenser.DispenserBehavior; +import net.minecraft.block.dispenser.ItemDispenserBehavior; +import net.minecraft.entity.*; import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.item.ArmorItem; import net.minecraft.item.ArmorMaterials; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.item.Wearable; +import net.minecraft.predicate.entity.EntityPredicates; import net.minecraft.sound.SoundEvent; import net.minecraft.util.Hand; import net.minecraft.util.TypedActionResult; +import net.minecraft.util.math.*; import net.minecraft.world.World; public abstract class WearableItem extends Item implements Wearable { public WearableItem(FabricItemSettings settings) { super(configureEquipmentSlotSupplier(settings)); - DispenserBlock.registerBehavior(this, ArmorItem.DISPENSER_BEHAVIOR); + DispenserBlock.registerBehavior(this, DISPENSER_BEHAVIOR); TrinketsDelegate.getInstance().registerTrinket(this); } @@ -49,4 +52,26 @@ public SoundEvent getEquipSound() { public EquipmentSlot getPreferredSlot(ItemStack stack) { return EquipmentSlot.OFFHAND; } + + public static boolean dispenseArmor(BlockPointer pointer, ItemStack armor) { + return pointer.getWorld().getEntitiesByClass( + LivingEntity.class, + new Box(pointer.getPos().offset(pointer.getBlockState().get(DispenserBlock.FACING))), + EntityPredicates.EXCEPT_SPECTATOR.and(new EntityPredicates.Equipable(armor)) + ) + .stream() + .flatMap(entity -> TrinketsDelegate.getInstance() + .getAvailableTrinketSlots(entity, TrinketsDelegate.ALL) + .stream() + .filter(slotId -> TrinketsDelegate.getInstance().equipStack(entity, slotId, armor))) + .findFirst() + .isPresent(); + } + + private static final DispenserBehavior DISPENSER_BEHAVIOR = new ItemDispenserBehavior(){ + @Override + protected ItemStack dispenseSilently(BlockPointer pointer, ItemStack stack) { + return dispenseArmor(pointer, stack) ? stack : super.dispenseSilently(pointer, stack); + } + }; } diff --git a/src/main/java/com/minelittlepony/unicopia/trinkets/TrinketsDelegate.java b/src/main/java/com/minelittlepony/unicopia/trinkets/TrinketsDelegate.java index 38369cbe6..fec45d148 100644 --- a/src/main/java/com/minelittlepony/unicopia/trinkets/TrinketsDelegate.java +++ b/src/main/java/com/minelittlepony/unicopia/trinkets/TrinketsDelegate.java @@ -39,6 +39,10 @@ default boolean equipStack(LivingEntity entity, Identifier slot, ItemStack stack } entity.equipStack(eq, stack.split(1)); + if (entity instanceof MobEntity mob) { + mob.setEquipmentDropChance(eq, 2.0f); + mob.setPersistent(); + } return true; }