Source PiglinInfo - Spiderman31807/Mob_Selector_Mod GitHub Wiki

package playasmob;

import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.CrossbowItem;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.monster.piglin.PiglinArmPose;
import net.minecraft.world.entity.monster.piglin.Piglin;
import net.minecraft.world.entity.monster.Creeper;
import net.minecraft.world.entity.Pose;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.EntityDimensions;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.tags.ItemTags;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.core.BlockPos;
import net.minecraft.client.renderer.entity.PiglinRenderer;
import net.minecraft.client.renderer.entity.EntityRenderer;
import net.minecraft.client.model.geom.ModelLayers;

import java.util.List;

public class PiglinInfo extends AbstractPiglinInfo {
	public static final EntityDimensions BabyHitbox = EntityType.PIGLIN.getDimensions().scale(0.5F).withEyeHeight(0.97F);
	public static final Synched dancing = Synched.Celebrating;
	public static final Synched loadingWeapon = Synched.LoadingCrossbow;
	public boolean cannotHunt;

	public PiglinInfo(Player player, CompoundTag compound) {
		super(EntityType.PIGLIN, Piglin.class, player, compound);
	}

	public PiglinInfo(Player player) {
		this(player, null);
	}

	public PiglinInfo() {
		this(null);
	}

	// Custom Code
	@Override
	public void reset() {
		super.reset();
		this.reset(dancing);
		this.reset(loadingWeapon);
		this.cannotHunt = false;
	}
	
	@Override
	public EntityRenderer getRenderer() {
		return new PiglinRenderer(Utils.getContext(), ModelLayers.PIGLIN, ModelLayers.PIGLIN_BABY, ModelLayers.PIGLIN_INNER_ARMOR, ModelLayers.PIGLIN_OUTER_ARMOR, ModelLayers.PIGLIN_BABY_INNER_ARMOR, ModelLayers.PIGLIN_BABY_OUTER_ARMOR);
	}

	@Override
	public float getSpeed(float original) {
		float baseSpeed = super.getSpeed(original);
		return this.isBaby() ? baseSpeed * 1.2f : baseSpeed;
	}

	@Override
	public List<HudIcon> getIcons(float partialTick) {
		return List.of(new HudIcon("playasmob:textures/screens/dance_icon.png").key(Keybinds.Key1).using(this.isDancing()).useable(this.canDance()));
	}

	@Override
	public void press(int key, boolean pressed, int time) {
		if (key == 1)
			this.setDancing(pressed);
		super.press(key, pressed, time);
	}

	public boolean canDance() {
		return !this.isVisuallySwimming() && (this.canUseHands(HandUse.None) || this.canUseHands(HandUse.Blocked));
	}

	@Override
	public boolean handUseageBlocked() {
		return super.handUseageBlocked() || (this.getHandUseage(false) == HandUse.None && this.isDancing());
	}

	// Piglin.class Code
	@Override
	public CompoundTag saveData() {
		CompoundTag compound = super.saveData();
		if (this.cannotHunt)
			compound.putBoolean("CannotHunt", true);
		return compound;
	}

	@Override
	public void loadData(CompoundTag compound) {
		super.loadData(compound);
		this.setCannotHunt(compound.getBoolean("CannotHunt"));
	}

	@Override
	public boolean dropCustomDeathLoot(Object object, ServerLevel server, DamageSource source, boolean booleanValue) {
		if (!super.dropCustomDeathLoot(null, server, source, booleanValue))
			return false;
		if (source.getEntity() instanceof Creeper creeper && creeper.canDropMobsSkull()) {
			ItemStack itemstack = new ItemStack(Items.PIGLIN_HEAD);
			creeper.increaseDroppedSkulls();
			this.spawnAtLocation(server, itemstack);
		}
		return true;
	}

	@Override
	public ItemStack spawnedHelmetStack() {
		return this.random.nextFloat() < 0.1f ? new ItemStack(Items.GOLDEN_HELMET) : ItemStack.EMPTY;
	}

