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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
package gregtech.api.gui;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.api.metatileentity.implementations.GT_MetaTileEntity_BasicTank;
import gregtech.api.util.GT_Utility;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.ICrafting;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ChatComponentText;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.IFluidContainerItem;
/**
* NEVER INCLUDE THIS FILE IN YOUR MOD!!!
* <p/>
* The Container I use for all my Basic Tanks
*/
public class GT_Container_BasicTank extends GT_ContainerMetaTile_Machine {
public int mContent = 0;
private int oContent = 0;
public GT_Container_BasicTank(InventoryPlayer aInventoryPlayer, IGregTechTileEntity aTileEntity) {
super(aInventoryPlayer, aTileEntity);
}
@Override
public void addSlots(InventoryPlayer aInventoryPlayer) {
addSlotToContainer(new Slot(mTileEntity, 0, 80, 17));
addSlotToContainer(new GT_Slot_Output(mTileEntity, 1, 80, 53));
addSlotToContainer(new GT_Slot_Render(mTileEntity, 2, 59, 42));
}
@Override
public ItemStack slotClick(int aSlotIndex, int aMouseclick, int aShifthold, EntityPlayer aPlayer) {
if (aSlotIndex == 2 && aMouseclick < 2) {
if (mTileEntity.isClientSide()) {
/*
* While a logical client don't really need to process fluid cells upon click (it could have just wait
* for server side to send the result), doing so would result in every fluid interaction having a
* noticeable delay between clicking and changes happening even on single player.
* I'd imagine this lag to become only more severe when playing MP over ethernet, which would have much more latency
* than a memory connection
*/
GT_MetaTileEntity_BasicTank tTank = (GT_MetaTileEntity_BasicTank) mTileEntity.getMetaTileEntity();
tTank.setDrainableStack(GT_Utility.getFluidFromDisplayStack(tTank.getStackInSlot(2)));
}
GT_MetaTileEntity_BasicTank tTank = (GT_MetaTileEntity_BasicTank) mTileEntity.getMetaTileEntity();
IFluidAccess tDrainableAccess = IFluidAccess.from(tTank, false);
return handleFluidSlotClick(tDrainableAccess, aPlayer, aMouseclick == 0, true, !tTank.isDrainableStackSeparate());
}
return super.slotClick(aSlotIndex, aMouseclick, aShifthold, aPlayer);
}
protected static ItemStack handleFluidSlotClick(IFluidAccess aFluidAccess, EntityPlayer aPlayer, boolean aProcessFullStack, boolean aCanDrain, boolean aCanFill) {
ItemStack tStackHeld = aPlayer.inventory.getItemStack();
ItemStack tStackSizedOne = GT_Utility.copyAmount(1, tStackHeld);
if (tStackSizedOne == null || tStackHeld.stackSize == 0) return null;
FluidStack tInputFluid = aFluidAccess.get();
FluidStack tFluidHeld = GT_Utility.getFluidForFilledItem(tStackSizedOne, true);
if (tFluidHeld != null && tFluidHeld.amount <= 0)
tFluidHeld = null;
if (tInputFluid == null) {
// tank empty, consider fill only from now on
if (!aCanFill)
// cannot fill and nothing to take, bail out
return null;
if (tFluidHeld == null)
// no fluid to fill
return null;
return fillFluid(aFluidAccess, aPlayer, tFluidHeld, aProcessFullStack);
}
// tank not empty, both action possible
if (tFluidHeld != null && tInputFluid.amount < aFluidAccess.getCapacity()) {
// both nonnull and have space left for filling.
if (aCanFill)
// actually both pickup and fill is reasonable, but I'll go with fill here
return fillFluid(aFluidAccess, aPlayer, tFluidHeld, aProcessFullStack);
if (!aCanDrain)
// cannot take AND cannot fill, why make this call then?
return null;
// the slot does not allow filling, so try take some
return drainFluid(aFluidAccess, aPlayer, aProcessFullStack);
} else {
// cannot fill and there is something to take
if (!aCanDrain)
// but the slot does not allow taking, so bail out
return null;
return drainFluid(aFluidAccess, aPlayer, aProcessFullStack);
}
}
protected static ItemStack drainFluid(IFluidAccess aFluidAccess, EntityPlayer aPlayer, boolean aProcessFullStack) {
FluidStack tTankStack = aFluidAccess.get();
if (tTankStack == null) return null;
ItemStack tStackHeld = aPlayer.inventory.getItemStack();
ItemStack tStackSizedOne = GT_Utility.copyAmount(1, tStackHeld);
if (tStackSizedOne == null || tStackHeld.stackSize == 0) return null;
int tOriginalFluidAmount = tTankStack.amount;
ItemStack tFilledContainer = GT_Utility.fillFluidContainer(tTankStack, tStackSizedOne, true, false);
if (tFilledContainer == null && tStackSizedOne.getItem() instanceof IFluidContainerItem) {
IFluidContainerItem tContainerItem = (IFluidContainerItem) tStackSizedOne.getItem();
int tFilledAmount = tContainerItem.fill(tStackSizedOne, tTankStack, true);
if (tFilledAmount > 0) {
tFilledContainer = tStackSizedOne;
tTankStack.amount -= tFilledAmount;
}
}
if (tFilledContainer != null) {
if (aProcessFullStack) {
int tFilledAmount = tOriginalFluidAmount - tTankStack.amount;
/*
work out how many more items we can fill
one cell is already used, so account for that
the round down behavior will left over a fraction of a cell worth of fluid
the user then get to decide what to do with it
it will not be too fancy if it spills out partially filled cells
*/
int tAdditionalParallel = Math.min(tStackHeld.stackSize - 1, tTankStack.amount / tFilledAmount);
tTankStack.amount -= tFilledAmount * tAdditionalParallel;
tFilledContainer.stackSize += tAdditionalParallel;
}
replaceCursorItemStack(aPlayer, tFilledContainer);
}
if (tTankStack.amount <= 0)
aFluidAccess.set(null);
return tFilledContainer;
}
protected static ItemStack fillFluid(IFluidAccess aFluidAccess, EntityPlayer aPlayer, FluidStack aFluidHeld, boolean aProcessFullStack) {
// we are not using aMachine.fill() here any more, so we need to check for fluid type here ourselves
if (aFluidAccess.get() != null && !aFluidAccess.get().isFluidEqual(aFluidHeld))
return null;
ItemStack tStackHeld = aPlayer.inventory.getItemStack();
ItemStack tStackSizedOne = GT_Utility.copyAmount(1, tStackHeld);
if (tStackSizedOne == null)
return null;
int tFreeSpace = aFluidAccess.getCapacity() - (aFluidAccess.get() != null ? aFluidAccess.get().amount : 0);
if (tFreeSpace <= 0)
// no space left
return null;
// find out how much fluid can be taken
// some cells cannot be partially filled
ItemStack tStackEmptied = null;
int tAmountTaken = 0;
if (tFreeSpace >= aFluidHeld.amount) {
// fully accepted - try take it from item now
// IFluidContainerItem is intentionally not checked here. it will be checked later
tStackEmptied = GT_Utility.getContainerForFilledItem(tStackSizedOne, false);
tAmountTaken = aFluidHeld.amount;
}
if (tStackEmptied == null && tStackSizedOne.getItem() instanceof IFluidContainerItem) {
// either partially accepted, or is IFluidContainerItem
IFluidContainerItem container = (IFluidContainerItem) tStackSizedOne.getItem();
FluidStack tDrained = container.drain(tStackSizedOne, tFreeSpace, true);
if (tDrained != null && tDrained.amount > 0) {
// something is actually drained - change the cell and drop it to player
tStackEmptied = tStackSizedOne;
tAmountTaken = tDrained.amount;
}
}
if (tStackEmptied == null)
// somehow the cell refuse to give out that amount of fluid, no op then
return null;
// find out how many fill can we do
// same round down behavior as above
// however here the fluid stack is not changed at all, so the exact code will slightly differ
int tParallel = aProcessFullStack ? Math.min(tFreeSpace / tAmountTaken, tStackHeld.stackSize) : 1;
if (aFluidAccess.get() == null) {
FluidStack tNewFillableStack = aFluidHeld.copy();
tNewFillableStack.amount = tAmountTaken * tParallel;
aFluidAccess.set(tNewFillableStack);
} else {
aFluidAccess.get().amount += tAmountTaken * tParallel;
}
tStackEmptied.stackSize = tParallel;
replaceCursorItemStack(aPlayer, tStackEmptied);
return tStackEmptied;
}
private static void replaceCursorItemStack(EntityPlayer aPlayer, ItemStack tStackResult) {
int tStackResultMaxStackSize = tStackResult.getMaxStackSize();
while (tStackResult.stackSize > tStackResultMaxStackSize) {
aPlayer.inventory.getItemStack().stackSize -= tStackResultMaxStackSize;
GT_Utility.addItemToPlayerInventory(aPlayer, tStackResult.splitStack(tStackResultMaxStackSize));
}
if (aPlayer.inventory.getItemStack().stackSize == tStackResult.stackSize) {
// every cell is mutated. it could just stay on the cursor.
aPlayer.inventory.setItemStack(tStackResult);
} else {
// some cells not mutated. The mutated cells must go into the inventory
// or drop into the world if there isn't enough space.
ItemStack tStackHeld = aPlayer.inventory.getItemStack();
tStackHeld.stackSize -= tStackResult.stackSize;
GT_Utility.addItemToPlayerInventory(aPlayer, tStackResult);
}
}
@Override
public void detectAndSendChanges() {
super.detectAndSendChanges();
if (mTileEntity.isClientSide() || mTileEntity.getMetaTileEntity() == null) return;
if (((GT_MetaTileEntity_BasicTank) mTileEntity.getMetaTileEntity()).mFluid != null)
mContent = ((GT_MetaTileEntity_BasicTank) mTileEntity.getMetaTileEntity()).mFluid.amount;
else
mContent = 0;
for (Object crafter : this.crafters) {
ICrafting var1 = (ICrafting) crafter;
if (mTimer % 500 == 0 || oContent != mContent) {
var1.sendProgressBarUpdate(this, 100, mContent & 65535);
var1.sendProgressBarUpdate(this, 101, mContent >>> 16);
}
}
oContent = mContent;
}
@Override
@SideOnly(Side.CLIENT)
public void updateProgressBar(int par1, int par2) {
super.updateProgressBar(par1, par2);
switch (par1) {
case 100:
mContent = mContent & 0xffff0000 | par2 & 0x0000ffff;
break;
case 101:
mContent = mContent & 0xffff | par2 << 16;
break;
}
}
@Override
public int getSlotCount() {
return 2;
}
@Override
public int getShiftClickSlotCount() {
return 1;
}
protected interface IFluidAccess {
void set(FluidStack stack);
FluidStack get();
int getCapacity();
static IFluidAccess from(GT_MetaTileEntity_BasicTank aTank, boolean aIsFillableStack) {
return new BasicTankFluidAccess(aTank, aIsFillableStack);
}
}
static class BasicTankFluidAccess implements IFluidAccess {
private final GT_MetaTileEntity_BasicTank mTank;
private final boolean mIsFillableStack;
public BasicTankFluidAccess(GT_MetaTileEntity_BasicTank aTank, boolean aIsFillableStack) {
this.mTank = aTank;
this.mIsFillableStack = aIsFillableStack;
}
@Override
public void set(FluidStack stack) {
if (mIsFillableStack)
mTank.setFillableStack(stack);
else
mTank.setDrainableStack(stack);
}
@Override
public FluidStack get() {
return mIsFillableStack ? mTank.getFillableStack() : mTank.getDrainableStack();
}
@Override
public int getCapacity() {
return mTank.getCapacity();
}
}
}
|