Pufferfish .java

net.minecraft.world.entity.animal.fish.Pufferfish (class): members: DEFAULT_PUFF_STATE, PufferfishPuffGoal, inflateCounter, deflateTimer, getPuffState.

net.minecraft.world.entity.animal.fishclass221 linescommonextends AbstractFish6.7 KB

API Surface (18)

void defineSynchedData(final SynchedEntityData.Builder entityData)
int getPuffState()
void setPuffState(final int state)
void onSyncedDataUpdated(final EntityDataAccessor<?> accessor)
void addAdditionalSaveData(final ValueOutput output)
void readAdditionalSaveData(final ValueInput input)
ItemStack getBucketItemStack()
void registerGoals()
void tick()
void aiStep()
void playerTouch(final Player player)
SoundEvent getDeathSound()
SoundEvent getHurtSound(final DamageSource source)
SoundEvent getFlopSound()
EntityDimensions getDefaultDimensions(final Pose pose)
boolean canUse()
void start()
void stop()

Fields & Constants (9)

PUFF_STATE
SCARY_MOB
TARGETING_CONDITIONS
STATE_SMALL
STATE_MID
STATE_FULL
DEFAULT_PUFF_STATE
inflateCounter
deflateTimer

String literals (1)

"PuffState"

Full Source

Pufferfish.java221 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.animal.fish;
2
3
import java.util.List;
4
import net.minecraft.network.protocol.game.ClientboundGameEventPacket;
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.server.level.ServerPlayer;
10
import net.minecraft.sounds.SoundEvent;
11
import net.minecraft.sounds.SoundEvents;
12
import net.minecraft.tags.EntityTypeTags;
13
import net.minecraft.world.damagesource.DamageSource;
14
import net.minecraft.world.effect.MobEffectInstance;
15
import net.minecraft.world.effect.MobEffects;
16
import net.minecraft.world.entity.EntityDimensions;
17
import net.minecraft.world.entity.EntityType;
18
import net.minecraft.world.entity.LivingEntity;
19
import net.minecraft.world.entity.Mob;
20
import net.minecraft.world.entity.Pose;
21
import net.minecraft.world.entity.ai.goal.Goal;
22
import net.minecraft.world.entity.ai.targeting.TargetingConditions;
23
import net.minecraft.world.entity.player.Player;
24
import net.minecraft.world.item.ItemStack;
25
import net.minecraft.world.item.Items;
26
import net.minecraft.world.level.Level;
27
import net.minecraft.world.level.storage.ValueInput;
28
import net.minecraft.world.level.storage.ValueOutput;
29
30
public class Pufferfish extends AbstractFish {
31
	private static final EntityDataAccessor<Integer> PUFF_STATE = SynchedEntityData.defineId(Pufferfish.class, EntityDataSerializers.INT);
32
	private int inflateCounter;
33
	private int deflateTimer;
34
	private static final TargetingConditions.Selector SCARY_MOB = (target, level) -> target instanceof Player player && player.isCreative()
35
		? false
36
		: !target.is(EntityTypeTags.NOT_SCARY_FOR_PUFFERFISH);
37
	private static final TargetingConditions TARGETING_CONDITIONS = TargetingConditions.forNonCombat()
38
		.ignoreInvisibilityTesting()
39
		.ignoreLineOfSight()
40
		.selector(SCARY_MOB);
41
	public static final int STATE_SMALL = 0;
42
	public static final int STATE_MID = 1;
43
	public static final int STATE_FULL = 2;
44
	private static final int DEFAULT_PUFF_STATE = 0;
45
46
	public Pufferfish(final EntityType<? extends Pufferfish> type, final Level level) {
47
		super(type, level);
48
		this.refreshDimensions();
49
	}
50
51
	@Override
52
	protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
53
		super.defineSynchedData(entityData);
54
		entityData.define(PUFF_STATE, 0);
55
	}
56
57
	public int getPuffState() {
58
		return this.entityData.get(PUFF_STATE);
59
	}
60
61
	public void setPuffState(final int state) {
62
		this.entityData.set(PUFF_STATE, state);
63
	}
64
65
	@Override
66
	public void onSyncedDataUpdated(final EntityDataAccessor<?> accessor) {
67
		if (PUFF_STATE.equals(accessor)) {
68
			this.refreshDimensions();
69
		}
70
71
		super.onSyncedDataUpdated(accessor);
72
	}
73
74
	@Override
75
	protected void addAdditionalSaveData(final ValueOutput output) {
76
		super.addAdditionalSaveData(output);
77
		output.putInt("PuffState", this.getPuffState());
78
	}
79
80
	@Override
81
	protected void readAdditionalSaveData(final ValueInput input) {
82
		super.readAdditionalSaveData(input);
83
		this.setPuffState(Math.min(input.getIntOr("PuffState", 0), 2));
84
	}
85
86
	@Override
87
	public ItemStack getBucketItemStack() {
88
		return new ItemStack(Items.PUFFERFISH_BUCKET);
89
	}
90
91
	@Override
