Pig .java

net.minecraft.world.entity.animal.pig.Pig (class): members: DATA_BOOST_TIME, steering, boost, DATA_SOUND_VARIANT_ID, getRiddenSpeed, getEquipSound, isFood.

net.minecraft.world.entity.animal.pigclass319 linescommonextends Animal12.7 KB

Implements

API Surface (27)

void registerGoals()
AttributeSupplier.Builder createAttributes()
void onSyncedDataUpdated(final EntityDataAccessor<?> accessor)
void defineSynchedData(final SynchedEntityData.Builder entityData)
void addAdditionalSaveData(final ValueOutput output)
void readAdditionalSaveData(final ValueInput input)
SoundEvent getAmbientSound()
SoundEvent getHurtSound(final DamageSource source)
SoundEvent getDeathSound()
void playEatingSound()
void playStepSound(final BlockPos pos, final BlockState blockState)
InteractionResult mobInteract(final Player player, final InteractionHand hand)
boolean canUseSlot(final EquipmentSlot slot)
boolean canDispenserEquipIntoSlot(final EquipmentSlot slot)
Holder<SoundEvent> getEquipSound(final EquipmentSlot slot, final ItemStack stack, final Equippable equippable)
void thunderHit(final ServerLevel level, final LightningBolt lightningBolt)
void tickRidden(final Player controller, final Vec3 riddenInput)
Vec3 getRiddenInput(final Player controller, final Vec3 selfInput)
float getRiddenSpeed(final Player controller)
boolean boost()
EntityDimensions getDefaultDimensions(final Pose pose)
boolean isFood(final ItemStack itemStack)
Vec3 getLeashOffset()
Holder<PigVariant> getVariant()
void applyImplicitComponents(final DataComponentGetter components)
<T> boolean applyImplicitComponent(final DataComponentType<T> type, final T value)
SpawnGroupData finalizeSpawn(final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, final @Nullable SpawnGroupData groupData)

Fields & Constants (6)

DATA_BOOST_TIME
DATA_VARIANT_ID
DATA_SOUND_VARIANT_ID
BABY_DIMENSIONS
steering
spawnReason

String literals (1)

"sound_variant"

Code Graph: Dependencies (65)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

