Salmon .java

net.minecraft.world.entity.animal.fish.Salmon (class, 185 lines): members: getSalmonScale, TAG_TYPE, getMaxSchoolSize, DATA_TYPE, saveToBucketTag, Variant.

net.minecraft.world.entity.animal.fishclass185 linescommonextends AbstractSchoolingFish6.0 KB

API Surface (17)

int getMaxSchoolSize()
ItemStack getBucketItemStack()
SoundEvent getAmbientSound()
SoundEvent getDeathSound()
SoundEvent getHurtSound(final DamageSource source)
SoundEvent getFlopSound()
void defineSynchedData(final SynchedEntityData.Builder entityData)
void onSyncedDataUpdated(final EntityDataAccessor<?> accessor)
void addAdditionalSaveData(final ValueOutput output)
void readAdditionalSaveData(final ValueInput input)
void saveToBucketTag(final ItemStack bucket)
Salmon.Variant getVariant()
void applyImplicitComponents(final DataComponentGetter components)
<T> boolean applyImplicitComponent(final DataComponentType<T> type, final T value)
float getSalmonScale()
EntityDimensions getDefaultDimensions(final Pose pose)
String getSerializedName()

Fields & Constants (3)

TAG_TYPE
DATA_TYPE
spawnReason

String literals (3)

"small"
"medium"
"large"

Full Source

Salmon.java185 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.animal.fish;
2
3
import com.mojang.serialization.Codec;
4
import io.netty.buffer.ByteBuf;
5
import java.util.function.IntFunction;
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.network.codec.ByteBufCodecs;
10
import net.minecraft.network.codec.StreamCodec;
11
import net.minecraft.network.syncher.EntityDataAccessor;
12
import net.minecraft.network.syncher.EntityDataSerializers;
13
import net.minecraft.network.syncher.SynchedEntityData;
14
import net.minecraft.sounds.SoundEvent;
15
import net.minecraft.sounds.SoundEvents;
16
import net.minecraft.util.ByIdMap;
17
import net.minecraft.util.StringRepresentable;
18
import net.minecraft.util.random.WeightedList;
19
import net.minecraft.world.DifficultyInstance;
20
import net.minecraft.world.damagesource.DamageSource;
21
import net.minecraft.world.entity.Bucketable;
22
import net.minecraft.world.entity.EntityDimensions;
23
import net.minecraft.world.entity.EntitySpawnReason;
24
import net.minecraft.world.entity.EntityType;
25
import net.minecraft.world.entity.Pose;
26
import net.minecraft.world.entity.SpawnGroupData;
27
import net.minecraft.world.item.ItemStack;
28
import net.minecraft.world.item.Items;
29
import net.minecraft.world.level.Level;
30
import net.minecraft.world.level.ServerLevelAccessor;
31
import net.minecraft.world.level.storage.ValueInput;
32
import net.minecraft.world.level.storage.ValueOutput;
33
import org.jspecify.annotations.Nullable;
34
35
public class Salmon extends AbstractSchoolingFish {
36
	private static final String TAG_TYPE = "type";
37
	private static final EntityDataAccessor<Integer> DATA_TYPE = SynchedEntityData.defineId(Salmon.class, EntityDataSerializers.INT);
38
39
	public Salmon(final EntityType<? extends Salmon> type, final Level level) {
40
		super(type, level);
41
		this.refreshDimensions();
42
	}
43
44
	@Override
45
	public int getMaxSchoolSize() {
46
		return 5;
47
	}
48
49
	@Override
50
	public ItemStack getBucketItemStack() {
51
		return new ItemStack(Items.SALMON_BUCKET);
52
	}
53
54
	@Override
55
	protected SoundEvent getAmbientSound() {
56
		return SoundEvents.SALMON_AMBIENT;
57
	}
58
59
	@Override
60
	protected SoundEvent getDeathSound() {
61
		return SoundEvents.SALMON_DEATH;
62
	}
63
64
	@Override
65
	protected SoundEvent getHurtSound(final DamageSource source) {
66
		return SoundEvents.SALMON_HURT;
67
	}
68
69
	@Override
70
	protected SoundEvent getFlopSound() {
71
		return SoundEvents.SALMON_FLOP;
72
	}
73
74
	@Override
75
	protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
76
		super.defineSynchedData(entityData);
77
		entityData.define(DATA_TYPE, Salmon.Variant.DEFAULT.id());
78
	}
79
80
	@Override
