Skip to content

Commit

Permalink
Fixed gear not rendering on ponies
Browse files Browse the repository at this point in the history
  • Loading branch information
Sollace committed Sep 21, 2022
1 parent f45817b commit a1f8487
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -43,7 +41,7 @@ public BodyPart getGearLocation() {

@Override
public <T extends Entity> Identifier getTexture(T entity, Context<T, ?> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
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;
import net.minecraft.client.render.VertexConsumer;
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 {
Expand All @@ -33,16 +33,18 @@ 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());
}

@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
Expand All @@ -57,27 +59,26 @@ public <T extends Entity> Identifier getTexture(T entity, Context<T, ?> 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
public void render(MatrixStack stack, VertexConsumer consumer, int light, int overlay, float red, float green, float blue, float alpha, UUID interpolatorId) {
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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

Expand Down
31 changes: 28 additions & 3 deletions src/main/java/com/minelittlepony/unicopia/item/WearableItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit a1f8487

Please sign in to comment.