Pig.java319 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.animal.pig;
2
3
import net.minecraft.core.BlockPos;
4
import net.minecraft.core.Holder;
5
import net.minecraft.core.Registry;
6
import net.minecraft.core.component.DataComponentGetter;
7
import net.minecraft.core.component.DataComponentType;
8
import net.minecraft.core.component.DataComponents;
9
import net.minecraft.core.registries.Registries;
10
import net.minecraft.network.syncher.EntityDataAccessor;
11
import net.minecraft.network.syncher.EntityDataSerializers;
12
import net.minecraft.network.syncher.SynchedEntityData;
13
import net.minecraft.resources.ResourceKey;
14
import net.minecraft.server.level.ServerLevel;
15
import net.minecraft.sounds.SoundEvent;
16
import net.minecraft.sounds.SoundEvents;
17
import net.minecraft.tags.ItemTags;
18
import net.minecraft.world.Difficulty;
19
import net.minecraft.world.DifficultyInstance;
20
import net.minecraft.world.InteractionHand;
21
import net.minecraft.world.InteractionResult;
22
import net.minecraft.world.damagesource.DamageSource;
23
import net.minecraft.world.entity.AgeableMob;
24
import net.minecraft.world.entity.ConversionParams;
25
import net.minecraft.world.entity.EntityAttachment;
26
import net.minecraft.world.entity.EntityAttachments;
27
import net.minecraft.world.entity.EntityDimensions;
28
import net.minecraft.world.entity.EntitySpawnReason;
29
import net.minecraft.world.entity.EntityType;
30
import net.minecraft.world.entity.EntityTypes;
31
import net.minecraft.world.entity.EquipmentSlot;
32
import net.minecraft.world.entity.ItemBasedSteering;
33
import net.minecraft.world.entity.ItemSteerable;
34
import net.minecraft.world.entity.LightningBolt;
35
import net.minecraft.world.entity.LivingEntity;
36
import net.minecraft.world.entity.Pose;
37
import net.minecraft.world.entity.SpawnGroupData;
38
import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
39
import net.minecraft.world.entity.ai.attributes.Attributes;
40
import net.minecraft.world.entity.ai.goal.BreedGoal;
41
import net.minecraft.world.entity.ai.goal.FloatGoal;
42
import net.minecraft.world.entity.ai.goal.FollowParentGoal;
43
import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;
44
import net.minecraft.world.entity.ai.goal.PanicGoal;
45
import net.minecraft.world.entity.ai.goal.RandomLookAroundGoal;
46
import net.minecraft.world.entity.ai.goal.TemptGoal;
47
import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal;
48
import net.minecraft.world.entity.animal.Animal;
49
import net.minecraft.world.entity.monster.zombie.ZombifiedPiglin;
50
import net.minecraft.world.entity.player.Player;
51
import net.minecraft.world.entity.variant.SpawnContext;
52
import net.minecraft.world.entity.variant.VariantUtils;
53
import net.minecraft.world.item.ItemStack;
54
import net.minecraft.world.item.Items;
55
import net.minecraft.world.item.equipment.Equippable;
56
import net.minecraft.world.level.Level;
57
import net.minecraft.world.level.ServerLevelAccessor;
58
import net.minecraft.world.level.block.state.BlockState;
59
import net.minecraft.world.level.storage.ValueInput;
60
import net.minecraft.world.level.storage.ValueOutput;
61
import net.minecraft.world.phys.Vec3;
62
import org.jspecify.annotations.Nullable;
63
64
public class Pig extends Animal implements ItemSteerable {
65
	private static final EntityDataAccessor<Integer> DATA_BOOST_TIME = SynchedEntityData.defineId(Pig.class, EntityDataSerializers.INT);
66
	private static final EntityDataAccessor<Holder<PigVariant>> DATA_VARIANT_ID = SynchedEntityData.defineId(Pig.class, EntityDataSerializers.PIG_VARIANT);
67
	private static final EntityDataAccessor<Holder<PigSoundVariant>> DATA_SOUND_VARIANT_ID = SynchedEntityData.defineId(
68
		Pig.class, EntityDataSerializers.PIG_SOUND_VARIANT
69
	);
70
	private static final EntityDimensions BABY_DIMENSIONS = EntityDimensions.scalable(0.45F, 0.45F)
71
		.withEyeHeight(0.40625F)
72
		.withAttachments(EntityAttachments.builder().attach(EntityAttachment.PASSENGER, 0.0F, 0.5F, 0.0F));
73
	private final ItemBasedSteering steering = new ItemBasedSteering(this.entityData, DATA_BOOST_TIME);
74
75
	public Pig(final EntityType<? extends Pig> type, final Level level) {
76
		super(type, level);
77
	}
78
79
	@Override
80
	protected void registerGoals() {
81
		this.goalSelector.addGoal(0, new FloatGoal(this));
82
		this.goalSelector.addGoal(1, new PanicGoal(this, 1.25));
83
		this.goalSelector.addGoal(3, new BreedGoal(this, 1.0));
84
		this.goalSelector.addGoal(4, new TemptGoal(this, 1.2, i -> i.is(Items.CARROT_ON_A_STICK), false));
85
		this.goalSelector.addGoal(4, new TemptGoal(this, 1.2, i -> i.is(ItemTags.PIG_FOOD), false));
86
		this.goalSelector.addGoal(5, new FollowParentGoal(this, 1.1));
87
		this.goalSelector.addGoal(6, new WaterAvoidingRandomStrollGoal(this, 1.0));
88
		this.goalSelector.addGoal(7, new LookAtPlayerGoal(this, Player.class, 6.0F));
89
		this.goalSelector.addGoal(8, new RandomLookAroundGoal(this));
90
	}
91
92
	public static AttributeSupplier.Builder createAttributes() {
93
		return Animal.createAnimalAttributes().add(Attributes.MAX_HEALTH, 10.0).add(Attributes.MOVEMENT_SPEED, 0.25);
94
	}
95
96
	@Override
97
	public @Nullable LivingEntity getControllingPassenger() {
98
		return this.isSaddled() && this.getFirstPassenger() instanceof Player player && player.isHolding(Items.CARROT_ON_A_STICK)
99
			? player
100
			: super.getControllingPassenger();
101
	}
102
103
	@Override
104
	public void onSyncedDataUpdated(final EntityDataAccessor<?> accessor) {
105
		if (DATA_BOOST_TIME.equals(accessor) && this.level().isClientSide()) {
106
			this.steering.onSynced();
107
		}
108
109
		super.onSyncedDataUpdated(accessor);
110
	}
111
112
	@Override
113
	protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
114
		super.defineSynchedData(entityData);
115
		Registry<PigSoundVariant> pigSoundVariants = this.registryAccess().lookupOrThrow(Registries.PIG_SOUND_VARIANT);
116
		entityData.define(DATA_BOOST_TIME, 0);
117
		entityData.define(DATA_VARIANT_ID, VariantUtils.getDefaultOrAny(this.registryAccess(), PigVariants.DEFAULT));
118
		entityData.define(DATA_SOUND_VARIANT_ID, pigSoundVariants.get(PigSoundVariants.CLASSIC).or(pigSoundVariants::getAny).orElseThrow());
119
	}
