Skip to content

Commit ebed960

Browse files
committed
add locking to qchest
fix locking for qtank
1 parent af727e8 commit ebed960

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import codechicken.lib.render.pipeline.IVertexOperation;
4444
import codechicken.lib.vec.Matrix4;
4545
import com.cleanroommc.modularui.api.drawable.IKey;
46+
import com.cleanroommc.modularui.network.NetworkUtils;
4647
import com.cleanroommc.modularui.screen.ModularPanel;
4748
import com.cleanroommc.modularui.value.sync.PanelSyncManager;
4849
import com.cleanroommc.modularui.value.sync.SyncHandler;
@@ -71,6 +72,7 @@ public class MetaTileEntityQuantumChest extends MetaTileEntityQuantumStorage<IIt
7172
protected IItemHandler outputItemInventory;
7273
private ItemHandlerList combinedInventory;
7374
protected ItemStack previousStack;
75+
protected @NotNull ItemStack lockedStack = ItemStack.EMPTY;
7476
protected long previousStackSize;
7577

7678
public MetaTileEntityQuantumChest(ResourceLocation metaTileEntityId, int tier, long maxStoredItems) {
@@ -252,6 +254,8 @@ public NBTTagCompound writeToNBT(NBTTagCompound data) {
252254
tagCompound.setTag(NBT_ITEMSTACK, virtualItemStack.writeToNBT(new NBTTagCompound()));
253255
tagCompound.setLong(NBT_ITEMCOUNT, itemsStoredInside);
254256
}
257+
if (locked && !lockedStack.isEmpty())
258+
data.setTag("LockedStack", lockedStack.serializeNBT());
255259
return tagCompound;
256260
}
257261

@@ -264,6 +268,7 @@ public void readFromNBT(NBTTagCompound data) {
264268
this.itemsStoredInside = data.getLong(NBT_ITEMCOUNT);
265269
}
266270
}
271+
if (locked) this.lockedStack = new ItemStack(data.getCompoundTag("LockedStack"));
267272
}
268273

269274
@Override
@@ -307,11 +312,10 @@ public void writeItemStackData(NBTTagCompound itemStack) {
307312

308313
@Override
309314
protected void createWidgets(ModularPanel mainPanel, PanelSyncManager syncManager) {
310-
mainPanel
311-
.child(createQuantumDisplay("gregtech.machine.quantum_chest.items_stored",
312-
() -> IKey.lang(virtualItemStack.getDisplayName()).get(),
313-
textWidget -> !virtualItemStack.isEmpty(),
314-
() -> TextFormattingUtil.formatNumbers(itemsStoredInside)))
315+
mainPanel.child(createQuantumDisplay("gregtech.machine.quantum_chest.items_stored",
316+
() -> IKey.lang(virtualItemStack.getDisplayName()).get(),
317+
textWidget -> !virtualItemStack.isEmpty(),
318+
() -> TextFormattingUtil.formatNumbers(itemsStoredInside)))
315319
.child(new QuantumItemRendererWidget(itemInventory)
316320
.pos(148, 41));
317321
}
@@ -336,6 +340,7 @@ public void writeInitialSyncData(@NotNull PacketBuffer buf) {
336340
super.writeInitialSyncData(buf);
337341
this.virtualItemStack.setCount(1);
338342
buf.writeItemStack(virtualItemStack);
343+
NetworkUtils.writeItemStack(buf, lockedStack);
339344
buf.writeLong(itemsStoredInside);
340345
}
341346

@@ -348,6 +353,7 @@ public void receiveInitialSyncData(@NotNull PacketBuffer buf) {
348353
GTLog.logger.warn("Failed to load item from NBT in a quantum chest at " + this.getPos() +
349354
" on initial server/client sync");
350355
}
356+
this.lockedStack = NetworkUtils.readItemStack(buf);
351357
this.itemsStoredInside = buf.readLong();
352358
}
353359

@@ -412,6 +418,14 @@ public void setFrontFacing(EnumFacing frontFacing) {
412418
}
413419
}
414420

421+
@Override
422+
protected void setLocked(boolean locked) {
423+
super.setLocked(locked);
424+
if (locked && !this.virtualItemStack.isEmpty())
425+
this.lockedStack = this.virtualItemStack.copy();
426+
else this.lockedStack = ItemStack.EMPTY;
427+
}
428+
415429
@Override
416430
public void clearMachineInventory(@NotNull List<@NotNull ItemStack> itemBuffer) {
417431
clearInventory(itemBuffer, importItems);
@@ -509,6 +523,10 @@ public ItemStack insertItem(int slot, @NotNull ItemStack insertedStack, boolean
509523
return ItemStack.EMPTY;
510524
}
511525

526+
// check locked and if locked stack matches
527+
if (locked && !areItemStackIdentical(lockedStack, insertedStack))
528+
return insertedStack;
529+
512530
// If there is a virtualized stack and the stack to insert does not match it, do not insert anything
513531
if (itemsStoredInside > 0L &&
514532
!virtualItemStack.isEmpty() &&

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import codechicken.lib.render.pipeline.IVertexOperation;
5454
import codechicken.lib.vec.Matrix4;
5555
import com.cleanroommc.modularui.api.drawable.IKey;
56+
import com.cleanroommc.modularui.network.NetworkUtils;
5657
import com.cleanroommc.modularui.screen.ModularPanel;
5758
import com.cleanroommc.modularui.value.sync.PanelSyncManager;
5859
import org.apache.commons.lang3.ArrayUtils;
@@ -370,8 +371,7 @@ public boolean isValidFrontFacing(EnumFacing facing) {
370371
@Override
371372
public void writeInitialSyncData(@NotNull PacketBuffer buf) {
372373
super.writeInitialSyncData(buf);
373-
buf.writeCompoundTag(
374-
fluidTank.getFluid() == null ? null : fluidTank.getFluid().writeToNBT(new NBTTagCompound()));
374+
NetworkUtils.writeFluidStack(buf, fluidTank.getFluid());
375375
}
376376

377377
@Override
@@ -385,12 +385,7 @@ public void receiveInitialSyncData(@NotNull PacketBuffer buf) {
385385
this.frontFacing = EnumFacing.NORTH;
386386
}
387387
}
388-
try {
389-
this.fluidTank.setFluid(FluidStack.loadFluidStackFromNBT(buf.readCompoundTag()));
390-
} catch (IOException e) {
391-
GTLog.logger.warn("Failed to load fluid from NBT in a quantum tank at " + this.getPos() +
392-
" on initial server/client sync");
393-
}
388+
this.fluidTank.setFluid(NetworkUtils.readFluidStack(buf));
394389
}
395390

396391
@Override
@@ -454,6 +449,15 @@ public boolean onScrewdriverClick(EntityPlayer playerIn, EnumHand hand, EnumFaci
454449
return super.onScrewdriverClick(playerIn, hand, facing, hitResult);
455450
}
456451

452+
@Override
453+
protected void setLocked(boolean locked) {
454+
super.setLocked(locked);
455+
if (locked && fluidTank.getFluid() != null) {
456+
this.lockedFluid = fluidTank.getFluid().copy();
457+
this.lockedFluid.amount = 1;
458+
} else this.lockedFluid = null;
459+
}
460+
457461
@Override
458462
public ItemStack getPickItem(EntityPlayer player) {
459463
if (!player.isCreative()) return super.getPickItem(player);

0 commit comments

Comments
 (0)