From 52a0e0a4ed2aa33fabb02acb05edce9c42618559 Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Sun, 29 Mar 2020 04:54:58 -0400 Subject: [PATCH] Fix empty locked drawer being cleared on chunk reload; fix rendering 0 items in UI --- .../block/tile/TileEntityDrawers.java | 5 ++- .../tile/tiledata/FractionalDrawerGroup.java | 3 ++ .../tile/tiledata/StandardDrawerGroup.java | 6 ++- .../client/renderer/StorageRenderItem.java | 4 +- .../inventory/ItemStackHelper.java | 40 +++++++++++++------ .../storagedrawers/inventory/SlotDrawer.java | 2 +- 6 files changed, 41 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java b/src/main/java/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java index f2a05a086..24f2988b2 100644 --- a/src/main/java/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java +++ b/src/main/java/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java @@ -60,12 +60,13 @@ public abstract class TileEntityDrawers extends ChamTileEntity implements IDrawe private long lastClickTime; private UUID lastClickUUID; + private boolean loading; private class DrawerAttributes extends BasicDrawerAttributes { @Override protected void onAttributeChanged () { - if (!TileEntityDrawers.this.drawerAttributes.isItemLocked(LockAttribute.LOCK_POPULATED)) { + if (!loading && !TileEntityDrawers.this.drawerAttributes.isItemLocked(LockAttribute.LOCK_POPULATED)) { for (int slot = 0; slot < TileEntityDrawers.this.getGroup().getDrawerCount(); slot++) { if (TileEntityDrawers.this.emptySlotCanBeCleared(slot)) { IDrawer drawer = TileEntityDrawers.this.getGroup().getDrawer(slot); @@ -459,6 +460,7 @@ public int interactPutItemsIntoSlot (int slot, PlayerEntity player) { @Override public void readPortable (CompoundNBT tag) { + loading = true; super.readPortable(tag); //material = null; @@ -494,6 +496,7 @@ public void readPortable (CompoundNBT tag) { securityKey = null; if (tag.hasKey("Sec")) securityKey = tag.getString("Sec");*/ + loading = false; } @Override diff --git a/src/main/java/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/FractionalDrawerGroup.java b/src/main/java/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/FractionalDrawerGroup.java index f69db632a..1bafcc208 100644 --- a/src/main/java/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/FractionalDrawerGroup.java +++ b/src/main/java/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/FractionalDrawerGroup.java @@ -590,6 +590,9 @@ public ItemStack getStoredItemPrototype () { @Nonnull @Override public IDrawer setStoredItem (@Nonnull ItemStack itemPrototype) { + if (ItemStackHelper.isStackEncoded(itemPrototype)) + itemPrototype = ItemStackHelper.decodeItemStackPrototype(itemPrototype); + return storage.setStoredItem(slot, itemPrototype); } diff --git a/src/main/java/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/StandardDrawerGroup.java b/src/main/java/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/StandardDrawerGroup.java index 829072d0c..76ee0b7d2 100644 --- a/src/main/java/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/StandardDrawerGroup.java +++ b/src/main/java/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/StandardDrawerGroup.java @@ -181,9 +181,11 @@ public IDrawer setStoredItem (@Nonnull ItemStack itemPrototype) { } protected IDrawer setStoredItem (@Nonnull ItemStack itemPrototype, boolean notify) { - if (matcher.matches(itemPrototype)) { + if (ItemStackHelper.isStackEncoded(itemPrototype)) + itemPrototype = ItemStackHelper.decodeItemStackPrototype(itemPrototype); + + if (matcher.matches(itemPrototype)) return this; - } itemPrototype = ItemStackHelper.getItemPrototype(itemPrototype); if (itemPrototype.isEmpty()) { diff --git a/src/main/java/com/jaquadro/minecraft/storagedrawers/client/renderer/StorageRenderItem.java b/src/main/java/com/jaquadro/minecraft/storagedrawers/client/renderer/StorageRenderItem.java index 6966b41cf..44dff3094 100644 --- a/src/main/java/com/jaquadro/minecraft/storagedrawers/client/renderer/StorageRenderItem.java +++ b/src/main/java/com/jaquadro/minecraft/storagedrawers/client/renderer/StorageRenderItem.java @@ -90,10 +90,10 @@ public void renderItemOverlayIntoGUI (FontRenderer font, @Nonnull ItemStack item return; } - item = ItemStackHelper.decodeItemStack(item); - if (!item.isEmpty()) { + item = ItemStackHelper.decodeItemStack(item); + float scale = .5f; float xoff = 0; //if (font.getUnicodeFlag()) { diff --git a/src/main/java/com/jaquadro/minecraft/storagedrawers/inventory/ItemStackHelper.java b/src/main/java/com/jaquadro/minecraft/storagedrawers/inventory/ItemStackHelper.java index 66ff4d25c..1e3b84913 100644 --- a/src/main/java/com/jaquadro/minecraft/storagedrawers/inventory/ItemStackHelper.java +++ b/src/main/java/com/jaquadro/minecraft/storagedrawers/inventory/ItemStackHelper.java @@ -89,25 +89,39 @@ public static ItemStack encodeItemStack (@Nonnull ItemStack proto, int count) { } public static ItemStack decodeItemStack (@Nonnull ItemStack stack) { + int count = ItemStackHelper.decodedCount(stack); + ItemStack decode = ItemStackHelper.stripDecoding(stack); + decode.setCount(count); + return decode; + } + + public static ItemStack decodeItemStackPrototype (@Nonnull ItemStack stack) { + ItemStack decode = ItemStackHelper.stripDecoding(stack); + decode.setCount(1); + return decode; + } + + public static int decodedCount (@Nonnull ItemStack stack) { CompoundNBT tag = stack.getTag(); - if (tag == null) - return stack; + if (tag != null && tag.contains("__storagedrawers_count")) + return tag.getInt("__storagedrawers_count"); - if (tag.contains("__storagedrawers_count")) { - ItemStack decode = stack.copy(); - decode.setCount(tag.getInt("__storagedrawers_count")); + return stack.getCount(); + } - tag = decode.getTag(); - if (tag != null) { - decode.getTag().remove("__storagedrawers_count"); - if (tag.size() == 0) - decode.setTag(null); - } + public static ItemStack stripDecoding (@Nonnull ItemStack stack) { + ItemStack decode = stack.copy(); + CompoundNBT tag = decode.getTag(); - return decode; + if (tag != null && tag.contains("__storagedrawers_count")) { + tag.remove("__storagedrawers_count"); + if (tag.size() == 0) + decode.setTag(null); + else + decode.setTag(tag); } - return stack; + return decode; } public static boolean isStackEncoded (@Nonnull ItemStack stack) { diff --git a/src/main/java/com/jaquadro/minecraft/storagedrawers/inventory/SlotDrawer.java b/src/main/java/com/jaquadro/minecraft/storagedrawers/inventory/SlotDrawer.java index 831fbf747..46049403d 100644 --- a/src/main/java/com/jaquadro/minecraft/storagedrawers/inventory/SlotDrawer.java +++ b/src/main/java/com/jaquadro/minecraft/storagedrawers/inventory/SlotDrawer.java @@ -42,8 +42,8 @@ public ItemStack getStack () { @Override public void putStack (@Nonnull ItemStack stack) { - stack = ItemStackHelper.decodeItemStack(stack); IDrawer target = drawer.setStoredItem(stack); + stack = ItemStackHelper.decodeItemStack(stack); target.setStoredItemCount(stack.getCount()); }