92
	protected void registerGoals() {
93
		super.registerGoals();
94
		this.goalSelector.addGoal(1, new Pufferfish.PufferfishPuffGoal(this));
95
	}
96
97
	@Override
98
	public void tick() {
99
		if (!this.level().isClientSide() && this.isAlive() && this.isEffectiveAi()) {
100
			if (this.inflateCounter > 0) {
101
				if (this.getPuffState() == 0) {
102
					this.makeSound(SoundEvents.PUFFER_FISH_BLOW_UP);
103
					this.setPuffState(1);
104
				} else if (this.inflateCounter > 40 && this.getPuffState() == 1) {
105
					this.makeSound(SoundEvents.PUFFER_FISH_BLOW_UP);
106
					this.setPuffState(2);
107
				}
108
109
				this.inflateCounter++;
110
			} else if (this.getPuffState() != 0) {
111
				if (this.deflateTimer > 60 && this.getPuffState() == 2) {
112
					this.makeSound(SoundEvents.PUFFER_FISH_BLOW_OUT);
113
					this.setPuffState(1);
114
				} else if (this.deflateTimer > 100 && this.getPuffState() == 1) {
115
					this.makeSound(SoundEvents.PUFFER_FISH_BLOW_OUT);
116
					this.setPuffState(0);
117
				}
118
119
				this.deflateTimer++;
120
			}
121
		}
122
123
		super.tick();
124
	}
125
126
	@Override
127
	public void aiStep() {
128
		super.aiStep();
129
		if (this.level() instanceof ServerLevel level && this.isAlive() && this.getPuffState() > 0) {
130
			for (Mob mob : this.level().getEntitiesOfClass(Mob.class, this.getBoundingBox().inflate(0.3), target -> TARGETING_CONDITIONS.test(level, this, target))) {
131
				if (mob.isAlive()) {
132
					this.touch(level, mob);
133
				}
134
			}
135
		}
136
	}
137
138
	private void touch(final ServerLevel level, final Mob mob) {
139
		if (this.doTeamsAllowDamage(mob)) {
140
			int puffState = this.getPuffState();
141
			if (mob.hurtServer(level, this.damageSources().mobAttack(this), 1 + puffState)) {
142
				mob.addEffect(new MobEffectInstance(MobEffects.POISON, 60 * puffState, 0), this);
143
				this.playSound(SoundEvents.PUFFER_FISH_STING, 1.0F, 1.0F);
144
			}
145
		}
146
	}
147
148
	@Override
149
	public void playerTouch(final Player player) {
150
		if (this.doTeamsAllowDamage(player)) {
151
			int puffState = this.getPuffState();
152
			if (player instanceof ServerPlayer serverPlayer
153
				&& puffState > 0
154
				&& player.hurtServer(serverPlayer.level(), this.damageSources().mobAttack(this), 1 + puffState)) {
155
				if (!this.isSilent()) {
156
					serverPlayer.connection.send(new ClientboundGameEventPacket(ClientboundGameEventPacket.PUFFER_FISH_STING, 0.0F));
157
				}
158
159
				player.addEffect(new MobEffectInstance(MobEffects.POISON, 60 * puffState, 0), this);
160
			}
161
		}
162
	}
163
164
	@Override
165
	protected SoundEvent getDeathSound() {
166
		return SoundEvents.PUFFER_FISH_DEATH;
167
	}
168
169
	@Override
170
	protected SoundEvent getHurtSound(final DamageSource source) {
171
		return SoundEvents.PUFFER_FISH_HURT;
172
	}
173
174
	@Override
175
	protected SoundEvent getFlopSound() {
176
		return SoundEvents.PUFFER_FISH_FLOP;
177
	}
178
179
	@Override
180
	public EntityDimensions getDefaultDimensions(final Pose pose) {
181
		return super.getDefaultDimensions(pose).scale(getScale(this.getPuffState()));
182
	}
183
184
	private static float getScale(final int state) {
185
		return switch (state) {
186
			case 0 -> 0.5F;
187
			case 1 -> 0.7F;
188
			default -> 1.0F;
189
		};
190
	}
191
192
	private static class PufferfishPuffGoal extends Goal {
193
		private final Pufferfish fish;
194
195
		public PufferfishPuffGoal(final Pufferfish fish) {
196
			this.fish = fish;
197
		}
198
199
		@Override
200
		public boolean canUse() {
201
			List<LivingEntity> entities = this.fish
202
				.level()
203
				.getEntitiesOfClass(
204
					LivingEntity.class, this.fish.getBoundingBox().inflate(2.0), target -> Pufferfish.TARGETING_CONDITIONS.test(getServerLevel(this.fish), this.fish, target)
205
				);
206
			return !entities.isEmpty();
207
		}
208
209
		@Override
210
		public void start() {
211
			this.fish.inflateCounter = 1;
212
			this.fish.deflateTimer = 0;
213
		}
214
215
		@Override
216
		public void stop() {
217
			this.fish.inflateCounter = 0;
218
		}
219
	}
220
}
221

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