PolarBear .java

net.minecraft.world.entity.animal.polarbear.PolarBear (class, 338 lines): members: PolarBearAttackPlayersGoal, PolarBearHurtByTargetGoal, DATA_STANDING_ID.

net.minecraft.world.entity.animal.polarbearclass338 linescommonextends Animal11.4 KB

Implements

API Surface (29)

boolean isFood(final ItemStack itemStack)
void registerGoals()
AttributeSupplier.Builder createAttributes()
boolean checkPolarBearSpawnRules(final EntityType<PolarBear> type, final LevelAccessor level, final EntitySpawnReason spawnReason, final BlockPos pos, final RandomSource random)
void readAdditionalSaveData(final ValueInput input)
void addAdditionalSaveData(final ValueOutput output)
void startPersistentAngerTimer()
void setPersistentAngerEndTime(final long endTime)
long getPersistentAngerEndTime()
void setPersistentAngerTarget(final @Nullable EntityReference<LivingEntity> persistentAngerTarget)
SoundEvent getAmbientSound()
SoundEvent getHurtSound(final DamageSource source)
SoundEvent getDeathSound()
void playStepSound(final BlockPos pos, final BlockState blockState)
void playWarningSound()
void defineSynchedData(final SynchedEntityData.Builder entityData)
void tick()
EntityDimensions getDefaultDimensions(final Pose pose)
boolean isStanding()
void setStanding(final boolean value)
float getStandingAnimationScale(final float a)
float getWaterSlowDown()
SpawnGroupData finalizeSpawn(final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, @Nullable SpawnGroupData groupData)
boolean canUse()
double getFollowDistance()
void start()
void alertOther(final Mob other, final LivingEntity hurtByMob)
void checkAndPerformAttack(final LivingEntity target)
void stop()

Fields & Constants (11)

DATA_STANDING_ID
STAND_ANIMATION_TICKS
PERSISTENT_ANGER_TIME
BABY_DIMENSIONS
clientSideStandAnimationO
clientSideStandAnimation
warningSoundTicks
persistentAngerEndTime
persistentAngerTarget
pos
spawnReason

Same Package (1)

Code Graph: Dependencies (52)

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

Full Source