120
121
	@Override
122
	protected void addAdditionalSaveData(final ValueOutput output) {
123
		super.addAdditionalSaveData(output);
124
		VariantUtils.writeVariant(output, this.getVariant());
125
		this.getSoundVariant()
126
			.unwrapKey()
127
			.ifPresent(soundVariant -> output.store("sound_variant", ResourceKey.codec(Registries.PIG_SOUND_VARIANT), (ResourceKey<PigSoundVariant>)soundVariant));
128
	}
129
130
	@Override
131
	protected void readAdditionalSaveData(final ValueInput input) {
132
		super.readAdditionalSaveData(input);
133
		VariantUtils.readVariant(input, Registries.PIG_VARIANT).ifPresent(this::setVariant);
134
		input.read("sound_variant", ResourceKey.codec(Registries.PIG_SOUND_VARIANT))
135
			.flatMap(soundVariant -> this.registryAccess().lookupOrThrow(Registries.PIG_SOUND_VARIANT).get((ResourceKey<PigSoundVariant>)soundVariant))
136
			.ifPresent(this::setSoundVariant);
137
	}
138
139
	@Override
140
	protected SoundEvent getAmbientSound() {
141
		return this.getSoundSet().ambientSound().value();
142
	}
143
144
	@Override
145
	protected SoundEvent getHurtSound(final DamageSource source) {
146
		return this.getSoundSet().hurtSound().value();
147
	}
148
149
	@Override
150
	protected SoundEvent getDeathSound() {
151
		return this.getSoundSet().deathSound().value();
152
	}
153
154
	@Override
155
	protected void playEatingSound() {
156
		this.makeSound(this.getSoundSet().eatSound().value());
157
	}
158
159
	@Override
160
	protected void playStepSound(final BlockPos pos, final BlockState blockState) {
161
		this.playSound(this.getSoundSet().stepSound().value(), 0.15F, 1.0F);
162
	}
163
164
	@Override
165
	public InteractionResult mobInteract(final Player player, final InteractionHand hand) {
166
		boolean hasFood = this.isFood(player.getItemInHand(hand));
167
		if (!hasFood && this.isSaddled() && !this.isVehicle() && !player.isSecondaryUseActive()) {
168
			if (!this.level().isClientSide()) {
169
				player.startRiding(this);
170
			}
171
172
			return InteractionResult.SUCCESS;
173
		} else {
174
			InteractionResult interactionResult = super.mobInteract(player, hand);
175
			if (!interactionResult.consumesAction()) {
176
				ItemStack itemStack = player.getItemInHand(hand);
177
				return this.isEquippableInSlot(itemStack, EquipmentSlot.SADDLE) ? itemStack.interactLivingEntity(player, this, hand) : InteractionResult.PASS;
178
			} else {
179
				return interactionResult;
180
			}
181
		}
182
	}
183
184
	@Override
185
	public boolean canUseSlot(final EquipmentSlot slot) {
186
		return slot != EquipmentSlot.SADDLE ? super.canUseSlot(slot) : this.isAlive() && !this.isBaby();
187
	}
188
189
	@Override
190
	protected boolean canDispenserEquipIntoSlot(final EquipmentSlot slot) {
191
		return slot == EquipmentSlot.SADDLE || super.canDispenserEquipIntoSlot(slot);
192
	}
193
194
	@Override
195
	protected Holder<SoundEvent> getEquipSound(final EquipmentSlot slot, final ItemStack stack, final Equippable equippable) {
196
		return slot == EquipmentSlot.SADDLE ? SoundEvents.PIG_SADDLE : super.getEquipSound(slot, stack, equippable);
197
	}
198
199
	@Override
200
	public void thunderHit(final ServerLevel level, final LightningBolt lightningBolt) {
201
		if (level.getDifficulty() != Difficulty.PEACEFUL) {
202
			ZombifiedPiglin zombifiedPiglin = this.convertTo(EntityTypes.ZOMBIFIED_PIGLIN, ConversionParams.single(this, false, true), zp -> {
203
				zp.populateDefaultEquipmentSlots(this.getRandom(), level.getCurrentDifficultyAt(this.blockPosition()));
204
				zp.setPersistenceRequired();
205
			});
206
			if (zombifiedPiglin == null) {
207
				super.thunderHit(level, lightningBolt);
208
			}
209
		} else {
210
			super.thunderHit(level, lightningBolt);
211
		}
212
	}
213
214
	@Override
