-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathGTFluidSyncHandler.java
More file actions
596 lines (515 loc) · 21.9 KB
/
GTFluidSyncHandler.java
File metadata and controls
596 lines (515 loc) · 21.9 KB
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
package gregtech.api.mui.sync;
import gregtech.api.util.FluidTooltipUtil;
import gregtech.api.util.GTUtility;
import gregtech.api.util.KeyUtil;
import gregtech.common.covers.filter.readers.SimpleFluidFilterReader;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvent;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTank;
import net.minecraftforge.fluids.FluidUtil;
import net.minecraftforge.fluids.IFluidTank;
import net.minecraftforge.fluids.capability.IFluidHandlerItem;
import com.cleanroommc.modularui.api.drawable.IKey;
import com.cleanroommc.modularui.network.NetworkUtils;
import com.cleanroommc.modularui.screen.RichTooltip;
import com.cleanroommc.modularui.utils.BooleanConsumer;
import com.cleanroommc.modularui.utils.MouseData;
import com.cleanroommc.modularui.value.sync.SyncHandler;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.function.BooleanSupplier;
import java.util.function.Consumer;
import java.util.function.Supplier;
public class GTFluidSyncHandler extends SyncHandler {
public static final int TRY_CLICK_CONTAINER = 1;
public static final int UPDATE_TANK = 2;
public static final int UPDATE_AMOUNT = 3;
public static final int PHANTOM_SCROLL = 4;
public static final int LOCK_FLUID = 5;
private final IFluidTank tank;
private Consumer<FluidStack> jeiHandler;
private BooleanConsumer lockHandler;
private BooleanSupplier isLocked;
private Supplier<FluidStack> lockedFluid;
private FluidStack lastFluid;
private FluidStack phantomFluid;
private boolean canDrainSlot = true;
private boolean canFillSlot = true;
private boolean phantom;
private BooleanSupplier showAmountInTooltip = () -> true;
private BooleanSupplier showAmountOnSlot = () -> true;
private BooleanSupplier drawAlwaysFull = () -> true;
@Nullable
private Consumer<@Nullable FluidStack> changeConsumer;
public GTFluidSyncHandler(IFluidTank tank) {
this.tank = tank;
}
@Override
public void detectAndSendChanges(boolean init) {
FluidStack current = getFluid();
if (init || !GTUtility.areFluidStacksEqual(current, lastFluid)) {
lastFluid = current == null ? null : current.copy();
syncToClient(UPDATE_TANK, buffer -> NetworkUtils.writeFluidStack(buffer, current));
} else if (lastFluid != null && current.amount != lastFluid.amount) {
lastFluid.amount = current.amount;
syncToClient(UPDATE_AMOUNT, buffer -> buffer.writeInt(current.amount));
}
}
public void lockFluid(FluidStack stack, boolean sync) {
if (!canLockFluid()) return;
this.jeiHandler.accept(stack);
if (sync) sync(LOCK_FLUID, buffer -> {
buffer.writeBoolean(stack != null);
NetworkUtils.writeFluidStack(buffer, stack);
});
}
public void lockFluid(boolean locked, boolean sync) {
this.lockHandler.accept(locked);
if (sync) sync(LOCK_FLUID, buffer -> {
buffer.writeBoolean(locked);
NetworkUtils.writeFluidStack(buffer, null);
});
}
public GTFluidSyncHandler handleLocking(@NotNull Supplier<FluidStack> lockedFluid,
@NotNull Consumer<FluidStack> jeiHandler,
@NotNull BooleanConsumer lockHandler, @NotNull BooleanSupplier isLocked) {
this.lockedFluid = lockedFluid;
this.jeiHandler = jeiHandler;
this.lockHandler = lockHandler;
this.isLocked = isLocked;
return this;
}
public FluidStack getFluid() {
return this.tank.getFluid();
}
public void setFluid(FluidStack fluid) {
if (tank instanceof FluidTank fluidTank) {
fluidTank.setFluid(fluid);
} else {
tank.drain(Integer.MAX_VALUE, true);
if (fluid != null) tank.fill(fluid, true);
}
if (!isPhantom() || fluid == null) return;
if (this.phantomFluid == null || this.phantomFluid.getFluid() != fluid.getFluid()) {
this.phantomFluid = fluid;
}
}
public void setAmount(int amount) {
if (this.tank instanceof SimpleFluidFilterReader.WritableFluidTank writableFluidTank) {
writableFluidTank.setFluidAmount(amount);
return;
}
FluidStack stack = getFluid();
if (stack == null) return;
stack.amount = amount;
}
public int getCapacity() {
return this.tank.getCapacity();
}
public GTFluidSyncHandler accessibility(boolean canDrain, boolean canFill) {
this.canDrainSlot = canDrain;
this.canFillSlot = canFill;
return this;
}
public boolean canDrainSlot() {
return this.canDrainSlot;
}
public boolean canFillSlot() {
return this.canFillSlot;
}
public GTFluidSyncHandler phantom(boolean phantom) {
this.phantom = phantom;
if (phantom && this.tank.getFluid() != null)
this.phantomFluid = this.tank.getFluid().copy();
return this;
}
public boolean isPhantom() {
return phantom;
}
public GTFluidSyncHandler showAmountInTooltip(boolean showAmount) {
this.showAmountInTooltip = () -> showAmount;
return this;
}
public GTFluidSyncHandler showAmountInTooltip(BooleanSupplier showAmount) {
this.showAmountInTooltip = showAmount;
return this;
}
public boolean showAmountInTooltip() {
if (!isPhantom() && phantomFluid != null)
return false;
return this.showAmountInTooltip.getAsBoolean();
}
public GTFluidSyncHandler showAmountOnSlot(boolean showAmount) {
this.showAmountOnSlot = () -> showAmount;
return this;
}
public GTFluidSyncHandler showAmountOnSlot(BooleanSupplier showAmount) {
this.showAmountOnSlot = showAmount;
return this;
}
public boolean showAmountOnSlot() {
if (!isPhantom() && phantomFluid != null)
return false;
return this.showAmountOnSlot.getAsBoolean();
}
public GTFluidSyncHandler drawAlwaysFull(boolean drawAsFull) {
this.drawAlwaysFull = () -> drawAsFull;
return this;
}
public GTFluidSyncHandler drawAlwaysFull(BooleanSupplier drawAsFull) {
this.drawAlwaysFull = drawAsFull;
return this;
}
public boolean drawAlwaysFull() {
return this.drawAlwaysFull.getAsBoolean();
}
public void setChangeConsumer(@Nullable Consumer<@Nullable FluidStack> changeConsumer) {
this.changeConsumer = changeConsumer;
}
protected void onChange(@Nullable FluidStack fluidStack) {
if (changeConsumer != null) {
changeConsumer.accept(fluidStack);
}
}
public @NotNull String getFormattedFluidAmount() {
var tankFluid = this.tank.getFluid();
return String.format("%,d", tankFluid == null ? 0 : tankFluid.amount);
}
public int getFluidAmount() {
FluidStack tankFluid = tank.getFluid();
return tankFluid == null ? 0 : tankFluid.amount;
}
public @Nullable String getFluidLocalizedName() {
var tankFluid = this.tank.getFluid();
if (tankFluid == null)
tankFluid = getLockedFluid();
return tankFluid == null ? null : tankFluid.getLocalizedName();
}
public @NotNull IKey getFluidNameKey() {
FluidStack tankFluid = tank.getFluid();
if (tankFluid == null) {
tankFluid = getLockedFluid();
}
return tankFluid == null ? IKey.EMPTY : KeyUtil.fluid(tankFluid);
}
public void handleTooltip(@NotNull RichTooltip tooltip) {
IKey nameKey = getFluidNameKey();
if (nameKey != IKey.EMPTY) {
tooltip.addLine(nameKey);
}
if (showAmountInTooltip()) {
tooltip.addLine(IKey.lang("gregtech.fluid.amount", getFluidAmount(), getCapacity()));
}
if (isPhantom() && showAmountInTooltip()) {
tooltip.addLine(IKey.lang("modularui.fluid.phantom.control"));
}
FluidStack tankFluid = getFluid();
if (tankFluid == null) {
tankFluid = getLockedFluid();
}
if (tankFluid != null) {
FluidTooltipUtil.handleFluidTooltip(tooltip, tankFluid);
if (showAmountInTooltip()) {
FluidTooltipUtil.addIngotMolFluidTooltip(tooltip, tankFluid);
}
}
}
@Override
public void readOnClient(int id, PacketBuffer buf) {
switch (id) {
case TRY_CLICK_CONTAINER -> replaceCursorItemStack(NetworkUtils.readItemStack(buf));
case UPDATE_TANK -> {
FluidStack stack = NetworkUtils.readFluidStack(buf);
setFluid(stack);
onChange(stack);
}
case UPDATE_AMOUNT -> {
setAmount(buf.readInt());
onChange(getFluid());
}
case LOCK_FLUID -> {
lockFluid(NetworkUtils.readFluidStack(buf), false);
FluidStack stack = getFluid();
onChange(stack == null ? getLockedFluid() : stack);
}
}
}
public void handlePhantomScroll(MouseData data) {
syncToServer(PHANTOM_SCROLL, data::writeToPacket);
}
public void handleClick(MouseData data) {
syncToServer(TRY_CLICK_CONTAINER, data::writeToPacket);
}
@Override
public void readOnServer(int id, PacketBuffer buf) {
if (id == TRY_CLICK_CONTAINER) {
var data = MouseData.readPacket(buf);
if (isPhantom()) {
tryClickPhantom(data);
} else {
var stack = tryClickContainer(data.mouseButton == 0);
if (!stack.isEmpty())
syncToClient(TRY_CLICK_CONTAINER, buffer -> NetworkUtils.writeItemStack(buffer, stack));
}
} else if (id == UPDATE_TANK) {
var fluid = NetworkUtils.readFluidStack(buf);
setFluid(fluid);
} else if (id == PHANTOM_SCROLL) {
tryScrollPhantom(MouseData.readPacket(buf));
} else if (id == LOCK_FLUID) {
boolean locked = buf.readBoolean();
var fluidStack = NetworkUtils.readFluidStack(buf);
if (fluidStack == null) {
this.lockHandler.accept(locked);
} else {
this.jeiHandler.accept(fluidStack);
}
}
}
public void tryClickPhantom(MouseData data) {
EntityPlayer player = getSyncManager().getPlayer();
ItemStack currentStack = player.inventory.getItemStack();
FluidStack currentFluid = this.tank.getFluid();
if (currentStack.getCount() > 1) currentStack = GTUtility.copy(1, currentStack);
var fluidHandlerItem = FluidUtil.getFluidHandler(currentStack);
switch (data.mouseButton) {
case 0 -> {
if (currentStack.isEmpty() || fluidHandlerItem == null) {
if (this.canDrainSlot()) {
this.tank.drain(data.shift ? Integer.MAX_VALUE : 1000, true);
}
} else {
FluidStack cellFluid = fluidHandlerItem.drain(Integer.MAX_VALUE, false);
if ((this.showAmountOnSlot.getAsBoolean() || currentFluid == null) && cellFluid != null) {
if (this.canFillSlot()) {
if (!this.showAmountOnSlot.getAsBoolean()) {
cellFluid.amount = 1;
}
if (this.tank.fill(cellFluid, true) > 0) {
this.phantomFluid = cellFluid.copy();
}
}
} else {
if (this.canDrainSlot()) {
this.tank.drain(data.shift ? Integer.MAX_VALUE : 1000, true);
}
}
}
}
case 1 -> {
if (this.canFillSlot()) {
if (currentFluid != null) {
if (this.showAmountOnSlot.getAsBoolean()) {
FluidStack toFill = currentFluid.copy();
toFill.amount = 1000;
this.tank.fill(toFill, true);
}
} else if (this.phantomFluid != null) {
FluidStack toFill = this.phantomFluid.copy();
toFill.amount = this.showAmountOnSlot.getAsBoolean() ? 1 : toFill.amount;
this.tank.fill(toFill, true);
}
}
}
case 2 -> {
if (currentFluid != null && canDrainSlot())
this.tank.drain(data.shift ? Integer.MAX_VALUE : 1000, true);
}
}
}
public void tryScrollPhantom(MouseData mouseData) {
FluidStack currentFluid = this.tank.getFluid();
int amount = mouseData.mouseButton;
if (!this.showAmountOnSlot()) {
int newAmt = amount == 1 ? 1 : 0;
if (newAmt == 0) {
setFluid(null);
} else if (currentFluid != null && currentFluid.amount != newAmt) {
setAmount(newAmt);
}
return;
}
if (mouseData.shift) {
amount *= 10;
}
if (mouseData.ctrl) {
amount *= 100;
}
if (mouseData.alt) {
amount *= 1000;
}
if (currentFluid == null) {
if (amount > 0 && this.phantomFluid != null) {
FluidStack toFill = this.phantomFluid.copy();
toFill.amount = this.showAmountOnSlot() ? amount : 1;
this.tank.fill(toFill, true);
}
return;
}
if (amount > 0) {
FluidStack toFill = currentFluid.copy();
toFill.amount = amount;
this.tank.fill(toFill, true);
} else if (amount < 0) {
this.tank.drain(-amount, true);
}
}
public ItemStack tryClickContainer(boolean tryFillAll) {
ItemStack playerHeldStack = getSyncManager().getCursorItem();
if (playerHeldStack.isEmpty())
return ItemStack.EMPTY;
ItemStack useStack = GTUtility.copy(1, playerHeldStack);
var fluidHandlerItem = FluidUtil.getFluidHandler(useStack);
if (fluidHandlerItem == null) return ItemStack.EMPTY;
FluidStack tankFluid = tank.getFluid();
FluidStack heldFluid = fluidHandlerItem.drain(Integer.MAX_VALUE, false);
// nothing to do, return
if (tankFluid == null && heldFluid == null)
return ItemStack.EMPTY;
ItemStack returnable = ItemStack.EMPTY;
// tank is empty, try to fill tank
if (canFillSlot && tankFluid == null) {
returnable = fillTankFromStack(fluidHandlerItem, heldFluid, tryFillAll);
// hand is empty, try to drain tank
} else if (canDrainSlot && heldFluid == null) {
returnable = drainTankIntoStack(fluidHandlerItem, tankFluid, tryFillAll);
// neither is empty but tank is not full, try to fill tank
} else if (canFillSlot && tank.getFluidAmount() < tank.getCapacity() && heldFluid != null) {
returnable = fillTankFromStack(fluidHandlerItem, heldFluid, tryFillAll);
}
syncToClient(UPDATE_TANK, buffer -> NetworkUtils.writeFluidStack(buffer, tank.getFluid()));
return returnable;
}
private ItemStack fillTankFromStack(IFluidHandlerItem fluidHandler, @NotNull FluidStack heldFluid,
boolean tryFillAll) {
ItemStack heldItem = getSyncManager().getCursorItem();
if (heldItem.isEmpty()) return ItemStack.EMPTY;
FluidStack currentFluid = tank.getFluid();
// Fluid type does not match
if (currentFluid != null && !currentFluid.isFluidEqual(heldFluid)) return ItemStack.EMPTY;
int freeSpace = tank.getCapacity() - tank.getFluidAmount();
if (freeSpace <= 0) return ItemStack.EMPTY;
ItemStack itemStackEmptied = ItemStack.EMPTY;
int fluidAmountTaken = 0;
FluidStack drained = fluidHandler.drain(freeSpace, true);
if (drained != null && drained.amount > 0) {
itemStackEmptied = fluidHandler.getContainer();
fluidAmountTaken = drained.amount;
}
if (itemStackEmptied == ItemStack.EMPTY) {
return ItemStack.EMPTY;
}
// find out how many fills we can do
// same round down behavior as drain
int additional = tryFillAll ? Math.min(freeSpace / fluidAmountTaken, heldItem.getCount()) : 1;
FluidStack copiedFluidStack = heldFluid.copy();
copiedFluidStack.amount = fluidAmountTaken * additional;
tank.fill(copiedFluidStack, true);
itemStackEmptied.setCount(additional);
replaceCursorItemStack(itemStackEmptied);
playSound(heldFluid, true);
return itemStackEmptied;
}
private ItemStack drainTankIntoStack(IFluidHandlerItem fluidHandler, FluidStack tankFluid, boolean tryFillAll) {
ItemStack heldItem = getSyncManager().getCursorItem();
if (heldItem.isEmpty()) return ItemStack.EMPTY;
ItemStack fluidContainer = ItemStack.EMPTY;
int filled = fluidHandler.fill(tankFluid, false);
int stored = tankFluid.amount;
if (filled > 0) {
fluidHandler.fill(tankFluid, true);
tank.drain(filled, true);
fluidContainer = fluidHandler.getContainer();
if (tryFillAll) {
// Determine how many more items we can fill. One item is already filled.
// Integer division means it will round down, so it will only fill equivalent fluid amounts.
// For example:
// Click with 3 cells, with 2500L of fluid in the tank.
// 2 cells will be filled, and 500L will be left behind in the tank.
int additional = Math.min(heldItem.getCount(), stored / filled) - 1;
tank.drain(filled * additional, true);
fluidContainer.grow(additional);
}
replaceCursorItemStack(fluidContainer);
playSound(tankFluid, false);
}
return fluidContainer;
}
/**
* Replace the ItemStack on the player's cursor with the passed stack. Use to replace empty cells with filled, or
* filled cells with empty. If it is not fully emptied/filled, it will place the new items into the player inventory
* instead, and shrink the held stack by the appropriate amount.
*/
private void replaceCursorItemStack(ItemStack resultStack) {
int resultStackSize = resultStack.getMaxStackSize();
ItemStack playerStack = getSyncManager().getCursorItem();
if (!getSyncManager().isClient())
syncToClient(TRY_CLICK_CONTAINER, buffer -> NetworkUtils.writeItemStack(buffer, resultStack));
while (resultStack.getCount() > resultStackSize) {
playerStack.shrink(resultStackSize);
addItemToPlayerInventory(resultStack.splitStack(resultStackSize));
}
if (playerStack.getCount() == resultStack.getCount()) {
// every item on the cursor is mutated, so leave it there
getSyncManager().setCursorItem(resultStack);
} else {
// some items not mutated. Mutated items go into the inventory/world.
playerStack.shrink(resultStack.getCount());
getSyncManager().setCursorItem(playerStack);
addItemToPlayerInventory(resultStack);
}
}
/** Place an item into the player's inventory, or drop it in-world as an item entity if it cannot fit. */
private void addItemToPlayerInventory(ItemStack stack) {
if (stack == null) return;
var player = getSyncManager().getPlayer();
if (!player.inventory.addItemStackToInventory(stack) && !player.world.isRemote) {
EntityItem dropItem = player.entityDropItem(stack, 0);
if (dropItem != null) dropItem.setPickupDelay(0);
}
}
/**
* Play the appropriate fluid interaction sound for the fluid. <br />
* Must be called on server to work correctly
**/
private void playSound(FluidStack fluid, boolean fill) {
if (fluid == null) return;
SoundEvent soundEvent;
if (fill) {
soundEvent = fluid.getFluid().getFillSound(fluid);
} else {
soundEvent = fluid.getFluid().getEmptySound(fluid);
}
EntityPlayer player = getSyncManager().getPlayer();
player.world.playSound(null, player.posX, player.posY + 0.5, player.posZ,
soundEvent, SoundCategory.PLAYERS, 1.0F, 1.0F);
}
public FluidStack getPhantomFluid() {
return isPhantom() ? phantomFluid : null;
}
public FluidStack getLockedFluid() {
return !isPhantom() && canLockFluid() ? lockedFluid.get() : null;
}
public boolean canLockFluid() {
return jeiHandler != null && lockHandler != null && lockedFluid != null && isLocked != null;
}
public void toggleLockFluid() {
var cursorItem = getSyncManager().getCursorItem();
if (getLockedFluid() == null) {
if (cursorItem.isEmpty()) return;
if (cursorItem.getCount() > 1) cursorItem = GTUtility.copy(1, cursorItem);
var fluidHandler = FluidUtil.getFluidHandler(cursorItem);
if (fluidHandler == null) return;
var fluidStack = fluidHandler.getTankProperties()[0].getContents();
if (fluidStack == null) return;
lockFluid(fluidStack.copy(), true);
} else if (cursorItem.isEmpty()) {
lockFluid(null, true);
}
}
}