	@Override
	public ItemStack spawnedChestplateStack() {
		return this.random.nextFloat() < 0.1f ? new ItemStack(Items.GOLDEN_CHESTPLATE) : ItemStack.EMPTY;
	}

	@Override
	public ItemStack spawnedLeggingsStack() {
		return this.random.nextFloat() < 0.1f ? new ItemStack(Items.GOLDEN_LEGGINGS) : ItemStack.EMPTY;
	}

	@Override
	public ItemStack spawnedBootsStack() {
		return this.random.nextFloat() < 0.1f ? new ItemStack(Items.GOLDEN_BOOTS) : ItemStack.EMPTY;
	}

	@Override
	public EntityDimensions getDimensions(Pose pose) {
		return this.isBaby() ? BabyHitbox : super.getDimensions(pose);
	}

	public void setCannotHunt(boolean cantHunt) {
		this.cannotHunt = cantHunt;
	}

	@Override
	public boolean canHunt() {
		return !this.cannotHunt;
	}

	@Override
	public ItemStack spawnedMainHandStack() {
		return (double) this.random.nextFloat() < 0.5 ? new ItemStack(Items.CROSSBOW) : new ItemStack(Items.GOLDEN_SWORD);
	}

	public boolean isChargingCrossbow() {
		return this.getUseItem() instanceof ItemStack stack && stack.getItem() instanceof CrossbowItem;
	}

	@Override
	public void shotProjectile(ServerLevel server, InteractionHand hand, ItemStack weapon, List<ItemStack> projectiles, boolean isCrit, LivingEntity target) {
		super.shotProjectile(server, hand, weapon, projectiles, isCrit, target);
		if (weapon.getItem() instanceof CrossbowItem)
			this.onCrossbowAttackPerformed();
	}

	public void onCrossbowAttackPerformed() {
		this.setNoActionTime(0);
	}

	@Override
	public PiglinArmPose getArmPose() {
		if (this.isDancing() && this.canDance())
			return PiglinArmPose.DANCING;
		if (this.getOffhandItem().is(ItemTags.PIGLIN_LOVED))
			return PiglinArmPose.ADMIRING_ITEM;
		if (this.isAggressive() && this.isHoldingMeleeWeapon())
			return PiglinArmPose.ATTACKING_WITH_MELEE_WEAPON;
		if (this.isChargingCrossbow())
			return PiglinArmPose.CROSSBOW_CHARGE;
		return this.isHolding(stack -> stack.getItem() instanceof CrossbowItem) && CrossbowItem.isCharged(this.getWeaponItem()) ? PiglinArmPose.CROSSBOW_HOLD : PiglinArmPose.DEFAULT;
	}

	public boolean isDancing() {
		return this.get(dancing);
	}

	public void setDancing(boolean dancing) {
		this.set(this.dancing, dancing);
	}

	@Override
	public SoundEvent getAmbientSound() {
		if (this.isAggressive())
			return SoundEvents.PIGLIN_ANGRY;
		if (this.isConverting())
			return SoundEvents.PIGLIN_RETREAT;
		if (this.getOffhandItem().is(ItemTags.PIGLIN_LOVED))
			return SoundEvents.PIGLIN_ADMIRING_ITEM;
		if (this.isDancing())
			return SoundEvents.PIGLIN_CELEBRATE;
		return SoundEvents.PIGLIN_AMBIENT;
	}

	@Override
	public SoundEvent getHurtSound(SoundEvent original, DamageSource source) {
		return SoundEvents.PIGLIN_HURT;
	}

	@Override
	public SoundEvent getDeathSound(SoundEvent original) {
		return SoundEvents.PIGLIN_DEATH;
	}

	@Override
	public boolean playStepSound(Object object, BlockPos pos, BlockState state) {
		this.playSound(SoundEvents.PIGLIN_STEP, 0.15f, 1);
		return false;
	}

	@Override
	public void playConvertedSound() {
		this.makeSound(SoundEvents.PIGLIN_CONVERTED_TO_ZOMBIFIED);
	}
}
⚠️ **GitHub.com Fallback** ⚠️