PolarBear.java338 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.animal.polarbear;
2
3
import net.minecraft.core.BlockPos;
4
import net.minecraft.core.Holder;
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.sounds.SoundEvent;
10
import net.minecraft.sounds.SoundEvents;
11
import net.minecraft.tags.BiomeTags;
12
import net.minecraft.tags.BlockTags;
13
import net.minecraft.tags.DamageTypeTags;
14
import net.minecraft.util.Mth;
15
import net.minecraft.util.RandomSource;
16
import net.minecraft.util.TimeUtil;
17
import net.minecraft.util.valueproviders.UniformInt;
18
import net.minecraft.world.DifficultyInstance;
19
import net.minecraft.world.damagesource.DamageSource;
20
import net.minecraft.world.entity.AgeableMob;
21
import net.minecraft.world.entity.EntityAttachment;
22
import net.minecraft.world.entity.EntityAttachments;
23
import net.minecraft.world.entity.EntityDimensions;
24
import net.minecraft.world.entity.EntityReference;
25
import net.minecraft.world.entity.EntitySpawnReason;
26
import net.minecraft.world.entity.EntityType;
27
import net.minecraft.world.entity.EntityTypes;
28
import net.minecraft.world.entity.LivingEntity;
29
import net.minecraft.world.entity.Mob;
30
import net.minecraft.world.entity.NeutralMob;
31
import net.minecraft.world.entity.Pose;
32
import net.minecraft.world.entity.SpawnGroupData;
33
import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
34
import net.minecraft.world.entity.ai.attributes.Attributes;
35
import net.minecraft.world.entity.ai.goal.FloatGoal;
36
import net.minecraft.world.entity.ai.goal.FollowParentGoal;
37
import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;
38
import net.minecraft.world.entity.ai.goal.MeleeAttackGoal;
39
import net.minecraft.world.entity.ai.goal.PanicGoal;
40
import net.minecraft.world.entity.ai.goal.RandomLookAroundGoal;
41
import net.minecraft.world.entity.ai.goal.RandomStrollGoal;
42
import net.minecraft.world.entity.ai.goal.target.HurtByTargetGoal;
43
import net.minecraft.world.entity.ai.goal.target.NearestAttackableTargetGoal;
44
import net.minecraft.world.entity.ai.goal.target.ResetUniversalAngerTargetGoal;
45
import net.minecraft.world.entity.animal.Animal;
46
import net.minecraft.world.entity.animal.fox.Fox;
47
import net.minecraft.world.entity.player.Player;
48
import net.minecraft.world.item.ItemStack;
49
import net.minecraft.world.level.Level;
50
import net.minecraft.world.level.LevelAccessor;
51
import net.minecraft.world.level.ServerLevelAccessor;
52
import net.minecraft.world.level.biome.Biome;
53
import net.minecraft.world.level.block.state.BlockState;
54
import net.minecraft.world.level.storage.ValueInput;
55
import net.minecraft.world.level.storage.ValueOutput;
56
import org.jspecify.annotations.Nullable;
57
58
public class PolarBear extends Animal implements NeutralMob {
59
	private static final EntityDataAccessor<Boolean> DATA_STANDING_ID = SynchedEntityData.defineId(PolarBear.class, EntityDataSerializers.BOOLEAN);
60
	private static final float STAND_ANIMATION_TICKS = 6.0F;
61
	private float clientSideStandAnimationO;
62
	private float clientSideStandAnimation;
63
	private int warningSoundTicks;
64
	private static final UniformInt PERSISTENT_ANGER_TIME = TimeUtil.rangeOfSeconds(20, 39);
65
	private long persistentAngerEndTime;
66
	private @Nullable EntityReference<LivingEntity> persistentAngerTarget;
67
	private static final EntityDimensions BABY_DIMENSIONS = EntityDimensions.scalable(0.7F, 0.7F)
68
		.withEyeHeight(0.34375F)
69
		.withAttachments(EntityAttachments.builder().attach(EntityAttachment.PASSENGER, 0.0F, 0.625F, 0.0F));
70
71
	public PolarBear(final EntityType<? extends PolarBear> type, final Level level) {
72
		super(type, level);
73
	}
74
75
	@Override
76
	public @Nullable AgeableMob getBreedOffspring(final ServerLevel level, final AgeableMob partner) {
77
		return EntityTypes.POLAR_BEAR.create(level, EntitySpawnReason.BREEDING);
78
	}
79
80
	@Override
81
	public boolean isFood(final ItemStack itemStack) {
82
		return false;
83
	}
84
85
	@Override
86
	protected void registerGoals() {
87
		super.registerGoals();
88
		this.goalSelector.addGoal(0, new FloatGoal(this));
89
		this.goalSelector.addGoal(1, new PolarBear.PolarBearMeleeAttackGoal());
90
		this.goalSelector.addGoal(1, new PanicGoal(this, 2.0, bear -> bear.isBaby() ? DamageTypeTags.PANIC_CAUSES : DamageTypeTags.PANIC_ENVIRONMENTAL_CAUSES));
91
		this.goalSelector.addGoal(4, new FollowParentGoal(this, 1.25));
92
		this.goalSelector.addGoal(5, new RandomStrollGoal(this, 1.0));
93
		this.goalSelector.addGoal(6, new LookAtPlayerGoal(this, Player.class, 6.0F));
94
		this.goalSelector.addGoal(7, new RandomLookAroundGoal(this));
95
		this.targetSelector.addGoal(1, new PolarBear.PolarBearHurtByTargetGoal());
96
		this.targetSelector.addGoal(2, new PolarBear.PolarBearAttackPlayersGoal());
97
		this.targetSelector.addGoal(3, new NearestAttackableTargetGoal<>(this, Player.class, 10, true, false, this::isAngryAt));
98
		this.targetSelector.addGoal(4, new NearestAttackableTargetGoal<>(this, Fox.class, 10, true, true, (target, level) -> !this.isBaby()));
99
		this.targetSelector.addGoal(5, new ResetUniversalAngerTargetGoal<>(this, false));
100
	}
101
102
	public static AttributeSupplier.Builder createAttributes() {
103
		return Animal.createAnimalAttributes()
104
			.add(Attributes.MAX_HEALTH, 30.0)
105
			.add(Attributes.FOLLOW_RANGE, 20.0)
106
			.add(Attributes.MOVEMENT_SPEED, 0.25)
107
			.add(Attributes.ATTACK_DAMAGE, 6.0);
108
	}
109
110
	public static boolean checkPolarBearSpawnRules(
111
		final EntityType<PolarBear> type, final LevelAccessor level, final EntitySpawnReason spawnReason, final BlockPos pos, final RandomSource random
112
	) {
113
		Holder<Biome> biome = level.getBiome(pos);
114
		return !biome.is(BiomeTags.POLAR_BEARS_SPAWN_ON_ALTERNATE_BLOCKS)
115
			? checkAnimalSpawnRules(type, level, spawnReason, pos, random)
116
			: isBrightEnoughToSpawn(level, pos) && level.getBlockState(pos.below()).is(BlockTags.POLAR_BEARS_SPAWNABLE_ON_ALTERNATE);
117
	}
118
119
	@Override
120
	protected void readAdditionalSaveData(final ValueInput input) {
121
		super.readAdditionalSaveData(input);
122
		this.readPersistentAngerSaveData(this.level(), input);
123
	}
124
125
	@Override
126
	protected void addAdditionalSaveData(final ValueOutput output) {
127
		super.addAdditionalSaveData(output);
128
		this.addPersistentAngerSaveData(output);
129
	}
130
131
	@Override
132
	public void startPersistentAngerTimer() {
133
		this.setTimeToRemainAngry(PERSISTENT_ANGER_TIME.sample(this.random));
134
	}
135
136
	@Override
137
	public void setPersistentAngerEndTime(final long endTime) {
138
		this.persistentAngerEndTime = endTime;
139
	}
140
141
	@Override
142
	public long getPersistentAngerEndTime() {
143
		return this.persistentAngerEndTime;
144
	}
145
146
	@Override
147
	public void setPersistentAngerTarget(final @Nullable EntityReference<LivingEntity> persistentAngerTarget) {
148
		this.persistentAngerTarget = persistentAngerTarget;
149
	}
150
151
	@Override
152
	public @Nullable EntityReference<LivingEntity> getPersistentAngerTarget() {
153
		return this.persistentAngerTarget;
154
	}
155
156
	@Override
157
	protected SoundEvent getAmbientSound() {
158
		return this.isBaby() ? SoundEvents.POLAR_BEAR_AMBIENT_BABY : SoundEvents.POLAR_BEAR_AMBIENT;
159
	}
160
161
	@Override
162
	protected SoundEvent getHurtSound(final DamageSource source) {
163
		return SoundEvents.POLAR_BEAR_HURT;
164
	}
165
166
	@Override
167
	protected SoundEvent getDeathSound() {
168
		return SoundEvents.POLAR_BEAR_DEATH;
169
	}
170
171
	@Override
172
	protected void playStepSound(final BlockPos pos, final BlockState blockState) {
173
		this.playSound(SoundEvents.POLAR_BEAR_STEP, 0.15F, 1.0F);
174
	}
175
176
	protected void playWarningSound() {
177
		if (this.warningSoundTicks <= 0) {
178
			this.makeSound(SoundEvents.POLAR_BEAR_WARNING);
179
			this.warningSoundTicks = 40;
180
		}
181
	}
182
183
	@Override
184
	protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
185
		super.defineSynchedData(entityData);
186
		entityData.define(DATA_STANDING_ID, false);
187
	}
