Skip to content

Commit

Permalink
simplify client syncing
Browse files Browse the repository at this point in the history
  • Loading branch information
ghzdude committed Apr 20, 2024
1 parent 6ea28ca commit 85a5c84
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -334,23 +328,6 @@ public CachedRecipeData getCachedRecipeData() {
return this.cachedRecipeData;
}

public void writeAvailableStacks(PacketBuffer buffer) {
this.collectAvailableItems();
Map<Integer, ItemStack> 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++) {
Expand All @@ -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));
Expand All @@ -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 -> {
Expand All @@ -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 {
Expand All @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand All @@ -271,14 +271,15 @@ 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")
.coverChildrenHeight()
.widthRel(1f)
// crafting grid
.child(createCraftingGrid())
// crafting output slot
.child(createCraftingOutput(guiData, guiSyncManager))
// recipe memory
.child(createRecipeMemoryGrid(guiSyncManager)))
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 85a5c84

Please sign in to comment.