forked from CleanroomMC/ModularUI
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathFluidSlotSyncHandler.java
More file actions
428 lines (377 loc) · 14.6 KB
/
FluidSlotSyncHandler.java
File metadata and controls
428 lines (377 loc) · 14.6 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
package com.cleanroommc.modularui.value.sync;
import com.cleanroommc.modularui.network.NetworkUtils;
import com.cleanroommc.modularui.utils.MouseData;
import com.cleanroommc.modularui.utils.fluid.FluidInteractions;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.IFluidContainerItem;
import net.minecraftforge.fluids.IFluidTank;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import static com.google.common.primitives.Ints.saturatedCast;
public class FluidSlotSyncHandler extends ValueSyncHandler<FluidStack> {
public static boolean isFluidEmpty(@Nullable FluidStack fluidStack) {
return fluidStack == null || fluidStack.amount <= 0;
}
@Nullable
public static FluidStack copyFluid(@Nullable FluidStack fluidStack) {
return isFluidEmpty(fluidStack) ? null : fluidStack.copy();
}
public static final int SYNC_FLUID = 0;
public static final int SYNC_CLICK = 1;
public static final int SYNC_SCROLL = 2;
public static final int SYNC_CONTROLS_AMOUNT = 3;
@Nullable
private FluidStack cache;
private final IFluidTank fluidTank;
private boolean canFillSlot = true, canDrainSlot = true, controlsAmount = true, phantom = false;
@Nullable
private FluidStack lastStoredPhantomFluid;
public FluidSlotSyncHandler(IFluidTank fluidTank) {
this.fluidTank = fluidTank;
}
@Nullable
@Override
public FluidStack getValue() {
return this.cache;
}
@Override
public void setValue(@Nullable FluidStack value, boolean setSource, boolean sync) {
this.cache = copyFluid(value);
if (setSource) {
this.fluidTank.drain(Integer.MAX_VALUE, true);
if (!isFluidEmpty(value)) {
this.fluidTank.fill(value.copy(), true);
}
}
if (sync) {
if (NetworkUtils.isClient()) {
syncToServer(SYNC_FLUID, this::write);
} else {
syncToClient(SYNC_FLUID, this::write);
}
}
onValueChanged();
}
public boolean needsSync() {
FluidStack current = this.fluidTank.getFluid();
if (current == this.cache)
return false;
if (current == null || this.cache == null)
return true;
return current.amount != this.cache.amount || !current.isFluidEqual(this.cache);
}
@Override
public boolean updateCacheFromSource(boolean isFirstSync) {
if (isFirstSync || needsSync()) {
setValue(this.fluidTank.getFluid(), false, false);
return true;
}
return false;
}
@Override
public void notifyUpdate() {
setValue(this.fluidTank.getFluid(), false, true);
}
@Override
public void write(PacketBuffer buffer) {
NetworkUtils.writeFluidStack(buffer, this.cache);
}
@Override
public void read(PacketBuffer buffer) {
setValue(NetworkUtils.readFluidStack(buffer), true, false);
}
@Override
public void readOnClient(int id, PacketBuffer buf) {
if (id == SYNC_FLUID) {
read(buf);
} else if (id == SYNC_CONTROLS_AMOUNT) {
this.controlsAmount = buf.readBoolean();
}
}
@Override
public void readOnServer(int id, PacketBuffer buf) {
if (id == SYNC_FLUID) {
if (this.phantom) {
read(buf);
}
} else if (id == SYNC_CLICK) {
if (this.phantom) {
tryClickPhantom(MouseData.readPacket(buf));
} else {
tryClickContainer(MouseData.readPacket(buf));
}
} else if (id == SYNC_SCROLL) {
if (this.phantom) {
tryScrollPhantom(MouseData.readPacket(buf));
}
} else if (id == SYNC_CONTROLS_AMOUNT) {
this.controlsAmount = buf.readBoolean();
} else if (id == 4) {
MouseData mouseData = MouseData.readPacket(buf);
ItemStack draggedStack = NetworkUtils.readItemStack(buf);
if (this.phantom && draggedStack != null) {
tryClickPhantom(mouseData, draggedStack);
}
}
}
private void tryClickContainer(MouseData mouseData) {
boolean processFullStack = mouseData.shift;
ItemStack heldItem = getSyncManager().getCursorItem();
if (heldItem == null || heldItem.stackSize == 0) {
return;
}
ItemStack heldItemSizedOne = heldItem.copy();
heldItemSizedOne.stackSize = 1;
FluidStack currentFluid = fluidTank.getFluid();
FluidStack heldFluid = FluidInteractions.getFluidForRealItem(heldItemSizedOne);
if (heldFluid != null && heldFluid.amount <= 0) {
heldFluid = null;
}
if (currentFluid == null) {
if (!canFillSlot) {
return;
}
if (heldFluid == null) {
return;
}
fillFluid(heldFluid, processFullStack);
return;
}
if (heldFluid != null && fluidTank.getFluidAmount() < fluidTank.getCapacity()) {
if (canFillSlot) {
fillFluid(heldFluid, processFullStack);
return;
}
if (!canDrainSlot) {
return;
}
drainFluid(processFullStack);
} else {
if (!canDrainSlot) {
return;
}
drainFluid(processFullStack);
}
}
private void tryClickPhantom(MouseData mouseData) {
ItemStack cursorStack = getSyncManager().getCursorItem();
tryClickPhantom(mouseData, cursorStack);
}
private void tryClickPhantom(MouseData mouseData, ItemStack cursorStack) {
FluidStack currentFluid = fluidTank.getFluid();
if (mouseData.mouseButton == 0) {
if (cursorStack == null) {
if (canDrainSlot) {
fluidTank.drain(mouseData.shift ? Integer.MAX_VALUE : 1000, true);
}
} else {
ItemStack heldItemSizedOne = cursorStack.copy();
heldItemSizedOne.stackSize = 1;
FluidStack heldFluid = FluidInteractions.getFluidForPhantomItem(heldItemSizedOne);
if ((controlsAmount || currentFluid == null) && heldFluid != null) {
fillPhantom(heldFluid);
} else {
if (canDrainSlot) {
fluidTank.drain(mouseData.shift ? Integer.MAX_VALUE : 1000, true);
// "Swap" fluid
if (!controlsAmount && heldFluid != null && fluidTank.getFluidAmount() <= 0) {
fillPhantom(heldFluid);
}
}
}
}
} else if (mouseData.mouseButton == 1) {
if (canFillSlot) {
if (currentFluid != null) {
if (controlsAmount) {
FluidStack toFill = currentFluid.copy();
toFill.amount = 1000;
fluidTank.fill(toFill, true);
}
} else if (lastStoredPhantomFluid != null) {
FluidStack toFill = lastStoredPhantomFluid.copy();
toFill.amount = controlsAmount ? 1000 : 1;
fluidTank.fill(lastStoredPhantomFluid, true);
}
}
} else if (mouseData.mouseButton == 2 && currentFluid != null && canDrainSlot) {
fluidTank.drain(mouseData.shift ? Integer.MAX_VALUE : 1000, true);
}
}
private void fillPhantom(@NotNull FluidStack heldFluid) {
if (canFillSlot) {
if (!controlsAmount) {
heldFluid.amount = 1;
}
if (fluidTank.fill(heldFluid, true) > 0) {
lastStoredPhantomFluid = heldFluid.copy();
}
}
}
protected void drainFluid(boolean processFullStack) {
ItemStack heldItem = getSyncManager().getCursorItem();
if (heldItem == null || heldItem.stackSize == 0) {
return;
}
ItemStack heldItemSizedOne = heldItem.copy();
heldItemSizedOne.stackSize = 1;
FluidStack currentFluid = fluidTank.getFluid();
if (currentFluid == null) {
return;
}
currentFluid = currentFluid.copy();
int originalFluidAmount = fluidTank.getFluidAmount();
ItemStack filledContainer = FluidInteractions.fillFluidContainer(currentFluid, heldItemSizedOne);
if (filledContainer != null) {
int filledAmount = originalFluidAmount - currentFluid.amount;
if (filledAmount < 1) {
return;
}
fluidTank.drain(filledAmount, true);
if (processFullStack) {
int additionalParallel = Math.min(heldItem.stackSize - 1, currentFluid.amount / filledAmount);
fluidTank.drain(filledAmount * additionalParallel, true);
filledContainer.stackSize += additionalParallel;
}
replaceCursorItemStack(filledContainer);
playSound(currentFluid, false);
}
}
protected void fillFluid(@NotNull FluidStack heldFluid, boolean processFullStack) {
ItemStack heldItem = getSyncManager().getCursorItem();
if (heldItem == null || heldItem.stackSize == 0) {
return;
}
ItemStack heldItemSizedOne = heldItem.copy();
heldItemSizedOne.stackSize = 1;
FluidStack currentFluid = fluidTank.getFluid();
if (currentFluid != null && !currentFluid.isFluidEqual(heldFluid)) {
return;
}
int freeSpace = fluidTank.getCapacity() - fluidTank.getFluidAmount();
if (freeSpace <= 0) {
return;
}
ItemStack itemStackEmptied = null;
int fluidAmountTaken = 0;
if (freeSpace >= heldFluid.amount) {
itemStackEmptied = FluidInteractions.getContainerForFilledItem(heldItemSizedOne);
fluidAmountTaken = heldFluid.amount;
}
if (itemStackEmptied == null && heldItemSizedOne.getItem() instanceof IFluidContainerItem container) {
// either partially accepted, or is IFluidContainerItem
FluidStack tDrained = container.drain(heldItemSizedOne, saturatedCast(freeSpace), true);
if (tDrained != null && tDrained.amount > 0) {
// something is actually drained - change the cell and drop it to player
itemStackEmptied = heldItemSizedOne;
fluidAmountTaken = tDrained.amount;
}
}
if (itemStackEmptied == null) {
return;
}
int parallel = processFullStack ? Math.min(freeSpace / fluidAmountTaken, heldItem.stackSize) : 1;
FluidStack copiedFluidStack = heldFluid.copy();
copiedFluidStack.amount = fluidAmountTaken * parallel;
fluidTank.fill(copiedFluidStack, true);
itemStackEmptied.stackSize = parallel;
replaceCursorItemStack(itemStackEmptied);
playSound(heldFluid, true);
}
public void tryScrollPhantom(MouseData mouseData) {
FluidStack currentFluid = this.fluidTank.getFluid();
int amount = mouseData.mouseButton;
if (mouseData.shift) {
amount *= 10;
}
if (mouseData.ctrl) {
amount *= 100;
}
if (mouseData.alt) {
amount *= 1000;
}
if (currentFluid == null) {
if (amount > 0 && this.lastStoredPhantomFluid != null) {
FluidStack toFill = this.lastStoredPhantomFluid.copy();
toFill.amount = this.controlsAmount ? amount : 1;
this.fluidTank.fill(toFill, true);
}
return;
}
if (amount > 0 && this.controlsAmount) {
FluidStack toFill = currentFluid.copy();
toFill.amount = amount;
this.fluidTank.fill(toFill, true);
} else if (amount < 0) {
this.fluidTank.drain(-amount, true);
}
}
/**
* In 1.7.10 placing water or lava does not play sound, so we do nothing here.
* Override if you want to play something.
*/
private void playSound(FluidStack fluid, boolean fill) {
}
protected void replaceCursorItemStack(ItemStack resultStack) {
EntityPlayer player = getSyncManager().getPlayer();
int resultStackMaxStackSize = resultStack.getMaxStackSize();
while (resultStack.stackSize > resultStackMaxStackSize) {
player.inventory.getItemStack().stackSize -= resultStackMaxStackSize;
addItemToPlayerInventory(player, resultStack.splitStack(resultStackMaxStackSize));
}
if (getSyncManager().getCursorItem().stackSize == resultStack.stackSize) {
getSyncManager().setCursorItem(resultStack);
} else {
ItemStack heldItem = getSyncManager().getCursorItem();
heldItem.stackSize -= resultStack.stackSize;
getSyncManager().setCursorItem(heldItem);
addItemToPlayerInventory(player, resultStack);
}
}
protected static void addItemToPlayerInventory(EntityPlayer player, ItemStack stack) {
if (stack == null)
return;
if (!player.inventory.addItemStackToInventory(stack) && !player.worldObj.isRemote) {
EntityItem dropItem = player.entityDropItem(stack, 0);
dropItem.delayBeforeCanPickup = 0;
}
}
public IFluidTank getFluidTank() {
return this.fluidTank;
}
public boolean canDrainSlot() {
return this.canDrainSlot;
}
public boolean canFillSlot() {
return this.canFillSlot;
}
public boolean controlsAmount() {
return this.controlsAmount;
}
public boolean isPhantom() {
return this.phantom;
}
public FluidSlotSyncHandler phantom(boolean phantom) {
this.phantom = phantom;
return this;
}
public FluidSlotSyncHandler controlsAmount(boolean controlsAmount) {
this.controlsAmount = controlsAmount;
if (isValid()) {
sync(SYNC_CONTROLS_AMOUNT, buffer -> buffer.writeBoolean(controlsAmount));
}
return this;
}
public FluidSlotSyncHandler canDrainSlot(boolean canDrainSlot) {
this.canDrainSlot = canDrainSlot;
return this;
}
public FluidSlotSyncHandler canFillSlot(boolean canFillSlot) {
this.canFillSlot = canFillSlot;
return this;
}
}