188
189
	@Override
190
	public void tick() {
191
		super.tick();
192
		if (this.level().isClientSide()) {
193
			if (this.clientSideStandAnimation != this.clientSideStandAnimationO) {
194
				this.refreshDimensions();
195
			}
196
197
			this.clientSideStandAnimationO = this.clientSideStandAnimation;
198
			if (this.isStanding()) {
199
				this.clientSideStandAnimation = Mth.clamp(this.clientSideStandAnimation + 1.0F, 0.0F, 6.0F);
200
			} else {
201
				this.clientSideStandAnimation = Mth.clamp(this.clientSideStandAnimation - 1.0F, 0.0F, 6.0F);
202
			}
203
		}
204
205
		if (this.warningSoundTicks > 0) {
206
			this.warningSoundTicks--;
207
		}
208
209
		if (!this.level().isClientSide()) {
210
			this.updatePersistentAnger((ServerLevel)this.level(), true);
211
		}
212
	}
213
214
	@Override
215
	public EntityDimensions getDefaultDimensions(final Pose pose) {
216
		EntityDimensions dimension = this.isBaby() ? BABY_DIMENSIONS : super.getDefaultDimensions(pose);
217
		if (this.clientSideStandAnimation > 0.0F) {
218
			float standFactor = this.clientSideStandAnimation / 6.0F;
219
			float heightScaleFactor = 1.0F + standFactor;
220
			return dimension.scale(1.0F, heightScaleFactor);
221
		} else {
222
			return dimension;
223
		}
224
	}
