Creeper .java

net.minecraft.world.entity.monster.Creeper (class): members: DEFAULT_EXPLOSION_RADIUS, DEFAULT_MAX_SWELL, DATA_IS_IGNITED, DATA_IS_POWERED, DATA_SWELL_DIR.

net.minecraft.world.entity.monsterclass268 linescommonextends Monster9.3 KB

API Surface (21)

void registerGoals()
AttributeSupplier.Builder createAttributes()
int getMaxFallDistance()
boolean causeFallDamage(final double fallDistance, final float damageModifier, final DamageSource damageSource)
void defineSynchedData(final SynchedEntityData.Builder entityData)
void addAdditionalSaveData(final ValueOutput output)
void readAdditionalSaveData(final ValueInput input)
void tick()
void setTarget(final @Nullable LivingEntity target)
SoundEvent getHurtSound(final DamageSource source)
SoundEvent getDeathSound()
boolean killedEntity(final ServerLevel level, final LivingEntity entity, final DamageSource source)
boolean doHurtTarget(final ServerLevel level, final Entity target)
boolean isPowered()
float getSwelling(final float a)
int getSwellDir()
void setSwellDir(final int dir)
void thunderHit(final ServerLevel level, final LightningBolt lightningBolt)
InteractionResult mobInteract(final Player player, final InteractionHand hand)
boolean isIgnited()
void ignite()

Fields & Constants (12)

DATA_SWELL_DIR
DATA_IS_POWERED
DATA_IS_IGNITED
DEFAULT_IGNITED
DEFAULT_POWERED
DEFAULT_MAX_SWELL
DEFAULT_EXPLOSION_RADIUS
oldSwell
swell
maxSwell
explosionRadius
droppedSkulls

String literals (4)

"powered"
"Fuse"
"ExplosionRadius"
"ignited"

Full Source

Creeper.java268 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.monster;
2
3
import java.util.Collection;
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.tags.ItemTags;
11
import net.minecraft.util.Mth;
12
import net.minecraft.world.InteractionHand;
13
import net.minecraft.world.InteractionResult;
14
import net.minecraft.world.damagesource.DamageSource;
15
import net.minecraft.world.effect.MobEffectInstance;
16
import net.minecraft.world.entity.AreaEffectCloud;
17
import net.minecraft.world.entity.Entity;
18
import net.minecraft.world.entity.EntityType;
19
import net.minecraft.world.entity.LightningBolt;
20
import net.minecraft.world.entity.LivingEntity;
21
import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
22
import net.minecraft.world.entity.ai.attributes.Attributes;
23
import net.minecraft.world.entity.ai.goal.AvoidEntityGoal;
24
import net.minecraft.world.entity.ai.goal.FloatGoal;
25
import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;
26
import net.minecraft.world.entity.ai.goal.MeleeAttackGoal;
27
import net.minecraft.world.entity.ai.goal.RandomLookAroundGoal;
28
import net.minecraft.world.entity.ai.goal.SwellGoal;
29
import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal;
30
import net.minecraft.world.entity.ai.goal.target.HurtByTargetGoal;
31
import net.minecraft.world.entity.ai.goal.target.NearestAttackableTargetGoal;
32
import net.minecraft.world.entity.animal.feline.Cat;
33
import net.minecraft.world.entity.animal.feline.Ocelot;
34
import net.minecraft.world.entity.animal.goat.Goat;
35
import net.minecraft.world.entity.player.Player;
36
import net.minecraft.world.item.ItemStack;
37
import net.minecraft.world.item.Items;
38
import net.minecraft.world.level.Level;
39
import net.minecraft.world.level.gameevent.GameEvent;
40
import net.minecraft.world.level.storage.ValueInput;
41
import net.minecraft.world.level.storage.ValueOutput;
42
import net.minecraft.world.level.storage.loot.BuiltInLootTables;
43
import org.jspecify.annotations.Nullable;
44
45
public class Creeper extends Monster {
46
	private static final EntityDataAccessor<Integer> DATA_SWELL_DIR = SynchedEntityData.defineId(Creeper.class, EntityDataSerializers.INT);
47
	private static final EntityDataAccessor<Boolean> DATA_IS_POWERED = SynchedEntityData.defineId(Creeper.class, EntityDataSerializers.BOOLEAN);
48
	private static final EntityDataAccessor<Boolean> DATA_IS_IGNITED = SynchedEntityData.defineId(Creeper.class, EntityDataSerializers.BOOLEAN);
49
	private static final boolean DEFAULT_IGNITED = false;
50
	private static final boolean DEFAULT_POWERED = false;
51
	private static final short DEFAULT_MAX_SWELL = 30;
52
	private static final byte DEFAULT_EXPLOSION_RADIUS = 3;
53
	private int oldSwell;
54
	private int swell;
55
	private int maxSwell = 30;
56
	private int explosionRadius = 3;
57
	private boolean droppedSkulls;
58
59
	public Creeper(final EntityType<? extends Creeper> type, final Level level) {
60
		super(type, level);
61
	}
62
63
	@Override
64
	protected void registerGoals() {
65
		this.goalSelector.addGoal(1, new FloatGoal(this));
66
		this.goalSelector.addGoal(2, new SwellGoal(this));
67
		this.goalSelector.addGoal(3, new AvoidEntityGoal<>(this, Ocelot.class, 6.0F, 1.0, 1.2));
68
		this.goalSelector.addGoal(3, new AvoidEntityGoal<>(this, Cat.class, 6.0F, 1.0, 1.2));
69
		this.goalSelector.addGoal(4, new MeleeAttackGoal(this, 1.0, false));
70
		this.goalSelector.addGoal(5, new WaterAvoidingRandomStrollGoal(this, 0.8));
71
		this.goalSelector.addGoal(6, new LookAtPlayerGoal(this, Player.class, 8.0F));
72
		this.goalSelector.addGoal(6, new RandomLookAroundGoal(this));
73
		this.targetSelector.addGoal(1, new NearestAttackableTargetGoal<>(this, Player.class, true));
74
		this.targetSelector.addGoal(2, new HurtByTargetGoal(this));
75
	}
76
77
	public static AttributeSupplier.Builder createAttributes() {
78
		return Monster.createMonsterAttributes().add(Attributes.MOVEMENT_SPEED, 0.25);
79
	}
80
81
	@Override
82
	public int getMaxFallDistance() {
83
		return this.getTarget() == null ? this.getComfortableFallDistance(0.0F) : this.getComfortableFallDistance(this.getHealth() - 1.0F);
84
	}
85
86
	@Override
87
	public boolean causeFallDamage(final double fallDistance, final float damageModifier, final DamageSource damageSource) {
88
		boolean damaged = super.causeFallDamage(fallDistance, damageModifier, damageSource);
89
		this.swell += (int)(fallDistance * 1.5);
90
		if (this.swell > this.maxSwell - 5) {
91
			this.swell = this.maxSwell - 5;
92
		}
93
94
		return damaged;
95
	}
96
97
	@Override
98
	protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
99
		super.defineSynchedData(entityData);
100
		entityData.define(DATA_SWELL_DIR, -1);
101
		entityData.define(DATA_IS_POWERED, false);
102
		entityData.define(DATA_IS_IGNITED, false);
103
	}
