Skip to content

Commit 6ea28ca

Browse files
committed
more work on syncing and recipe logic part 2 + spotless
1 parent d118fc9 commit 6ea28ca

File tree

2 files changed

+47
-44
lines changed

2 files changed

+47
-44
lines changed

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ public void performRecipe() {
217217
return;
218218
}
219219

220+
// updateClientCraft();
221+
syncToClient(4, buffer -> writeStackSafe(buffer, getSyncManager().getCursorItem()));
222+
220223
var cachedRecipe = cachedRecipeData.getRecipe();
221224
var player = getSyncManager().getPlayer();
222225
ForgeHooks.setCraftingPlayer(player);
@@ -362,7 +365,7 @@ public void collectAvailableItems() {
362365
@Override
363366
public void readOnClient(int id, PacketBuffer buf) {
364367
if (id == 1) {
365-
updateClientStacks(buf);
368+
// updateClientStacks(buf);
366369
} else if (id == 3) {
367370
syncToServer(3);
368371
} else if (id == 4) {
@@ -386,13 +389,7 @@ public void readOnServer(int id, PacketBuffer buf) {
386389
} else if (id == 1) {
387390
syncToClient(1, this::writeAvailableStacks);
388391
} else if (id == 3) {
389-
// syncToClient(1, this::writeAvailableStacks);
390-
var curStack = getSyncManager().getCursorItem();
391-
var outStack = getCachedRecipe().getRecipeOutput();
392-
if (ItemStack.areItemStacksEqual(curStack, outStack)) {
393-
curStack.grow(outStack.getCount());
394-
syncToClient(4, buffer -> writeStackSafe(buffer, curStack));
395-
}
392+
// syncToClient(1, this::writeAvailableStacks);
396393
} else if (id == 4) {
397394
int slot = buf.readVarInt();
398395
syncToClient(5, buffer -> {
@@ -434,6 +431,16 @@ private static ItemStack readStackSafe(PacketBuffer buffer) {
434431
return stack;
435432
}
436433

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+
437444
private static void writeStackSafe(PacketBuffer buffer, ItemStack stack) {
438445
var tag = stack.serializeNBT();
439446
// GTLog.logger.warn(String.format("Sent: %s", tag));

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

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package gregtech.common.metatileentities.storage;
22

3-
import com.cleanroommc.modularui.api.drawable.IDrawable;
4-
5-
import com.cleanroommc.modularui.widget.ParentWidget;
6-
73
import gregtech.api.capability.GregtechDataCodes;
84
import gregtech.api.capability.impl.ItemHandlerList;
95
import gregtech.api.items.itemhandlers.GTItemStackHandler;
@@ -39,6 +35,7 @@
3935
import codechicken.lib.render.pipeline.ColourMultiplier;
4036
import codechicken.lib.render.pipeline.IVertexOperation;
4137
import codechicken.lib.vec.Matrix4;
38+
import com.cleanroommc.modularui.api.drawable.IDrawable;
4239
import com.cleanroommc.modularui.api.drawable.IKey;
4340
import com.cleanroommc.modularui.api.widget.IWidget;
4441
import com.cleanroommc.modularui.api.widget.Interactable;
@@ -54,6 +51,7 @@
5451
import com.cleanroommc.modularui.value.sync.GuiSyncManager;
5552
import com.cleanroommc.modularui.value.sync.IntSyncValue;
5653
import com.cleanroommc.modularui.value.sync.SyncHandlers;
54+
import com.cleanroommc.modularui.widget.ParentWidget;
5755
import com.cleanroommc.modularui.widget.Widget;
5856
import com.cleanroommc.modularui.widget.scroll.VerticalScrollData;
5957
import com.cleanroommc.modularui.widgets.ItemSlot;
@@ -242,9 +240,9 @@ public boolean usesMui2() {
242240
@Override
243241
public ModularPanel buildUI(PosGuiData guiData, GuiSyncManager guiSyncManager) {
244242
getCraftingRecipeLogic().updateCurrentRecipe();
245-
if (!guiSyncManager.isClient()) {
246-
writeCustomData(UPDATE_CLIENT_STACKS, getCraftingRecipeLogic()::writeAvailableStacks);
247-
}
243+
// if (!guiSyncManager.isClient()) {
244+
// writeCustomData(UPDATE_CLIENT_STACKS, getCraftingRecipeLogic()::writeAvailableStacks);
245+
// }
248246

249247
guiSyncManager.syncValue("recipe_logic", this.recipeLogic);
250248

@@ -270,7 +268,6 @@ public ModularPanel buildUI(PosGuiData guiData, GuiSyncManager guiSyncManager) {
270268
.top(22)
271269
.margin(7)
272270
.widthRel(0.9f)
273-
// .bottom(100)
274271
.controller(controller)
275272
// workstation page
276273
.addPage(new Column()
@@ -284,13 +281,7 @@ public ModularPanel buildUI(PosGuiData guiData, GuiSyncManager guiSyncManager) {
284281
.child(createCraftingGrid())
285282
.child(createCraftingOutput(guiData, guiSyncManager))
286283
// recipe memory
287-
.child(SlotGroupWidget.builder()
288-
.matrix("XXX",
289-
"XXX",
290-
"XXX")
291-
.key('X', i -> new RecipeMemorySlot(this.recipeMemory, i))
292-
.build().right(0))
293-
)
284+
.child(createRecipeMemoryGrid(guiSyncManager)))
294285
// tool inventory
295286
.child(createToolInventory(guiSyncManager))
296287
// internal inventory
@@ -360,6 +351,15 @@ public IWidget createCraftingOutput(PosGuiData guiData, GuiSyncManager syncManag
360351
.asWidget().widthRel(1f));
361352
}
362353

354+
public IWidget createRecipeMemoryGrid(GuiSyncManager syncManager) {
355+
return SlotGroupWidget.builder()
356+
.matrix("XXX",
357+
"XXX",
358+
"XXX")
359+
.key('X', i -> new RecipeMemorySlot(this.recipeMemory, i))
360+
.build().right(0);
361+
}
362+
363363
public IWidget createInventoryPage(GuiSyncManager syncManager) {
364364
var connected = new SlotGroup("connected_inventory", 8, true);
365365
syncManager.registerSlotGroup(connected);
@@ -399,35 +399,26 @@ public IWidget createInventoryPage(GuiSyncManager syncManager) {
399399
}
400400

401401
public void sendHandlerToClient(PacketBuffer buffer) {
402-
int combined = this.combinedInventory.getSlots(),
403-
connected = this.connectedInventory.getSlots();
404-
405-
buffer.writeVarInt(connected);
406-
buffer.writeVarInt(combined - connected);
407-
getCraftingRecipeLogic().writeAvailableStacks(buffer);
402+
buffer.writeVarInt(this.connectedInventory.getSlots());
408403
}
409404

410405
@Override
411406
public void receiveCustomData(int dataId, @NotNull PacketBuffer buf) {
412407
super.receiveCustomData(dataId, buf);
413-
if (dataId == UPDATE_CLIENT_STACKS) {
414-
getCraftingRecipeLogic()
415-
.updateClientStacks(buf);
408+
if (dataId == UPDATE_CLIENT_HANDLER) {
409+
int connected = buf.readVarInt();
416410

417-
} else if (dataId == UPDATE_CLIENT_HANDLER) {
418-
int connected = buf.readVarInt(), internal = buf.readVarInt();
411+
// check if sizes have changed, and keep any existing items
419412

420413
// set connected inventory
421414
this.connectedInventory = new ItemStackHandler(connected);
422415

423416
// set combined inventory
424-
this.combinedInventory = new ItemHandlerList(Arrays.asList(this.connectedInventory, new ItemStackHandler(internal)));
417+
this.combinedInventory = new ItemHandlerList(
418+
Arrays.asList(this.connectedInventory, this.internalInventory));
425419

426420
getCraftingRecipeLogic()
427421
.updateInventory(this.combinedInventory);
428-
429-
getCraftingRecipeLogic()
430-
.updateClientStacks(buf);
431422
}
432423
}
433424

@@ -532,10 +523,14 @@ public CraftingOutputSlot(IItemHandler itemHandler, IntSyncValue syncValue) {
532523
@Override
533524
public boolean canTakeStack(EntityPlayer playerIn) {
534525
if (recipeLogic.getSyncManager().isClient()) {
535-
recipeLogic.syncToServer(3);
526+
// recipeLogic.syncToServer(3);
536527
return false;
537528
}
538-
return recipeLogic.isRecipeValid() && recipeLogic.consumeRecipeItems(true);
529+
530+
if (recipeLogic.isRecipeValid())
531+
recipeLogic.collectAvailableItems();
532+
533+
return recipeLogic.attemptMatchRecipe();
539534
}
540535

541536
@Override
@@ -547,16 +542,16 @@ public ItemStack onTake(EntityPlayer thePlayer, ItemStack stack) {
547542

548543
@Override
549544
public void putStack(@NotNull ItemStack stack) {
550-
super.putStack(recipeLogic.getCachedRecipeData().getRecipeOutput());
545+
super.putStack(getStack());
551546
}
552547

553548
@Override
554-
public ItemStack decrStackSize(int amount) {
549+
public @NotNull ItemStack decrStackSize(int amount) {
555550
return getStack();
556551
}
557552

558553
public void handleItemCraft(ItemStack itemStack, EntityPlayer player) {
559-
itemStack.onCrafting(getWorld(), player, 1);
554+
itemStack.onCrafting(player.world, player, 1);
560555

561556
var inventoryCrafting = recipeLogic.getCraftingMatrix();
562557

@@ -569,10 +564,11 @@ public void handleItemCraft(ItemStack itemStack, EntityPlayer player) {
569564
}
570565
if (cachedRecipe != null) {
571566
ItemStack resultStack = cachedRecipe.getCraftingResult(inventoryCrafting);
572-
this.syncValue.setValue(this.syncValue.getValue() + resultStack.getCount(), true, false);
567+
this.syncValue.setValue(this.syncValue.getValue() + resultStack.getCount(), true, true);
573568
// itemsCrafted += resultStack.getCount();
574569
recipeMemory.notifyRecipePerformed(craftingGrid, resultStack);
575570
}
571+
// call method from recipe logic to sync to client
576572
}
577573
}
578574

0 commit comments

Comments
 (0)