Skip to content

Commit 5ac270b

Browse files
authored
Fix Crafting Station Connected Inventory Desync (#2789)
1 parent b24ff3e commit 5ac270b

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityWorkbench.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ public void writeInitialSyncData(@NotNull PacketBuffer buf) {
124124
NetworkUtils.writeItemStack(buf, craftingGrid.getStackInSlot(i));
125125
}
126126
this.recipeMemory.writeInitialSyncData(buf);
127+
buf.writeVarInt(computeConnectedInventory().getSlots());
127128
}
128129

129130
@Override
@@ -134,6 +135,8 @@ public void receiveInitialSyncData(@NotNull PacketBuffer buf) {
134135
craftingGrid.setStackInSlot(i, NetworkUtils.readItemStack(buf));
135136
}
136137
this.recipeMemory.receiveInitialSyncData(buf);
138+
this.connectedInventory = new ItemHandlerList(
139+
Collections.singletonList(new GTItemStackHandler(this, buf.readVarInt())));
137140
}
138141

139142
@Override
@@ -158,28 +161,39 @@ public void readFromNBT(NBTTagCompound data) {
158161
}
159162

160163
public IItemHandlerModifiable getAvailableHandlers() {
161-
var handlers = new ArrayList<IItemHandler>();
164+
ArrayList<IItemHandler> handlers = new ArrayList<>();
165+
handlers.add(this.internalInventory);
166+
handlers.add(this.toolInventory);
167+
if (getWorld().isRemote) {
168+
// this might be called on client, so just return the existing inventory instead
169+
handlers.add(this.connectedInventory);
170+
} else {
171+
handlers.add(computeConnectedInventory());
172+
}
173+
return this.combinedInventory = new ItemHandlerList(handlers);
174+
}
175+
176+
// this should only be called server-side
177+
private ItemHandlerList computeConnectedInventory() {
178+
ArrayList<IItemHandler> handlers = new ArrayList<>();
162179
for (var facing : EnumFacing.VALUES) {
163180
var neighbor = getNeighbor(facing);
164181
if (neighbor == null) continue;
165182
var handler = neighbor.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, facing.getOpposite());
166183
if (handler != null) handlers.add(handler);
167184
}
168-
this.connectedInventory = new ItemHandlerList(handlers);
169-
handlers.clear();
170-
171-
handlers.add(this.internalInventory);
172-
handlers.add(this.toolInventory);
173-
handlers.add(this.connectedInventory);
174-
return this.combinedInventory = new ItemHandlerList(handlers);
185+
return this.connectedInventory = new ItemHandlerList(handlers);
175186
}
176187

177188
@Override
178189
public void onNeighborChanged() {
179190
getCraftingRecipeLogic().updateInventory(getAvailableHandlers());
180-
writeCustomData(GregtechDataCodes.UPDATE_CLIENT_HANDLER, this::sendHandlerToClient);
191+
if (!getWorld().isRemote) {
192+
writeCustomData(GregtechDataCodes.UPDATE_CLIENT_HANDLER, this::sendHandlerToClient);
193+
}
181194
}
182195

196+
// this is called on client and server
183197
public @NotNull CraftingRecipeLogic getCraftingRecipeLogic() {
184198
Preconditions.checkState(getWorld() != null, "getRecipeResolver called too early");
185199
if (this.recipeLogic == null) {

0 commit comments

Comments
 (0)