-
Notifications
You must be signed in to change notification settings - Fork 18
/
CauldronTank.java
157 lines (130 loc) · 4.15 KB
/
CauldronTank.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package knightminer.inspirations.recipes.tank;
import knightminer.inspirations.library.recipe.cauldron.CauldronContentTypes;
import knightminer.inspirations.library.recipe.cauldron.recipe.ICauldronRecipe;
import knightminer.inspirations.recipes.tileentity.CauldronTileEntity;
import net.minecraft.fluid.Fluid;
import net.minecraftforge.fluids.FluidAttributes;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.capability.IFluidHandler;
/**
* Logic to treat the cauldron as a fluid tank
*/
public class CauldronTank implements IFluidHandler {
private final CauldronTileEntity cauldron;
public CauldronTank(CauldronTileEntity cauldron) {
this.cauldron = cauldron;
}
/* Properties */
@Override
public int getTanks() {
return 1;
}
@Override
public int getTankCapacity(int tank) {
return 1000;
}
@Override
public boolean isFluidValid(int tank, FluidStack stack) {
return !stack.hasTag();
}
@Override
public FluidStack getFluidInTank(int tank) {
return cauldron.getContents()
.get(CauldronContentTypes.FLUID)
.map(fluid -> new FluidStack(fluid, 1000))
.orElse(FluidStack.EMPTY);
}
/* Filling and draining */
@Override
public int fill(FluidStack stack, FluidAction action) {
// cannot fill with NBT stacks
if (stack.hasTag()) {
return 0;
}
// if full, block
int level = cauldron.getLevel();
if (level == ICauldronRecipe.MAX) {
return 0;
}
// if the fluid is different, block
Fluid fluid = stack.getFluid();
if (level > 0 && !cauldron.getContents().matches(CauldronContentTypes.FLUID, fluid)) {
return 0;
}
// determine how much fluid we can insert
int toInsert = Math.min(getLevels(stack.getAmount()), ICauldronRecipe.MAX - level);
if (toInsert == 0) {
return 0;
}
// update on execute
if (action == FluidAction.EXECUTE) {
cauldron.setContents(CauldronContentTypes.FLUID.of(fluid));
cauldron.setLevel(toInsert + level);
}
return getAmount(toInsert);
}
/**
* Shared logic for fluid draining
* @param fluid Fluid to drain
* @param maxDrain Amount to drain
* @param action Whether to execute the drain
* @return Drained fluid stack
*/
private FluidStack drain(Fluid fluid, int maxDrain, FluidAction action) {
// nothing to drain
int level = cauldron.getLevel();
if (level == 0) {
return FluidStack.EMPTY;
}
// nothing can drain
int toDrain = Math.min(getLevels(maxDrain), level);
if (toDrain == 0) {
return FluidStack.EMPTY;
}
// update on execute
if (action == FluidAction.EXECUTE) {
cauldron.setLevel(level - toDrain);
}
// return fluid
return new FluidStack(fluid, getAmount(toDrain));
}
@Override
public FluidStack drain(FluidStack stack, FluidAction action) {
// cannot drain with NBT stacks
if (stack.hasTag()) {
return FluidStack.EMPTY;
}
// need contents as a fluid type matching the given fluid
return cauldron.getContents()
.get(CauldronContentTypes.FLUID)
.filter(stack.getFluid()::equals)
.map(fluid -> drain(fluid, stack.getAmount(), action))
.orElse(FluidStack.EMPTY);
}
@Override
public FluidStack drain(int maxDrain, FluidAction action) {
// simply need contents to be a fluid type
return cauldron.getContents()
.get(CauldronContentTypes.FLUID)
.map(fluid -> drain(fluid, maxDrain, action))
.orElse(FluidStack.EMPTY);
}
/* Helpers */
/**
* Converts a fluid amount to cauldron levels
* @param amount Amount to convert
* @return Cauldron levels amount
*/
private static int getLevels(int amount) {
// regular is just 3 or 0
return amount >= FluidAttributes.BUCKET_VOLUME ? ICauldronRecipe.MAX : 0;
}
/**
* Converts a level amount to a fluid amount
* @param levels Levels amount
* @return Fluid amount
*/
private static int getAmount(int levels) {
return levels == ICauldronRecipe.MAX ? FluidAttributes.BUCKET_VOLUME : 0;
}
}