IronGolem .java

net.minecraft.world.entity.animal.golem.IronGolem (class, 330 lines): members: DEFAULT_PLAYER_CREATED, IRON_INGOT_HEAL_AMOUNT, getAttackAnimationTick, die.

net.minecraft.world.entity.animal.golemclass330 linescommonextends AbstractGolem11.3 KB

Implements

API Surface (30)

void registerGoals()
void defineSynchedData(final SynchedEntityData.Builder entityData)
AttributeSupplier.Builder createAttributes()
int decreaseAirSupply(final int currentSupply)
void doPush(final Entity entity)
void aiStep()
boolean canSpawnSprintParticle()
boolean canAttack(final LivingEntity target)
void addAdditionalSaveData(final ValueOutput output)
void readAdditionalSaveData(final ValueInput input)
void startPersistentAngerTimer()
void setPersistentAngerEndTime(final long endTime)
long getPersistentAngerEndTime()
void setPersistentAngerTarget(final @Nullable EntityReference<LivingEntity> persistentAngerTarget)
boolean doHurtTarget(final ServerLevel level, final Entity target)
boolean hurtServer(final ServerLevel level, final DamageSource source, final float damage)
Crackiness.Level getCrackiness()
void handleEntityEvent(final @EntityEvent.Value byte id)
int getAttackAnimationTick()
void offerFlower(final boolean offer)
SoundEvent getHurtSound(final DamageSource source)
SoundEvent getDeathSound()
InteractionResult mobInteract(final Player player, final InteractionHand hand)
void playStepSound(final BlockPos pos, final BlockState blockState)
int getOfferFlowerTick()
boolean isPlayerCreated()
void setPlayerCreated(final boolean value)
void die(final DamageSource source)
boolean checkSpawnObstruction(final LevelReader level)
Vec3 getLeashOffset()

Fields & Constants (8)

DATA_FLAGS_ID
IRON_INGOT_HEAL_AMOUNT
DEFAULT_PLAYER_CREATED
PERSISTENT_ANGER_TIME
attackAnimationTick
offerFlowerTick
persistentAngerEndTime
persistentAngerTarget

String literals (1)

"PlayerCreated"

Full Source

