From 95f68b1b7cb8235e6f5deaeb1238a54f2a83d082 Mon Sep 17 00:00:00 2001 From: Justin Vitale <4002351+justinvvitale@users.noreply.github.com> Date: Wed, 8 Jul 2020 21:59:09 +1000 Subject: [PATCH] Storage unit cleanup, optimization and fix displayed item --- .../item/StorageUnitBaseBlockEntity.java | 88 ++++++++++--------- .../techreborn/client/gui/GuiStorageUnit.java | 12 +-- 2 files changed, 51 insertions(+), 49 deletions(-) diff --git a/src/main/java/techreborn/blockentity/storage/item/StorageUnitBaseBlockEntity.java b/src/main/java/techreborn/blockentity/storage/item/StorageUnitBaseBlockEntity.java index 4cdd53850..f4653cb5e 100644 --- a/src/main/java/techreborn/blockentity/storage/item/StorageUnitBaseBlockEntity.java +++ b/src/main/java/techreborn/blockentity/storage/item/StorageUnitBaseBlockEntity.java @@ -25,8 +25,8 @@ package techreborn.blockentity.storage.item; import net.minecraft.block.BlockState; -import net.minecraft.block.entity.BlockEntityType; import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.CompoundTag; import net.minecraft.text.LiteralText; @@ -51,18 +51,18 @@ import techreborn.init.TRContent; import java.util.List; public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity - implements InventoryProvider, IToolDrop, IListInfoProvider, BuiltScreenHandlerProvider { + implements InventoryProvider, IToolDrop, IListInfoProvider, BuiltScreenHandlerProvider { // Inventory constants private static final int INPUT_SLOT = 0; private static final int OUTPUT_SLOT = 1; - + // Client sync variable, how much stored + public int storedAmount = 0; protected RebornInventory inventory; private int maxCapacity; - private boolean shouldUpdate = true; - private int prevCount = -1; + private Item prevItem = null; private ItemStack storeItemStack; @@ -93,46 +93,34 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity @Override public void tick() { super.tick(); - if (world.isClient) { + if (world == null || world.isClient) { return; } - // If there is an item in the input AND stored is less than max capacity if (!inventory.getStack(INPUT_SLOT).isEmpty() && !isFull()) { inventory.setStack(INPUT_SLOT, processInput(inventory.getStack(INPUT_SLOT))); - - shouldUpdate = true; } // Fill output slot with goodies when stored has items and output count is less than max stack size if (storeItemStack.getCount() > 0 && inventory.getStack(OUTPUT_SLOT).getCount() < getStoredStack().getMaxCount()) { populateOutput(); - - shouldUpdate = true; } if (type == TRContent.StorageUnit.CREATIVE) { if (!isFull() && !isEmpty()) { fillToCapacity(); - shouldUpdate = true; } } - // Update for locked item for clients - if(isLocked() && prevCount != this.getCurrentCapacity()){ - prevCount = this.getCurrentCapacity(); - syncWithAll(); - } - - - if (shouldUpdate) { - inventory.setChanged(); - markDirty(); - syncWithAll(); - - shouldUpdate = false; - } +// // Update for stored item for clients if out of date +// if(world.getTime() % 20 == 0) { +// Item storedItem = this.getStoredStack().getItem(); +// if (!isLocked() && prevItem != storedItem) { +// prevItem = storedItem; +// syncWithAll(); +// } +// } } private void populateOutput() { @@ -171,6 +159,10 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity return storeItemStack.isEmpty() ? inventory.getStack(OUTPUT_SLOT) : storeItemStack; } + public void setStoredStack(ItemStack itemStack) { + storeItemStack = itemStack; + } + // Returns the ItemStack to be displayed to the player via UI / model public ItemStack getDisplayedStack() { if (!isLocked()) { @@ -192,10 +184,6 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity return returnStack; } - public void setStoredStack(ItemStack itemStack) { - storeItemStack = itemStack; - } - public ItemStack processInput(ItemStack inputStack) { boolean isSameStack = isSameType(inputStack); @@ -357,9 +345,9 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity if (isReal || hasData) { if (!this.isEmpty()) { info.add( - new LiteralText(String.valueOf(this.getCurrentCapacity())) - .append(new TranslatableText("techreborn.tooltip.unit.divider")) - .append(this.getStoredStack().getName()) + new LiteralText(String.valueOf(this.getCurrentCapacity())) + .append(new TranslatableText("techreborn.tooltip.unit.divider")) + .append(this.getStoredStack().getName()) ); } else { info.add(new TranslatableText("techreborn.tooltip.unit.empty")); @@ -368,14 +356,14 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity info.add( new TranslatableText("techreborn.tooltip.unit.capacity") - .formatted(Formatting.GRAY) - .append( - new LiteralText(String.valueOf(this.getMaxCapacity())) - .formatted(Formatting.GOLD) - .append(" items (") - .append(String.valueOf(this.getMaxCapacity() / 64)) - .append(")") - ) + .formatted(Formatting.GRAY) + .append( + new LiteralText(String.valueOf(this.getMaxCapacity())) + .formatted(Formatting.GOLD) + .append(" items (") + .append(String.valueOf(this.getMaxCapacity() / 64)) + .append(")") + ) ); } @@ -447,11 +435,25 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity return !isEmpty(); } + public int getStoredAmount() { + return this.getCurrentCapacity(); + } + + public void setStoredAmount(int storedAmount) { + this.storedAmount = storedAmount; + } + + @Override public BuiltScreenHandler createScreenHandler(int syncID, final PlayerEntity playerEntity) { return new ScreenHandlerBuilder("chest").player(playerEntity.inventory).inventory().hotbar().addInventory() - .blockEntity(this).slot(0, 100, 53).outputSlot(1, 140, 53) - .sync(this::isLockedInt, this::setLockedInt).addInventory().create(this, syncID); + .blockEntity(this).slot(0, 100, 53) + .outputSlot(1, 140, 53) + .sync(this::isLockedInt, this::setLockedInt) + .sync(this::getStoredAmount, this::setStoredAmount) + .addInventory().create(this, syncID); + + // Note that inventory is synced, and it gets the stack from that } @Override diff --git a/src/main/java/techreborn/client/gui/GuiStorageUnit.java b/src/main/java/techreborn/client/gui/GuiStorageUnit.java index 11054c032..68a2eb905 100644 --- a/src/main/java/techreborn/client/gui/GuiStorageUnit.java +++ b/src/main/java/techreborn/client/gui/GuiStorageUnit.java @@ -51,7 +51,7 @@ public class GuiStorageUnit extends GuiBase { drawSlot(matrixStack, 100, 53, layer); drawSlot(matrixStack, 140, 53, layer); - builder.drawLockButton(matrixStack,this, 150, 4, mouseX, mouseY, layer, storageEntity.isLocked()); + builder.drawLockButton(matrixStack, this, 150, 4, mouseX, mouseY, layer, storageEntity.isLocked()); } @Override @@ -59,19 +59,19 @@ public class GuiStorageUnit extends GuiBase { super.drawForeground(matrixStack, mouseX, mouseY); // Draw in/out labels - builder.drawText(matrixStack,this, new TranslatableText("gui.techreborn.unit.in"), 100, 43, 4210752); - builder.drawText(matrixStack,this, new TranslatableText("gui.techreborn.unit.out"), 140, 43, 4210752); + builder.drawText(matrixStack, this, new TranslatableText("gui.techreborn.unit.in"), 100, 43, 4210752); + builder.drawText(matrixStack, this, new TranslatableText("gui.techreborn.unit.out"), 140, 43, 4210752); - if (storageEntity.isEmpty() && !storageEntity.isLocked()) { + if (storageEntity.storedAmount == 0 && !storageEntity.isLocked()) { textRenderer.draw(matrixStack, new TranslatableText("techreborn.tooltip.unit.empty"), 10, 20, 4210752); } else { textRenderer.draw(matrixStack, new TranslatableText("gui.techreborn.storage.store"), 10, 20, 4210752); textRenderer.draw(matrixStack, storageEntity.getDisplayedStack().getName(), 10, 30, 4210752); textRenderer.draw(matrixStack, new TranslatableText("gui.techreborn.storage.amount"), 10, 50, 4210752); - textRenderer.draw(matrixStack, String.valueOf(storageEntity.getCurrentCapacity()), 10, 60, 4210752); + textRenderer.draw(matrixStack, String.valueOf(storageEntity.storedAmount), 10, 60, 4210752); - String percentFilled = String.valueOf((int) ((double) storageEntity.getCurrentCapacity() / (double) storageEntity.getMaxCapacity() * 100)); + String percentFilled = String.valueOf((int) ((double) storageEntity.storedAmount / (double) storageEntity.getMaxCapacity() * 100)); textRenderer.draw(matrixStack, new TranslatableText("gui.techreborn.unit.used").append(percentFilled + "%"), 10, 70, 4210752);