Source AbstractPiglinInfo - Spiderman31807/Mob_Selector_Mod GitHub Wiki

package playasmob;

import net.minecraft.world.level.levelgen.structure.Structure;
import net.minecraft.world.level.levelgen.structure.BuiltinStructures;
import net.minecraft.world.level.Level;
import net.minecraft.world.item.component.Consumable;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.CrossbowItem;
import net.minecraft.world.food.FoodProperties;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.monster.piglin.PiglinArmPose;
import net.minecraft.world.entity.EntityType;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.resources.ResourceKey;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.core.component.DataComponents;
import net.minecraft.client.renderer.entity.state.PiglinRenderState;
import net.minecraft.client.renderer.entity.state.EntityRenderState;

import java.util.List;

public abstract class AbstractPiglinInfo extends MonsterInfo {
	public static final Synched immune = Synched.ZombificationImmune;
	public int timeInOverworld;

	public AbstractPiglinInfo(EntityType type, Class typeClass, Player player, CompoundTag compound) {
		super(type, typeClass, player, compound);
	}

	// Custom Code
	@Override
	public void reset() {
		super.reset();
		this.reset(immune);
		this.timeInOverworld = 0;
	}
	
	@Override
	public EntityRenderState createState() {
		return new PiglinRenderState();
	}

	@Override
	public void modifyState(EntityRenderState state) {
		super.modifyState(state);
		if (state instanceof PiglinRenderState piglinState) {
			piglinState.isBrute = this.getType() == EntityType.PIGLIN_BRUTE;
			piglinState.armPose = this.getArmPose();
			piglinState.maxCrossbowChageDuration = (float) CrossbowItem.getChargeDuration(this.getUseItem(), this.player);
			piglinState.isConverting = this.isConverting();
		}
	}

	@Override
	public ResourceKey<Level> getRespawnDimension() {
		return Level.NETHER;
	}

	@Override
	public List<ResourceKey<Structure>> getRespawnStructures(ResourceKey<Level> dimension) {
		return dimension.equals(Level.NETHER) ? List.of(BuiltinStructures.BASTION_REMNANT) : List.of();
	}

	@Override
	public float getSpeed(float original) {
		return super.getSpeed(original) * 0.75f;
	}

	@Override
	public Diet getDiet() {
		Diet diet = Diet.carnivore(false);
		diet.addFood(Items.CARROT);
		diet.addFood(Items.POTATO);
		diet.addFood(Items.BAKED_POTATO);
		diet.addFood(Items.GOLDEN_CARROT);
		diet.addFood(Items.BEETROOT);
		diet.addFood(Items.BEETROOT_SOUP);
		diet.addFood(Items.NETHER_WART, FoodFactory.createConsumeData(FoodFactory.addEffect(CustomEffects.NetherProtection, 1200)), FoodFactory.createFoodData(2, 0.1f));
		return diet;
	}

	@Override
	public void ate(ItemStack stack, Consumable consumer, FoodProperties foodInfo) {
		if (stack.is(Items.NETHER_WART))
			this.getCooldowns().addCooldown(stack, 20);
	}

	// AbstractPiglin.class Code
	public abstract boolean canHunt();

	public void setImmuneToZombification(boolean immune) {
		this.set(this.immune, immune);
	}

	public boolean isImmuneToZombification() {
		return this.get(immune);
	}

	@Override
	public CompoundTag saveData() {
		CompoundTag compound = super.saveData();
		if (this.isImmuneToZombification())
			compound.putBoolean("IsImmuneToZombification", true);
		compound.putInt("TimeInOverworld", this.timeInOverworld);
		return compound;
	}

	@Override
	public void loadData(CompoundTag compound) {
		super.loadData(compound);
		this.setImmuneToZombification(compound.getBoolean("IsImmuneToZombification"));
		this.timeInOverworld = compound.getInt("TimeInOverworld");
	}

	@Override
	public boolean aiStep(Object object) {
		if (!super.aiStep(null))
			return false;
		if (this.isConverting()) {
			this.timeInOverworld++;
		} else {
			this.timeInOverworld = 0;
		}
		if (this.timeInOverworld > 300 && this.level() instanceof ServerLevel server) {
			this.playConvertedSound();
			this.finishConversion(server);
		}
		return true;
	}

	public void setTimeInOverworld(int time) {
		this.timeInOverworld = time;
	}

	public boolean isConverting() {
		return !this.level().dimensionType().piglinSafe() && !this.isImmuneToZombification() && !this.hasEffect(CustomEffects.NetherProtection);
	}

	public void finishConversion(ServerLevel server) {
		this.changeInfo(EntityType.ZOMBIFIED_PIGLIN, false);
	}

	public boolean isAdult() {
		return !this.isBaby();
	}

	public abstract PiglinArmPose getArmPose();

	public boolean isHoldingMeleeWeapon() {
		return this.getMainHandItem().has(DataComponents.TOOL);
	}

	public abstract void playConvertedSound();
}
⚠️ **GitHub.com Fallback** ⚠️