| 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.List;
|
| 6 | import java.util.function.Consumer;
|
| 7 | import java.util.function.IntFunction;
|
| 8 | import net.minecraft.ChatFormatting;
|
| 9 | import net.minecraft.core.BlockPos;
|
| 10 | import net.minecraft.core.component.DataComponentGetter;
|
| 11 | import net.minecraft.core.component.DataComponentType;
|
| 12 | import net.minecraft.core.component.DataComponents;
|
| 13 | import net.minecraft.network.chat.Component;
|
| 14 | import net.minecraft.network.chat.MutableComponent;
|
| 15 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 16 | import net.minecraft.network.codec.StreamCodec;
|
| 17 | import net.minecraft.network.syncher.EntityDataAccessor;
|
| 18 | import net.minecraft.network.syncher.EntityDataSerializers;
|
| 19 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 20 | import net.minecraft.sounds.SoundEvent;
|
| 21 | import net.minecraft.sounds.SoundEvents;
|
| 22 | import net.minecraft.tags.BiomeTags;
|
| 23 | import net.minecraft.tags.FluidTags;
|
| 24 | import net.minecraft.util.ByIdMap;
|
| 25 | import net.minecraft.util.RandomSource;
|
| 26 | import net.minecraft.util.StringRepresentable;
|
| 27 | import net.minecraft.util.Util;
|
| 28 | import net.minecraft.world.DifficultyInstance;
|
| 29 | import net.minecraft.world.damagesource.DamageSource;
|
| 30 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 31 | import net.minecraft.world.entity.EntityType;
|
| 32 | import net.minecraft.world.entity.SpawnGroupData;
|
| 33 | import net.minecraft.world.item.DyeColor;
|
| 34 | import net.minecraft.world.item.Item;
|
| 35 | import net.minecraft.world.item.ItemStack;
|
| 36 | import net.minecraft.world.item.Items;
|
| 37 | import net.minecraft.world.item.TooltipFlag;
|
| 38 | import net.minecraft.world.item.component.TooltipProvider;
|
| 39 | import net.minecraft.world.level.Level;
|
| 40 | import net.minecraft.world.level.LevelAccessor;
|
| 41 | import net.minecraft.world.level.ServerLevelAccessor;
|
| 42 | import net.minecraft.world.level.block.Blocks;
|
| 43 | import net.minecraft.world.level.storage.ValueInput;
|
| 44 | import net.minecraft.world.level.storage.ValueOutput;
|
| 45 | import org.jspecify.annotations.Nullable;
|
| 46 |
|
| 47 | public class TropicalFish extends AbstractSchoolingFish {
|
| 48 | public static final TropicalFish.Variant DEFAULT_VARIANT = new TropicalFish.Variant(TropicalFish.Pattern.KOB, DyeColor.WHITE, DyeColor.WHITE);
|
| 49 | private static final EntityDataAccessor<Integer> DATA_ID_TYPE_VARIANT = SynchedEntityData.defineId(TropicalFish.class, EntityDataSerializers.INT);
|
| 50 | public static final List<TropicalFish.Variant> COMMON_VARIANTS = List.of(
|
| 51 | new TropicalFish.Variant(TropicalFish.Pattern.STRIPEY, DyeColor.ORANGE, DyeColor.GRAY),
|
| 52 | new TropicalFish.Variant(TropicalFish.Pattern.FLOPPER, DyeColor.GRAY, DyeColor.GRAY),
|
| 53 | new TropicalFish.Variant(TropicalFish.Pattern.FLOPPER, DyeColor.GRAY, DyeColor.BLUE),
|
| 54 | new TropicalFish.Variant(TropicalFish.Pattern.CLAYFISH, DyeColor.WHITE, DyeColor.GRAY),
|
| 55 | new TropicalFish.Variant(TropicalFish.Pattern.SUNSTREAK, DyeColor.BLUE, DyeColor.GRAY),
|
| 56 | new TropicalFish.Variant(TropicalFish.Pattern.KOB, DyeColor.ORANGE, DyeColor.WHITE),
|
| 57 | new TropicalFish.Variant(TropicalFish.Pattern.SPOTTY, DyeColor.PINK, DyeColor.LIGHT_BLUE),
|
| 58 | new TropicalFish.Variant(TropicalFish.Pattern.BLOCKFISH, DyeColor.PURPLE, DyeColor.YELLOW),
|
| 59 | new TropicalFish.Variant(TropicalFish.Pattern.CLAYFISH, DyeColor.WHITE, DyeColor.RED),
|
| 60 | new TropicalFish.Variant(TropicalFish.Pattern.SPOTTY, DyeColor.WHITE, DyeColor.YELLOW),
|
| 61 | new TropicalFish.Variant(TropicalFish.Pattern.GLITTER, DyeColor.WHITE, DyeColor.GRAY),
|
| 62 | new TropicalFish.Variant(TropicalFish.Pattern.CLAYFISH, DyeColor.WHITE, DyeColor.ORANGE),
|
| 63 | new TropicalFish.Variant(TropicalFish.Pattern.DASHER, DyeColor.CYAN, DyeColor.PINK),
|
| 64 | new TropicalFish.Variant(TropicalFish.Pattern.BRINELY, DyeColor.LIME, DyeColor.LIGHT_BLUE),
|
| 65 | new TropicalFish.Variant(TropicalFish.Pattern.BETTY, DyeColor.RED, DyeColor.WHITE),
|
| 66 | new TropicalFish.Variant(TropicalFish.Pattern.SNOOPER, DyeColor.GRAY, DyeColor.RED),
|
| 67 | new TropicalFish.Variant(TropicalFish.Pattern.BLOCKFISH, DyeColor.RED, DyeColor.WHITE),
|
| 68 | new TropicalFish.Variant(TropicalFish.Pattern.FLOPPER, DyeColor.WHITE, DyeColor.YELLOW),
|
| 69 | new TropicalFish.Variant(TropicalFish.Pattern.KOB, DyeColor.RED, DyeColor.WHITE),
|
| 70 | new TropicalFish.Variant(TropicalFish.Pattern.SUNSTREAK, DyeColor.GRAY, DyeColor.WHITE),
|
| 71 | new TropicalFish.Variant(TropicalFish.Pattern.DASHER, DyeColor.CYAN, DyeColor.YELLOW),
|
| 72 | new TropicalFish.Variant(TropicalFish.Pattern.FLOPPER, DyeColor.YELLOW, DyeColor.YELLOW)
|
| 73 | );
|
| 74 | private boolean isSchool = true;
|
| 75 |
|
| 76 | public TropicalFish(final EntityType<? extends TropicalFish> type, final Level level) {
|
| 77 | super(type, level);
|
| 78 | }
|
| 79 |
|
| 80 | public static String getPredefinedName(final int index) {
|
| 81 | return "entity.minecraft.tropical_fish.predefined." + index;
|
| 82 | }
|
| 83 |
|
| 84 | private static int packVariant(final TropicalFish.Pattern pattern, final DyeColor baseColor, final DyeColor patternColor) {
|
| 85 | return pattern.getPackedId() & 65535 | (baseColor.getId() & 0xFF) << 16 | (patternColor.getId() & 0xFF) << 24;
|
| 86 | }
|
| 87 |
|
| 88 | public static DyeColor getBaseColor(final int packedVariant) {
|
| 89 | return DyeColor.byId(packedVariant >> 16 & 0xFF);
|
| 90 | }
|
| 91 |
|
| 92 | public static DyeColor getPatternColor(final int packedVariant) {
|
| 93 | return DyeColor.byId(packedVariant >> 24 & 0xFF);
|
| 94 | }
|
| 95 |
|
| 96 | public static TropicalFish.Pattern getPattern(final int packedVariant) {
|
| 97 | return TropicalFish.Pattern.byId(packedVariant & 65535);
|
| 98 | }
|
| 99 |
|
| 100 | @Override
|
| 101 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 102 | super.defineSynchedData(entityData);
|
| 103 | entityData.define(DATA_ID_TYPE_VARIANT, DEFAULT_VARIANT.getPackedId());
|
| 104 | }
|
| 105 |
|
| 106 | @Override
|
| 107 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 108 | super.addAdditionalSaveData(output);
|
| 109 | output.store("Variant", TropicalFish.Variant.CODEC, new TropicalFish.Variant(this.getPackedVariant()));
|
| 110 | }
|
| 111 |
|
| 112 | @Override
|
| 113 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 114 | super.readAdditionalSaveData(input);
|
| 115 | TropicalFish.Variant variant = input.read("Variant", TropicalFish.Variant.CODEC).orElse(DEFAULT_VARIANT);
|
| 116 | this.setPackedVariant(variant.getPackedId());
|
| 117 | }
|
| 118 |
|
| 119 | private void setPackedVariant(final int i) {
|
| 120 | this.entityData.set(DATA_ID_TYPE_VARIANT, i);
|
| 121 | }
|
| 122 |
|
| 123 | @Override
|
| 124 | public boolean isMaxGroupSizeReached(final int groupSize) {
|
| 125 | return !this.isSchool;
|
| 126 | }
|
| 127 |
|
| 128 | private int getPackedVariant() {
|
| 129 | return this.entityData.get(DATA_ID_TYPE_VARIANT);
|
| 130 | }
|
| 131 |
|
| 132 | public DyeColor getBaseColor() {
|
| 133 | return getBaseColor(this.getPackedVariant());
|
| 134 | }
|
| 135 |
|
| 136 | public DyeColor getPatternColor() {
|
| 137 | return getPatternColor(this.getPackedVariant());
|
| 138 | }
|
| 139 |
|
| 140 | public TropicalFish.Pattern getPattern() {
|
| 141 | return getPattern(this.getPackedVariant());
|
| 142 | }
|
| 143 |
|
| 144 | private void setPattern(final TropicalFish.Pattern pattern) {
|
| 145 | int base = this.getPackedVariant();
|
| 146 | DyeColor baseColor = getBaseColor(base);
|
| 147 | DyeColor patternColor = getPatternColor(base);
|
| 148 | this.setPackedVariant(packVariant(pattern, baseColor, patternColor));
|
| 149 | }
|
| 150 |
|
| 151 | private void setBaseColor(final DyeColor baseColor) {
|
| 152 | int base = this.getPackedVariant();
|
| 153 | TropicalFish.Pattern pattern = getPattern(base);
|
| 154 | DyeColor patternColor = getPatternColor(base);
|
| 155 | this.setPackedVariant(packVariant(pattern, baseColor, patternColor));
|
| 156 | }
|
| 157 |
|
| 158 | private void setPatternColor(final DyeColor patternColor) {
|
| 159 | int base = this.getPackedVariant();
|
| 160 | TropicalFish.Pattern pattern = getPattern(base);
|
| 161 | DyeColor baseColor = getBaseColor(base);
|
| 162 | this.setPackedVariant(packVariant(pattern, baseColor, patternColor));
|
| 163 | }
|
| 164 |
|
| 165 | @Override
|
| 166 | public <T> @Nullable T get(final DataComponentType<? extends T> type) {
|
| 167 | if (type == DataComponents.TROPICAL_FISH_PATTERN) {
|
| 168 | return castComponentValue((DataComponentType<T>)type, this.getPattern());
|
| 169 | } else if (type == DataComponents.TROPICAL_FISH_BASE_COLOR) {
|
| 170 | return castComponentValue((DataComponentType<T>)type, this.getBaseColor());
|
| 171 | } else {
|
| 172 | return type == DataComponents.TROPICAL_FISH_PATTERN_COLOR ? castComponentValue((DataComponentType<T>)type, this.getPatternColor()) : super.get(type);
|
| 173 | }
|
| 174 | }
|
| 175 |
|
| 176 | @Override
|
| 177 | protected void applyImplicitComponents(final DataComponentGetter components) {
|
| 178 | this.applyImplicitComponentIfPresent(components, DataComponents.TROPICAL_FISH_PATTERN);
|
| 179 | this.applyImplicitComponentIfPresent(components, DataComponents.TROPICAL_FISH_BASE_COLOR);
|
| 180 | this.applyImplicitComponentIfPresent(components, DataComponents.TROPICAL_FISH_PATTERN_COLOR);
|
| 181 | super.applyImplicitComponents(components);
|
| 182 | }
|
| 183 |
|
| 184 | @Override
|
| 185 | protected <T> boolean applyImplicitComponent(final DataComponentType<T> type, final T value) {
|
| 186 | if (type == DataComponents.TROPICAL_FISH_PATTERN) {
|
| 187 | this.setPattern(castComponentValue(DataComponents.TROPICAL_FISH_PATTERN, value));
|
| 188 | return true;
|
| 189 | } else if (type == DataComponents.TROPICAL_FISH_BASE_COLOR) {
|
| 190 | this.setBaseColor(castComponentValue(DataComponents.TROPICAL_FISH_BASE_COLOR, value));
|
| 191 | return true;
|
| 192 | } else if (type == DataComponents.TROPICAL_FISH_PATTERN_COLOR) {
|
| 193 | this.setPatternColor(castComponentValue(DataComponents.TROPICAL_FISH_PATTERN_COLOR, value));
|
| 194 | return true;
|
| 195 | } else {
|
| 196 | return super.applyImplicitComponent(type, value);
|
| 197 | }
|
| 198 | }
|
| 199 |
|
| 200 | @Override
|
| 201 | public void saveToBucketTag(final ItemStack bucket) {
|
| 202 | super.saveToBucketTag(bucket);
|
| 203 | bucket.copyFrom(DataComponents.TROPICAL_FISH_PATTERN, this);
|
| 204 | bucket.copyFrom(DataComponents.TROPICAL_FISH_BASE_COLOR, this);
|
| 205 | bucket.copyFrom(DataComponents.TROPICAL_FISH_PATTERN_COLOR, this);
|
| 206 | }
|
| 207 |
|
| 208 | @Override
|
| 209 | public ItemStack getBucketItemStack() {
|
| 210 | return new ItemStack(Items.TROPICAL_FISH_BUCKET);
|
| 211 | }
|
| 212 |
|
| 213 | @Override
|
| 214 | protected SoundEvent getAmbientSound() {
|
| 215 | return SoundEvents.TROPICAL_FISH_AMBIENT;
|
| 216 | }
|
| 217 |
|
| 218 | @Override
|
| 219 | protected SoundEvent getDeathSound() {
|
| 220 | return SoundEvents.TROPICAL_FISH_DEATH;
|
| 221 | }
|
| 222 |
|
| 223 | @Override
|
| 224 | protected SoundEvent getHurtSound(final DamageSource source) {
|
| 225 | return SoundEvents.TROPICAL_FISH_HURT;
|
| 226 | }
|
| 227 |
|
| 228 | @Override
|
| 229 | protected SoundEvent getFlopSound() {
|
| 230 | return SoundEvents.TROPICAL_FISH_FLOP;
|
| 231 | }
|
| 232 |
|
| 233 | @Override
|
| 234 | public @Nullable SpawnGroupData finalizeSpawn(
|
| 235 | final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, @Nullable SpawnGroupData groupData
|
| 236 | ) {
|
| 237 | groupData = super.finalizeSpawn(level, difficulty, spawnReason, groupData);
|
| 238 | RandomSource random = level.getRandom();
|
| 239 | TropicalFish.Variant variant;
|
| 240 | if (groupData instanceof TropicalFish.TropicalFishGroupData tropicalFishGroupData) {
|
| 241 | variant = tropicalFishGroupData.variant;
|
| 242 | } else if (random.nextFloat() < 0.9) {
|
| 243 | variant = Util.getRandom(COMMON_VARIANTS, random);
|
| 244 | groupData = new TropicalFish.TropicalFishGroupData(this, variant);
|
| 245 | } else {
|
| 246 | this.isSchool = false;
|
| 247 | TropicalFish.Pattern[] patterns = TropicalFish.Pattern.values();
|
| 248 | DyeColor[] colors = DyeColor.values();
|
| 249 | TropicalFish.Pattern pattern = Util.getRandom(patterns, random);
|
| 250 | DyeColor baseColor = Util.getRandom(colors, random);
|
| 251 | DyeColor patternColor = Util.getRandom(colors, random);
|
| 252 | variant = new TropicalFish.Variant(pattern, baseColor, patternColor);
|
| 253 | }
|
| 254 |
|
| 255 | this.setPackedVariant(variant.getPackedId());
|
| 256 | return groupData;
|
| 257 | }
|
| 258 |
|
| 259 | public static boolean checkTropicalFishSpawnRules(
|
| 260 | final EntityType<TropicalFish> type, final LevelAccessor level, final EntitySpawnReason spawnReason, final BlockPos pos, final RandomSource random
|
| 261 | ) {
|
| 262 | return level.getFluidState(pos.below()).is(FluidTags.WATER)
|
| 263 | && level.getBlockState(pos.above()).is(Blocks.WATER)
|
| 264 | && (
|
| 265 | level.getBiome(pos).is(BiomeTags.ALLOWS_TROPICAL_FISH_SPAWNS_AT_ANY_HEIGHT)
|
| 266 | || WaterAnimal.checkSurfaceWaterAnimalSpawnRules(type, level, spawnReason, pos, random)
|
| 267 | );
|
| 268 | }
|
| 269 |
|
| 270 | public enum Base {
|
| 271 | SMALL(0),
|
| 272 | LARGE(1);
|
| 273 |
|
| 274 | private final int id;
|
| 275 |
|
| 276 | Base(final int id) {
|
| 277 | this.id = id;
|
| 278 | }
|
| 279 | }
|
| 280 |
|
| 281 | public enum Pattern implements StringRepresentable, TooltipProvider {
|
| 282 | KOB("kob", TropicalFish.Base.SMALL, 0),
|
| 283 | SUNSTREAK("sunstreak", TropicalFish.Base.SMALL, 1),
|
| 284 | SNOOPER("snooper", TropicalFish.Base.SMALL, 2),
|
| 285 | DASHER("dasher", TropicalFish.Base.SMALL, 3),
|
| 286 | BRINELY("brinely", TropicalFish.Base.SMALL, 4),
|
| 287 | SPOTTY("spotty", TropicalFish.Base.SMALL, 5),
|
| 288 | FLOPPER("flopper", TropicalFish.Base.LARGE, 0),
|
| 289 | STRIPEY("stripey", TropicalFish.Base.LARGE, 1),
|
| 290 | GLITTER("glitter", TropicalFish.Base.LARGE, 2),
|
| 291 | BLOCKFISH("blockfish", TropicalFish.Base.LARGE, 3),
|
| 292 | BETTY("betty", TropicalFish.Base.LARGE, 4),
|
| 293 | CLAYFISH("clayfish", TropicalFish.Base.LARGE, 5);
|
| 294 |
|
| 295 | public static final Codec<TropicalFish.Pattern> CODEC = StringRepresentable.fromEnum(TropicalFish.Pattern::values);
|
| 296 | private static final IntFunction<TropicalFish.Pattern> BY_ID = ByIdMap.sparse(TropicalFish.Pattern::getPackedId, values(), KOB);
|
| 297 | public static final StreamCodec<ByteBuf, TropicalFish.Pattern> STREAM_CODEC = ByteBufCodecs.idMapper(BY_ID, TropicalFish.Pattern::getPackedId);
|
| 298 | private final String name;
|
| 299 | private final Component displayName;
|
| 300 | private final TropicalFish.Base base;
|
| 301 | private final int packedId;
|
| 302 |
|
| 303 | Pattern(final String name, final TropicalFish.Base base, final int index) {
|
| 304 | this.name = name;
|
| 305 | this.base = base;
|
| 306 | this.packedId = base.id | index << 8;
|
| 307 | this.displayName = Component.translatable("entity.minecraft.tropical_fish.type." + this.name);
|
| 308 | }
|
| 309 |
|
| 310 | public static TropicalFish.Pattern byId(final int packedId) {
|
| 311 | return BY_ID.apply(packedId);
|
| 312 | }
|
| 313 |
|
| 314 | public TropicalFish.Base base() {
|
| 315 | return this.base;
|
| 316 | }
|
| 317 |
|
| 318 | public int getPackedId() {
|
| 319 | return this.packedId;
|
| 320 | }
|
| 321 |
|
| 322 | @Override
|
| 323 | public String getSerializedName() {
|
| 324 | return this.name;
|
| 325 | }
|
| 326 |
|
| 327 | public Component displayName() {
|
| 328 | return this.displayName;
|
| 329 | }
|
| 330 |
|
| 331 | @Override
|
| 332 | public void addToTooltip(final Item.TooltipContext context, final Consumer<Component> consumer, final TooltipFlag flag, final DataComponentGetter components) {
|
| 333 | DyeColor baseColor = components.getOrDefault(DataComponents.TROPICAL_FISH_BASE_COLOR, TropicalFish.DEFAULT_VARIANT.baseColor());
|
| 334 | DyeColor patternColor = components.getOrDefault(DataComponents.TROPICAL_FISH_PATTERN_COLOR, TropicalFish.DEFAULT_VARIANT.patternColor());
|
| 335 | ChatFormatting[] styles = new ChatFormatting[]{ChatFormatting.ITALIC, ChatFormatting.GRAY};
|
| 336 | int commonIndex = TropicalFish.COMMON_VARIANTS.indexOf(new TropicalFish.Variant(this, baseColor, patternColor));
|
| 337 | if (commonIndex != -1) {
|
| 338 | consumer.accept(Component.translatable(TropicalFish.getPredefinedName(commonIndex)).withStyle(styles));
|
| 339 | } else {
|
| 340 | consumer.accept(this.displayName.plainCopy().withStyle(styles));
|
| 341 | MutableComponent colorComponent = Component.translatable("color.minecraft." + baseColor.getName());
|
| 342 | if (baseColor != patternColor) {
|
| 343 | colorComponent.append(", ").append(Component.translatable("color.minecraft." + patternColor.getName()));
|
| 344 | }
|
| 345 |
|
| 346 | colorComponent.withStyle(styles);
|
| 347 | consumer.accept(colorComponent);
|
| 348 | }
|
| 349 | }
|
| 350 | }
|
| 351 |
|
| 352 | private static class TropicalFishGroupData extends AbstractSchoolingFish.SchoolSpawnGroupData {
|
| 353 | private final TropicalFish.Variant variant;
|
| 354 |
|
| 355 | private TropicalFishGroupData(final TropicalFish leader, final TropicalFish.Variant variant) {
|
| 356 | super(leader);
|
| 357 | this.variant = variant;
|
| 358 | }
|
| 359 | }
|
| 360 |
|
| 361 | public record Variant(TropicalFish.Pattern pattern, DyeColor baseColor, DyeColor patternColor) {
|
| 362 | public static final Codec<TropicalFish.Variant> CODEC = Codec.INT.xmap(TropicalFish.Variant::new, TropicalFish.Variant::getPackedId);
|
| 363 |
|
| 364 | public Variant(final int packedId) {
|
| 365 | this(TropicalFish.getPattern(packedId), TropicalFish.getBaseColor(packedId), TropicalFish.getPatternColor(packedId));
|
| 366 | }
|
| 367 |
|
| 368 | public int getPackedId() {
|
| 369 | return TropicalFish.packVariant(this.pattern, this.baseColor, this.patternColor);
|
| 370 | }
|
| 371 | }
|
| 372 | }
|
| 373 |
|