-
Notifications
You must be signed in to change notification settings - Fork 18
/
EmptyBottleItem.java
75 lines (65 loc) · 2.68 KB
/
EmptyBottleItem.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package knightminer.inspirations.cauldrons.item;
import knightminer.inspirations.common.Config;
import knightminer.inspirations.common.IHidable;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.BottleItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.alchemy.PotionUtils;
import net.minecraft.world.item.alchemy.Potions;
import net.minecraft.tags.FluidTags;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.InteractionHand;
import net.minecraft.core.NonNullList;
import net.minecraft.sounds.SoundSource;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.core.BlockPos;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.level.ClipContext;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.level.Level;
import java.util.function.Supplier;
/**
* Item representing an empty potion bottle. Can be filled with water from a source block
*/
public class EmptyBottleItem extends BottleItem implements IHidable {
private final Supplier<Item> filled;
/**
* Creates a new bottle instance
* @param properties Item properties
* @param filled Supplier returning the filled potion item
*/
public EmptyBottleItem(Properties properties, Supplier<Item> filled) {
super(properties);
this.filled = filled;
}
@Override
public boolean isEnabled() {
return Config.enableCauldronPotions.getAsBoolean();
}
@Override
public void fillItemCategory(CreativeModeTab group, NonNullList<ItemStack> items) {
if (shouldAddtoItemGroup(group)) {
super.fillItemCategory(group, items);
}
}
@Override
public InteractionResultHolder<ItemStack> use(Level world, Player player, InteractionHand hand) {
ItemStack stack = player.getItemInHand(hand);
// must hit a block
BlockHitResult trace = getPlayerPOVHitResult(world, player, ClipContext.Fluid.SOURCE_ONLY);
if (trace.getType() == HitResult.Type.BLOCK) {
BlockPos pos = trace.getBlockPos();
if (!world.mayInteract(player, pos)) {
return InteractionResultHolder.pass(stack);
}
// if the block contains water, give filled stack
if (world.getFluidState(pos).is(FluidTags.WATER)) {
world.playSound(player, player.getX(), player.getY(), player.getZ(), SoundEvents.BOTTLE_FILL, SoundSource.NEUTRAL, 1.0F, 1.0F);
return InteractionResultHolder.sidedSuccess(this.turnBottleIntoItem(stack, player, PotionUtils.setPotion(new ItemStack(filled.get()), Potions.WATER)), world.isClientSide());
}
}
return InteractionResultHolder.pass(stack);
}
}