From 85a5c846beab476656cf51109e66d962e7e4f87d Mon Sep 17 00:00:00 2001 From: Ghzdude <44148655+ghzdude@users.noreply.github.com> Date: Sat, 20 Apr 2024 13:49:29 -0700 Subject: [PATCH] simplify client syncing --- .../storage/CraftingRecipeLogic.java | 64 +------------------ .../storage/MetaTileEntityWorkbench.java | 12 ++-- 2 files changed, 10 insertions(+), 66 deletions(-) diff --git a/src/main/java/gregtech/common/metatileentities/storage/CraftingRecipeLogic.java b/src/main/java/gregtech/common/metatileentities/storage/CraftingRecipeLogic.java index 30328fc73ce..fef6e098a95 100644 --- a/src/main/java/gregtech/common/metatileentities/storage/CraftingRecipeLogic.java +++ b/src/main/java/gregtech/common/metatileentities/storage/CraftingRecipeLogic.java @@ -210,8 +210,8 @@ private boolean simulateExtractItem(ItemStack itemStack) { public void performRecipe() { if (!isRecipeValid()) return; - if (!getSyncManager().isClient()) - syncToClient(1, this::writeAvailableStacks); + // if (!getSyncManager().isClient()) + // syncToClient(1, this::writeAvailableStacks); if (!attemptMatchRecipe() || !consumeRecipeItems(false)) { return; @@ -234,12 +234,6 @@ public void performRecipe() { continue; } - // ItemStack current = craftingMatrix.getStackInSlot(i); - // craftingMatrix.setInventorySlotContents(i, itemStack); - // if (!cachedRecipe.matches(craftingMatrix, this.world)) { - // craftingMatrix.setInventorySlotContents(i, current); - // } - int remainingAmount = GTTransferUtils.insertItem(this.availableHandlers, itemStack, true).getCount(); if (remainingAmount > 0) { itemStack.setCount(remainingAmount); @@ -334,23 +328,6 @@ public CachedRecipeData getCachedRecipeData() { return this.cachedRecipeData; } - public void writeAvailableStacks(PacketBuffer buffer) { - this.collectAvailableItems(); - Map written = new Int2ObjectArrayMap<>(); - for (var slots : this.stackLookupMap.entrySet()) { - for (var slot : slots.getValue()) { - var stack = this.availableHandlers.getStackInSlot(slot); - written.put(slot, stack); - } - } - - buffer.writeInt(written.size()); - for (var entry : written.entrySet()) { - buffer.writeInt(entry.getKey()); - writeStackSafe(buffer, entry.getValue()); - } - } - public void collectAvailableItems() { this.stackLookupMap.clear(); for (int i = 0; i < this.availableHandlers.getSlots(); i++) { @@ -364,9 +341,7 @@ public void collectAvailableItems() { @Override public void readOnClient(int id, PacketBuffer buf) { - if (id == 1) { - // updateClientStacks(buf); - } else if (id == 3) { + if (id == 3) { syncToServer(3); } else if (id == 4) { getSyncManager().setCursorItem(readStackSafe(buf)); @@ -386,10 +361,6 @@ public void readOnServer(int id, PacketBuffer buf) { this.craftingMatrix.setInventorySlotContents(i, buf.readItemStack()); } catch (IOException ignore) {} } - } else if (id == 1) { - syncToClient(1, this::writeAvailableStacks); - } else if (id == 3) { - // syncToClient(1, this::writeAvailableStacks); } else if (id == 4) { int slot = buf.readVarInt(); syncToClient(5, buffer -> { @@ -399,25 +370,6 @@ public void readOnServer(int id, PacketBuffer buf) { } } - public void updateClientStacks(PacketBuffer buffer) { - this.stackLookupMap.clear(); - int size = buffer.readInt(); - for (int i = 0; i < size; i++) { - int slot = buffer.readInt(); - var serverStack = readStackSafe(buffer); - var clientStack = this.availableHandlers.extractItem(slot, Integer.MAX_VALUE, true); - - if (clientStack.isEmpty() || !ItemStack.areItemStacksEqual(clientStack, serverStack)) { - this.availableHandlers.extractItem(slot, Integer.MAX_VALUE, false); - this.availableHandlers.insertItem(slot, serverStack.copy(), false); - } - - this.stackLookupMap - .computeIfAbsent(serverStack, k -> new IntArrayList()) - .add(slot); - } - } - private static ItemStack readStackSafe(PacketBuffer buffer) { var stack = ItemStack.EMPTY; try { @@ -431,16 +383,6 @@ private static ItemStack readStackSafe(PacketBuffer buffer) { return stack; } - // public void updateClientCraft() { - // var curStack = getSyncManager().getCursorItem(); - // var outStack = getCachedRecipe().getRecipeOutput(); - // if (curStack.isEmpty()) { - // getSyncManager().setCursorItem(outStack); - // } else if (ItemStack.areItemStacksEqual(curStack, outStack)) { - // curStack.grow(outStack.getCount()); - // } - // } - private static void writeStackSafe(PacketBuffer buffer, ItemStack stack) { var tag = stack.serializeNBT(); // GTLog.logger.warn(String.format("Sent: %s", tag)); diff --git a/src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityWorkbench.java b/src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityWorkbench.java index ce6143ff106..2916e8dce7c 100644 --- a/src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityWorkbench.java +++ b/src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityWorkbench.java @@ -79,7 +79,6 @@ public class MetaTileEntityWorkbench extends MetaTileEntity { // todo move these to GregtechDataCodes - public static final int UPDATE_CLIENT_STACKS = GregtechDataCodes.assignId(); public static final int UPDATE_CLIENT_HANDLER = GregtechDataCodes.assignId(); private static final IDrawable CHEST = new ItemDrawable(new ItemStack(Blocks.CHEST)) @@ -250,6 +249,7 @@ public ModularPanel buildUI(PosGuiData guiData, GuiSyncManager guiSyncManager) { return GTGuis.createPanel(this, 176, 224) .child(new Row() + .debugName("tab row") .widthRel(1f) .leftRel(0.5f) .margin(3, 0) @@ -271,7 +271,7 @@ public ModularPanel buildUI(PosGuiData guiData, GuiSyncManager guiSyncManager) { .controller(controller) // workstation page .addPage(new Column() - .debugName("crafting section") + .debugName("crafting page") .coverChildrenWidth() .child(new Row() .debugName("crafting row") @@ -279,6 +279,7 @@ public ModularPanel buildUI(PosGuiData guiData, GuiSyncManager guiSyncManager) { .widthRel(1f) // crafting grid .child(createCraftingGrid()) + // crafting output slot .child(createCraftingOutput(guiData, guiSyncManager)) // recipe memory .child(createRecipeMemoryGrid(guiSyncManager))) @@ -338,12 +339,11 @@ public IWidget createCraftingOutput(PosGuiData guiData, GuiSyncManager syncManag return new Column() .size(54) - // crafting output slot .child(new ItemSlot().marginTop(18) // todo figure this shit (recipe output slot) out .slot(new CraftingOutputSlot(new InventoryWrapper( this.recipeLogic.getCraftingResultInventory(), - guiData.getPlayer()), amountCrafted)) + guiData.getPlayer()), amountCrafted, getCraftingRecipeLogic())) .background(GTGuiTextures.SLOT.asIcon().size(22)) .marginBottom(4)) .child(IKey.dynamic(amountCrafted::getStringValue) @@ -514,10 +514,12 @@ public Result onMousePressed(int mouseButton) { private class CraftingOutputSlot extends ModularSlot { IntSyncValue syncValue; + private final CraftingRecipeLogic recipeLogic; - public CraftingOutputSlot(IItemHandler itemHandler, IntSyncValue syncValue) { + public CraftingOutputSlot(IItemHandler itemHandler, IntSyncValue syncValue, CraftingRecipeLogic recipeLogic) { super(itemHandler, 0, false); this.syncValue = syncValue; + this.recipeLogic = recipeLogic; } @Override