IronGolem.java330 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.animal.golem;
2
3
import net.minecraft.core.BlockPos;
4
import net.minecraft.network.syncher.EntityDataAccessor;
5
import net.minecraft.network.syncher.EntityDataSerializers;
6
import net.minecraft.network.syncher.SynchedEntityData;
7
import net.minecraft.server.level.ServerLevel;
8
import net.minecraft.sounds.SoundEvent;
9
import net.minecraft.sounds.SoundEvents;
10
import net.minecraft.util.TimeUtil;
11
import net.minecraft.util.valueproviders.UniformInt;
12
import net.minecraft.world.InteractionHand;
13
import net.minecraft.world.InteractionResult;
14
import net.minecraft.world.damagesource.DamageSource;
15
import net.minecraft.world.entity.Crackiness;
16
import net.minecraft.world.entity.Entity;
17
import net.minecraft.world.entity.EntityEvent;
18
import net.minecraft.world.entity.EntityReference;
19
import net.minecraft.world.entity.EntityType;
20
import net.minecraft.world.entity.EntityTypes;
21
import net.minecraft.world.entity.LivingEntity;
22
import net.minecraft.world.entity.Mob;
23
import net.minecraft.world.entity.NeutralMob;
24
import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
25
import net.minecraft.world.entity.ai.attributes.Attributes;
26
import net.minecraft.world.entity.ai.goal.GolemRandomStrollInVillageGoal;
27
import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;
28
import net.minecraft.world.entity.ai.goal.MeleeAttackGoal;
29
import net.minecraft.world.entity.ai.goal.MoveBackToVillageGoal;
30
import net.minecraft.world.entity.ai.goal.MoveTowardsTargetGoal;
31
import net.minecraft.world.entity.ai.goal.OfferFlowerGoal;
32
import net.minecraft.world.entity.ai.goal.RandomLookAroundGoal;
33
import net.minecraft.world.entity.ai.goal.target.DefendVillageTargetGoal;
34
import net.minecraft.world.entity.ai.goal.target.HurtByTargetGoal;
35
import net.minecraft.world.entity.ai.goal.target.NearestAttackableTargetGoal;
36
import net.minecraft.world.entity.ai.goal.target.ResetUniversalAngerTargetGoal;
37
import net.minecraft.world.entity.monster.Creeper;
38
import net.minecraft.world.entity.monster.Enemy;
39
import net.minecraft.world.entity.player.Player;
40
import net.minecraft.world.item.ItemStack;
41
import net.minecraft.world.item.Items;
42
import net.minecraft.world.item.enchantment.EnchantmentHelper;
43
import net.minecraft.world.level.Level;
44
import net.minecraft.world.level.LevelReader;
45
import net.minecraft.world.level.NaturalSpawner;
46
import net.minecraft.world.level.block.state.BlockState;
47
import net.minecraft.world.level.material.Fluids;
48
import net.minecraft.world.level.storage.ValueInput;
49
import net.minecraft.world.level.storage.ValueOutput;
50
import net.minecraft.world.phys.Vec3;
51
import org.jspecify.annotations.Nullable;
52
53
public class IronGolem extends AbstractGolem implements NeutralMob {
54
	protected static final EntityDataAccessor<Byte> DATA_FLAGS_ID = SynchedEntityData.defineId(IronGolem.class, EntityDataSerializers.BYTE);
55
	private static final int IRON_INGOT_HEAL_AMOUNT = 25;
56
	private static final boolean DEFAULT_PLAYER_CREATED = false;
57
	private int attackAnimationTick;
58
	private int offerFlowerTick;
59
	private static final UniformInt PERSISTENT_ANGER_TIME = TimeUtil.rangeOfSeconds(20, 39);
60
	private long persistentAngerEndTime;
61
	private @Nullable EntityReference<LivingEntity> persistentAngerTarget;
62
63
	public IronGolem(final EntityType<? extends IronGolem> type, final Level level) {
64
		super(type, level);
65
	}
66
67
	@Override
68
	protected void registerGoals() {
69
		this.goalSelector.addGoal(1, new MeleeAttackGoal(this, 1.0, true));
70
		this.goalSelector.addGoal(2, new MoveTowardsTargetGoal(this, 0.9, 32.0F));
71
		this.goalSelector.addGoal(2, new MoveBackToVillageGoal(this, 0.6, false));
72
		this.goalSelector.addGoal(4, new GolemRandomStrollInVillageGoal(this, 0.6));
73
		this.goalSelector.addGoal(5, new OfferFlowerGoal(this));
74
		this.goalSelector.addGoal(7, new LookAtPlayerGoal(this, Player.class, 6.0F));
75
		this.goalSelector.addGoal(8, new RandomLookAroundGoal(this));
76
		this.targetSelector.addGoal(1, new DefendVillageTargetGoal(this));
77
		this.targetSelector.addGoal(2, new HurtByTargetGoal(this));
78
		this.targetSelector.addGoal(3, new NearestAttackableTargetGoal<>(this, Player.class, 10, true, false, this::isAngryAt));
79
		this.targetSelector
80
			.addGoal(3, new NearestAttackableTargetGoal<>(this, Mob.class, 5, false, false, (target, var1) -> target instanceof Enemy && !(target instanceof Creeper)));
81
		this.targetSelector.addGoal(4, new ResetUniversalAngerTargetGoal<>(this, false));
82
	}
83
84
	@Override
85
	protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
86
		super.defineSynchedData(entityData);
87
		entityData.define(DATA_FLAGS_ID, (byte)0);
88
	}
89
90
	public static AttributeSupplier.Builder createAttributes() {
91
		return Mob.createMobAttributes()
92
			.add(Attributes.MAX_HEALTH, 100.0)
93
			.add(Attributes.MOVEMENT_SPEED, 0.25)
94
			.add(Attributes.KNOCKBACK_RESISTANCE, 1.0)
95
			.add(Attributes.ATTACK_DAMAGE, 15.0)
96
			.add(Attributes.STEP_HEIGHT, 1.0);
97
	}
98
99
	@Override
100
	protected int decreaseAirSupply(final int currentSupply) {
101
		return currentSupply;
102
	}
103
104
	@Override
105
	protected void doPush(final Entity entity) {
106
		if (entity instanceof Enemy && !(entity instanceof Creeper) && this.getRandom().nextInt(20) == 0) {
107
			this.setTarget((LivingEntity)entity);
108
		}
109
110
		super.doPush(entity);
111
	}
112
113
	@Override
