Skip to content

Commit 85a5c84

Browse files
committed
simplify client syncing
1 parent 6ea28ca commit 85a5c84

File tree

2 files changed

+10
-66
lines changed

2 files changed

+10
-66
lines changed

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

Lines changed: 3 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,8 @@ private boolean simulateExtractItem(ItemStack itemStack) {
210210
public void performRecipe() {
211211
if (!isRecipeValid()) return;
212212

213-
if (!getSyncManager().isClient())
214-
syncToClient(1, this::writeAvailableStacks);
213+
// if (!getSyncManager().isClient())
214+
// syncToClient(1, this::writeAvailableStacks);
215215

216216
if (!attemptMatchRecipe() || !consumeRecipeItems(false)) {
217217
return;
@@ -234,12 +234,6 @@ public void performRecipe() {
234234
continue;
235235
}
236236

237-
// ItemStack current = craftingMatrix.getStackInSlot(i);
238-
// craftingMatrix.setInventorySlotContents(i, itemStack);
239-
// if (!cachedRecipe.matches(craftingMatrix, this.world)) {
240-
// craftingMatrix.setInventorySlotContents(i, current);
241-
// }
242-
243237
int remainingAmount = GTTransferUtils.insertItem(this.availableHandlers, itemStack, true).getCount();
244238
if (remainingAmount > 0) {
245239
itemStack.setCount(remainingAmount);
@@ -334,23 +328,6 @@ public CachedRecipeData getCachedRecipeData() {
334328
return this.cachedRecipeData;
335329
}
336330

337-
public void writeAvailableStacks(PacketBuffer buffer) {
338-
this.collectAvailableItems();
339-
Map<Integer, ItemStack> written = new Int2ObjectArrayMap<>();
340-
for (var slots : this.stackLookupMap.entrySet()) {
341-
for (var slot : slots.getValue()) {
342-
var stack = this.availableHandlers.getStackInSlot(slot);
343-
written.put(slot, stack);
344-
}
345-
}
346-
347-
buffer.writeInt(written.size());
348-
for (var entry : written.entrySet()) {
349-
buffer.writeInt(entry.getKey());
350-
writeStackSafe(buffer, entry.getValue());
351-
}
352-
}
353-
354331
public void collectAvailableItems() {
355332
this.stackLookupMap.clear();
356333
for (int i = 0; i < this.availableHandlers.getSlots(); i++) {
@@ -364,9 +341,7 @@ public void collectAvailableItems() {
364341

365342
@Override
366343
public void readOnClient(int id, PacketBuffer buf) {
367-
if (id == 1) {
368-
// updateClientStacks(buf);
369-
} else if (id == 3) {
344+
if (id == 3) {
370345
syncToServer(3);
371346
} else if (id == 4) {
372347
getSyncManager().setCursorItem(readStackSafe(buf));
@@ -386,10 +361,6 @@ public void readOnServer(int id, PacketBuffer buf) {
386361
this.craftingMatrix.setInventorySlotContents(i, buf.readItemStack());
387362
} catch (IOException ignore) {}
388363
}
389-
} else if (id == 1) {
390-
syncToClient(1, this::writeAvailableStacks);
391-
} else if (id == 3) {
392-
// syncToClient(1, this::writeAvailableStacks);
393364
} else if (id == 4) {
394365
int slot = buf.readVarInt();
395366
syncToClient(5, buffer -> {
@@ -399,25 +370,6 @@ public void readOnServer(int id, PacketBuffer buf) {
399370
}
400371
}
401372

402-
public void updateClientStacks(PacketBuffer buffer) {
403-
this.stackLookupMap.clear();
404-
int size = buffer.readInt();
405-
for (int i = 0; i < size; i++) {
406-
int slot = buffer.readInt();
407-
var serverStack = readStackSafe(buffer);
408-
var clientStack = this.availableHandlers.extractItem(slot, Integer.MAX_VALUE, true);
409-
410-
if (clientStack.isEmpty() || !ItemStack.areItemStacksEqual(clientStack, serverStack)) {
411-
this.availableHandlers.extractItem(slot, Integer.MAX_VALUE, false);
412-
this.availableHandlers.insertItem(slot, serverStack.copy(), false);
413-
}
414-
415-
this.stackLookupMap
416-
.computeIfAbsent(serverStack, k -> new IntArrayList())
417-
.add(slot);
418-
}
419-
}
420-
421373
private static ItemStack readStackSafe(PacketBuffer buffer) {
422374
var stack = ItemStack.EMPTY;
423375
try {
@@ -431,16 +383,6 @@ private static ItemStack readStackSafe(PacketBuffer buffer) {
431383
return stack;
432384
}
433385

434-
// public void updateClientCraft() {
435-
// var curStack = getSyncManager().getCursorItem();
436-
// var outStack = getCachedRecipe().getRecipeOutput();
437-
// if (curStack.isEmpty()) {
438-
// getSyncManager().setCursorItem(outStack);
439-
// } else if (ItemStack.areItemStacksEqual(curStack, outStack)) {
440-
// curStack.grow(outStack.getCount());
441-
// }
442-
// }
443-
444386
private static void writeStackSafe(PacketBuffer buffer, ItemStack stack) {
445387
var tag = stack.serializeNBT();
446388
// GTLog.logger.warn(String.format("Sent: %s", tag));

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@
7979
public class MetaTileEntityWorkbench extends MetaTileEntity {
8080

8181
// todo move these to GregtechDataCodes
82-
public static final int UPDATE_CLIENT_STACKS = GregtechDataCodes.assignId();
8382
public static final int UPDATE_CLIENT_HANDLER = GregtechDataCodes.assignId();
8483

8584
private static final IDrawable CHEST = new ItemDrawable(new ItemStack(Blocks.CHEST))
@@ -250,6 +249,7 @@ public ModularPanel buildUI(PosGuiData guiData, GuiSyncManager guiSyncManager) {
250249

251250
return GTGuis.createPanel(this, 176, 224)
252251
.child(new Row()
252+
.debugName("tab row")
253253
.widthRel(1f)
254254
.leftRel(0.5f)
255255
.margin(3, 0)
@@ -271,14 +271,15 @@ public ModularPanel buildUI(PosGuiData guiData, GuiSyncManager guiSyncManager) {
271271
.controller(controller)
272272
// workstation page
273273
.addPage(new Column()
274-
.debugName("crafting section")
274+
.debugName("crafting page")
275275
.coverChildrenWidth()
276276
.child(new Row()
277277
.debugName("crafting row")
278278
.coverChildrenHeight()
279279
.widthRel(1f)
280280
// crafting grid
281281
.child(createCraftingGrid())
282+
// crafting output slot
282283
.child(createCraftingOutput(guiData, guiSyncManager))
283284
// recipe memory
284285
.child(createRecipeMemoryGrid(guiSyncManager)))
@@ -338,12 +339,11 @@ public IWidget createCraftingOutput(PosGuiData guiData, GuiSyncManager syncManag
338339

339340
return new Column()
340341
.size(54)
341-
// crafting output slot
342342
.child(new ItemSlot().marginTop(18)
343343
// todo figure this shit (recipe output slot) out
344344
.slot(new CraftingOutputSlot(new InventoryWrapper(
345345
this.recipeLogic.getCraftingResultInventory(),
346-
guiData.getPlayer()), amountCrafted))
346+
guiData.getPlayer()), amountCrafted, getCraftingRecipeLogic()))
347347
.background(GTGuiTextures.SLOT.asIcon().size(22))
348348
.marginBottom(4))
349349
.child(IKey.dynamic(amountCrafted::getStringValue)
@@ -514,10 +514,12 @@ public Result onMousePressed(int mouseButton) {
514514
private class CraftingOutputSlot extends ModularSlot {
515515

516516
IntSyncValue syncValue;
517+
private final CraftingRecipeLogic recipeLogic;
517518

518-
public CraftingOutputSlot(IItemHandler itemHandler, IntSyncValue syncValue) {
519+
public CraftingOutputSlot(IItemHandler itemHandler, IntSyncValue syncValue, CraftingRecipeLogic recipeLogic) {
519520
super(itemHandler, 0, false);
520521
this.syncValue = syncValue;
522+
this.recipeLogic = recipeLogic;
521523
}
522524

523525
@Override

0 commit comments

Comments
 (0)