From b1a975233a6a98e1fd8e21162134e594e2c83af9 Mon Sep 17 00:00:00 2001 From: Justin Vitale <4002351+justinvvitale@users.noreply.github.com> Date: Thu, 18 Feb 2021 01:14:27 +1100 Subject: [PATCH] fix #2296 - Sync server config for fluid/item storage for clients --- .../fluid/TankUnitBaseBlockEntity.java | 22 ++++++++++++++++-- .../item/StorageUnitBaseBlockEntity.java | 23 +++++++++++++++---- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/main/java/techreborn/blockentity/storage/fluid/TankUnitBaseBlockEntity.java b/src/main/java/techreborn/blockentity/storage/fluid/TankUnitBaseBlockEntity.java index 5cb5bfd2b..f597381a3 100644 --- a/src/main/java/techreborn/blockentity/storage/fluid/TankUnitBaseBlockEntity.java +++ b/src/main/java/techreborn/blockentity/storage/fluid/TankUnitBaseBlockEntity.java @@ -43,6 +43,7 @@ import reborncore.client.screen.builder.ScreenHandlerBuilder; import reborncore.common.blockentity.MachineBaseBlockEntity; import reborncore.common.fluid.FluidUtil; import reborncore.common.fluid.FluidValue; +import reborncore.common.fluid.container.FluidInstance; import reborncore.common.util.RebornInventory; import reborncore.common.util.Tank; import techreborn.init.TRBlockEntities; @@ -53,6 +54,8 @@ import java.util.List; public class TankUnitBaseBlockEntity extends MachineBaseBlockEntity implements InventoryProvider, IToolDrop, IListInfoProvider, BuiltScreenHandlerProvider { protected Tank tank; + private int serverMaxCapacity = -1; + protected RebornInventory inventory = new RebornInventory<>(2, "TankInventory", 64, this); private TRContent.TankUnit type; @@ -68,7 +71,7 @@ public class TankUnitBaseBlockEntity extends MachineBaseBlockEntity implements I private void configureEntity(TRContent.TankUnit type) { this.type = type; - this.tank = new Tank("TankStorage", type.capacity, this); + this.tank = new Tank("TankStorage", serverMaxCapacity == -1 ? type.capacity : FluidValue.fromRaw(serverMaxCapacity), this); } public ItemStack getDropWithNBT() { @@ -174,7 +177,22 @@ public class TankUnitBaseBlockEntity extends MachineBaseBlockEntity implements I public BuiltScreenHandler createScreenHandler(int syncID, final PlayerEntity player) { return new ScreenHandlerBuilder("tank").player(player.inventory).inventory().hotbar() .addInventory().blockEntity(this).fluidSlot(0, 100, 53).outputSlot(1, 140, 53) - .sync(tank).addInventory().create(this, syncID); + .sync(tank) + .sync(this::getMaxCapacity, this::setMaxCapacity) + + .addInventory().create(this, syncID); + } + + // Sync between server/client if configs are mis-matched. + public int getMaxCapacity() { + return this.tank.getCapacity().getRawValue(); + } + + public void setMaxCapacity(int maxCapacity) { + FluidInstance instance = tank.getFluidInstance(); + this.tank = new Tank("TankStorage", FluidValue.fromRaw(maxCapacity), this); + this.tank.setFluidInstance(instance); + this.serverMaxCapacity = maxCapacity; } @Nullable diff --git a/src/main/java/techreborn/blockentity/storage/item/StorageUnitBaseBlockEntity.java b/src/main/java/techreborn/blockentity/storage/item/StorageUnitBaseBlockEntity.java index ef35b554a..3989081b3 100644 --- a/src/main/java/techreborn/blockentity/storage/item/StorageUnitBaseBlockEntity.java +++ b/src/main/java/techreborn/blockentity/storage/item/StorageUnitBaseBlockEntity.java @@ -63,6 +63,7 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity implement protected RebornInventory inventory; private int maxCapacity; + private int serverCapacity = -1; private ItemStack storeItemStack; @@ -82,7 +83,12 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity implement } private void configureEntity(TRContent.StorageUnit type) { - this.maxCapacity = type.capacity; + + // Set capacity to local config unless overridden by server + if(serverCapacity == -1){ + this.maxCapacity = type.capacity; + } + storeItemStack = ItemStack.EMPTY; inventory = new RebornInventory<>(2, "ItemInventory", 64, this); @@ -236,10 +242,6 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity implement return storeItemStack.getCount() + inventory.getStack(OUTPUT_SLOT).getCount(); } - public int getMaxCapacity() { - return maxCapacity; - } - // MachineBaseBlockEntity @Override public void tick() { @@ -457,6 +459,7 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity implement .sync(this::isLockedInt, this::setLockedInt) .sync(this::getStoredStackNBT, this::setStoredStackFromNBT) .sync(this::getStoredAmount, this::setStoredAmount) + .sync(this::getMaxCapacity, this::setMaxCapacity) .addInventory().create(this, syncID); // Note that inventory is synced, and it gets the stack from that @@ -479,6 +482,16 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity implement this.storedAmount = storedAmount; } + // Sync between server/client if configs are mis-matched. + public int getMaxCapacity() { + return this.maxCapacity; + } + + public void setMaxCapacity(int maxCapacity) { + this.maxCapacity = maxCapacity; + this.serverCapacity = maxCapacity; + } + public CompoundTag getStoredStackNBT() { CompoundTag tag = new CompoundTag(); getStoredStack().toTag(tag);