114
	public void aiStep() {
115
		super.aiStep();
116
		if (this.attackAnimationTick > 0) {
117
			this.attackAnimationTick--;
118
		}
119
120
		if (this.offerFlowerTick > 0) {
121
			this.offerFlowerTick--;
122
		}
123
124
		if (!this.level().isClientSide()) {
125
			this.updatePersistentAnger((ServerLevel)this.level(), true);
126
		}
127
	}
128
129
	@Override
130
	public boolean canSpawnSprintParticle() {
131
		return this.getDeltaMovement().horizontalDistanceSqr() > 2.5000003E-7F && this.random.nextInt(5) == 0;
132
	}
133
134
	@Override
135
	public boolean canAttack(final LivingEntity target) {
136
		if (this.isPlayerCreated() && target.is(EntityTypes.PLAYER)) {
137
			return false;
138
		} else {
139
			return target.is(EntityTypes.CREEPER) ? false : super.canAttack(target);
140
		}
141
	}
142
143
	@Override
144
	protected void addAdditionalSaveData(final ValueOutput output) {
145
		super.addAdditionalSaveData(output);
146
		output.putBoolean("PlayerCreated", this.isPlayerCreated());
147
		this.addPersistentAngerSaveData(output);
148
	}
149
150
	@Override
151
	protected void readAdditionalSaveData(final ValueInput input) {
152
		super.readAdditionalSaveData(input);
153
		this.setPlayerCreated(input.getBooleanOr("PlayerCreated", false));
154
		this.readPersistentAngerSaveData(this.level(), input);
155
	}
156
157
	@Override
158
	public void startPersistentAngerTimer() {
159
		this.setTimeToRemainAngry(PERSISTENT_ANGER_TIME.sample(this.random));
160
	}
161
162
	@Override
163
	public void setPersistentAngerEndTime(final long endTime) {
164
		this.persistentAngerEndTime = endTime;
165
	}
166
167
	@Override
168
	public long getPersistentAngerEndTime() {
169
		return this.persistentAngerEndTime;
170
	}
171
172
	@Override
173
	public void setPersistentAngerTarget(final @Nullable EntityReference<LivingEntity> persistentAngerTarget) {
174
		this.persistentAngerTarget = persistentAngerTarget;
175
	}
176
177
	@Override
178
	public @Nullable EntityReference<LivingEntity> getPersistentAngerTarget() {
179
		return this.persistentAngerTarget;
180
	}
181
182
	private float getAttackDamage() {
183
		return (float)this.getAttributeValue(Attributes.ATTACK_DAMAGE);
184
	}
185
186
	@Override
187
	public boolean doHurtTarget(final ServerLevel level, final Entity target) {
188
		this.attackAnimationTick = 10;
189
		level.broadcastEntityEvent(this, (byte)4);
190
		float attackDamage = this.getAttackDamage();
191
		float damage = (int)attackDamage > 0 ? attackDamage / 2.0F + this.random.nextInt((int)attackDamage) : attackDamage;
192
		DamageSource damageSource = this.damageSources().mobAttack(this);
193
		boolean hurt = target.hurtServer(level, damageSource, damage);
194
		if (hurt) {
195
			double knockbackResistance = target instanceof LivingEntity livingEntity ? livingEntity.getAttributeValue(Attributes.KNOCKBACK_RESISTANCE) : 0.0;
196
			double scale = Math.max(0.0, 1.0 - knockbackResistance);
197
			target.setDeltaMovement(target.getDeltaMovement().add(0.0, 0.4F * scale, 0.0));
198
			EnchantmentHelper.doPostAttackEffects(level, target, damageSource);
199
		}
200
201
		this.playSound(SoundEvents.IRON_GOLEM_ATTACK, 1.0F, 1.0F);
202
		return hurt;
203
	}
204
205
	@Override
206
	public boolean hurtServer(final ServerLevel level, final DamageSource source, final float damage) {
207
		Crackiness.Level previousCrackiness = this.getCrackiness();
208
		boolean wasHurt = super.hurtServer(level, source, damage);
209
		if (wasHurt && this.getCrackiness() != previousCrackiness) {
210
			this.playSound(SoundEvents.IRON_GOLEM_DAMAGE, 1.0F, 1.0F);
211
		}
212
213
		return wasHurt;
214
	}
215
216
	public Crackiness.Level getCrackiness() {
217
		return Crackiness.GOLEM.byFraction(this.getHealth() / this.getMaxHealth());
218
	}
