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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
|
package gregtech.common.gui.modularui.widget;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Predicate;
import java.util.function.Supplier;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.IFluidContainerItem;
import com.gtnewhorizons.modularui.api.forge.IItemHandlerModifiable;
import com.gtnewhorizons.modularui.common.internal.wrapper.BaseSlot;
import com.gtnewhorizons.modularui.common.widget.SlotWidget;
import gregtech.GT_Mod;
import gregtech.api.interfaces.IFluidAccess;
import gregtech.api.interfaces.IHasFluidDisplayItem;
import gregtech.api.interfaces.metatileentity.IFluidLockable;
import gregtech.api.util.GT_Utility;
public class FluidDisplaySlotWidget extends SlotWidget {
private IHasFluidDisplayItem iHasFluidDisplay;
private Supplier<IFluidAccess> fluidAccessConstructor;
private Supplier<Boolean> canDrainGetter;
private Supplier<Boolean> canFillGetter;
private Predicate<Fluid> canFillFilter;
private Action actionRealClick = Action.NONE;
private Action actionDragAndDrop = Action.NONE;
private BiFunction<ClickData, FluidDisplaySlotWidget, Boolean> beforeRealClick;
private BiFunction<ClickData, FluidDisplaySlotWidget, Boolean> beforeDragAndDrop;
private Runnable updateFluidDisplayItem = () -> {
if (iHasFluidDisplay != null) {
iHasFluidDisplay.updateFluidDisplayItem();
}
};
public FluidDisplaySlotWidget(BaseSlot slot) {
super(slot);
setAccess(false, false);
disableShiftInsert();
}
public FluidDisplaySlotWidget(IItemHandlerModifiable handler, int index) {
this(new BaseSlot(handler, index, true));
}
// === client actions ===
@Override
public ClickResult onClick(int buttonId, boolean doubleClick) {
if (actionRealClick == Action.NONE) return ClickResult.REJECT;
if (interactionDisabled) return ClickResult.REJECT;
/*
* 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
*/
ClickData clickData = ClickData.create(buttonId, doubleClick);
ItemStack verifyToken = executeRealClick(clickData);
syncToServer(2, buffer -> {
clickData.writeToPacket(buffer);
try {
buffer.writeItemStackToBuffer(verifyToken);
} catch (IOException e) {
e.printStackTrace();
}
});
return ClickResult.ACCEPT;
}
@Override
public boolean handleDragAndDrop(ItemStack draggedStack, int button) {
if (actionDragAndDrop == Action.NONE || actionDragAndDrop == Action.TRANSFER) return false;
if (interactionDisabled) return false;
ClickData clickData = ClickData.create(button, false);
executeDragAndDrop(clickData, draggedStack);
syncToServer(5, buffer -> {
try {
clickData.writeToPacket(buffer);
buffer.writeItemStackToBuffer(draggedStack);
} catch (IOException e) {
e.printStackTrace();
}
});
draggedStack.stackSize = 0;
return true;
}
@Override
public List<String> getExtraTooltip() {
return Collections.emptyList();
}
// === server actions ===
@Override
public void readOnServer(int id, PacketBuffer buf) throws IOException {
if (id == 1) {
getMcSlot().xDisplayPosition = buf.readVarIntFromBuffer();
getMcSlot().yDisplayPosition = buf.readVarIntFromBuffer();
} else if (id == 2) {
onClickServer(ClickData.readPacket(buf), buf.readItemStackFromBuffer());
} else if (id == 3) {
phantomScroll(buf.readVarIntFromBuffer());
} else if (id == 4) {
setEnabled(buf.readBoolean());
} else if (id == 5) {
executeDragAndDrop(ClickData.readPacket(buf), buf.readItemStackFromBuffer());
if (onDragAndDropComplete != null) {
onDragAndDropComplete.accept(this);
}
}
markForUpdate();
}
private void onClickServer(ClickData clickData, ItemStack clientVerifyToken) {
ItemStack serverVerifyToken = executeRealClick(clickData);
// similar to what NetHandlerPlayServer#processClickWindow does
if (!ItemStack.areItemStacksEqual(clientVerifyToken, serverVerifyToken)) {
((EntityPlayerMP) getContext().getPlayer()).sendContainerToPlayer(getContext().getContainer());
}
}
// === client/server actions ===
private ItemStack executeRealClick(ClickData clickData) {
if (actionRealClick == Action.NONE) return null;
if (beforeRealClick != null && !beforeRealClick.apply(clickData, this)) return null;
ItemStack ret = null;
if (actionRealClick == Action.TRANSFER) {
if (fluidAccessConstructor == null) {
GT_Mod.GT_FML_LOGGER.warn(
"FluidDisplaySlotWidget is asked to transfer fluid, but fluidAccessConstructor is null!");
return null;
}
ret = transferFluid(
fluidAccessConstructor.get(),
getContext().getPlayer(),
clickData.mouseButton == 0,
canDrainGetter != null ? canDrainGetter.get() : true,
canFillGetter != null ? canFillGetter.get() : true);
} else if (actionRealClick == Action.LOCK) {
lockFluid(getContext().getPlayer().inventory.getItemStack());
}
updateFluidDisplayItem.run();
return ret;
}
protected ItemStack transferFluid(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;
// apply filter here
if (canFillFilter != null && !canFillFilter.test(tFluidHeld.getFluid())) 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
// there is already fluid in here. so we assume the slot will not accept this fluid anyway if it doesn't
// pass the filter.
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 tContainerItem) {
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);
}
aFluidAccess.verifyFluidStack();
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 container) {
// either partially accepted, or is IFluidContainerItem
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.addAmount(tAmountTaken * tParallel);
}
tStackEmptied.stackSize = tParallel;
replaceCursorItemStack(aPlayer, tStackEmptied);
return tStackEmptied;
}
protected 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);
}
}
protected void executeDragAndDrop(ClickData clickData, ItemStack draggedStack) {
if (actionDragAndDrop == Action.NONE || actionDragAndDrop == Action.TRANSFER) return;
if (beforeDragAndDrop != null && !beforeDragAndDrop.apply(clickData, this)) return;
if (actionDragAndDrop == Action.LOCK) {
lockFluid(draggedStack);
}
updateFluidDisplayItem.run();
}
protected void lockFluid(ItemStack cursorStack) {
if (!(iHasFluidDisplay instanceof IFluidLockable mteToLock)) return;
if (cursorStack == null) {
if (!mteToLock.allowChangingLockedFluid(null)) return;
mteToLock.lockFluid(false);
mteToLock.setLockedFluidName(null);
GT_Utility.sendChatToPlayer(getContext().getPlayer(), GT_Utility.trans("300.1", "Fluid Lock Cleared."));
if (!isClient()) {
mteToLock.onFluidLockPacketReceived(null);
}
} else {
FluidStack fluidStack = GT_Utility.getFluidFromContainerOrFluidDisplay(cursorStack);
if (fluidStack == null) return;
Fluid tFluid = fluidStack.getFluid();
if (tFluid == null) return;
if (!mteToLock.allowChangingLockedFluid(tFluid.getName())) return;
mteToLock.lockFluid(true);
mteToLock.setLockedFluidName(tFluid.getName());
GT_Utility.sendChatToPlayer(
getContext().getPlayer(),
String.format(
GT_Utility.trans("151.4", "Successfully locked Fluid to %s"),
new FluidStack(tFluid, 1).getLocalizedName()));
if (!isClient()) {
mteToLock.onFluidLockPacketReceived(tFluid.getName());
}
}
}
protected void updateFluidDisplayItem() {
if (iHasFluidDisplay != null) {
iHasFluidDisplay.updateFluidDisplayItem();
}
}
// === setters ===
public FluidDisplaySlotWidget setFluidAccessConstructor(Supplier<IFluidAccess> fluidAccessConstructor) {
this.fluidAccessConstructor = fluidAccessConstructor;
return this;
}
public FluidDisplaySlotWidget setIHasFluidDisplay(IHasFluidDisplayItem iHasFluidDisplay) {
this.iHasFluidDisplay = iHasFluidDisplay;
return this;
}
public FluidDisplaySlotWidget setCanDrainGetter(Supplier<Boolean> canDrainGetter) {
this.canDrainGetter = canDrainGetter;
return this;
}
public FluidDisplaySlotWidget setCanDrain(boolean canDrain) {
return setCanDrainGetter(() -> canDrain);
}
public FluidDisplaySlotWidget setCanFillGetter(Supplier<Boolean> canFillGetter) {
this.canFillGetter = canFillGetter;
return this;
}
public FluidDisplaySlotWidget setCanFill(boolean canFill) {
return setCanFillGetter(() -> canFill);
}
/**
* Sets action called on click while holding the real item.
*/
public FluidDisplaySlotWidget setActionRealClick(Action actionRealClick) {
this.actionRealClick = actionRealClick;
return this;
}
/**
* Add a predicate on whether a client stack will be accepted. Note this will only be called when this slot is
* already empty. It is assumed whatever is already in the slot will pass the filter.
*/
public FluidDisplaySlotWidget setEmptyCanFillFilter(Predicate<Fluid> canFillFilter) {
this.canFillFilter = canFillFilter;
return this;
}
/**
* Sets action called on drag-and-drop from NEI. You can't use {@link Action#TRANSFER} here.
*/
public FluidDisplaySlotWidget setActionDragAndDrop(Action actionDragAndDrop) {
this.actionDragAndDrop = actionDragAndDrop;
return this;
}
/**
* Sets function called before {@link #executeRealClick}.
*
* @param beforeRealClick (click data, this widget) -> if allow click
*/
public FluidDisplaySlotWidget setBeforeRealClick(
BiFunction<ClickData, FluidDisplaySlotWidget, Boolean> beforeRealClick) {
this.beforeRealClick = beforeRealClick;
return this;
}
/**
* Sets function called before {@link #executeDragAndDrop}.
*
* @param beforeDragAndDrop (click data, this widget) -> if allow click
*/
public FluidDisplaySlotWidget setBeforeDragAndDrop(
BiFunction<ClickData, FluidDisplaySlotWidget, Boolean> beforeDragAndDrop) {
this.beforeDragAndDrop = beforeDragAndDrop;
return this;
}
/**
* Sets function called before both of {@link #executeRealClick} and {@link #executeDragAndDrop}.
*
* @param beforeClick (click data, this widget) -> if allow click
*/
public FluidDisplaySlotWidget setBeforeClick(BiFunction<ClickData, FluidDisplaySlotWidget, Boolean> beforeClick) {
setBeforeRealClick(beforeClick);
setBeforeDragAndDrop(beforeClick);
return this;
}
/**
* By default, this widget runs {@link IHasFluidDisplayItem#updateFluidDisplayItem} after click. You can specify
* custom update action with this method.
*/
public FluidDisplaySlotWidget setUpdateFluidDisplayItem(Runnable updateFluidDisplayItem) {
this.updateFluidDisplayItem = updateFluidDisplayItem;
return this;
}
/**
* Action triggered on mouse click or NEI drag-and-drop.
*/
public enum Action {
/**
* Fill/drain fluid into/from the tank. Uses fluid amount, so drag-and-drop cannot use this mode.
*/
TRANSFER,
/**
* Lock fluid for {@link IFluidLockable}. Does not use fluid amount.
*/
LOCK,
/**
* Set filter for the tank. (not implemented yet) Does not use fluid amount.
*/
FILTER,
/**
* Does nothing.
*/
NONE
}
}
|