-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathModularContainer.java
More file actions
560 lines (488 loc) · 24.2 KB
/
ModularContainer.java
File metadata and controls
560 lines (488 loc) · 24.2 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
package com.cleanroommc.modularui.screen;
import com.cleanroommc.modularui.ModularUI;
import com.cleanroommc.modularui.core.mixins.early.minecraft.ContainerAccessor;
import com.cleanroommc.modularui.factory.GuiData;
import com.cleanroommc.modularui.network.NetworkUtils;
import com.cleanroommc.modularui.utils.Platform;
import com.cleanroommc.modularui.value.sync.ModularSyncManager;
import com.cleanroommc.modularui.widgets.slot.ModularSlot;
import com.cleanroommc.modularui.widgets.slot.PlayerSlotGroup;
import com.cleanroommc.modularui.widgets.slot.SlotGroup;
import com.cleanroommc.bogosorter.api.ISortingContextBuilder;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.ClickType;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.common.Optional;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.items.ItemHandlerHelper;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.MustBeInvokedByOverriders;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
public class ModularContainer extends Container {
public static ModularContainer getCurrent(EntityPlayer player) {
if (player.openContainer instanceof ModularContainer container) {
return container;
}
return null;
}
private static final int DROP_TO_WORLD = -999;
private static final int LEFT_MOUSE = 0;
private static final int RIGHT_MOUSE = 1;
private EntityPlayer player;
private ModularSyncManager syncManager;
private boolean init = true;
// all phantom slots (inventory don't contain phantom slots)
private final List<ModularSlot> phantomSlots = new ArrayList<>();
private final List<ModularSlot> shiftClickSlots = new ArrayList<>();
private GuiData guiData;
private UISettings settings;
@SideOnly(Side.CLIENT)
private ModularScreen optionalScreen;
public ModularContainer() {}
@ApiStatus.Internal
public void construct(EntityPlayer player, ModularSyncManager msm, UISettings settings, String mainPanelName, GuiData guiData) {
this.player = player;
this.syncManager = msm;
this.syncManager.construct(this, mainPanelName);
this.settings = settings;
this.guiData = guiData;
sortShiftClickSlots();
}
@SideOnly(Side.CLIENT)
void initializeClient(ModularScreen screen) {
this.optionalScreen = screen;
}
@ApiStatus.Internal
@SideOnly(Side.CLIENT)
public void constructClientOnly() {
this.player = Platform.getClientPlayer();
this.syncManager = null;
}
public boolean isInitialized() {
return this.player != null;
}
@SideOnly(Side.CLIENT)
public ModularScreen getScreen() {
if (this.optionalScreen == null) throw new NullPointerException("ModularScreen is not yet initialised!");
return optionalScreen;
}
public ContainerAccessor acc() {
return (ContainerAccessor) this;
}
public void onModularContainerOpened() {}
/**
* Called when this container closes. This is different to {@link Container#onContainerClosed(EntityPlayer)}, since that one is also
* called from {@link GuiContainer#onGuiClosed()}, which means it is called even when the container may still exist.
* This happens when a temporary client screen takes over (like JEI,NEI,etc.). This is only called when the container actually closes.
*/
public void onModularContainerClosed() {}
public void onModularContainerDisposed() {}
@MustBeInvokedByOverriders
@Override
public void detectAndSendChanges() {
super.detectAndSendChanges();
if (this.syncManager != null) {
this.syncManager.detectAndSendChanges(this.init);
}
this.init = false;
}
@MustBeInvokedByOverriders
public void onUpdate() {
// detectAndSendChanges is potentially called multiple times per tick, while this method is called exactly once per tick
if (this.syncManager != null) {
this.syncManager.onUpdate();
}
}
private void sortShiftClickSlots() {
this.shiftClickSlots.sort(Comparator.comparingInt(slot -> Objects.requireNonNull(slot.getSlotGroup()).getShiftClickPriority()));
}
@Override
public void setAll(@NotNull List<ItemStack> items) {
if (this.inventorySlots.size() != items.size()) {
ModularUI.LOGGER.error("Here are {} slots, but expected {}", this.inventorySlots.size(), items.size());
}
for (int i = 0; i < Math.min(this.inventorySlots.size(), items.size()); ++i) {
this.getSlot(i).putStack(items.get(i));
}
}
@ApiStatus.Internal
public void registerSlot(String panelName, ModularSlot slot) {
if (slot.isPhantom()) {
if (this.phantomSlots.contains(slot)) {
throw new IllegalArgumentException("Tried to register slot which already exists!");
}
this.phantomSlots.add(slot);
} else {
if (this.inventorySlots.contains(slot)) {
throw new IllegalArgumentException("Tried to register slot which already exists!");
}
addSlotToContainer(slot);
}
if (slot.getSlotGroupName() != null) {
SlotGroup slotGroup = getSyncManager().getSlotGroup(panelName, slot.getSlotGroupName());
if (slotGroup == null) {
ModularUI.LOGGER.throwing(new IllegalArgumentException("SlotGroup '" + slot.getSlotGroupName() + "' is not registered!"));
return;
}
slot.slotGroup(slotGroup);
}
if (slot.getSlotGroup() != null) {
SlotGroup slotGroup = slot.getSlotGroup();
if (slotGroup.allowShiftTransfer()) {
this.shiftClickSlots.add(slot);
if (!this.init) {
sortShiftClickSlots();
}
}
}
}
@Contract("_, null, null -> fail")
@NotNull
@ApiStatus.Internal
public SlotGroup validateSlotGroup(String panelName, @Nullable String slotGroupName, @Nullable SlotGroup slotGroup) {
if (slotGroup != null) {
if (getSyncManager().getSlotGroup(panelName, slotGroup.getName()) == null) {
throw new IllegalArgumentException("Slot group is not registered in the GUI.");
}
return slotGroup;
}
if (slotGroupName != null) {
slotGroup = getSyncManager().getSlotGroup(panelName, slotGroupName);
if (slotGroup == null) {
throw new IllegalArgumentException("Can't find slot group for name " + slotGroupName);
}
return slotGroup;
}
throw new IllegalArgumentException("Either the slot group or the name must not be null!");
}
public ModularSyncManager getSyncManager() {
if (this.syncManager == null) {
throw new IllegalStateException("GuiSyncManager is not available for client only GUI's.");
}
return this.syncManager;
}
public boolean isClient() {
return this.syncManager == null || NetworkUtils.isClient(this.player);
}
public boolean isClientOnly() {
return this.syncManager == null;
}
public EntityPlayer getPlayer() {
return player;
}
public GuiData getGuiData() {
return guiData;
}
public ModularSlot getModularSlot(int index) {
Slot slot = this.inventorySlots.get(index);
if (slot instanceof ModularSlot modularSlot) {
return modularSlot;
}
throw new IllegalStateException("A non-ModularSlot was found, but all slots in a ModularContainer must extend ModularSlot.");
}
public List<ModularSlot> getShiftClickSlots() {
return Collections.unmodifiableList(this.shiftClickSlots);
}
public void onSlotChanged(ModularSlot slot, ItemStack stack, boolean onlyAmountChanged) {}
@Override
public boolean canDragIntoSlot(@NotNull Slot slotIn) {
return !(slotIn instanceof ModularSlot slot) || slot.canDragIntoSlot();
}
@Override
public boolean canInteractWith(@NotNull EntityPlayer playerIn) {
return this.settings.canPlayerInteractWithUI(playerIn);
}
@Override
public @NotNull ItemStack slotClick(int slotId, int mouseButton, @NotNull ClickType clickTypeIn, @NotNull EntityPlayer player) {
ItemStack returnable = Platform.EMPTY_STACK;
InventoryPlayer inventoryplayer = player.inventory;
if (clickTypeIn == ClickType.QUICK_CRAFT || acc().getDragEvent() != 0) {
return superSlotClick(slotId, mouseButton, clickTypeIn, player);
}
if ((clickTypeIn == ClickType.PICKUP || clickTypeIn == ClickType.QUICK_MOVE) &&
(mouseButton == LEFT_MOUSE || mouseButton == RIGHT_MOUSE)) {
if (slotId == DROP_TO_WORLD) {
return superSlotClick(slotId, mouseButton, clickTypeIn, player);
}
// early return
if (slotId < 0) return Platform.EMPTY_STACK;
if (clickTypeIn == ClickType.QUICK_MOVE) {
Slot fromSlot = getSlot(slotId);
if (!fromSlot.canTakeStack(player)) {
return Platform.EMPTY_STACK;
}
if (NEAAnimationHandler.shouldHandleNEA(this)) {
returnable = NEAAnimationHandler.injectQuickMove(this, player, slotId, fromSlot);
} else {
returnable = handleQuickMove(player, slotId, fromSlot);
}
} else {
Slot clickedSlot = getSlot(slotId);
ItemStack slotStack = clickedSlot.getStack();
ItemStack heldStack = inventoryplayer.getItemStack();
if (slotStack.isEmpty()) {
// no dif
if (!heldStack.isEmpty() && clickedSlot.isItemValid(heldStack)) {
int stackCount = mouseButton == LEFT_MOUSE ? heldStack.getCount() : 1;
int lim = clickedSlot.getItemStackLimit(heldStack);
if (stackCount > lim) {
stackCount = lim;
}
clickedSlot.putStack(heldStack.splitStack(stackCount));
}
} else if (clickedSlot.canTakeStack(player)) {
if (heldStack.isEmpty() && !slotStack.isEmpty()) {
int s = Math.min(slotStack.getCount(), slotStack.getMaxStackSize()); // checking max stack size here, probably for oversized slots
int toRemove = mouseButton == LEFT_MOUSE ? s : (s + 1) / 2;
inventoryplayer.setItemStack(slotStack.splitStack(toRemove));
clickedSlot.putStack(slotStack);
clickedSlot.onTake(player, inventoryplayer.getItemStack());
} else if (clickedSlot.isItemValid(heldStack)) {
if (slotStack.getItem() == heldStack.getItem() &&
slotStack.getMetadata() == heldStack.getMetadata() &&
ItemStack.areItemStackTagsEqual(slotStack, heldStack)) {
int stackCount = mouseButton == LEFT_MOUSE ? heldStack.getCount() : 1;
int lim = clickedSlot.getItemStackLimit(heldStack);
if (stackCount > lim - slotStack.getCount()) {
stackCount = lim - slotStack.getCount();
}
heldStack.shrink(stackCount);
slotStack.grow(stackCount);
clickedSlot.putStack(slotStack);
} else if (heldStack.getCount() <= clickedSlot.getItemStackLimit(heldStack)) {
clickedSlot.putStack(heldStack);
inventoryplayer.setItemStack(slotStack);
}
} else if (slotStack.getItem() == heldStack.getItem() &&
heldStack.getMaxStackSize() > 1 &&
(!slotStack.getHasSubtypes() || slotStack.getMetadata() == heldStack.getMetadata()) &&
ItemStack.areItemStackTagsEqual(slotStack, heldStack) && !slotStack.isEmpty()) {
int stackCount = slotStack.getCount();
if (stackCount + heldStack.getCount() <= heldStack.getMaxStackSize()) {
heldStack.grow(stackCount);
slotStack = clickedSlot.decrStackSize(stackCount);
if (slotStack.isEmpty()) {
clickedSlot.putStack(Platform.EMPTY_STACK);
}
clickedSlot.onTake(player, inventoryplayer.getItemStack());
}
}
}
clickedSlot.onSlotChanged();
}
detectAndSendChanges();
return returnable;
} else if (clickTypeIn == ClickType.SWAP && mouseButton >= 0 && mouseButton < 9) {
Slot hotbarSlot = findPlayerSlot(player, mouseButton);
if (hotbarSlot != null && slotId >= 0) {
Slot fromSlot = getSlot(slotId);
ItemStack fromStack = fromSlot.getStack();
ItemStack hotbarStack = hotbarSlot.getStack();
if (!fromStack.isEmpty() && !hotbarSlot.isItemValid(fromStack)) return Platform.EMPTY_STACK;
if (!hotbarStack.isEmpty() && !fromSlot.isItemValid(hotbarStack)) return Platform.EMPTY_STACK;
if (!fromStack.isEmpty() && !fromSlot.canTakeStack(player)) return Platform.EMPTY_STACK;
if (!hotbarStack.isEmpty() && !hotbarSlot.canTakeStack(player)) return Platform.EMPTY_STACK;
if (!fromStack.isEmpty() && !hotbarStack.isEmpty() &&
fromStack.getItem() == hotbarStack.getItem() &&
fromStack.getMetadata() == hotbarStack.getMetadata() &&
ItemStack.areItemStackTagsEqual(fromStack, hotbarStack)) {
int hotbarLimit = hotbarSlot.getItemStackLimit(fromStack);
if (hotbarStack.getCount() < hotbarLimit) {
int toMove = Math.min(fromStack.getCount(), hotbarLimit - hotbarStack.getCount());
hotbarStack.grow(toMove);
fromSlot.decrStackSize(toMove);
hotbarSlot.onSlotChanged();
fromSlot.onSlotChanged();
}
} else {
boolean canFitInHotbar = fromStack.isEmpty() || fromStack.getCount() <= hotbarSlot.getItemStackLimit(fromStack);
boolean canFitInFromSlot = hotbarStack.isEmpty() || hotbarStack.getCount() <= fromSlot.getItemStackLimit(hotbarStack);
if (canFitInHotbar && canFitInFromSlot) {
fromSlot.putStack(hotbarStack);
hotbarSlot.putStack(fromStack);
fromSlot.onTake(player, fromStack);
fromSlot.onSlotChanged();
hotbarSlot.onSlotChanged();
} else {
if (hotbarStack.isEmpty() && !fromStack.isEmpty()) {
int moveAmt = hotbarSlot.getItemStackLimit(fromStack);
hotbarSlot.putStack(fromSlot.decrStackSize(moveAmt));
fromSlot.onSlotChanged();
hotbarSlot.onSlotChanged();
} else if (fromStack.isEmpty() && !hotbarStack.isEmpty()) {
int moveAmt = fromSlot.getItemStackLimit(hotbarStack);
fromSlot.putStack(hotbarSlot.decrStackSize(moveAmt));
fromSlot.onSlotChanged();
hotbarSlot.onSlotChanged();
}
}
}
return Platform.EMPTY_STACK;
}
} else if (clickTypeIn == ClickType.THROW && inventoryplayer.getItemStack().isEmpty() && slotId >= 0) {
Slot slot = getSlot(slotId);
if (slot.getHasStack() && slot.canTakeStack(player)) {
ItemStack stackInSlot = slot.getStack();
int amountToDrop = 1;
// mouseButton 1 is CTRL+Q
if (mouseButton == 1) {
amountToDrop = Math.min(stackInSlot.getCount(), stackInSlot.getMaxStackSize());
}
if (amountToDrop > 0) {
ItemStack droppedStack = slot.decrStackSize(amountToDrop);
player.dropItem(droppedStack, true);
slot.onSlotChanged();
detectAndSendChanges();
}
return Platform.EMPTY_STACK;
}
}
return superSlotClick(slotId, mouseButton, clickTypeIn, player);
}
protected final @NotNull ItemStack superSlotClick(int slotId, int mouseButton, @NotNull ClickType clickTypeIn, @NotNull EntityPlayer player) {
return super.slotClick(slotId, mouseButton, clickTypeIn, player);
}
protected Slot findPlayerSlot(EntityPlayer player, int index) {
if (player == this.player || player.getEntityId() == this.player.getEntityId()) {
// if we want a slot of the player who opened the ui, we can just use the slot group
SlotGroup slotGroup = this.syncManager.getSlotGroup(PlayerSlotGroup.NAME);
if (slotGroup == null) return findExternalPlayerSlot(player, index);
for (Slot slot : slotGroup.getSlots()) {
if (slot.getSlotIndex() == index) {
return slot;
}
}
}
return findExternalPlayerSlot(player, index);
}
protected Slot findExternalPlayerSlot(EntityPlayer player, int index) {
// go through all slots and find a slot with a matching player and index
for (Slot slot : this.inventorySlots) {
EntityPlayer slotPlayer = ModularSlot.getPlayerSlotPlayer(slot);
if (slotPlayer != null &&
(player == slotPlayer || player.getEntityId() == slotPlayer.getEntityId()) &&
slot.getSlotIndex() == index) {
return slot;
}
}
return null;
}
public final ItemStack handleQuickMove(EntityPlayer player, int slotId, Slot fromSlot) {
// looping so that crafting works properly
ItemStack returnable;
ItemStack remainder;
do {
remainder = transferStackInSlot(player, slotId);
returnable = Platform.copyStack(remainder);
} while (!Platform.isStackEmpty(remainder) && ItemHandlerHelper.canItemStacksStack(fromSlot.getStack(), remainder));
return returnable;
}
@Override
public @NotNull ItemStack transferStackInSlot(@NotNull EntityPlayer playerIn, int index) {
ModularSlot slot = getModularSlot(index);
if (!slot.isPhantom()) {
ItemStack stack = slot.getStack();
if (!stack.isEmpty()) {
ItemStack copy = stack.copy();
stack = stack.copy();
int base = 0;
if (stack.getCount() > stack.getMaxStackSize()) {
base = stack.getCount() - stack.getMaxStackSize();
stack.setCount(stack.getMaxStackSize());
}
ItemStack remainder = transferItem(slot, stack.copy());
if (ItemStack.areItemStacksEqual(remainder, stack)) return Platform.EMPTY_STACK;
if (base == 0 && remainder.isEmpty()) stack = Platform.EMPTY_STACK;
else stack.setCount(base + remainder.getCount());
slot.putStack(stack);
slot.onSlotChange(remainder, copy);
slot.onTake(playerIn, remainder);
slot.onCraftShiftClick(playerIn, remainder);
return copy; // return a non-empty stack if insertion was successful, this causes this function to be called again, important for crafting
}
}
return Platform.EMPTY_STACK;
}
protected ItemStack transferItem(ModularSlot fromSlot, ItemStack fromStack) {
@Nullable SlotGroup fromSlotGroup = fromSlot.getSlotGroup();
// in first iteration only insert into non-empty, non-phantom slots
for (ModularSlot toSlot : getShiftClickSlots()) {
SlotGroup slotGroup = Objects.requireNonNull(toSlot.getSlotGroup());
if (slotGroup != fromSlotGroup && toSlot.isEnabled() && toSlot.isItemValid(fromStack)) {
ItemStack toStack = toSlot.getStack().copy();
if (!fromSlot.isPhantom() && ItemHandlerHelper.canItemStacksStack(fromStack, toStack)) {
int j = toStack.getCount() + fromStack.getCount();
int maxSize = toSlot.getItemStackLimit(fromStack);//Math.min(toSlot.getSlotStackLimit(), fromStack.getMaxStackSize());
if (j <= maxSize) {
fromStack.setCount(0);
toStack.setCount(j);
toSlot.putStack(toStack);
} else if (toStack.getCount() < maxSize) {
fromStack.shrink(maxSize - toStack.getCount());
toStack.setCount(maxSize);
toSlot.putStack(toStack);
}
if (fromStack.isEmpty()) {
return fromStack;
}
}
}
}
boolean hasNonEmptyPhantom = false;
// now insert into first empty slot (phantom or not) and check if we have any non-empty phantom slots
for (ModularSlot toSlot : getShiftClickSlots()) {
ItemStack itemstack = toSlot.getStack();
SlotGroup slotGroup = Objects.requireNonNull(toSlot.getSlotGroup());
if (slotGroup != fromSlotGroup && toSlot.isEnabled() && toSlot.isItemValid(fromStack)) {
if (toSlot.isPhantom()) {
if (!itemstack.isEmpty()) {
// skip non-empty phantom for now
hasNonEmptyPhantom = true;
} else {
toSlot.putStack(fromStack.copy());
return fromStack;
}
} else if (itemstack.isEmpty()) {
if (fromStack.getCount() > toSlot.getItemStackLimit(fromStack)) {
toSlot.putStack(fromStack.splitStack(toSlot.getItemStackLimit(fromStack)));
} else {
toSlot.putStack(fromStack.splitStack(fromStack.getCount()));
}
if (fromStack.getCount() < 1) {
break;
}
}
}
}
if (!hasNonEmptyPhantom) return fromStack;
// now insert into the first phantom slot we can find (will be non-empty)
// unfortunately, when all phantom slots are used it will always overwrite the first one
for (ModularSlot toSlot : getShiftClickSlots()) {
SlotGroup slotGroup = Objects.requireNonNull(toSlot.getSlotGroup());
if (slotGroup != fromSlotGroup && toSlot.isPhantom() && toSlot.isEnabled() && toSlot.isItemValid(fromStack)) {
// don't check for stackable, just overwrite
toSlot.putStack(fromStack.copy());
return fromStack;
}
}
return fromStack;
}
@Optional.Method(modid = ModularUI.BOGO_SORT)
public void buildSortingContext(ISortingContextBuilder builder) {
if (this.syncManager != null) {
this.syncManager.buildSortingContext(builder);
}
}
}