Skip to content

Commit 7b9eaba

Browse files
authored
fix quantum chest storage overflow (GregTechCEu#2036)
1 parent 26e236c commit 7b9eaba

File tree

2 files changed

+60
-59
lines changed

2 files changed

+60
-59
lines changed

src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityCreativeChest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public void renderMetaTileEntity(CCRenderState renderState, Matrix4 translation,
7373

7474
@Override
7575
public void renderMetaTileEntity(double x, double y, double z, float partialTicks) {
76-
QuantumStorageRenderer.renderChestStack(x, y, z, this, this.itemStack, 420, partialTicks);
76+
QuantumStorageRenderer.renderChestStack(x, y, z, this, this.virtualItemStack, 420, partialTicks);
7777
}
7878

7979

@@ -116,7 +116,7 @@ protected ModularUI createUI(EntityPlayer entityPlayer) {
116116
@Override
117117
public void update() {
118118
ItemStack stack = handler.getStackInSlot(0).copy();
119-
this.itemStack = stack; // For rendering purposes
119+
this.virtualItemStack = stack; // For rendering purposes
120120
super.update();
121121
if (ticksPerCycle == 0 || getOffsetTimer() % ticksPerCycle != 0) return;
122122
if (getWorld().isRemote || !active || stack.isEmpty()) return;
@@ -150,7 +150,7 @@ public NBTTagCompound writeToNBT(NBTTagCompound data) {
150150
public void readFromNBT(NBTTagCompound data) {
151151
super.readFromNBT(data);
152152
handler.deserializeNBT(data.getCompoundTag("ItemStackHandler"));
153-
this.itemStack = handler.getStackInSlot(0); // For rendering purposes
153+
this.virtualItemStack = handler.getStackInSlot(0); // For rendering purposes
154154
itemsPerCycle = data.getInteger("ItemsPerCycle");
155155
ticksPerCycle = data.getInteger("TicksPerCycle");
156156
active = data.getBoolean("Active");
@@ -188,6 +188,6 @@ public void addInformation(ItemStack stack, @Nullable World player, List<String>
188188
@Override
189189
public void receiveInitialSyncData(PacketBuffer buf) {
190190
super.receiveInitialSyncData(buf);
191-
this.handler.setStackInSlot(0, this.itemStack);
191+
this.handler.setStackInSlot(0, this.virtualItemStack);
192192
}
193193
}

src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityQuantumChest.java

Lines changed: 56 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,9 @@
5858

5959
public class MetaTileEntityQuantumChest extends MetaTileEntity implements ITieredMetaTileEntity, IActiveOutputSide, IFastRenderMetaTileEntity {
6060

61-
6261
private final int tier;
6362
private final long maxStoredItems;
64-
protected ItemStack itemStack = ItemStack.EMPTY;
63+
protected ItemStack virtualItemStack = ItemStack.EMPTY;
6564
private long itemsStoredInside = 0L;
6665
private boolean autoOutputItems;
6766
private EnumFacing outputFacing;
@@ -107,7 +106,7 @@ public void renderMetaTileEntity(CCRenderState renderState, Matrix4 translation,
107106

108107
@Override
109108
public void renderMetaTileEntity(double x, double y, double z, float partialTicks) {
110-
QuantumStorageRenderer.renderChestStack(x, y, z, this, itemStack, itemsStoredInside, partialTicks);
109+
QuantumStorageRenderer.renderChestStack(x, y, z, this, virtualItemStack, itemsStoredInside, partialTicks);
111110
}
112111

113112
@Override
@@ -124,10 +123,10 @@ public void update() {
124123
ItemStack inputStack = importItems.getStackInSlot(0);
125124
ItemStack outputStack = exportItems.getStackInSlot(0);
126125
if (outputStack.isEmpty() || outputStack.isItemEqual(inputStack) && ItemStack.areItemStackTagsEqual(inputStack, outputStack)) {
127-
if (!inputStack.isEmpty() && (itemStack.isEmpty() || areItemStackIdentical(itemStack, inputStack))) {
126+
if (!inputStack.isEmpty() && (virtualItemStack.isEmpty() || areItemStackIdentical(virtualItemStack, inputStack))) {
128127
int amountOfItemsToInsert = (int) Math.min(inputStack.getCount(), maxStoredItems - itemsStoredInside);
129-
if (this.itemsStoredInside == 0L || itemStack.isEmpty()) {
130-
this.itemStack = GTUtility.copy(1, inputStack);
128+
if (this.itemsStoredInside == 0L || virtualItemStack.isEmpty()) {
129+
this.virtualItemStack = GTUtility.copy(1, inputStack);
131130
}
132131
inputStack.shrink(amountOfItemsToInsert);
133132
importItems.setStackInSlot(0, inputStack);
@@ -137,18 +136,18 @@ public void update() {
137136
}
138137
}
139138
}
140-
if (itemsStoredInside > 0 && !itemStack.isEmpty()) {
139+
if (itemsStoredInside > 0 && !virtualItemStack.isEmpty()) {
141140
ItemStack outputStack = exportItems.getStackInSlot(0);
142-
int maxStackSize = itemStack.getMaxStackSize();
143-
if (outputStack.isEmpty() || (areItemStackIdentical(itemStack, outputStack) && outputStack.getCount() < maxStackSize)) {
141+
int maxStackSize = virtualItemStack.getMaxStackSize();
142+
if (outputStack.isEmpty() || (areItemStackIdentical(virtualItemStack, outputStack) && outputStack.getCount() < maxStackSize)) {
144143
int amountOfItemsToRemove = (int) Math.min(maxStackSize - outputStack.getCount(), itemsStoredInside);
145144
if (outputStack.isEmpty()) {
146-
outputStack = GTUtility.copy(amountOfItemsToRemove, itemStack);
145+
outputStack = GTUtility.copy(amountOfItemsToRemove, virtualItemStack);
147146
} else outputStack.grow(amountOfItemsToRemove);
148147
exportItems.setStackInSlot(0, outputStack);
149148
this.itemsStoredInside -= amountOfItemsToRemove;
150149
if (this.itemsStoredInside == 0) {
151-
this.itemStack = ItemStack.EMPTY;
150+
this.virtualItemStack = ItemStack.EMPTY;
152151
}
153152

154153
markDirty();
@@ -158,9 +157,9 @@ public void update() {
158157
if (isAutoOutputItems()) {
159158
pushItemsIntoNearbyHandlers(currentOutputFacing);
160159
}
161-
if (previousStack == null || !areItemStackIdentical(previousStack, itemStack)) {
162-
writeCustomData(UPDATE_ITEM, buf -> buf.writeItemStack(itemStack));
163-
previousStack = itemStack;
160+
if (previousStack == null || !areItemStackIdentical(previousStack, virtualItemStack)) {
161+
writeCustomData(UPDATE_ITEM, buf -> buf.writeItemStack(virtualItemStack));
162+
previousStack = virtualItemStack;
164163
}
165164
if (previousStackSize != itemsStoredInside) {
166165
writeCustomData(UPDATE_ITEM_COUNT, buf -> buf.writeLong(itemsStoredInside));
@@ -246,8 +245,8 @@ public NBTTagCompound writeToNBT(NBTTagCompound data) {
246245
data.setInteger("OutputFacing", getOutputFacing().getIndex());
247246
data.setBoolean("AutoOutputItems", autoOutputItems);
248247
data.setBoolean("AllowInputFromOutputSide", allowInputFromOutputSide);
249-
if (!itemStack.isEmpty() && itemsStoredInside > 0L) {
250-
tagCompound.setTag(NBT_ITEMSTACK, itemStack.writeToNBT(new NBTTagCompound()));
248+
if (!virtualItemStack.isEmpty() && itemsStoredInside > 0L) {
249+
tagCompound.setTag(NBT_ITEMSTACK, virtualItemStack.writeToNBT(new NBTTagCompound()));
251250
tagCompound.setLong(NBT_ITEMCOUNT, itemsStoredInside);
252251
}
253252
return tagCompound;
@@ -260,8 +259,8 @@ public void readFromNBT(NBTTagCompound data) {
260259
this.autoOutputItems = data.getBoolean("AutoOutputItems");
261260
this.allowInputFromOutputSide = data.getBoolean("AllowInputFromOutputSide");
262261
if (data.hasKey("ItemStack", NBT.TAG_COMPOUND)) {
263-
this.itemStack = new ItemStack(data.getCompoundTag("ItemStack"));
264-
if (!itemStack.isEmpty()) {
262+
this.virtualItemStack = new ItemStack(data.getCompoundTag("ItemStack"));
263+
if (!virtualItemStack.isEmpty()) {
265264
this.itemsStoredInside = data.getLong(NBT_ITEMCOUNT);
266265
}
267266
}
@@ -271,8 +270,8 @@ public void readFromNBT(NBTTagCompound data) {
271270
public void initFromItemStackData(NBTTagCompound itemStack) {
272271
super.initFromItemStackData(itemStack);
273272
if (itemStack.hasKey(NBT_ITEMSTACK, NBT.TAG_COMPOUND)) {
274-
this.itemStack = new ItemStack(itemStack.getCompoundTag(NBT_ITEMSTACK));
275-
if (!this.itemStack.isEmpty()) {
273+
this.virtualItemStack = new ItemStack(itemStack.getCompoundTag(NBT_ITEMSTACK));
274+
if (!this.virtualItemStack.isEmpty()) {
276275
this.itemsStoredInside = itemStack.getLong(NBT_ITEMCOUNT);
277276
}
278277
} else if (itemStack.hasKey(NBT_PARTIALSTACK, NBT.TAG_COMPOUND)) {
@@ -283,16 +282,16 @@ public void initFromItemStackData(NBTTagCompound itemStack) {
283282
@Override
284283
public void writeItemStackData(NBTTagCompound itemStack) {
285284
super.writeItemStackData(itemStack);
286-
if (!this.itemStack.isEmpty()) {
287-
itemStack.setTag(NBT_ITEMSTACK, this.itemStack.writeToNBT(new NBTTagCompound()));
285+
if (!this.virtualItemStack.isEmpty()) {
286+
itemStack.setTag(NBT_ITEMSTACK, this.virtualItemStack.writeToNBT(new NBTTagCompound()));
288287
itemStack.setLong(NBT_ITEMCOUNT, itemsStoredInside + this.exportItems.getStackInSlot(0).getCount());
289288
} else {
290289
ItemStack partialStack = exportItems.extractItem(0, 64, false);
291290
if (!partialStack.isEmpty()) {
292291
itemStack.setTag(NBT_PARTIALSTACK, partialStack.writeToNBT(new NBTTagCompound()));
293292
}
294293
}
295-
this.itemStack = ItemStack.EMPTY;
294+
this.virtualItemStack = ItemStack.EMPTY;
296295
this.itemsStoredInside = 0;
297296
exportItems.setStackInSlot(0, ItemStack.EMPTY);
298297
}
@@ -346,7 +345,7 @@ public void writeInitialSyncData(PacketBuffer buf) {
346345
super.writeInitialSyncData(buf);
347346
buf.writeByte(getOutputFacing().getIndex());
348347
buf.writeBoolean(autoOutputItems);
349-
buf.writeItemStack(itemStack);
348+
buf.writeItemStack(virtualItemStack);
350349
buf.writeLong(itemsStoredInside);
351350
}
352351

@@ -356,7 +355,7 @@ public void receiveInitialSyncData(PacketBuffer buf) {
356355
this.outputFacing = EnumFacing.VALUES[buf.readByte()];
357356
this.autoOutputItems = buf.readBoolean();
358357
try {
359-
this.itemStack = buf.readItemStack();
358+
this.virtualItemStack = buf.readItemStack();
360359
} catch (IOException ignored) {
361360
GTLog.logger.warn("Failed to load item from NBT in a quantum chest at " + this.getPos() + " on initial server/client sync");
362361
}
@@ -381,7 +380,7 @@ public void receiveCustomData(int dataId, PacketBuffer buf) {
381380
scheduleRenderUpdate();
382381
} else if (dataId == UPDATE_ITEM) {
383382
try {
384-
this.itemStack = buf.readItemStack();
383+
this.virtualItemStack = buf.readItemStack();
385384
} catch (IOException e) {
386385
GTLog.logger.error("Failed to read item stack in a quantum chest!");
387386
}
@@ -484,7 +483,7 @@ public int getSlots() {
484483
@Nonnull
485484
@Override
486485
public ItemStack getStackInSlot(int slot) {
487-
ItemStack itemStack = MetaTileEntityQuantumChest.this.itemStack;
486+
ItemStack itemStack = MetaTileEntityQuantumChest.this.virtualItemStack;
488487
long itemsStored = MetaTileEntityQuantumChest.this.itemsStoredInside;
489488
if (itemStack.isEmpty() || itemsStored == 0L) {
490489
return ItemStack.EMPTY;
@@ -503,15 +502,15 @@ public int getSlotLimit(int slot) {
503502
@Override
504503
public ItemStack extractItem(int slot, int amount, boolean simulate) {
505504
int extractedAmount = (int) Math.min(amount, itemsStoredInside);
506-
if (itemStack.isEmpty() || extractedAmount == 0) {
505+
if (virtualItemStack.isEmpty() || extractedAmount == 0) {
507506
return ItemStack.EMPTY;
508507
}
509-
ItemStack extractedStack = itemStack.copy();
508+
ItemStack extractedStack = virtualItemStack.copy();
510509
extractedStack.setCount(extractedAmount);
511510
if (!simulate) {
512511
MetaTileEntityQuantumChest.this.itemsStoredInside -= extractedAmount;
513512
if (itemsStoredInside == 0L) {
514-
MetaTileEntityQuantumChest.this.itemStack = ItemStack.EMPTY;
513+
MetaTileEntityQuantumChest.this.virtualItemStack = ItemStack.EMPTY;
515514
}
516515
}
517516
return extractedStack;
@@ -524,66 +523,68 @@ public ItemStack insertItem(int slot, @Nonnull ItemStack insertedStack, boolean
524523
return ItemStack.EMPTY;
525524
}
526525

526+
// If there is a virtualized stack and the stack to insert does not match it, do not insert anything
527527
if (itemsStoredInside > 0L &&
528-
!itemStack.isEmpty() &&
529-
!areItemStackIdentical(itemStack, insertedStack)) {
528+
!virtualItemStack.isEmpty() &&
529+
!areItemStackIdentical(virtualItemStack, insertedStack)) {
530530
return insertedStack;
531531
}
532532

533533
// The Quantum Chest automatically populates the export slot, so we need to check what is contained in it
534534
ItemStack exportItems = getExportItems().getStackInSlot(0);
535535

536-
// Check if the item being inserted matches the item in the export slot
537-
boolean insertMatching = areItemStackIdentical(insertedStack, exportItems);
538-
539536
// If the item being inserted does not match the item in the export slot, insert into the input slot and do not virtualize
540-
if (!insertMatching) {
537+
if (!areItemStackIdentical(insertedStack, exportItems)) {
541538
return MetaTileEntityQuantumChest.this.importItems.insertItem(0, insertedStack, simulate);
542539
}
543540

544-
int virtualizedAmount;
545-
int amountInsertedIntoExport;
546-
547-
int spaceInExport = Math.abs(exportItems.getCount() - exportItems.getMaxStackSize());
541+
int spaceInExport = Math.abs(exportItems.getMaxStackSize() - exportItems.getCount());
548542

549543
// Attempt to insert into the export slot first
550-
amountInsertedIntoExport = Math.min(spaceInExport, insertedStack.getCount());
551-
552-
// If we had more Items than would fit into the export slot, virtualize the remainder
553-
if (amountInsertedIntoExport < insertedStack.getCount()) {
554-
long amountLeftInChest = itemStack.isEmpty() ? maxStoredItems : maxStoredItems - itemsStoredInside;
555-
virtualizedAmount = (int) Math.min(insertedStack.getCount() - amountInsertedIntoExport, amountLeftInChest);
544+
int amountCanInsertIntoExport = Math.min(spaceInExport, insertedStack.getCount());
556545

557-
} else {
558-
// Return early, as we did not virtualize anything, as it all fit into the output slot
546+
if (insertedStack.getCount() <= amountCanInsertIntoExport) {
547+
// If all the items can fit into export slot, store it there
559548
return MetaTileEntityQuantumChest.this.exportItems.insertItem(0, insertedStack, simulate);
560549
}
561550

551+
// Have more items than would fit into the export slot, so virtualize the remainder
552+
long amountLeftInChest = virtualItemStack.isEmpty() ? maxStoredItems : maxStoredItems - itemsStoredInside;
553+
554+
int maxVirtualAmount = insertedStack.getCount() - amountCanInsertIntoExport;
555+
int virtualizedAmount = (int) Math.min(maxVirtualAmount, amountLeftInChest);
556+
562557
ItemStack remainingStack = ItemStack.EMPTY;
563558

564-
// If we are at the maximum that the chest can hold
565-
if (insertedStack.getCount() - amountInsertedIntoExport > virtualizedAmount) {
559+
// If we are at the maximum that the chest can hold, the remainder stack has all items that could not fit
560+
if (virtualizedAmount < maxVirtualAmount) {
566561
remainingStack = insertedStack.copy();
567562
remainingStack.setCount(insertedStack.getCount() - virtualizedAmount);
568563
}
564+
569565
if (!simulate) {
570566
if (remainingStack.isEmpty()) {
571-
if (itemStack.isEmpty()) {
567+
// inserted everything
568+
if (virtualItemStack.isEmpty()) {
569+
// have no virtual stack, so set it to the inserted stack
572570
ItemStack virtualStack = insertedStack.copy();
573571
virtualStack.setCount(virtualizedAmount);
574-
MetaTileEntityQuantumChest.this.itemStack = virtualStack;
572+
MetaTileEntityQuantumChest.this.virtualItemStack = virtualStack;
575573
MetaTileEntityQuantumChest.this.itemsStoredInside = virtualizedAmount;
576574
} else {
575+
// update the virtualized total count
577576
MetaTileEntityQuantumChest.this.itemsStoredInside += virtualizedAmount;
578577
}
579578

580-
if (amountInsertedIntoExport != 0) {
579+
if (amountCanInsertIntoExport != 0) {
580+
// fill the export slot as much as possible
581581
ItemStack insertedStackCopy = insertedStack.copy();
582-
insertedStackCopy.setCount(amountInsertedIntoExport);
582+
insertedStackCopy.setCount(amountCanInsertIntoExport);
583583
MetaTileEntityQuantumChest.this.exportItems.insertItem(0, insertedStackCopy, simulate);
584584
}
585585
} else {
586-
MetaTileEntityQuantumChest.this.itemsStoredInside += remainingStack.getCount();
586+
// could not fit everything, but still need to update the virtualized total count
587+
MetaTileEntityQuantumChest.this.itemsStoredInside += virtualizedAmount;
587588
}
588589
}
589590

0 commit comments

Comments
 (0)