| 1 | package net.minecraft.world.item;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Direction;
|
| 5 | import net.minecraft.core.component.DataComponents;
|
| 6 | import net.minecraft.core.particles.ParticleTypes;
|
| 7 | import net.minecraft.network.chat.Component;
|
| 8 | import net.minecraft.server.level.ServerLevel;
|
| 9 | import net.minecraft.sounds.SoundEvents;
|
| 10 | import net.minecraft.sounds.SoundSource;
|
| 11 | import net.minecraft.tags.BlockTags;
|
| 12 | import net.minecraft.world.InteractionResult;
|
| 13 | import net.minecraft.world.entity.player.Player;
|
| 14 | import net.minecraft.world.item.alchemy.PotionContents;
|
| 15 | import net.minecraft.world.item.alchemy.Potions;
|
| 16 | import net.minecraft.world.item.context.UseOnContext;
|
| 17 | import net.minecraft.world.level.Level;
|
| 18 | import net.minecraft.world.level.block.Blocks;
|
| 19 | import net.minecraft.world.level.block.state.BlockState;
|
| 20 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 21 |
|
| 22 | public class PotionItem extends Item {
|
| 23 | public PotionItem(final Item.Properties properties) {
|
| 24 | super(properties);
|
| 25 | }
|
| 26 |
|
| 27 | @Override
|
| 28 | public ItemStack getDefaultInstance() {
|
| 29 | ItemStack itemStack = super.getDefaultInstance();
|
| 30 | itemStack.set(DataComponents.POTION_CONTENTS, new PotionContents(Potions.WATER));
|
| 31 | return itemStack;
|
| 32 | }
|
| 33 |
|
| 34 | @Override
|
| 35 | public InteractionResult useOn(final UseOnContext context) {
|
| 36 | Level level = context.getLevel();
|
| 37 | BlockPos pos = context.getClickedPos();
|
| 38 | Player player = context.getPlayer();
|
| 39 | ItemStack itemStack = context.getItemInHand();
|
| 40 | PotionContents potionContents = itemStack.getOrDefault(DataComponents.POTION_CONTENTS, PotionContents.EMPTY);
|
| 41 | BlockState blockState = level.getBlockState(pos);
|
| 42 | if (context.getClickedFace() != Direction.DOWN && blockState.is(BlockTags.CONVERTIBLE_TO_MUD) && potionContents.is(Potions.WATER)) {
|
| 43 | level.playSound(null, pos, SoundEvents.GENERIC_SPLASH, SoundSource.BLOCKS, 1.0F, 1.0F);
|
| 44 | player.setItemInHand(context.getHand(), ItemUtils.createFilledResult(itemStack, player, new ItemStack(Items.GLASS_BOTTLE)));
|
| 45 | if (!level.isClientSide()) {
|
| 46 | ServerLevel serverLevel = (ServerLevel)level;
|
| 47 |
|
| 48 | for (int i = 0; i < 5; i++) {
|
| 49 | serverLevel.sendParticles(
|
| 50 | ParticleTypes.SPLASH, pos.getX() + level.getRandom().nextDouble(), pos.getY() + 1, pos.getZ() + level.getRandom().nextDouble(), 1, 0.0, 0.0, 0.0, 1.0
|
| 51 | );
|
| 52 | }
|
| 53 | }
|
| 54 |
|
| 55 | level.playSound(null, pos, SoundEvents.BOTTLE_EMPTY, SoundSource.BLOCKS, 1.0F, 1.0F);
|
| 56 | level.gameEvent(null, GameEvent.FLUID_PLACE, pos);
|
| 57 | level.setBlockAndUpdate(pos, Blocks.MUD.defaultBlockState());
|
| 58 | return InteractionResult.SUCCESS;
|
| 59 | } else {
|
| 60 | return InteractionResult.PASS;
|
| 61 | }
|
| 62 | }
|
| 63 |
|
| 64 | @Override
|
| 65 | public Component getName(final ItemStack itemStack) {
|
| 66 | PotionContents potion = itemStack.get(DataComponents.POTION_CONTENTS);
|
| 67 | return potion != null ? potion.getName(this.descriptionId + ".effect.") : super.getName(itemStack);
|
| 68 | }
|
| 69 | }
|
| 70 |
|