219
220
	@Override
221
	public void handleEntityEvent(final @EntityEvent.Value byte id) {
222
		if (id == 4) {
223
			this.attackAnimationTick = 10;
224
			this.playSound(SoundEvents.IRON_GOLEM_ATTACK, 1.0F, 1.0F);
225
		} else if (id == 11) {
226
			this.offerFlowerTick = 400;
227
		} else if (id == 34) {
228
			this.offerFlowerTick = 0;
229
		} else {
230
			super.handleEntityEvent(id);
231
		}
232
	}
233
234
	public int getAttackAnimationTick() {
235
		return this.attackAnimationTick;
236
	}
237
238
	public void offerFlower(final boolean offer) {
239
		if (offer) {
240
			this.offerFlowerTick = 400;
241
			this.level().broadcastEntityEvent(this, (byte)11);
242
		} else {
243
			this.offerFlowerTick = 0;
244
			this.level().broadcastEntityEvent(this, (byte)34);
245
		}
246
	}
247
248
	@Override
249
	protected SoundEvent getHurtSound(final DamageSource source) {
250
		return SoundEvents.IRON_GOLEM_HURT;
251
	}
252
253
	@Override
254
	protected SoundEvent getDeathSound() {
255
		return SoundEvents.IRON_GOLEM_DEATH;
256
	}
257
258
	@Override
259
	protected InteractionResult mobInteract(final Player player, final InteractionHand hand) {
260
		ItemStack itemStack = player.getItemInHand(hand);
261
		if (!itemStack.is(Items.IRON_INGOT)) {
262
			return InteractionResult.PASS;
263
		}
264
265
		float healthBefore = this.getHealth();
266
		this.heal(25.0F);
267
		if (this.getHealth() == healthBefore) {
268
			return InteractionResult.PASS;
269
		}
270
271
		float pitch = 1.0F + (this.random.nextFloat() - this.random.nextFloat()) * 0.2F;
272
		this.playSound(SoundEvents.IRON_GOLEM_REPAIR, 1.0F, pitch);
273
		itemStack.consume(1, player);
274
		return InteractionResult.SUCCESS;
275
	}
276
277
	@Override
278
	protected void playStepSound(final BlockPos pos, final BlockState blockState) {
279
		this.playSound(SoundEvents.IRON_GOLEM_STEP, 1.0F, 1.0F);
280
	}
281
282
	public int getOfferFlowerTick() {
283
		return this.offerFlowerTick;
284
	}
285
286
	public boolean isPlayerCreated() {
287
		return (this.entityData.get(DATA_FLAGS_ID) & 1) != 0;
288
	}
289
290
	public void setPlayerCreated(final boolean value) {
291
		byte current = this.entityData.get(DATA_FLAGS_ID);
292
		if (value) {
293
			this.entityData.set(DATA_FLAGS_ID, (byte)(current | 1));
294
		} else {
295
			this.entityData.set(DATA_FLAGS_ID, (byte)(current & -2));
296
		}
297
	}
298
299
	@Override
300
	public void die(final DamageSource source) {
301
		super.die(source);
302
	}
303
304
	@Override
305
	public boolean checkSpawnObstruction(final LevelReader level) {
306
		BlockPos pos = this.blockPosition();
307
		BlockPos belowPos = pos.below();
308
		BlockState below = level.getBlockState(belowPos);
309
		if (!below.entityCanStandOn(level, belowPos, this)) {
310
			return false;
311
		}
312
313
		for (int i = 1; i < 3; i++) {
314
			BlockPos abovePos = pos.above(i);
315
			BlockState above = level.getBlockState(abovePos);
316
			if (!NaturalSpawner.isValidEmptySpawnBlock(level, abovePos, above, above.getFluidState(), EntityTypes.IRON_GOLEM)) {
317
				return false;
318
			}
319
		}
320
321
		return NaturalSpawner.isValidEmptySpawnBlock(level, pos, level.getBlockState(pos), Fluids.EMPTY.defaultFluidState(), EntityTypes.IRON_GOLEM)
322
			&& level.isUnobstructed(this);
323
	}
324
325
	@Override
326
	public Vec3 getLeashOffset() {
327
		return new Vec3(0.0, 0.875F * this.getEyeHeight(), this.getBbWidth() * 0.4F);
328
	}
329
}
330

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