Storage unit cleanup, optimization and fix displayed item

This commit is contained in:
Justin Vitale 2020-07-08 21:59:09 +10:00
parent 92eafec301
commit 95f68b1b7c
2 changed files with 51 additions and 49 deletions

View file

@ -25,8 +25,8 @@
package techreborn.blockentity.storage.item; package techreborn.blockentity.storage.item;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.text.LiteralText; import net.minecraft.text.LiteralText;
@ -57,12 +57,12 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity
// Inventory constants // Inventory constants
private static final int INPUT_SLOT = 0; private static final int INPUT_SLOT = 0;
private static final int OUTPUT_SLOT = 1; private static final int OUTPUT_SLOT = 1;
// Client sync variable, how much stored
public int storedAmount = 0;
protected RebornInventory<StorageUnitBaseBlockEntity> inventory; protected RebornInventory<StorageUnitBaseBlockEntity> inventory;
private int maxCapacity; private int maxCapacity;
private boolean shouldUpdate = true; private boolean shouldUpdate = true;
private int prevCount = -1; private Item prevItem = null;
private ItemStack storeItemStack; private ItemStack storeItemStack;
@ -93,46 +93,34 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity
@Override @Override
public void tick() { public void tick() {
super.tick(); super.tick();
if (world.isClient) { if (world == null || world.isClient) {
return; return;
} }
// If there is an item in the input AND stored is less than max capacity // If there is an item in the input AND stored is less than max capacity
if (!inventory.getStack(INPUT_SLOT).isEmpty() && !isFull()) { if (!inventory.getStack(INPUT_SLOT).isEmpty() && !isFull()) {
inventory.setStack(INPUT_SLOT, processInput(inventory.getStack(INPUT_SLOT))); 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 // 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()) { if (storeItemStack.getCount() > 0 && inventory.getStack(OUTPUT_SLOT).getCount() < getStoredStack().getMaxCount()) {
populateOutput(); populateOutput();
shouldUpdate = true;
} }
if (type == TRContent.StorageUnit.CREATIVE) { if (type == TRContent.StorageUnit.CREATIVE) {
if (!isFull() && !isEmpty()) { if (!isFull() && !isEmpty()) {
fillToCapacity(); fillToCapacity();
shouldUpdate = true;
} }
} }
// Update for locked item for clients // // Update for stored item for clients if out of date
if(isLocked() && prevCount != this.getCurrentCapacity()){ // if(world.getTime() % 20 == 0) {
prevCount = this.getCurrentCapacity(); // Item storedItem = this.getStoredStack().getItem();
syncWithAll(); // if (!isLocked() && prevItem != storedItem) {
} // prevItem = storedItem;
// syncWithAll();
// }
if (shouldUpdate) { // }
inventory.setChanged();
markDirty();
syncWithAll();
shouldUpdate = false;
}
} }
private void populateOutput() { private void populateOutput() {
@ -171,6 +159,10 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity
return storeItemStack.isEmpty() ? inventory.getStack(OUTPUT_SLOT) : storeItemStack; 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 // Returns the ItemStack to be displayed to the player via UI / model
public ItemStack getDisplayedStack() { public ItemStack getDisplayedStack() {
if (!isLocked()) { if (!isLocked()) {
@ -192,10 +184,6 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity
return returnStack; return returnStack;
} }
public void setStoredStack(ItemStack itemStack) {
storeItemStack = itemStack;
}
public ItemStack processInput(ItemStack inputStack) { public ItemStack processInput(ItemStack inputStack) {
boolean isSameStack = isSameType(inputStack); boolean isSameStack = isSameType(inputStack);
@ -447,11 +435,25 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity
return !isEmpty(); return !isEmpty();
} }
public int getStoredAmount() {
return this.getCurrentCapacity();
}
public void setStoredAmount(int storedAmount) {
this.storedAmount = storedAmount;
}
@Override @Override
public BuiltScreenHandler createScreenHandler(int syncID, final PlayerEntity playerEntity) { public BuiltScreenHandler createScreenHandler(int syncID, final PlayerEntity playerEntity) {
return new ScreenHandlerBuilder("chest").player(playerEntity.inventory).inventory().hotbar().addInventory() return new ScreenHandlerBuilder("chest").player(playerEntity.inventory).inventory().hotbar().addInventory()
.blockEntity(this).slot(0, 100, 53).outputSlot(1, 140, 53) .blockEntity(this).slot(0, 100, 53)
.sync(this::isLockedInt, this::setLockedInt).addInventory().create(this, syncID); .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 @Override

View file

@ -62,16 +62,16 @@ public class GuiStorageUnit extends GuiBase<BuiltScreenHandler> {
builder.drawText(matrixStack, this, new TranslatableText("gui.techreborn.unit.in"), 100, 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); 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); textRenderer.draw(matrixStack, new TranslatableText("techreborn.tooltip.unit.empty"), 10, 20, 4210752);
} else { } else {
textRenderer.draw(matrixStack, new TranslatableText("gui.techreborn.storage.store"), 10, 20, 4210752); textRenderer.draw(matrixStack, new TranslatableText("gui.techreborn.storage.store"), 10, 20, 4210752);
textRenderer.draw(matrixStack, storageEntity.getDisplayedStack().getName(), 10, 30, 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, 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); textRenderer.draw(matrixStack, new TranslatableText("gui.techreborn.unit.used").append(percentFilled + "%"), 10, 70, 4210752);