Skip to content

Commit

Permalink
more work on syncing and recipe logic part 2 + spotless
Browse files Browse the repository at this point in the history
  • Loading branch information
ghzdude committed Apr 20, 2024
1 parent d118fc9 commit 6ea28ca
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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 -> {
Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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
Expand All @@ -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();

Expand All @@ -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
}
}

Expand Down

0 comments on commit 6ea28ca

Please sign in to comment.