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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
package gregtech.api.logic;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraftforge.common.util.Constants;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import com.gtnewhorizons.modularui.api.fluids.FluidTanksHandler;
import com.gtnewhorizons.modularui.api.fluids.IFluidTankLong;
import com.gtnewhorizons.modularui.api.fluids.IFluidTanksHandler;
import com.gtnewhorizons.modularui.api.fluids.ListFluidHandler;
import com.gtnewhorizons.modularui.api.math.Size;
import com.gtnewhorizons.modularui.api.widget.Widget;
import com.gtnewhorizons.modularui.common.widget.FluidSlotWidget;
import com.gtnewhorizons.modularui.common.widget.Scrollable;
/**
* Generic Fluid logic for MuTEs.
*
* @author BlueWeabo
*/
public class FluidInventoryLogic {
private static final int DEFAULT_COLUMNS_PER_ROW = 4;
private static final int POSITION_INTERVAL = 18;
private static final Size SIZE = new Size(18, 18);
protected String displayName = "";
@Nonnull
protected final IFluidTanksHandler inventory;
protected final Map<Fluid, IFluidTankLong> fluidToTankMap;
protected int tier = 0;
protected boolean isUpgradeInventory = false;
public FluidInventoryLogic(int numberOfSlots, long capacityOfEachTank) {
this(new FluidTanksHandler(numberOfSlots, capacityOfEachTank), 0, false);
}
public FluidInventoryLogic(int numberOfSlots, long capacityOfEachTank, int tier) {
this(new FluidTanksHandler(numberOfSlots, capacityOfEachTank), tier, false);
}
public FluidInventoryLogic(int numberOfSlots, long capacityOfEachTank, int tier, boolean isUpgradeInventory) {
this(new FluidTanksHandler(numberOfSlots, capacityOfEachTank), tier, isUpgradeInventory);
}
public FluidInventoryLogic(@Nonnull IFluidTanksHandler inventory, int tier, boolean isUpgradeInventory) {
this.inventory = inventory;
fluidToTankMap = new HashMap<>(inventory.getTanks());
this.tier = tier;
this.isUpgradeInventory = isUpgradeInventory;
}
public FluidInventoryLogic(Collection<IFluidTanksHandler> inventories) {
this(new ListFluidHandler(inventories), -1, false);
}
@Nullable
public String getDisplayName() {
return displayName;
}
public int getTier() {
return tier;
}
public boolean isUpgradeInventory() {
return isUpgradeInventory;
}
public void setDisplayName(@Nullable String displayName) {
this.displayName = displayName;
}
/**
*
* @return The Fluid Inventory Logic as an NBTTagList to be saved in another nbt as how one wants.
*/
@Nonnull
public NBTTagCompound saveToNBT() {
final NBTTagCompound nbt = new NBTTagCompound();
final NBTTagList tList = new NBTTagList();
for (int tankNumber = 0; tankNumber < inventory.getTanks(); tankNumber++) {
final IFluidTankLong tank = inventory.getFluidTank(tankNumber);
if (tank == null) continue;
final NBTTagCompound tag = new NBTTagCompound();
tag.setByte("s", (byte) tankNumber);
tank.saveToNBT(tag);
tList.appendTag(tag);
}
nbt.setTag("inventory", tList);
nbt.setInteger("tier", tier);
if (displayName != null) {
nbt.setString("displayName", displayName);
}
nbt.setBoolean("isUpgradeInventory", isUpgradeInventory);
return nbt;
}
/**
* Loads the Item Inventory Logic from an NBTTagList.
*/
public void loadFromNBT(@Nonnull NBTTagCompound nbt) {
NBTTagList nbtList = nbt.getTagList("inventory", Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < nbtList.tagCount(); i++) {
final NBTTagCompound tankNBT = nbtList.getCompoundTagAt(i);
final int tank = tankNBT.getShort("s");
if (tank >= 0 && tank < inventory.getTanks()) inventory.getFluidTank(tank)
.loadFromNBT(tankNBT);
if (inventory.getFluidInTank(tank) != null) {
fluidToTankMap.put(inventory.getFluidInTank(tank), inventory.getFluidTank(tank));
}
}
tier = nbt.getInteger("tier");
if (nbt.hasKey("displayName")) {
displayName = nbt.getString("displayName");
}
isUpgradeInventory = nbt.getBoolean("isUpgradeInventory");
}
@Nonnull
public IFluidTanksHandler getInventory() {
return inventory;
}
@Nonnull
public FluidStack[] getStoredFluids() {
final FluidStack[] fluids = inventory.getFluids()
.stream()
.filter(Objects::nonNull)
.toArray(FluidStack[]::new);
if (fluids == null) {
return new FluidStack[0];
}
return fluids;
}
public boolean isFluidValid(@Nullable Fluid fluid) {
return fluid != null;
}
/**
* @param fluid What we are trying to input
* @param amount amount of fluid we are trying to put
* @return amount of fluid filled into the tank
*/
public long fill(@Nullable Fluid fluid, long amount, boolean simulate) {
if (!isFluidValid(fluid)) return 0;
IFluidTankLong tank = fluidToTankMap.get(fluid);
if (tank != null) {
return tank.fill(fluid, amount, !simulate);
}
int tankNumber = 0;
tank = inventory.getFluidTank(tankNumber++);
while (tank.getStoredFluid() != fluid && tank.getStoredFluid() != null) {
tank = inventory.getFluidTank(tankNumber++);
}
fluidToTankMap.put(fluid, tank);
return tank.fill(fluid, amount, !simulate);
}
@Nullable
public FluidStack fill(@Nullable FluidStack fluid) {
if (fluid == null) return null;
for (int i = 0; i < inventory.getTanks(); i++) {
fill(fluid.getFluid(), fluid.amount, false);
}
return fluid;
}
/**
* Try and drain the first fluid found for that amount. Used by GT_Cover_Pump
*
* @param amount Fluid to drain from the tank
* @return A fluidstack with the possible amount drained
*/
@Nullable
public FluidStack drain(long amount, boolean simulate) {
for (int i = 0; i < inventory.getTanks(); i++) {
Fluid fluid = inventory.getFluidInTank(i);
FluidStack drained = drain(fluid, amount, simulate);
if (drained != null) return drained;
}
return null;
}
@Nullable
public FluidStack drain(Fluid fluid, long amount, boolean simulate) {
if (!isFluidValid(fluid)) return null;
IFluidTankLong tank = fluidToTankMap.get(fluid);
if (tank != null) {
return tank.drain(amount, !simulate);
}
int tankNumber = 0;
tank = inventory.getFluidTank(tankNumber++);
while (tank.getStoredFluid() != fluid) {
tank = inventory.getFluidTank(tankNumber++);
}
fluidToTankMap.put(fluid, tank);
return tank.drain(amount, !simulate);
}
public void update() {
for (int i = 0; i < inventory.getTanks(); i++) {
IFluidTankLong tank = inventory.getFluidTank(i);
if (tank.getFluidAmountLong() > 0) continue;
tank.setFluid(null, 0);
}
}
public long calculateAmountOfTimesFluidCanBeTaken(Fluid fluid, long amountToTake) {
if (!isFluidValid(fluid)) return 0;
IFluidTankLong tank = fluidToTankMap.get(fluid);
if (tank == null) return 0;
return tank.getFluidAmountLong() / amountToTake;
}
@Nonnull
public Map<Fluid, Long> getMapOfStoredFluids() {
Map<Fluid, Long> map = new HashMap<>();
for (int i = 0; i < inventory.getTanks(); i++) {
IFluidTankLong tank = inventory.getFluidTank(i);
if (tank == null) continue;
Fluid fluid = tank.getStoredFluid();
if (fluid == null) continue;
map.put(fluid, map.getOrDefault(fluid, 0L) + tank.getFluidAmountLong());
}
return map;
}
/**
* Return a scrollable widget with only the inventory.
*/
@Nonnull
public Widget getGuiPart() {
return getGUIPart(DEFAULT_COLUMNS_PER_ROW);
}
/**
* Return a scrollable widget with only the inventory.
*/
@Nonnull
public Widget getGUIPart(int columnsPerRow) {
final Scrollable scrollable = new Scrollable();
scrollable.setVerticalScroll();
for (int rows = 0; rows * 4 < inventory.getTanks(); rows++) {
final int columnsToMake = Math.min(inventory.getTanks() - rows * 4, 4);
for (int column = 0; column < columnsToMake; column++) {
final FluidSlotWidget fluidSlot = new FluidSlotWidget(inventory, rows * 4 + column);
scrollable.widget(
fluidSlot.setPos(column * POSITION_INTERVAL, rows * POSITION_INTERVAL)
.setSize(SIZE));
}
}
return scrollable;
}
}
|