/
TileCauldronInventory.java
106 lines (88 loc) · 2.83 KB
/
TileCauldronInventory.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
package knightminer.inspirations.recipes.recipe.inventory;
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.item.ItemStack;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvent;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.World;
import javax.annotation.Nullable;
import java.util.function.Consumer;
/**
* Inventory used in a cauldron tile entity that supports generic contexts for interaction
*/
public class TileCauldronInventory extends CauldronItemInventory {
private final CauldronTileEntity tile;
@Nullable
private ICauldronContents newContents = null;
private int newLevel = -1;
public TileCauldronInventory(CauldronTileEntity tile) {
this.tile = tile;
}
@Override
public boolean isSimple() {
return false;
}
@Override
public void playSound(SoundEvent sound) {
World world = tile.getWorld();
if (world != null) {
world.playSound(null, tile.getPos(), sound, SoundCategory.BLOCKS, 1.0f, 1.0f);
}
}
/* Item handling */
/**
* Sets the context for item stack handling
* @param stack Stack to match for recipes
* @param itemSetter Logic to update the stack in the context. If null, will assume {@link #getStack()} will be used for updates
* @param itemAdder Logic to add a new stack to the context
*/
public void setItemContext(ItemStack stack, @Nullable Consumer<ItemStack> itemSetter, Consumer<ItemStack> itemAdder) {
this.stack = stack;
this.itemSetter = itemSetter == null ? EMPTY_CONSUMER : itemSetter;
this.itemAdder = itemAdder;
this.newLevel = -1;
this.newContents = null;
}
/**
* Clears any context specific data from the wrapper
*/
public void clearContext() {
this.stack = ItemStack.EMPTY;
this.itemSetter = EMPTY_CONSUMER;
this.itemAdder = EMPTY_CONSUMER;
this.newLevel = -1;
this.newContents = null;
}
/* Level */
@Override
public int getLevel() {
if (newLevel != -1) {
return newLevel;
}
return tile.getLevel();
}
@Override
public void setLevel(int level) {
// set variable so we can just run updates at the end
newLevel = MathHelper.clamp(level, 0, ICauldronRecipe.MAX);
}
/* Contents */
@Override
public ICauldronContents getContents() {
if (newContents != null) {
return newContents;
}
return tile.getContents();
}
@Override
public void setContents(ICauldronContents contents) {
this.newContents = contents;
}
@Override
public CauldronTemperature getTemperature() {
return tile.getTemperature();
}
}