diff --git a/src/main/java/gregtech/common/metatileentities/storage/CraftingRecipeLogic.java b/src/main/java/gregtech/common/metatileentities/storage/CraftingRecipeLogic.java index 486bcf6296a..30328fc73ce 100644 --- a/src/main/java/gregtech/common/metatileentities/storage/CraftingRecipeLogic.java +++ b/src/main/java/gregtech/common/metatileentities/storage/CraftingRecipeLogic.java @@ -217,6 +217,9 @@ public void performRecipe() { return; } + // updateClientCraft(); + syncToClient(4, buffer -> writeStackSafe(buffer, getSyncManager().getCursorItem())); + var cachedRecipe = cachedRecipeData.getRecipe(); var player = getSyncManager().getPlayer(); ForgeHooks.setCraftingPlayer(player); @@ -362,7 +365,7 @@ public void collectAvailableItems() { @Override public void readOnClient(int id, PacketBuffer buf) { if (id == 1) { - updateClientStacks(buf); + // updateClientStacks(buf); } else if (id == 3) { syncToServer(3); } else if (id == 4) { @@ -386,13 +389,7 @@ public void readOnServer(int id, PacketBuffer buf) { } else if (id == 1) { syncToClient(1, this::writeAvailableStacks); } else if (id == 3) { -// syncToClient(1, this::writeAvailableStacks); - var curStack = getSyncManager().getCursorItem(); - var outStack = getCachedRecipe().getRecipeOutput(); - if (ItemStack.areItemStacksEqual(curStack, outStack)) { - curStack.grow(outStack.getCount()); - syncToClient(4, buffer -> writeStackSafe(buffer, curStack)); - } + // syncToClient(1, this::writeAvailableStacks); } else if (id == 4) { int slot = buf.readVarInt(); syncToClient(5, buffer -> { @@ -434,6 +431,16 @@ 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 ab35cba5469..ce6143ff106 100644 --- a/src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityWorkbench.java +++ b/src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityWorkbench.java @@ -1,9 +1,5 @@ package gregtech.common.metatileentities.storage; -import com.cleanroommc.modularui.api.drawable.IDrawable; - -import com.cleanroommc.modularui.widget.ParentWidget; - import gregtech.api.capability.GregtechDataCodes; import gregtech.api.capability.impl.ItemHandlerList; import gregtech.api.items.itemhandlers.GTItemStackHandler; @@ -39,6 +35,7 @@ import codechicken.lib.render.pipeline.ColourMultiplier; import codechicken.lib.render.pipeline.IVertexOperation; import codechicken.lib.vec.Matrix4; +import com.cleanroommc.modularui.api.drawable.IDrawable; import com.cleanroommc.modularui.api.drawable.IKey; import com.cleanroommc.modularui.api.widget.IWidget; import com.cleanroommc.modularui.api.widget.Interactable; @@ -54,6 +51,7 @@ import com.cleanroommc.modularui.value.sync.GuiSyncManager; import com.cleanroommc.modularui.value.sync.IntSyncValue; import com.cleanroommc.modularui.value.sync.SyncHandlers; +import com.cleanroommc.modularui.widget.ParentWidget; import com.cleanroommc.modularui.widget.Widget; import com.cleanroommc.modularui.widget.scroll.VerticalScrollData; import com.cleanroommc.modularui.widgets.ItemSlot; @@ -242,9 +240,9 @@ public boolean usesMui2() { @Override public ModularPanel buildUI(PosGuiData guiData, GuiSyncManager guiSyncManager) { getCraftingRecipeLogic().updateCurrentRecipe(); - if (!guiSyncManager.isClient()) { - writeCustomData(UPDATE_CLIENT_STACKS, getCraftingRecipeLogic()::writeAvailableStacks); - } + // if (!guiSyncManager.isClient()) { + // writeCustomData(UPDATE_CLIENT_STACKS, getCraftingRecipeLogic()::writeAvailableStacks); + // } guiSyncManager.syncValue("recipe_logic", this.recipeLogic); @@ -270,7 +268,6 @@ public ModularPanel buildUI(PosGuiData guiData, GuiSyncManager guiSyncManager) { .top(22) .margin(7) .widthRel(0.9f) -// .bottom(100) .controller(controller) // workstation page .addPage(new Column() @@ -284,13 +281,7 @@ public ModularPanel buildUI(PosGuiData guiData, GuiSyncManager guiSyncManager) { .child(createCraftingGrid()) .child(createCraftingOutput(guiData, guiSyncManager)) // recipe memory - .child(SlotGroupWidget.builder() - .matrix("XXX", - "XXX", - "XXX") - .key('X', i -> new RecipeMemorySlot(this.recipeMemory, i)) - .build().right(0)) - ) + .child(createRecipeMemoryGrid(guiSyncManager))) // tool inventory .child(createToolInventory(guiSyncManager)) // internal inventory @@ -360,6 +351,15 @@ public IWidget createCraftingOutput(PosGuiData guiData, GuiSyncManager syncManag .asWidget().widthRel(1f)); } + public IWidget createRecipeMemoryGrid(GuiSyncManager syncManager) { + return SlotGroupWidget.builder() + .matrix("XXX", + "XXX", + "XXX") + .key('X', i -> new RecipeMemorySlot(this.recipeMemory, i)) + .build().right(0); + } + public IWidget createInventoryPage(GuiSyncManager syncManager) { var connected = new SlotGroup("connected_inventory", 8, true); syncManager.registerSlotGroup(connected); @@ -399,35 +399,26 @@ public IWidget createInventoryPage(GuiSyncManager syncManager) { } public void sendHandlerToClient(PacketBuffer buffer) { - int combined = this.combinedInventory.getSlots(), - connected = this.connectedInventory.getSlots(); - - buffer.writeVarInt(connected); - buffer.writeVarInt(combined - connected); - getCraftingRecipeLogic().writeAvailableStacks(buffer); + buffer.writeVarInt(this.connectedInventory.getSlots()); } @Override public void receiveCustomData(int dataId, @NotNull PacketBuffer buf) { super.receiveCustomData(dataId, buf); - if (dataId == UPDATE_CLIENT_STACKS) { - getCraftingRecipeLogic() - .updateClientStacks(buf); + if (dataId == UPDATE_CLIENT_HANDLER) { + int connected = buf.readVarInt(); - } else if (dataId == UPDATE_CLIENT_HANDLER) { - int connected = buf.readVarInt(), internal = buf.readVarInt(); + // check if sizes have changed, and keep any existing items // set connected inventory this.connectedInventory = new ItemStackHandler(connected); // set combined inventory - this.combinedInventory = new ItemHandlerList(Arrays.asList(this.connectedInventory, new ItemStackHandler(internal))); + this.combinedInventory = new ItemHandlerList( + Arrays.asList(this.connectedInventory, this.internalInventory)); getCraftingRecipeLogic() .updateInventory(this.combinedInventory); - - getCraftingRecipeLogic() - .updateClientStacks(buf); } } @@ -532,10 +523,14 @@ public CraftingOutputSlot(IItemHandler itemHandler, IntSyncValue syncValue) { @Override public boolean canTakeStack(EntityPlayer playerIn) { if (recipeLogic.getSyncManager().isClient()) { - recipeLogic.syncToServer(3); + // recipeLogic.syncToServer(3); return false; } - return recipeLogic.isRecipeValid() && recipeLogic.consumeRecipeItems(true); + + if (recipeLogic.isRecipeValid()) + recipeLogic.collectAvailableItems(); + + return recipeLogic.attemptMatchRecipe(); } @Override @@ -547,16 +542,16 @@ public ItemStack onTake(EntityPlayer thePlayer, ItemStack stack) { @Override public void putStack(@NotNull ItemStack stack) { - super.putStack(recipeLogic.getCachedRecipeData().getRecipeOutput()); + super.putStack(getStack()); } @Override - public ItemStack decrStackSize(int amount) { + public @NotNull ItemStack decrStackSize(int amount) { return getStack(); } public void handleItemCraft(ItemStack itemStack, EntityPlayer player) { - itemStack.onCrafting(getWorld(), player, 1); + itemStack.onCrafting(player.world, player, 1); var inventoryCrafting = recipeLogic.getCraftingMatrix(); @@ -569,10 +564,11 @@ public void handleItemCraft(ItemStack itemStack, EntityPlayer player) { } if (cachedRecipe != null) { ItemStack resultStack = cachedRecipe.getCraftingResult(inventoryCrafting); - this.syncValue.setValue(this.syncValue.getValue() + resultStack.getCount(), true, false); + this.syncValue.setValue(this.syncValue.getValue() + resultStack.getCount(), true, true); // itemsCrafted += resultStack.getCount(); recipeMemory.notifyRecipePerformed(craftingGrid, resultStack); } + // call method from recipe logic to sync to client } }