81
	public void onSyncedDataUpdated(final EntityDataAccessor<?> accessor) {
82
		super.onSyncedDataUpdated(accessor);
83
		if (DATA_TYPE.equals(accessor)) {
84
			this.refreshDimensions();
85
		}
86
	}
87
88
	@Override
89
	protected void addAdditionalSaveData(final ValueOutput output) {
90
		super.addAdditionalSaveData(output);
91
		output.store("type", Salmon.Variant.CODEC, this.getVariant());
92
	}
93
94
	@Override
95
	protected void readAdditionalSaveData(final ValueInput input) {
96
		super.readAdditionalSaveData(input);
97
		this.setVariant(input.read("type", Salmon.Variant.CODEC).orElse(Salmon.Variant.DEFAULT));
98
	}
99
100
	@Override
101
	public void saveToBucketTag(final ItemStack bucket) {
102
		Bucketable.saveDefaultDataToBucketTag(this, bucket);
103
		bucket.copyFrom(DataComponents.SALMON_SIZE, this);
104
	}
105
106
	private void setVariant(final Salmon.Variant variant) {
107
		this.entityData.set(DATA_TYPE, variant.id);
108
	}
109
110
	public Salmon.Variant getVariant() {
111
		return Salmon.Variant.BY_ID.apply(this.entityData.get(DATA_TYPE));
112
	}
113
114
	@Override
115
	public <T> @Nullable T get(final DataComponentType<? extends T> type) {
116
		return type == DataComponents.SALMON_SIZE ? castComponentValue((DataComponentType<T>)type, this.getVariant()) : super.get(type);
117
	}
118
119
	@Override
120
	protected void applyImplicitComponents(final DataComponentGetter components) {
121
		this.applyImplicitComponentIfPresent(components, DataComponents.SALMON_SIZE);
122
		super.applyImplicitComponents(components);
123
	}
124
125
	@Override
126
	protected <T> boolean applyImplicitComponent(final DataComponentType<T> type, final T value) {
127
		if (type == DataComponents.SALMON_SIZE) {
128
			this.setVariant(castComponentValue(DataComponents.SALMON_SIZE, value));
129
			return true;
130
		} else {
131
			return super.applyImplicitComponent(type, value);
132
		}
133
	}
134
135
	@Override
136
	public @Nullable SpawnGroupData finalizeSpawn(
137
		final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, final @Nullable SpawnGroupData groupData
138
	) {
139
		WeightedList.Builder<Salmon.Variant> builder = WeightedList.builder();
140
		builder.add(Salmon.Variant.SMALL, 30);
141
		builder.add(Salmon.Variant.MEDIUM, 50);
142
		builder.add(Salmon.Variant.LARGE, 15);
143
		builder.build().getRandom(this.random).ifPresent(this::setVariant);
144
		return super.finalizeSpawn(level, difficulty, spawnReason, groupData);
145
	}
146
147
	public float getSalmonScale() {
148
		return this.getVariant().boundingBoxScale;
149
	}
150
151
	@Override
152
	protected EntityDimensions getDefaultDimensions(final Pose pose) {
153
		return super.getDefaultDimensions(pose).scale(this.getSalmonScale());
154
	}
155
156
	public enum Variant implements StringRepresentable {
157
		SMALL("small", 0, 0.5F),
158
		MEDIUM("medium", 1, 1.0F),
159
		LARGE("large", 2, 1.5F);
160
161
		public static final Salmon.Variant DEFAULT = MEDIUM;
162
		public static final Codec<Salmon.Variant> CODEC = StringRepresentable.fromEnum(Salmon.Variant::values);
163
		private static final IntFunction<Salmon.Variant> BY_ID = ByIdMap.continuous(Salmon.Variant::id, values(), ByIdMap.OutOfBoundsStrategy.CLAMP);
164
		public static final StreamCodec<ByteBuf, Salmon.Variant> STREAM_CODEC = ByteBufCodecs.idMapper(BY_ID, Salmon.Variant::id);
165
		private final String name;
166
		private final int id;
167
		private final float boundingBoxScale;
168
169
		Variant(final String name, final int id, final float boundingBoxScale) {
170
			this.name = name;
171
			this.id = id;
172
			this.boundingBoxScale = boundingBoxScale;
173
		}
174
175
		@Override
176
		public String getSerializedName() {
177
			return this.name;
178
		}
179
180
		private int id() {
181
			return this.id;
182
		}
183
	}
184
}
185

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