104
105
	@Override
106
	protected void addAdditionalSaveData(final ValueOutput output) {
107
		super.addAdditionalSaveData(output);
108
		output.putBoolean("powered", this.isPowered());
109
		output.putShort("Fuse", (short)this.maxSwell);
110
		output.putByte("ExplosionRadius", (byte)this.explosionRadius);
111
		output.putBoolean("ignited", this.isIgnited());
112
	}
113
114
	@Override
115
	protected void readAdditionalSaveData(final ValueInput input) {
116
		super.readAdditionalSaveData(input);
117
		this.entityData.set(DATA_IS_POWERED, input.getBooleanOr("powered", false));
118
		this.maxSwell = input.getShortOr("Fuse", (short)30);
119
		this.explosionRadius = input.getByteOr("ExplosionRadius", (byte)3);
120
		if (input.getBooleanOr("ignited", false)) {
121
			this.ignite();
122
		}
123
	}
124
125
	@Override
126
	public void tick() {
127
		if (this.isAlive()) {
128
			this.oldSwell = this.swell;
129
			if (this.isIgnited()) {
130
				this.setSwellDir(1);
131
			}
132
133
			int swellDir = this.getSwellDir();
134
			if (swellDir > 0 && this.swell == 0) {
135
				this.playSound(SoundEvents.CREEPER_PRIMED, 1.0F, 0.5F);
136
				this.gameEvent(GameEvent.PRIME_FUSE);
137
			}
138
139
			this.swell += swellDir;
140
			if (this.swell < 0) {
141
				this.swell = 0;
142
			}
143
144
			if (this.swell >= this.maxSwell) {
145
				this.swell = this.maxSwell;
146
				this.explodeCreeper();
147
			}
148
		}
149
150
		super.tick();
151
	}
152
153
	@Override
154
	public void setTarget(final @Nullable LivingEntity target) {
155
		if (!(target instanceof Goat)) {
156
			super.setTarget(target);
157
		}
158
	}
