diff --git a/src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityWorkbench.java b/src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityWorkbench.java index 6da4abc4031..45dcae211fe 100644 --- a/src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityWorkbench.java +++ b/src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityWorkbench.java @@ -124,6 +124,7 @@ public void writeInitialSyncData(@NotNull PacketBuffer buf) { NetworkUtils.writeItemStack(buf, craftingGrid.getStackInSlot(i)); } this.recipeMemory.writeInitialSyncData(buf); + buf.writeVarInt(computeConnectedInventory().getSlots()); } @Override @@ -134,6 +135,8 @@ public void receiveInitialSyncData(@NotNull PacketBuffer buf) { craftingGrid.setStackInSlot(i, NetworkUtils.readItemStack(buf)); } this.recipeMemory.receiveInitialSyncData(buf); + this.connectedInventory = new ItemHandlerList( + Collections.singletonList(new GTItemStackHandler(this, buf.readVarInt()))); } @Override @@ -158,28 +161,39 @@ public void readFromNBT(NBTTagCompound data) { } public IItemHandlerModifiable getAvailableHandlers() { - var handlers = new ArrayList(); + ArrayList handlers = new ArrayList<>(); + handlers.add(this.internalInventory); + handlers.add(this.toolInventory); + if (getWorld().isRemote) { + // this might be called on client, so just return the existing inventory instead + handlers.add(this.connectedInventory); + } else { + handlers.add(computeConnectedInventory()); + } + return this.combinedInventory = new ItemHandlerList(handlers); + } + + // this should only be called server-side + private ItemHandlerList computeConnectedInventory() { + ArrayList handlers = new ArrayList<>(); for (var facing : EnumFacing.VALUES) { var neighbor = getNeighbor(facing); if (neighbor == null) continue; var handler = neighbor.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, facing.getOpposite()); if (handler != null) handlers.add(handler); } - this.connectedInventory = new ItemHandlerList(handlers); - handlers.clear(); - - handlers.add(this.internalInventory); - handlers.add(this.toolInventory); - handlers.add(this.connectedInventory); - return this.combinedInventory = new ItemHandlerList(handlers); + return this.connectedInventory = new ItemHandlerList(handlers); } @Override public void onNeighborChanged() { getCraftingRecipeLogic().updateInventory(getAvailableHandlers()); - writeCustomData(GregtechDataCodes.UPDATE_CLIENT_HANDLER, this::sendHandlerToClient); + if (!getWorld().isRemote) { + writeCustomData(GregtechDataCodes.UPDATE_CLIENT_HANDLER, this::sendHandlerToClient); + } } + // this is called on client and server public @NotNull CraftingRecipeLogic getCraftingRecipeLogic() { Preconditions.checkState(getWorld() != null, "getRecipeResolver called too early"); if (this.recipeLogic == null) {