| 1 | package net.minecraft.world.item;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import net.minecraft.advancements.triggers.CriteriaTriggers;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.server.level.ServerPlayer;
|
| 7 | import net.minecraft.sounds.SoundEvents;
|
| 8 | import net.minecraft.sounds.SoundSource;
|
| 9 | import net.minecraft.stats.Stats;
|
| 10 | import net.minecraft.tags.FluidTags;
|
| 11 | import net.minecraft.world.InteractionHand;
|
| 12 | import net.minecraft.world.InteractionResult;
|
| 13 | import net.minecraft.world.entity.AreaEffectCloud;
|
| 14 | import net.minecraft.world.entity.boss.enderdragon.EnderDragon;
|
| 15 | import net.minecraft.world.entity.player.Player;
|
| 16 | import net.minecraft.world.item.alchemy.PotionContents;
|
| 17 | import net.minecraft.world.item.alchemy.Potions;
|
| 18 | import net.minecraft.world.level.ClipContext;
|
| 19 | import net.minecraft.world.level.Level;
|
| 20 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 21 | import net.minecraft.world.phys.BlockHitResult;
|
| 22 | import net.minecraft.world.phys.HitResult;
|
| 23 |
|
| 24 | public class BottleItem extends Item {
|
| 25 | public BottleItem(final Item.Properties properties) {
|
| 26 | super(properties);
|
| 27 | }
|
| 28 |
|
| 29 | @Override
|
| 30 | public InteractionResult use(final Level level, final Player player, final InteractionHand hand) {
|
| 31 | List<AreaEffectCloud> clouds = level.getEntitiesOfClass(
|
| 32 | AreaEffectCloud.class, player.getBoundingBox().inflate(2.0), input -> input.isAlive() && input.getOwner() instanceof EnderDragon
|
| 33 | );
|
| 34 | ItemStack itemStack = player.getItemInHand(hand);
|
| 35 | if (!clouds.isEmpty()) {
|
| 36 | AreaEffectCloud cloud = clouds.get(0);
|
| 37 | cloud.setRadius(cloud.getRadius() - 0.5F);
|
| 38 | level.playSound(null, player.getX(), player.getY(), player.getZ(), SoundEvents.BOTTLE_FILL_DRAGONBREATH, SoundSource.NEUTRAL, 1.0F, 1.0F);
|
| 39 | level.gameEvent(player, GameEvent.FLUID_PICKUP, player.position());
|
| 40 | if (player instanceof ServerPlayer serverPlayer) {
|
| 41 | CriteriaTriggers.PLAYER_INTERACTED_WITH_ENTITY.trigger(serverPlayer, itemStack, cloud);
|
| 42 | }
|
| 43 |
|
| 44 | return InteractionResult.SUCCESS.heldItemTransformedTo(this.turnBottleIntoItem(itemStack, player, new ItemStack(Items.DRAGON_BREATH)));
|
| 45 | } else {
|
| 46 | BlockHitResult hitResult = getPlayerPOVHitResult(level, player, ClipContext.Fluid.SOURCE_ONLY);
|
| 47 | if (hitResult.getType() == HitResult.Type.MISS) {
|
| 48 | return InteractionResult.PASS;
|
| 49 | }
|
| 50 |
|
| 51 | if (hitResult.getType() == HitResult.Type.BLOCK) {
|
| 52 | BlockPos pos = hitResult.getBlockPos();
|
| 53 | if (!level.mayInteract(player, pos)) {
|
| 54 | return InteractionResult.PASS;
|
| 55 | }
|
| 56 |
|
| 57 | if (level.getFluidState(pos).is(FluidTags.WATER)) {
|
| 58 | level.playSound(player, player.getX(), player.getY(), player.getZ(), SoundEvents.BOTTLE_FILL, SoundSource.NEUTRAL, 1.0F, 1.0F);
|
| 59 | level.gameEvent(player, GameEvent.FLUID_PICKUP, pos);
|
| 60 | return InteractionResult.SUCCESS
|
| 61 | .heldItemTransformedTo(this.turnBottleIntoItem(itemStack, player, PotionContents.createItemStack(Items.POTION, Potions.WATER)));
|
| 62 | }
|
| 63 | }
|
| 64 |
|
| 65 | return InteractionResult.PASS;
|
| 66 | }
|
| 67 | }
|
| 68 |
|
| 69 | protected ItemStack turnBottleIntoItem(final ItemStack itemStack, final Player player, final ItemStack itemStackToTurnInto) {
|
| 70 | player.awardStat(Stats.ITEM_USED.get(this));
|
| 71 | return ItemUtils.createFilledResult(itemStack, player, itemStackToTurnInto);
|
| 72 | }
|
| 73 | }
|
| 74 |
|