159
160
	@Override
161
	protected SoundEvent getHurtSound(final DamageSource source) {
162
		return SoundEvents.CREEPER_HURT;
163
	}
164
165
	@Override
166
	protected SoundEvent getDeathSound() {
167
		return SoundEvents.CREEPER_DEATH;
168
	}
169
170
	@Override
171
	public boolean killedEntity(final ServerLevel level, final LivingEntity entity, final DamageSource source) {
172
		if (this.shouldDropLoot(level) && this.isPowered() && !this.droppedSkulls) {
173
			entity.dropFromLootTable(level, source, false, BuiltInLootTables.CHARGED_CREEPER, itemStack -> {
174
				entity.spawnAtLocation(level, itemStack);
175
				this.droppedSkulls = true;
176
			});
177
		}
178
179
		return super.killedEntity(level, entity, source);
180
	}
181
182
	@Override
183
	public boolean doHurtTarget(final ServerLevel level, final Entity target) {
184
		return true;
185
	}
186
187
	public boolean isPowered() {
188
		return this.entityData.get(DATA_IS_POWERED);
189
	}
190
191
	public float getSwelling(final float a) {
192
		return Mth.lerp(a, this.oldSwell, this.swell) / (this.maxSwell - 2);
193
	}
194
195
	public int getSwellDir() {
196
		return this.entityData.get(DATA_SWELL_DIR);
197
	}
198
199
	public void setSwellDir(final int dir) {
200
		this.entityData.set(DATA_SWELL_DIR, dir);
201
	}
202
203
	@Override
204
	public void thunderHit(final ServerLevel level, final LightningBolt lightningBolt) {
205
		super.thunderHit(level, lightningBolt);
206
		this.entityData.set(DATA_IS_POWERED, true);
207
	}
208
209
	@Override
210
	protected InteractionResult mobInteract(final Player player, final InteractionHand hand) {
211
		ItemStack itemStack = player.getItemInHand(hand);
212
		if (itemStack.is(ItemTags.CREEPER_IGNITERS)) {
213
			SoundEvent soundEvent = itemStack.is(Items.FIRE_CHARGE) ? SoundEvents.FIRECHARGE_USE : SoundEvents.FLINTANDSTEEL_USE;
214
			this.level().playSound(player, this.getX(), this.getY(), this.getZ(), soundEvent, this.getSoundSource(), 1.0F, this.random.nextFloat() * 0.4F + 0.8F);
215
			if (!this.level().isClientSide()) {
216
				this.ignite();
217
				if (!itemStack.isDamageableItem()) {
218
					itemStack.shrink(1);
219
				} else {
220
					itemStack.hurtAndBreak(1, player, hand.asEquipmentSlot());
221
				}
222
			}
223
224
			return InteractionResult.SUCCESS;
225
		} else {
226
			return super.mobInteract(player, hand);
227
		}
228
	}
229
230
	private void explodeCreeper() {
231
		if (this.level() instanceof ServerLevel level) {
232
			float explosionMultiplier = this.isPowered() ? 2.0F : 1.0F;
233
			this.dead = true;
234
			level.explode(this, this.getX(), this.getY(), this.getZ(), this.explosionRadius * explosionMultiplier, Level.ExplosionInteraction.MOB);
235
			this.spawnLingeringCloud();
236
			this.triggerOnDeathMobEffects(level, Entity.RemovalReason.KILLED);
237
			this.discard();
238
		}
239
	}
240
241
	private void spawnLingeringCloud() {
242
		Collection<MobEffectInstance> activeEffects = this.getActiveEffects();
243
		if (!activeEffects.isEmpty()) {
244
			AreaEffectCloud cloud = new AreaEffectCloud(this.level(), this.getX(), this.getY(), this.getZ());
245
			cloud.setRadius(2.5F);
246
			cloud.setRadiusOnUse(-0.5F);
247
			cloud.setWaitTime(10);
248
			cloud.setDuration(300);
249
			cloud.setPotionDurationScale(0.25F);
250
			cloud.setRadiusPerTick(-cloud.getRadius() / cloud.getDuration());
251
252
			for (MobEffectInstance mobEffect : activeEffects) {
253
				cloud.addEffect(new MobEffectInstance(mobEffect));
254
			}
255
256
			this.level().addFreshEntity(cloud);
257
		}
258
	}
259
260
	public boolean isIgnited() {
261
		return this.entityData.get(DATA_IS_IGNITED);
262
	}
263
264
	public void ignite() {
265
		this.entityData.set(DATA_IS_IGNITED, true);
266
	}
267
}
268

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