| 1 | package net.minecraft.world.entity.animal.cow;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import io.netty.buffer.ByteBuf;
|
| 5 | import java.util.Optional;
|
| 6 | import java.util.UUID;
|
| 7 | import java.util.function.IntFunction;
|
| 8 | import net.minecraft.core.BlockPos;
|
| 9 | import net.minecraft.core.component.DataComponentGetter;
|
| 10 | import net.minecraft.core.component.DataComponentType;
|
| 11 | import net.minecraft.core.component.DataComponents;
|
| 12 | import net.minecraft.core.particles.ParticleTypes;
|
| 13 | import net.minecraft.core.particles.SpellParticleOption;
|
| 14 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 15 | import net.minecraft.network.codec.StreamCodec;
|
| 16 | import net.minecraft.network.protocol.game.ClientboundLevelParticlesPacket;
|
| 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.server.level.ServerLevel;
|
| 21 | import net.minecraft.sounds.SoundEvent;
|
| 22 | import net.minecraft.sounds.SoundEvents;
|
| 23 | import net.minecraft.sounds.SoundSource;
|
| 24 | import net.minecraft.tags.BlockTags;
|
| 25 | import net.minecraft.util.ByIdMap;
|
| 26 | import net.minecraft.util.RandomSource;
|
| 27 | import net.minecraft.util.StringRepresentable;
|
| 28 | import net.minecraft.world.InteractionHand;
|
| 29 | import net.minecraft.world.InteractionResult;
|
| 30 | import net.minecraft.world.entity.AgeableMob;
|
| 31 | import net.minecraft.world.entity.ConversionParams;
|
| 32 | import net.minecraft.world.entity.EntityAttachment;
|
| 33 | import net.minecraft.world.entity.EntityAttachments;
|
| 34 | import net.minecraft.world.entity.EntityDimensions;
|
| 35 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 36 | import net.minecraft.world.entity.EntityType;
|
| 37 | import net.minecraft.world.entity.EntityTypes;
|
| 38 | import net.minecraft.world.entity.LightningBolt;
|
| 39 | import net.minecraft.world.entity.Pose;
|
| 40 | import net.minecraft.world.entity.Shearable;
|
| 41 | import net.minecraft.world.entity.item.ItemEntity;
|
| 42 | import net.minecraft.world.entity.player.Player;
|
| 43 | import net.minecraft.world.item.ItemStack;
|
| 44 | import net.minecraft.world.item.ItemUtils;
|
| 45 | import net.minecraft.world.item.Items;
|
| 46 | import net.minecraft.world.item.component.SuspiciousStewEffects;
|
| 47 | import net.minecraft.world.level.Level;
|
| 48 | import net.minecraft.world.level.LevelAccessor;
|
| 49 | import net.minecraft.world.level.LevelReader;
|
| 50 | import net.minecraft.world.level.block.Blocks;
|
| 51 | import net.minecraft.world.level.block.SuspiciousEffectHolder;
|
| 52 | import net.minecraft.world.level.block.state.BlockState;
|
| 53 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 54 | import net.minecraft.world.level.storage.ValueInput;
|
| 55 | import net.minecraft.world.level.storage.ValueOutput;
|
| 56 | import net.minecraft.world.level.storage.loot.BuiltInLootTables;
|
| 57 | import org.jspecify.annotations.Nullable;
|
| 58 |
|
| 59 | public class MushroomCow extends AbstractCow implements Shearable {
|
| 60 | private static final EntityDataAccessor<Integer> DATA_TYPE = SynchedEntityData.defineId(MushroomCow.class, EntityDataSerializers.INT);
|
| 61 | private static final int MUTATE_CHANCE = 1024;
|
| 62 | private static final EntityDimensions BABY_DIMENSIONS = EntityDimensions.scalable(0.45F, 0.7F)
|
| 63 | .withEyeHeight(0.69F)
|
| 64 | .withAttachments(EntityAttachments.builder().attach(EntityAttachment.PASSENGER, 0.0F, 0.75F, 0.0F));
|
| 65 | private static final String TAG_STEW_EFFECTS = "stew_effects";
|
| 66 | private @Nullable SuspiciousStewEffects stewEffects;
|
| 67 | private @Nullable UUID lastLightningBoltUUID;
|
| 68 |
|
| 69 | public MushroomCow(final EntityType<? extends MushroomCow> type, final Level level) {
|
| 70 | super(type, level);
|
| 71 | }
|
| 72 |
|
| 73 | @Override
|
| 74 | public float getWalkTargetValue(final BlockPos pos, final LevelReader level) {
|
| 75 | return level.getBlockState(pos.below()).is(Blocks.MYCELIUM) ? 10.0F : level.getPathfindingCostFromLightLevels(pos);
|
| 76 | }
|
| 77 |
|
| 78 | public static boolean checkMushroomSpawnRules(
|
| 79 | final EntityType<MushroomCow> type, final LevelAccessor level, final EntitySpawnReason spawnReason, final BlockPos pos, final RandomSource random
|
| 80 | ) {
|
| 81 | return level.getBlockState(pos.below()).is(BlockTags.MOOSHROOMS_SPAWNABLE_ON) && isBrightEnoughToSpawn(level, pos);
|
| 82 | }
|
| 83 |
|
| 84 | @Override
|
| 85 | public void thunderHit(final ServerLevel level, final LightningBolt lightningBolt) {
|
| 86 | UUID lightningBoltUUID = lightningBolt.getUUID();
|
| 87 | if (!lightningBoltUUID.equals(this.lastLightningBoltUUID)) {
|
| 88 | this.setVariant(this.getVariant() == MushroomCow.Variant.RED ? MushroomCow.Variant.BROWN : MushroomCow.Variant.RED);
|
| 89 | this.lastLightningBoltUUID = lightningBoltUUID;
|
| 90 | this.playSound(SoundEvents.MOOSHROOM_CONVERT, 2.0F, 1.0F);
|
| 91 | }
|
| 92 | }
|
| 93 |
|
| 94 | @Override
|
| 95 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 96 | super.defineSynchedData(entityData);
|
| 97 | entityData.define(DATA_TYPE, MushroomCow.Variant.DEFAULT.id);
|
| 98 | }
|
| 99 |
|
| 100 | @Override
|
| 101 | public InteractionResult mobInteract(final Player player, final InteractionHand hand) {
|
| 102 | ItemStack itemStack = player.getItemInHand(hand);
|
| 103 | if (itemStack.is(Items.BOWL) && !this.isBaby()) {
|
| 104 | boolean isSuspicious = false;
|
| 105 | ItemStack stew;
|
| 106 | if (this.stewEffects != null) {
|
| 107 | isSuspicious = true;
|
| 108 | stew = new ItemStack(Items.SUSPICIOUS_STEW);
|
| 109 | stew.set(DataComponents.SUSPICIOUS_STEW_EFFECTS, this.stewEffects);
|
| 110 | this.stewEffects = null;
|
| 111 | } else {
|
| 112 | stew = new ItemStack(Items.MUSHROOM_STEW);
|
| 113 | }
|
| 114 |
|
| 115 | ItemStack bowlOrStew = ItemUtils.createFilledResult(itemStack, player, stew, false);
|
| 116 | player.setItemInHand(hand, bowlOrStew);
|
| 117 | SoundEvent milkSound;
|
| 118 | if (isSuspicious) {
|
| 119 | milkSound = SoundEvents.MOOSHROOM_MILK_SUSPICIOUSLY;
|
| 120 | } else {
|
| 121 | milkSound = SoundEvents.MOOSHROOM_MILK;
|
| 122 | }
|
| 123 |
|
| 124 | this.playSound(milkSound, 1.0F, 1.0F);
|
| 125 | return InteractionResult.SUCCESS;
|
| 126 | } else if (itemStack.is(Items.SHEARS) && this.readyForShearing()) {
|
| 127 | if (this.level() instanceof ServerLevel level) {
|
| 128 | this.shear(level, SoundSource.PLAYERS, itemStack);
|
| 129 | this.gameEvent(GameEvent.SHEAR, player);
|
| 130 | itemStack.hurtAndBreak(1, player, hand.asEquipmentSlot());
|
| 131 | }
|
| 132 |
|
| 133 | return InteractionResult.SUCCESS;
|
| 134 | } else if (this.getVariant() == MushroomCow.Variant.BROWN && !this.isBaby()) {
|
| 135 | Optional<SuspiciousStewEffects> effectsFromItemStack = this.getEffectsFromItemStack(itemStack);
|
| 136 | if (effectsFromItemStack.isEmpty()) {
|
| 137 | return super.mobInteract(player, hand);
|
| 138 | }
|
| 139 |
|
| 140 | if (this.stewEffects != null) {
|
| 141 | if (this.level() instanceof ServerLevel level) {
|
| 142 | level.sendParticles(
|
| 143 | ParticleTypes.SMOKE,
|
| 144 | this.getX(),
|
| 145 | this.getY(0.5),
|
| 146 | this.getZ(),
|
| 147 | 2,
|
| 148 | 0.5,
|
| 149 | 0.0,
|
| 150 | 0.5,
|
| 151 | 0.0,
|
| 152 | 0.2,
|
| 153 | 0.0,
|
| 154 | ClientboundLevelParticlesPacket.RandomizationType.ALTERNATIVE_WITH_SPEED
|
| 155 | );
|
| 156 | }
|
| 157 | } else {
|
| 158 | itemStack.consume(1, player);
|
| 159 | if (this.level() instanceof ServerLevel level) {
|
| 160 | SpellParticleOption particle = SpellParticleOption.create(ParticleTypes.EFFECT, -1, 1.0F);
|
| 161 | level.sendParticles(
|
| 162 | particle,
|
| 163 | this.getX(),
|
| 164 | this.getY(0.5),
|
| 165 | this.getZ(),
|
| 166 | 4,
|
| 167 | 0.5,
|
| 168 | 0.0,
|
| 169 | 0.5,
|
| 170 | 0.0,
|
| 171 | 0.2,
|
| 172 | 0.0,
|
| 173 | ClientboundLevelParticlesPacket.RandomizationType.ALTERNATIVE_WITH_SPEED
|
| 174 | );
|
| 175 | }
|
| 176 |
|
| 177 | this.stewEffects = effectsFromItemStack.get();
|
| 178 | this.playSound(SoundEvents.MOOSHROOM_EAT, 2.0F, 1.0F);
|
| 179 | }
|
| 180 |
|
| 181 | return InteractionResult.SUCCESS;
|
| 182 | } else {
|
| 183 | return super.mobInteract(player, hand);
|
| 184 | }
|
| 185 | }
|
| 186 |
|
| 187 | @Override
|
| 188 | public void shear(final ServerLevel level, final SoundSource soundSource, final ItemStack tool) {
|
| 189 | level.playSound(null, this, SoundEvents.MOOSHROOM_SHEAR, soundSource, 1.0F, 1.0F);
|
| 190 | this.convertTo(EntityTypes.COW, ConversionParams.single(this, false, false), cow -> {
|
| 191 | level.sendParticles(ParticleTypes.EXPLOSION, this.getX(), this.getY(0.5), this.getZ(), 1, 0.0, 0.0, 0.0, 0.0);
|
| 192 | this.dropFromShearingLootTable(level, BuiltInLootTables.SHEAR_MOOSHROOM, tool, (l, drop) -> {
|
| 193 | for (int i = 0; i < drop.getCount(); i++) {
|
| 194 | l.addFreshEntity(new ItemEntity(this.level(), this.getX(), this.getY(1.0), this.getZ(), drop.copyWithCount(1)));
|
| 195 | }
|
| 196 | });
|
| 197 | });
|
| 198 | }
|
| 199 |
|
| 200 | @Override
|
| 201 | public boolean readyForShearing() {
|
| 202 | return !this.isBaby();
|
| 203 | }
|
| 204 |
|
| 205 | @Override
|
| 206 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 207 | super.addAdditionalSaveData(output);
|
| 208 | output.store("Type", MushroomCow.Variant.CODEC, this.getVariant());
|
| 209 | output.storeNullable("stew_effects", SuspiciousStewEffects.CODEC, this.stewEffects);
|
| 210 | }
|
| 211 |
|
| 212 | @Override
|
| 213 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 214 | super.readAdditionalSaveData(input);
|
| 215 | this.setVariant(input.read("Type", MushroomCow.Variant.CODEC).orElse(MushroomCow.Variant.DEFAULT));
|
| 216 | this.stewEffects = input.read("stew_effects", SuspiciousStewEffects.CODEC).orElse(null);
|
| 217 | }
|
| 218 |
|
| 219 | private Optional<SuspiciousStewEffects> getEffectsFromItemStack(final ItemStack itemStack) {
|
| 220 | SuspiciousEffectHolder effectHolder = SuspiciousEffectHolder.tryGet(itemStack.getItem());
|
| 221 | return effectHolder != null ? Optional.of(effectHolder.getSuspiciousEffects()) : Optional.empty();
|
| 222 | }
|
| 223 |
|
| 224 | private void setVariant(final MushroomCow.Variant variant) {
|
| 225 | this.entityData.set(DATA_TYPE, variant.id);
|
| 226 | }
|
| 227 |
|
| 228 | public MushroomCow.Variant getVariant() {
|
| 229 | return MushroomCow.Variant.byId(this.entityData.get(DATA_TYPE));
|
| 230 | }
|
| 231 |
|
| 232 | @Override
|
| 233 | public <T> @Nullable T get(final DataComponentType<? extends T> type) {
|
| 234 | return type == DataComponents.MOOSHROOM_VARIANT ? castComponentValue((DataComponentType<T>)type, this.getVariant()) : super.get(type);
|
| 235 | }
|
| 236 |
|
| 237 | @Override
|
| 238 | protected void applyImplicitComponents(final DataComponentGetter components) {
|
| 239 | this.applyImplicitComponentIfPresent(components, DataComponents.MOOSHROOM_VARIANT);
|
| 240 | super.applyImplicitComponents(components);
|
| 241 | }
|
| 242 |
|
| 243 | @Override
|
| 244 | protected <T> boolean applyImplicitComponent(final DataComponentType<T> type, final T value) {
|
| 245 | if (type == DataComponents.MOOSHROOM_VARIANT) {
|
| 246 | this.setVariant(castComponentValue(DataComponents.MOOSHROOM_VARIANT, value));
|
| 247 | return true;
|
| 248 | } else {
|
| 249 | return super.applyImplicitComponent(type, value);
|
| 250 | }
|
| 251 | }
|
| 252 |
|
| 253 | @Override
|
| 254 | public EntityDimensions getDefaultDimensions(final Pose pose) {
|
| 255 | return this.isBaby() ? BABY_DIMENSIONS : super.getDefaultDimensions(pose);
|
| 256 | }
|
| 257 |
|
| 258 | public @Nullable MushroomCow getBreedOffspring(final ServerLevel level, final AgeableMob partner) {
|
| 259 | MushroomCow baby = EntityTypes.MOOSHROOM.create(level, EntitySpawnReason.BREEDING);
|
| 260 | if (baby != null) {
|
| 261 | baby.setVariant(this.getOffspringVariant((MushroomCow)partner));
|
| 262 | }
|
| 263 |
|
| 264 | return baby;
|
| 265 | }
|
| 266 |
|
| 267 | private MushroomCow.Variant getOffspringVariant(final MushroomCow mate) {
|
| 268 | MushroomCow.Variant variant = this.getVariant();
|
| 269 | MushroomCow.Variant mateVariant = mate.getVariant();
|
| 270 | MushroomCow.Variant babyVariant;
|
| 271 | if (variant == mateVariant && this.random.nextInt(1024) == 0) {
|
| 272 | babyVariant = variant == MushroomCow.Variant.BROWN ? MushroomCow.Variant.RED : MushroomCow.Variant.BROWN;
|
| 273 | } else {
|
| 274 | babyVariant = this.random.nextBoolean() ? variant : mateVariant;
|
| 275 | }
|
| 276 |
|
| 277 | return babyVariant;
|
| 278 | }
|
| 279 |
|
| 280 | public enum Variant implements StringRepresentable {
|
| 281 | RED("red", 0, Blocks.RED_MUSHROOM.defaultBlockState()),
|
| 282 | BROWN("brown", 1, Blocks.BROWN_MUSHROOM.defaultBlockState());
|
| 283 |
|
| 284 | public static final MushroomCow.Variant DEFAULT = RED;
|
| 285 | public static final Codec<MushroomCow.Variant> CODEC = StringRepresentable.fromEnum(MushroomCow.Variant::values);
|
| 286 | private static final IntFunction<MushroomCow.Variant> BY_ID = ByIdMap.continuous(MushroomCow.Variant::id, values(), ByIdMap.OutOfBoundsStrategy.CLAMP);
|
| 287 | public static final StreamCodec<ByteBuf, MushroomCow.Variant> STREAM_CODEC = ByteBufCodecs.idMapper(BY_ID, MushroomCow.Variant::id);
|
| 288 | private final String type;
|
| 289 | private final int id;
|
| 290 | private final BlockState blockState;
|
| 291 |
|
| 292 | Variant(final String type, final int id, final BlockState blockState) {
|
| 293 | this.type = type;
|
| 294 | this.id = id;
|
| 295 | this.blockState = blockState;
|
| 296 | }
|
| 297 |
|
| 298 | public BlockState getBlockState() {
|
| 299 | return this.blockState;
|
| 300 | }
|
| 301 |
|
| 302 | @Override
|
| 303 | public String getSerializedName() {
|
| 304 | return this.type;
|
| 305 | }
|
| 306 |
|
| 307 | private int id() {
|
| 308 | return this.id;
|
| 309 | }
|
| 310 |
|
| 311 | private static MushroomCow.Variant byId(final int id) {
|
| 312 | return BY_ID.apply(id);
|
| 313 | }
|
| 314 | }
|
| 315 | }
|
| 316 |
|