215
	protected void tickRidden(final Player controller, final Vec3 riddenInput) {
216
		super.tickRidden(controller, riddenInput);
217
		this.setRot(controller.getYRot(), controller.getXRot() * 0.5F);
218
		this.yRotO = this.yBodyRot = this.yHeadRot = this.getYRot();
219
		this.steering.tickBoost();
220
	}
221
222
	@Override
223
	protected Vec3 getRiddenInput(final Player controller, final Vec3 selfInput) {
224
		return new Vec3(0.0, 0.0, 1.0);
225
	}
226
227
	@Override
228
	protected float getRiddenSpeed(final Player controller) {
229
		return (float)(this.getAttributeValue(Attributes.MOVEMENT_SPEED) * 0.225 * this.steering.boostFactor());
230
	}
231
232
	@Override
233
	public boolean boost() {
234
		return this.steering.boost(this.getRandom());
235
	}
236
237
	@Override
238
	public EntityDimensions getDefaultDimensions(final Pose pose) {
239
		return this.isBaby() ? BABY_DIMENSIONS : super.getDefaultDimensions(pose);
240
	}
241
242
	public @Nullable Pig getBreedOffspring(final ServerLevel level, final AgeableMob partner) {
243
		Pig baby = EntityTypes.PIG.create(level, EntitySpawnReason.BREEDING);
244
		if (baby != null && partner instanceof Pig partnerPig) {
245
			baby.setVariant(this.random.nextBoolean() ? this.getVariant() : partnerPig.getVariant());
246
		}
247
248
		return baby;
249
	}
250
251
	@Override
252
	public boolean isFood(final ItemStack itemStack) {
253
		return itemStack.is(ItemTags.PIG_FOOD);
254
	}
255
256
	@Override
257
	public Vec3 getLeashOffset() {
258
		return new Vec3(0.0, 0.6F * this.getEyeHeight(), this.getBbWidth() * 0.4F);
259
	}
260
261
	private void setVariant(final Holder<PigVariant> variant) {
262
		this.entityData.set(DATA_VARIANT_ID, variant);
263
	}
264
265
	public Holder<PigVariant> getVariant() {
266
		return this.entityData.get(DATA_VARIANT_ID);
267
	}
268
269
	private Holder<PigSoundVariant> getSoundVariant() {
270
		return this.entityData.get(DATA_SOUND_VARIANT_ID);
271
	}
272
273
	private void setSoundVariant(final Holder<PigSoundVariant> soundVariant) {
274
		this.entityData.set(DATA_SOUND_VARIANT_ID, soundVariant);
275
	}
276
277
	private PigSoundVariant.PigSoundSet getSoundSet() {
278
		return this.isBaby() ? this.getSoundVariant().value().babySounds() : this.getSoundVariant().value().adultSounds();
279
	}
280
281
	@Override
282
	public <T> @Nullable T get(final DataComponentType<? extends T> type) {
283
		if (type == DataComponents.PIG_VARIANT) {
284
			return castComponentValue((DataComponentType<T>)type, this.getVariant());
285
		} else {
286
			return type == DataComponents.PIG_SOUND_VARIANT ? castComponentValue((DataComponentType<T>)type, this.getSoundVariant()) : super.get(type);
287
		}
288
	}
289
290
	@Override
291
	protected void applyImplicitComponents(final DataComponentGetter components) {
292
		this.applyImplicitComponentIfPresent(components, DataComponents.PIG_VARIANT);
293
		this.applyImplicitComponentIfPresent(components, DataComponents.PIG_SOUND_VARIANT);
294
		super.applyImplicitComponents(components);
295
	}
296
297
	@Override
298
	protected <T> boolean applyImplicitComponent(final DataComponentType<T> type, final T value) {
299
		if (type == DataComponents.PIG_VARIANT) {
300
			this.setVariant(castComponentValue(DataComponents.PIG_VARIANT, value));
301
			return true;
302
		} else if (type == DataComponents.PIG_SOUND_VARIANT) {
303
			this.setSoundVariant(castComponentValue(DataComponents.PIG_SOUND_VARIANT, value));
304
			return true;
305
		} else {
306
			return super.applyImplicitComponent(type, value);
307
		}
308
	}
309
310
	@Override
311
	public SpawnGroupData finalizeSpawn(
312
		final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, final @Nullable SpawnGroupData groupData
313
	) {
314
		VariantUtils.selectVariantToSpawn(SpawnContext.create(level, this.blockPosition()), Registries.PIG_VARIANT).ifPresent(this::setVariant);
315
		this.setSoundVariant(PigSoundVariants.pickRandomSoundVariant(this.registryAccess(), level.getRandom()));
316
		return super.finalizeSpawn(level, difficulty, spawnReason, groupData);
317
	}
318
}
319

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