AbstractPiglin .java

net.minecraft.world.entity.monster.piglin.AbstractPiglin (class): members: isImmuneToZombification, DEFAULT_PICK_UP_LOOT, isHoldingMeleeWeapon, canHunt.

net.minecraft.world.entity.monster.piglinclass139 linescommonextends Monster4.5 KB

API Surface (12)

void setImmuneToZombification(final boolean isImmuneToZombification)
boolean isImmuneToZombification()
void defineSynchedData(final SynchedEntityData.Builder entityData)
void addAdditionalSaveData(final ValueOutput output)
void readAdditionalSaveData(final ValueInput input)
void customServerAiStep(final ServerLevel level)
void setTimeInOverworld(final int timeInOverworld)
boolean isConverting()
void finishConversion(final ServerLevel level)
boolean isAdult()
boolean isHoldingMeleeWeapon()
void playAmbientSound()

Fields & Constants (6)

DATA_IMMUNE_TO_ZOMBIFICATION
CONVERSION_TIME
DEFAULT_IMMUNE_TO_ZOMBIFICATION
DEFAULT_PICK_UP_LOOT
DEFAULT_TIME_IN_OVERWORLD
timeInOverworld

String literals (3)

"IsImmuneToZombification"
"TimeInOverworld"
"CanPickUpLoot"

Full Source

AbstractPiglin.java139 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.monster.piglin;
2
3
import com.google.common.annotations.VisibleForTesting;
4
import net.minecraft.core.component.DataComponents;
5
import net.minecraft.network.syncher.EntityDataAccessor;
6
import net.minecraft.network.syncher.EntityDataSerializers;
7
import net.minecraft.network.syncher.SynchedEntityData;
8
import net.minecraft.server.level.ServerLevel;
9
import net.minecraft.world.Difficulty;
10
import net.minecraft.world.attribute.EnvironmentAttributes;
11
import net.minecraft.world.effect.MobEffectInstance;
12
import net.minecraft.world.effect.MobEffects;
13
import net.minecraft.world.entity.ConversionParams;
14
import net.minecraft.world.entity.EntityType;
15
import net.minecraft.world.entity.EntityTypes;
16
import net.minecraft.world.entity.LivingEntity;
17
import net.minecraft.world.entity.ai.util.GoalUtils;
18
import net.minecraft.world.entity.monster.Monster;
19
import net.minecraft.world.level.Level;
20
import net.minecraft.world.level.pathfinder.PathType;
21
import net.minecraft.world.level.storage.ValueInput;
22
import net.minecraft.world.level.storage.ValueOutput;
23
import org.jspecify.annotations.Nullable;
24
25
public abstract class AbstractPiglin extends Monster {
26
	protected static final EntityDataAccessor<Boolean> DATA_IMMUNE_TO_ZOMBIFICATION = SynchedEntityData.defineId(
27
		AbstractPiglin.class, EntityDataSerializers.BOOLEAN
28
	);
29
	public static final int CONVERSION_TIME = 300;
30
	private static final boolean DEFAULT_IMMUNE_TO_ZOMBIFICATION = false;
31
	private static final boolean DEFAULT_PICK_UP_LOOT = true;
32
	private static final int DEFAULT_TIME_IN_OVERWORLD = 0;
33
	protected int timeInOverworld = 0;
34
35
	public AbstractPiglin(final EntityType<? extends AbstractPiglin> type, final Level level) {
36
		super(type, level);
37
		this.setCanPickUpLoot(true);
38
		this.applyOpenDoorsAbility();
39
		this.setPathfindingMalus(PathType.FIRE_IN_NEIGHBOR, 16.0F);
40
		this.setPathfindingMalus(PathType.FIRE, -1.0F);
41
	}
42
43
	private void applyOpenDoorsAbility() {
44
		if (GoalUtils.hasGroundPathNavigation(this)) {
45
			this.getNavigation().setCanOpenDoors(true);
46
		}
47
	}
48
49
	protected abstract boolean canHunt();
50
51
	public void setImmuneToZombification(final boolean isImmuneToZombification) {
52
		this.getEntityData().set(DATA_IMMUNE_TO_ZOMBIFICATION, isImmuneToZombification);
53
	}
54
55
	protected boolean isImmuneToZombification() {
56
		return this.getEntityData().get(DATA_IMMUNE_TO_ZOMBIFICATION);
57
	}
58
59
	@Override
60
	protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
61
		super.defineSynchedData(entityData);
62
		entityData.define(DATA_IMMUNE_TO_ZOMBIFICATION, false);
63
	}
64
65
	@Override
66
	protected void addAdditionalSaveData(final ValueOutput output) {
67
		super.addAdditionalSaveData(output);
68
		output.putBoolean("IsImmuneToZombification", this.isImmuneToZombification());
69
		output.putInt("TimeInOverworld", this.timeInOverworld);
70
	}
71
72
	@Override
73
	protected void readAdditionalSaveData(final ValueInput input) {
74
		super.readAdditionalSaveData(input);
75
		this.setCanPickUpLoot(input.getBooleanOr("CanPickUpLoot", true));
76
		this.setImmuneToZombification(input.getBooleanOr("IsImmuneToZombification", false));
77
		this.timeInOverworld = input.getIntOr("TimeInOverworld", 0);
78
	}
79
80
	@Override
81
	protected void customServerAiStep(final ServerLevel level) {
82
		super.customServerAiStep(level);
83
		if (this.isConverting()) {
84
			this.timeInOverworld++;
85
		} else {
86
			this.timeInOverworld = 0;
87
		}
88
89
		if (this.timeInOverworld > 300) {
90
			if (level.getDifficulty() != Difficulty.PEACEFUL) {
91
				this.playConvertedSound();
92
			}
93
94
			this.finishConversion(level);
95
		}
96
	}
97
98
	@VisibleForTesting
99
	public void setTimeInOverworld(final int timeInOverworld) {
100
		this.timeInOverworld = timeInOverworld;
101
	}
102
103
	public boolean isConverting() {
104
		return !this.isImmuneToZombification()
105
			&& !this.isNoAi()
106
			&& this.level().environmentAttributes().getValue(EnvironmentAttributes.PIGLINS_ZOMBIFY, this.position());
107
	}
108
109
	protected void finishConversion(final ServerLevel level) {
110
		this.convertTo(
111
			EntityTypes.ZOMBIFIED_PIGLIN, ConversionParams.single(this, true, true), zombified -> zombified.addEffect(new MobEffectInstance(MobEffects.NAUSEA, 200, 0))
112
		);
113
	}
114
115
	public boolean isAdult() {
116
		return !this.isBaby();
117
	}
118
119
	public abstract PiglinArmPose getArmPose();
120
121
	@Override
122
	public @Nullable LivingEntity getTarget() {
123
		return this.getTargetFromBrain();
124
	}
125
126
	protected boolean isHoldingMeleeWeapon() {
127
		return this.getMainHandItem().has(DataComponents.TOOL);
128
	}
129
130
	@Override
131
	public void playAmbientSound() {
132
		if (PiglinAi.isIdle(this)) {
133
			super.playAmbientSound();
134
		}
135
	}
136
137
	protected abstract void playConvertedSound();
138
}
139

Decompiled Minecraft 26.3 source of AbstractPiglin (net/minecraft/world/entity/monster/piglin/AbstractPiglin.java) with Mojang official mappings, stored verbatim on the site.