151 lines
7.1 KiB
Java
151 lines
7.1 KiB
Java
package net.minecraft.world.item;
|
|
|
|
import net.minecraft.sounds.SoundEvent;
|
|
import net.minecraft.world.level.material.Material;
|
|
import net.minecraft.core.particles.ParticleOptions;
|
|
import net.minecraft.core.particles.ParticleTypes;
|
|
import net.minecraft.sounds.SoundSource;
|
|
import net.minecraft.world.level.BlockGetter;
|
|
import net.minecraft.world.level.material.FlowingFluid;
|
|
import javax.annotation.Nullable;
|
|
import net.minecraft.world.level.block.state.BlockState;
|
|
import net.minecraft.core.Direction;
|
|
import net.minecraft.core.BlockPos;
|
|
import net.minecraft.world.level.block.LiquidBlockContainer;
|
|
import net.minecraft.world.level.ItemLike;
|
|
import net.minecraft.server.level.ServerPlayer;
|
|
import net.minecraft.advancements.CriteriaTriggers;
|
|
import net.minecraft.sounds.SoundEvents;
|
|
import net.minecraft.tags.FluidTags;
|
|
import net.minecraft.stats.Stat;
|
|
import net.minecraft.stats.Stats;
|
|
import net.minecraft.world.level.LevelAccessor;
|
|
import net.minecraft.world.level.block.BucketPickup;
|
|
import net.minecraft.world.phys.BlockHitResult;
|
|
import net.minecraft.world.phys.HitResult;
|
|
import net.minecraft.world.level.ClipContext;
|
|
import net.minecraft.world.level.material.Fluids;
|
|
import net.minecraft.world.InteractionResultHolder;
|
|
import net.minecraft.world.InteractionHand;
|
|
import net.minecraft.world.entity.player.Player;
|
|
import net.minecraft.world.level.Level;
|
|
import net.minecraft.world.level.material.Fluid;
|
|
|
|
public class BucketItem extends Item {
|
|
private final Fluid content;
|
|
|
|
public BucketItem(final Fluid cof, final Properties a) {
|
|
super(a);
|
|
this.content = cof;
|
|
}
|
|
|
|
@Override
|
|
public InteractionResultHolder<ItemStack> use(final Level bjt, final Player ayg, final InteractionHand ajh) {
|
|
final ItemStack bek5 = ayg.getItemInHand(ajh);
|
|
final HitResult cvf6 = Item.getPlayerPOVHitResult(bjt, ayg, (this.content == Fluids.EMPTY) ? ClipContext.Fluid.SOURCE_ONLY : ClipContext.Fluid.NONE);
|
|
if (cvf6.getType() == HitResult.Type.MISS) {
|
|
return InteractionResultHolder.<ItemStack>pass(bek5);
|
|
}
|
|
if (cvf6.getType() != HitResult.Type.BLOCK) {
|
|
return InteractionResultHolder.<ItemStack>pass(bek5);
|
|
}
|
|
final BlockHitResult cvd7 = (BlockHitResult)cvf6;
|
|
final BlockPos fk8 = cvd7.getBlockPos();
|
|
final Direction fp9 = cvd7.getDirection();
|
|
final BlockPos fk9 = fk8.relative(fp9);
|
|
if (!bjt.mayInteract(ayg, fk8) || !ayg.mayUseItemAt(fk9, fp9, bek5)) {
|
|
return InteractionResultHolder.<ItemStack>fail(bek5);
|
|
}
|
|
if (this.content == Fluids.EMPTY) {
|
|
final BlockState byg11 = bjt.getBlockState(fk8);
|
|
if (byg11.getBlock() instanceof BucketPickup) {
|
|
final Fluid cof12 = ((BucketPickup)byg11.getBlock()).takeLiquid(bjt, fk8, byg11);
|
|
if (cof12 != Fluids.EMPTY) {
|
|
ayg.awardStat(Stats.ITEM_USED.get(this));
|
|
ayg.playSound(cof12.is(FluidTags.LAVA) ? SoundEvents.BUCKET_FILL_LAVA : SoundEvents.BUCKET_FILL, 1.0f, 1.0f);
|
|
final ItemStack bek6 = this.createResultItem(bek5, ayg, cof12.getBucket());
|
|
if (!bjt.isClientSide) {
|
|
CriteriaTriggers.FILLED_BUCKET.trigger((ServerPlayer)ayg, new ItemStack(cof12.getBucket()));
|
|
}
|
|
return InteractionResultHolder.<ItemStack>success(bek6);
|
|
}
|
|
}
|
|
return InteractionResultHolder.<ItemStack>fail(bek5);
|
|
}
|
|
final BlockState byg11 = bjt.getBlockState(fk8);
|
|
final BlockPos fk10 = (byg11.getBlock() instanceof LiquidBlockContainer && this.content == Fluids.WATER) ? fk8 : fk9;
|
|
if (this.emptyBucket(ayg, bjt, fk10, cvd7)) {
|
|
this.checkExtraContent(bjt, bek5, fk10);
|
|
if (ayg instanceof ServerPlayer) {
|
|
CriteriaTriggers.PLACED_BLOCK.trigger((ServerPlayer)ayg, fk10, bek5);
|
|
}
|
|
ayg.awardStat(Stats.ITEM_USED.get(this));
|
|
return InteractionResultHolder.<ItemStack>success(this.getEmptySuccessItem(bek5, ayg));
|
|
}
|
|
return InteractionResultHolder.<ItemStack>fail(bek5);
|
|
}
|
|
|
|
protected ItemStack getEmptySuccessItem(final ItemStack bek, final Player ayg) {
|
|
if (!ayg.abilities.instabuild) {
|
|
return new ItemStack(Items.BUCKET);
|
|
}
|
|
return bek;
|
|
}
|
|
|
|
public void checkExtraContent(final Level bjt, final ItemStack bek, final BlockPos fk) {
|
|
}
|
|
|
|
private ItemStack createResultItem(final ItemStack bek, final Player ayg, final Item bef) {
|
|
if (ayg.abilities.instabuild) {
|
|
return bek;
|
|
}
|
|
bek.shrink(1);
|
|
if (bek.isEmpty()) {
|
|
return new ItemStack(bef);
|
|
}
|
|
if (!ayg.inventory.add(new ItemStack(bef))) {
|
|
ayg.drop(new ItemStack(bef), false);
|
|
}
|
|
return bek;
|
|
}
|
|
|
|
public boolean emptyBucket(@Nullable final Player ayg, final Level bjt, final BlockPos fk, @Nullable final BlockHitResult cvd) {
|
|
if (!(this.content instanceof FlowingFluid)) {
|
|
return false;
|
|
}
|
|
final BlockState byg6 = bjt.getBlockState(fk);
|
|
final Material cok7 = byg6.getMaterial();
|
|
final boolean boolean8 = byg6.canBeReplaced(this.content);
|
|
if (byg6.isAir() || boolean8 || (byg6.getBlock() instanceof LiquidBlockContainer && ((LiquidBlockContainer)byg6.getBlock()).canPlaceLiquid(bjt, fk, byg6, this.content))) {
|
|
if (bjt.dimension.isUltraWarm() && this.content.is(FluidTags.WATER)) {
|
|
final int integer9 = fk.getX();
|
|
final int integer10 = fk.getY();
|
|
final int integer11 = fk.getZ();
|
|
bjt.playSound(ayg, fk, SoundEvents.FIRE_EXTINGUISH, SoundSource.BLOCKS, 0.5f, 2.6f + (bjt.random.nextFloat() - bjt.random.nextFloat()) * 0.8f);
|
|
for (int integer12 = 0; integer12 < 8; ++integer12) {
|
|
bjt.addParticle(ParticleTypes.LARGE_SMOKE, integer9 + Math.random(), integer10 + Math.random(), integer11 + Math.random(), 0.0, 0.0, 0.0);
|
|
}
|
|
}
|
|
else if (byg6.getBlock() instanceof LiquidBlockContainer && this.content == Fluids.WATER) {
|
|
if (((LiquidBlockContainer)byg6.getBlock()).placeLiquid(bjt, fk, byg6, ((FlowingFluid)this.content).getSource(false))) {
|
|
this.playEmptySound(ayg, bjt, fk);
|
|
}
|
|
}
|
|
else {
|
|
if (!bjt.isClientSide && boolean8 && !cok7.isLiquid()) {
|
|
bjt.destroyBlock(fk, true);
|
|
}
|
|
this.playEmptySound(ayg, bjt, fk);
|
|
bjt.setBlock(fk, this.content.defaultFluidState().createLegacyBlock(), 11);
|
|
}
|
|
return true;
|
|
}
|
|
return cvd != null && this.emptyBucket(ayg, bjt, cvd.getBlockPos().relative(cvd.getDirection()), null);
|
|
}
|
|
|
|
protected void playEmptySound(@Nullable final Player ayg, final LevelAccessor bju, final BlockPos fk) {
|
|
final SoundEvent aah5 = this.content.is(FluidTags.LAVA) ? SoundEvents.BUCKET_EMPTY_LAVA : SoundEvents.BUCKET_EMPTY;
|
|
bju.playSound(ayg, fk, aah5, SoundSource.BLOCKS, 1.0f, 1.0f);
|
|
}
|
|
}
|