-
Notifications
You must be signed in to change notification settings - Fork 18
/
TransformCauldronInteraction.java
54 lines (51 loc) · 2.44 KB
/
TransformCauldronInteraction.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
package knightminer.inspirations.recipes.cauldron;
import knightminer.inspirations.library.InspirationsTags;
import knightminer.inspirations.library.MiscUtil;
import knightminer.inspirations.recipes.block.FourLayerCauldronBlock;
import net.minecraft.core.BlockPos;
import net.minecraft.core.cauldron.CauldronInteraction;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
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.level.Level;
import net.minecraft.world.level.block.LayeredCauldronBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.IntegerProperty;
/** Transforms a cauldron into another cauldron */
public record TransformCauldronInteraction(boolean requireFire, int needed, IntegerProperty oldProp, FourLayerCauldronBlock block, SoundEvent sound) implements CauldronInteraction {
public TransformCauldronInteraction(boolean requireFire, int needed, IntegerProperty oldProp, FourLayerCauldronBlock block) {
this(requireFire, needed, oldProp, block, SoundEvents.BREWING_STAND_BREW);
}
@Override
public InteractionResult interact(BlockState state, Level level, BlockPos pos, Player player, InteractionHand pHand, ItemStack stack) {
if (requireFire && !level.getBlockState(pos.below()).is(InspirationsTags.Blocks.CAULDRON_FIRE)) {
return InteractionResult.PASS;
}
if (!level.isClientSide) {
int contentLevel = state.getValue(oldProp);
int needed = this.needed * contentLevel;
// for a 3 layer cauldron, treat 3 levels as 4 levels
if (oldProp == LayeredCauldronBlock.LEVEL) {
if (contentLevel == 3) {
contentLevel = 4;
}
} else {
// charge for a 4 layer cauldron as if it were 3 layer, that is 75% of cost, ceiling
needed = (needed + 1) * 3 / 4;
}
if (stack.getCount() >= needed) {
player.awardStat(Stats.ITEM_USED.get(stack.getItem()));
MiscUtil.shrinkHeldItem(player, pHand, stack, needed);
player.awardStat(Stats.USE_CAULDRON);
level.setBlockAndUpdate(pos, block.defaultBlockState().setValue(FourLayerCauldronBlock.LEVEL, contentLevel));
level.playSound(null, pos, sound, SoundSource.BLOCKS, 1.0F, 1.0F);
}
}
return InteractionResult.sidedSuccess(level.isClientSide);
}
}