| 1 | package net.minecraft.world.item;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.component.DataComponents;
|
| 5 | import net.minecraft.server.level.ServerLevel;
|
| 6 | import net.minecraft.sounds.SoundEvent;
|
| 7 | import net.minecraft.sounds.SoundSource;
|
| 8 | import net.minecraft.world.entity.Bucketable;
|
| 9 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 10 | import net.minecraft.world.entity.EntityType;
|
| 11 | import net.minecraft.world.entity.LivingEntity;
|
| 12 | import net.minecraft.world.entity.Mob;
|
| 13 | import net.minecraft.world.item.component.CustomData;
|
| 14 | import net.minecraft.world.level.ClipContext;
|
| 15 | import net.minecraft.world.level.Level;
|
| 16 | import net.minecraft.world.level.LevelAccessor;
|
| 17 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 18 | import net.minecraft.world.level.material.Fluid;
|
| 19 | import net.minecraft.world.level.material.Fluids;
|
| 20 | import net.minecraft.world.phys.BlockHitResult;
|
| 21 | import org.jspecify.annotations.Nullable;
|
| 22 |
|
| 23 | public class MobBucketItem extends BucketItem {
|
| 24 | private final EntityType<? extends Mob> type;
|
| 25 | private final SoundEvent emptySound;
|
| 26 |
|
| 27 | public MobBucketItem(final EntityType<? extends Mob> type, final Fluid content, final SoundEvent emptySound, final Item.Properties properties) {
|
| 28 | super(content, properties);
|
| 29 | this.type = type;
|
| 30 | this.emptySound = emptySound;
|
| 31 | }
|
| 32 |
|
| 33 | @Override
|
| 34 | public void checkExtraContent(final @Nullable LivingEntity user, final Level level, final ItemStack itemStack, final BlockPos pos) {
|
| 35 | if (level instanceof ServerLevel serverLevel) {
|
| 36 | this.spawn(serverLevel, itemStack, pos);
|
| 37 | level.gameEvent(user, GameEvent.ENTITY_PLACE, pos);
|
| 38 | }
|
| 39 | }
|
| 40 |
|
| 41 | @Override
|
| 42 | protected void playEmptySound(final @Nullable LivingEntity user, final LevelAccessor level, final BlockPos pos) {
|
| 43 | level.playSound(user, pos, this.emptySound, SoundSource.NEUTRAL, 1.0F, 1.0F);
|
| 44 | }
|
| 45 |
|
| 46 | private void spawn(final ServerLevel level, final ItemStack itemStack, final BlockPos spawnPos) {
|
| 47 | Mob mob = this.type.create(level, EntityType.createDefaultStackConfig(level, itemStack, null), spawnPos, EntitySpawnReason.BUCKET, true, false);
|
| 48 | if (mob instanceof Bucketable bucketable) {
|
| 49 | CustomData entityData = itemStack.getOrDefault(DataComponents.BUCKET_ENTITY_DATA, CustomData.EMPTY);
|
| 50 | bucketable.loadFromBucketTag(entityData.copyTag());
|
| 51 | bucketable.setFromBucket(true);
|
| 52 | }
|
| 53 |
|
| 54 | if (mob != null) {
|
| 55 | level.addFreshEntityWithPassengers(mob);
|
| 56 | mob.playAmbientSound();
|
| 57 | }
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | public boolean emptyContents(final @Nullable LivingEntity user, final Level level, final BlockPos pos, final @Nullable BlockHitResult hitResult) {
|
| 62 | if (this.content == Fluids.EMPTY) {
|
| 63 | this.playEmptySound(user, level, pos);
|
| 64 | return true;
|
| 65 | } else {
|
| 66 | return super.emptyContents(user, level, pos, hitResult);
|
| 67 | }
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | public ClipContext.Fluid getFluidContext() {
|
| 72 | return ClipContext.Fluid.NONE;
|
| 73 | }
|
| 74 | }
|
| 75 |
|