/
VanillaCauldronInventory.java
130 lines (111 loc) · 4.32 KB
/
VanillaCauldronInventory.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package knightminer.inspirations.recipes.recipe.inventory;
import knightminer.inspirations.Inspirations;
import knightminer.inspirations.library.recipe.RecipeTypes;
import knightminer.inspirations.library.recipe.cauldron.CauldronContentTypes;
import knightminer.inspirations.library.recipe.cauldron.contents.EmptyCauldronContents;
import knightminer.inspirations.library.recipe.cauldron.contents.ICauldronContents;
import knightminer.inspirations.library.recipe.cauldron.recipe.ICauldronRecipe;
import knightminer.inspirations.library.recipe.cauldron.util.CauldronTemperature;
import knightminer.inspirations.recipes.tileentity.CauldronTileEntity;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.CauldronBlock;
import net.minecraft.fluid.Fluids;
import net.minecraft.item.ItemStack;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvent;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.World;
import java.util.Optional;
import java.util.function.Consumer;
public class VanillaCauldronInventory extends CauldronItemInventory {
private static final ICauldronContents CONTENTS = CauldronContentTypes.FLUID.of(Fluids.WATER);
private final World world;
private final BlockPos pos;
private final BlockState state;
// cached temperate value
private CauldronTemperature temperature;
/**
* Main constructor with all parameters
* @param world World containing the cauldron
* @param pos Position of the cauldron
* @param state Cauldron block state
* @param stack Item stack used to interact
* @param itemSetter Logic to update the item stack in context
* @param itemAdder Logic to give the context a new item stack
*/
public VanillaCauldronInventory(World world, BlockPos pos, BlockState state, ItemStack stack, Consumer<ItemStack> itemSetter, Consumer<ItemStack> itemAdder) {
super(stack, itemSetter, itemAdder);
this.state = state;
this.world = world;
this.pos = pos;
}
/**
* Constructor for contexts which directly handle the item stack. Will use {@link #getStack()} to update stacks
* @param world World containing the cauldron
* @param pos Position of the cauldron
* @param state Cauldron block state
* @param stack Item stack used to interact
* @param itemAdder Logic to give the context a new item stack
*/
public VanillaCauldronInventory(World world, BlockPos pos, BlockState state, ItemStack stack, Consumer<ItemStack> itemAdder) {
this(world, pos, state, stack, EMPTY_CONSUMER, itemAdder);
}
@Override
public boolean isSimple() {
return true;
}
@Override
public void playSound(SoundEvent sound) {
world.playSound(null, pos, sound, SoundCategory.BLOCKS, 1.0f, 1.0f);
}
/* Levels */
@Override
public int getLevel() {
return state.get(CauldronBlock.LEVEL);
}
@Override
public void setLevel(int level) {
level = MathHelper.clamp(level, 0, ICauldronRecipe.MAX);
if (state.get(CauldronBlock.LEVEL) != level) {
((CauldronBlock) Blocks.CAULDRON).setWaterLevel(world, pos, state, level);
}
}
/* Contents */
@Override
public ICauldronContents getContents() {
if (getLevel() == 0) {
return EmptyCauldronContents.INSTANCE;
}
return CONTENTS;
}
@Override
public void setContents(ICauldronContents contents) {
if (contents != EmptyCauldronContents.INSTANCE && !contents.isSimple()) {
Inspirations.log.error("Cannot set cauldron contents of vanilla cauldron to non-water " + contents);
}
}
@Override
public CauldronTemperature getTemperature() {
if (temperature == null) {
temperature = CauldronTileEntity.calcTemperature(world, pos,
CauldronTileEntity.isCauldronFire(world.getBlockState(pos.down())),
CauldronTileEntity.isFreezing(world, pos));
}
return temperature;
}
/* Logic to run the recipe */
/**
* Handles the recipe using this object as the context
* @return True if a recipe happened, false otherwise
*/
public boolean handleRecipe() {
Optional<ICauldronRecipe> recipe = world.getRecipeManager().getRecipe(RecipeTypes.CAULDRON, this, world);
if (recipe.isPresent()) {
recipe.get().handleRecipe(this);
return true;
}
return false;
}
}