-
Notifications
You must be signed in to change notification settings - Fork 18
/
AbstractModifyCauldronInteraction.java
66 lines (59 loc) · 2.55 KB
/
AbstractModifyCauldronInteraction.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
package knightminer.inspirations.recipes.cauldron;
import knightminer.inspirations.library.MiscUtil;
import net.minecraft.core.BlockPos;
import net.minecraft.core.cauldron.CauldronInteraction;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundSource;
import net.minecraft.stats.Stats;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.ItemUtils;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.gameevent.GameEvent;
import javax.annotation.Nullable;
/** Shared logic for an interaction that consumes an item to change the cauldron */
public abstract class AbstractModifyCauldronInteraction implements CauldronInteraction {
private final ItemLike container;
private final SoundEvent sound;
private final ResourceLocation stat;
protected AbstractModifyCauldronInteraction(ItemLike container, SoundEvent sound, ResourceLocation stat) {
this.container = container;
this.sound = sound;
this.stat = stat;
}
/** Called after the state is set to set TE props */
protected void afterSetBlock(BlockState oldState, Level level, BlockPos pos, ItemStack stack) {}
/** Gets the new block state based on the old block state, or null if unable to change */
@Nullable
protected abstract BlockState getNewState(BlockState oldState);
@Override
public InteractionResult interact(BlockState oldState, Level level, BlockPos pos, Player player, InteractionHand hand, ItemStack filledStack) {
BlockState newState = getNewState(oldState);
if (newState == null) {
return InteractionResult.PASS;
}
if (!level.isClientSide) {
if (newState != oldState) {
level.setBlockAndUpdate(pos, newState);
}
afterSetBlock(oldState, level, pos, filledStack);
player.awardStat(stat);
player.awardStat(Stats.ITEM_USED.get(filledStack.getItem()));
// if no result, just empty the hand
if (container == Items.AIR) {
MiscUtil.shrinkHeldItem(player, hand, filledStack, 1);
} else {
player.setItemInHand(hand, ItemUtils.createFilledResult(filledStack, player, new ItemStack(container)));
}
level.playSound(null, pos, sound, SoundSource.BLOCKS, 1.0F, 1.0F);
level.gameEvent(null, GameEvent.FLUID_PLACE, pos);
}
return InteractionResult.sidedSuccess(level.isClientSide);
}
}