225
226
	public boolean isStanding() {
227
		return this.entityData.get(DATA_STANDING_ID);
228
	}
229
230
	public void setStanding(final boolean value) {
231
		this.entityData.set(DATA_STANDING_ID, value);
232
	}
233
234
	public float getStandingAnimationScale(final float a) {
235
		return Mth.lerp(a, this.clientSideStandAnimationO, this.clientSideStandAnimation) / 6.0F;
236
	}
237
238
	@Override
239
	protected float getWaterSlowDown() {
240
		return 0.98F;
241
	}
242
243
	@Override
244
	public SpawnGroupData finalizeSpawn(
245
		final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, @Nullable SpawnGroupData groupData
246
	) {
247
		if (groupData == null) {
248
			groupData = new AgeableMob.AgeableMobGroupData(1.0F);
249
		}
250
251
		return super.finalizeSpawn(level, difficulty, spawnReason, groupData);
252
	}
253
254
	private class PolarBearAttackPlayersGoal extends NearestAttackableTargetGoal<Player> {
255
		public PolarBearAttackPlayersGoal() {
256
			super(PolarBear.this, Player.class, 20, true, true, null);
257
		}
258
259
		@Override
260
		public boolean canUse() {
261
			if (PolarBear.this.isBaby()) {
262
				return false;
263
			}
264
265
			if (super.canUse()) {
266
				for (PolarBear bear : PolarBear.this.level().getEntitiesOfClass(PolarBear.class, PolarBear.this.getBoundingBox().inflate(8.0, 4.0, 8.0))) {
267
					if (bear.isBaby()) {
268
						return true;
269
					}
270
				}
271
			}
272
273
			return false;
274
		}
275
276
		@Override
277
		protected double getFollowDistance() {
278
			return super.getFollowDistance() * 0.5;
279
		}
280
	}
281
282
	private class PolarBearHurtByTargetGoal extends HurtByTargetGoal {
283
		public PolarBearHurtByTargetGoal() {
284
			super(PolarBear.this);
285
		}
286
287
		@Override
288
		public void start() {
289
			super.start();
290
			if (PolarBear.this.isBaby()) {
291
				this.alertOthers();
292
				this.stop();
293
			}
294
		}
295
296
		@Override
297
		protected void alertOther(final Mob other, final LivingEntity hurtByMob) {
298
			if (other instanceof PolarBear && !other.isBaby()) {
299
				super.alertOther(other, hurtByMob);
300
			}
301
		}
302
	}
303
304
	private class PolarBearMeleeAttackGoal extends MeleeAttackGoal {
305
		public PolarBearMeleeAttackGoal() {
306
			super(PolarBear.this, 1.25, true);
307
		}
308
309
		@Override
310
		protected void checkAndPerformAttack(final LivingEntity target) {
311
			if (this.canPerformAttack(target)) {
312
				this.resetAttackCooldown();
313
				this.mob.doHurtTarget(getServerLevel(this.mob), target);
314
				PolarBear.this.setStanding(false);
315
			} else if (this.mob.distanceToSqr(target) < (target.getBbWidth() + 3.0F) * (target.getBbWidth() + 3.0F)) {
316
				if (this.isTimeToAttack()) {
317
					PolarBear.this.setStanding(false);
318
					this.resetAttackCooldown();
319
				}
320
321
				if (this.getTicksUntilNextAttack() <= 10) {
322
					PolarBear.this.setStanding(true);
323
					PolarBear.this.playWarningSound();
324
				}
325
			} else {
326
				this.resetAttackCooldown();
327
				PolarBear.this.setStanding(false);
328
			}
329
		}
330
331
		@Override
332
		public void stop() {
333
			PolarBear.this.setStanding(false);
334
			super.stop();
335
		}
336
	}
337
}
338

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