| 1 | package net.minecraft.world.item;
|
| 2 |
|
| 3 | import net.minecraft.advancements.triggers.CriteriaTriggers;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.Direction;
|
| 6 | import net.minecraft.core.particles.ParticleTypes;
|
| 7 | import net.minecraft.network.protocol.game.ClientboundLevelParticlesPacket;
|
| 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.sounds.SoundSource;
|
| 13 | import net.minecraft.stats.Stats;
|
| 14 | import net.minecraft.tags.FluidTags;
|
| 15 | import net.minecraft.util.RandomSource;
|
| 16 | import net.minecraft.world.InteractionHand;
|
| 17 | import net.minecraft.world.InteractionResult;
|
| 18 | import net.minecraft.world.attribute.EnvironmentAttributes;
|
| 19 | import net.minecraft.world.entity.LivingEntity;
|
| 20 | import net.minecraft.world.entity.player.Player;
|
| 21 | import net.minecraft.world.level.ClipContext;
|
| 22 | import net.minecraft.world.level.Level;
|
| 23 | import net.minecraft.world.level.LevelAccessor;
|
| 24 | import net.minecraft.world.level.block.Block;
|
| 25 | import net.minecraft.world.level.block.BucketPickup;
|
| 26 | import net.minecraft.world.level.block.LiquidBlockContainer;
|
| 27 | import net.minecraft.world.level.block.state.BlockState;
|
| 28 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 29 | import net.minecraft.world.level.material.FlowingFluid;
|
| 30 | import net.minecraft.world.level.material.Fluid;
|
| 31 | import net.minecraft.world.level.material.Fluids;
|
| 32 | import net.minecraft.world.phys.BlockHitResult;
|
| 33 | import net.minecraft.world.phys.HitResult;
|
| 34 | import org.jspecify.annotations.Nullable;
|
| 35 |
|
| 36 | public class BucketItem extends Item implements DispensibleContainerItem {
|
| 37 | protected final Fluid content;
|
| 38 |
|
| 39 | public BucketItem(final Fluid content, final Item.Properties properties) {
|
| 40 | super(properties);
|
| 41 | this.content = content;
|
| 42 | }
|
| 43 |
|
| 44 | @Override
|
| 45 | public InteractionResult use(final Level level, final Player player, final InteractionHand hand) {
|
| 46 | ItemStack itemStack = player.getItemInHand(hand);
|
| 47 | BlockHitResult hitResult = getPlayerPOVHitResult(level, player, this.getFluidContext());
|
| 48 | if (hitResult.getType() == HitResult.Type.MISS) {
|
| 49 | return InteractionResult.PASS;
|
| 50 | }
|
| 51 |
|
| 52 | if (hitResult.getType() != HitResult.Type.BLOCK) {
|
| 53 | return InteractionResult.PASS;
|
| 54 | }
|
| 55 |
|
| 56 | BlockPos pos = hitResult.getBlockPos();
|
| 57 | Direction direction = hitResult.getDirection();
|
| 58 | BlockPos directionOffsetPos = pos.relative(direction);
|
| 59 | if (level.mayInteract(player, pos) && player.mayUseItemAt(directionOffsetPos, direction, itemStack)) {
|
| 60 | BlockState clicked = level.getBlockState(pos);
|
| 61 | BlockPos placePos = clicked.getBlock() instanceof LiquidBlockContainer && this.content == Fluids.WATER ? pos : directionOffsetPos;
|
| 62 | if (this.emptyContents(player, level, placePos, hitResult)) {
|
| 63 | this.checkExtraContent(player, level, itemStack, placePos);
|
| 64 | if (player instanceof ServerPlayer serverPlayer && this.content != Fluids.EMPTY) {
|
| 65 | CriteriaTriggers.PLACED_BLOCK.trigger(serverPlayer, placePos, itemStack);
|
| 66 | }
|
| 67 |
|
| 68 | player.awardStat(Stats.ITEM_USED.get(this));
|
| 69 | ItemStack emptyResult = ItemUtils.createFilledResult(itemStack, player, getEmptySuccessItem(itemStack, player));
|
| 70 | return InteractionResult.SUCCESS.heldItemTransformedTo(emptyResult);
|
| 71 | } else {
|
| 72 | if (this.content == Fluids.EMPTY) {
|
| 73 | BlockState blockState = level.getBlockState(pos);
|
| 74 | if (blockState.getBlock() instanceof BucketPickup bucketPickupBlock) {
|
| 75 | ItemStack taken = bucketPickupBlock.pickupBlock(player, level, pos, blockState);
|
| 76 | if (!taken.isEmpty()) {
|
| 77 | player.awardStat(Stats.ITEM_USED.get(this));
|
| 78 | bucketPickupBlock.getPickupSound().ifPresent(soundEvent -> player.playSound(soundEvent, 1.0F, 1.0F));
|
| 79 | level.gameEvent(player, GameEvent.FLUID_PICKUP, pos);
|
| 80 | ItemStack result = ItemUtils.createFilledResult(itemStack, player, taken);
|
| 81 | if (!level.isClientSide()) {
|
| 82 | CriteriaTriggers.FILLED_BUCKET.trigger((ServerPlayer)player, taken);
|
| 83 | }
|
| 84 |
|
| 85 | return InteractionResult.SUCCESS.heldItemTransformedTo(result);
|
| 86 | }
|
| 87 | }
|
| 88 | }
|
| 89 |
|
| 90 | return InteractionResult.FAIL;
|
| 91 | }
|
| 92 | } else {
|
| 93 | return InteractionResult.FAIL;
|
| 94 | }
|
| 95 | }
|
| 96 |
|
| 97 | public ClipContext.Fluid getFluidContext() {
|
| 98 | return this.content == Fluids.EMPTY ? ClipContext.Fluid.SOURCE_ONLY : ClipContext.Fluid.NONE;
|
| 99 | }
|
| 100 |
|
| 101 | public static ItemStack getEmptySuccessItem(final ItemStack itemStack, final Player player) {
|
| 102 | return !player.hasInfiniteMaterials() ? new ItemStack(Items.BUCKET) : itemStack;
|
| 103 | }
|
| 104 |
|
| 105 | @Override
|
| 106 | public void checkExtraContent(final @Nullable LivingEntity user, final Level level, final ItemStack itemStack, final BlockPos pos) {
|
| 107 | }
|
| 108 |
|
| 109 | @Override
|
| 110 | public boolean emptyContents(final @Nullable LivingEntity user, final Level level, final BlockPos pos, final @Nullable BlockHitResult hitResult) {
|
| 111 | if (!(this.content instanceof FlowingFluid flowingFluid)) {
|
| 112 | return false;
|
| 113 | } else {
|
| 114 | BlockState blockState = level.getBlockState(pos);
|
| 115 | Block block = blockState.getBlock();
|
| 116 | boolean mayReplace = blockState.canBeReplaced(this.content);
|
| 117 | boolean shiftKeyDown = user != null && user.isShiftKeyDown();
|
| 118 | boolean placeLiquid = mayReplace || block instanceof LiquidBlockContainer container && container.canPlaceLiquid(user, level, pos, blockState, this.content);
|
| 119 | boolean canPlaceFluidInsideBlock = blockState.isAir() || placeLiquid && (!shiftKeyDown || hitResult == null);
|
| 120 | if (canPlaceFluidInsideBlock) {
|
| 121 | if (level.environmentAttributes().getValue(EnvironmentAttributes.WATER_EVAPORATES, pos) && this.content.is(FluidTags.WATER)) {
|
| 122 | int x = pos.getX();
|
| 123 | int y = pos.getY();
|
| 124 | int z = pos.getZ();
|
| 125 | RandomSource random = level.getRandom();
|
| 126 | level.playSound(user, pos, SoundEvents.FIRE_EXTINGUISH, SoundSource.BLOCKS, 0.5F, 2.6F + (random.nextFloat() - random.nextFloat()) * 0.8F);
|
| 127 | if (level instanceof ServerLevel serverLevel) {
|
| 128 | serverLevel.sendParticles(ParticleTypes.LARGE_SMOKE, x, y, z, 8, 1.0, 1.0, 1.0, 0.0, ClientboundLevelParticlesPacket.RandomizationType.ALTERNATIVE);
|
| 129 | }
|
| 130 |
|
| 131 | return true;
|
| 132 | } else if (block instanceof LiquidBlockContainer container && this.content == Fluids.WATER) {
|
| 133 | container.placeLiquid(level, pos, blockState, flowingFluid.getSource(false));
|
| 134 | this.playEmptySound(user, level, pos);
|
| 135 | return true;
|
| 136 | } else {
|
| 137 | if (!level.isClientSide() && mayReplace && !blockState.liquid()) {
|
| 138 | level.destroyBlock(pos, true);
|
| 139 | }
|
| 140 |
|
| 141 | if (!level.setBlock(pos, this.content.defaultFluidState().createLegacyBlock(), 11) && !blockState.getFluidState().isSource()) {
|
| 142 | return false;
|
| 143 | }
|
| 144 |
|
| 145 | this.playEmptySound(user, level, pos);
|
| 146 | return true;
|
| 147 | }
|
| 148 | } else {
|
| 149 | return hitResult != null && this.emptyContents(user, level, hitResult.getBlockPos().relative(hitResult.getDirection()), null);
|
| 150 | }
|
| 151 | }
|
| 152 | }
|
| 153 |
|
| 154 | protected void playEmptySound(final @Nullable LivingEntity user, final LevelAccessor level, final BlockPos pos) {
|
| 155 | SoundEvent soundEvent = this.content.is(FluidTags.LAVA) ? SoundEvents.BUCKET_EMPTY_LAVA : SoundEvents.BUCKET_EMPTY;
|
| 156 | level.playSound(user, pos, soundEvent, SoundSource.BLOCKS, 1.0F, 1.0F);
|
| 157 | level.gameEvent(user, GameEvent.FLUID_PLACE, pos);
|
| 158 | }
|
| 159 |
|
| 160 | public Fluid getContent() {
|
| 161 | return this.content;
|
| 162 | }
|
| 163 | }
|
| 164 |
|