Skip to content

Commit e43fd6c

Browse files
committed
sync slot and player for toolbelt
simplify attemptMatchRecipe() add method to get base index offset from ItemHandlerList
1 parent e944873 commit e43fd6c

File tree

3 files changed

+43
-15
lines changed

3 files changed

+43
-15
lines changed

src/main/java/gregtech/api/capability/impl/ItemHandlerList.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
88
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
9+
import it.unimi.dsi.fastutil.objects.Object2IntArrayMap;
10+
import it.unimi.dsi.fastutil.objects.Object2IntMap;
911
import org.jetbrains.annotations.NotNull;
1012

1113
import java.util.*;
@@ -16,7 +18,7 @@
1618
public class ItemHandlerList implements IItemHandlerModifiable {
1719

1820
private final Int2ObjectMap<IItemHandler> handlerBySlotIndex = new Int2ObjectOpenHashMap<>();
19-
private final Map<IItemHandler, Integer> baseIndexOffset = new IdentityHashMap<>();
21+
private final Object2IntMap<IItemHandler> baseIndexOffset = new Object2IntArrayMap<>();
2022

2123
public ItemHandlerList(List<? extends IItemHandler> itemHandlerList) {
2224
int currentSlotIndex = 0;
@@ -33,6 +35,10 @@ public ItemHandlerList(List<? extends IItemHandler> itemHandlerList) {
3335
}
3436
}
3537

38+
public int getIndexOffset(IItemHandler handler) {
39+
return baseIndexOffset.getOrDefault(handler, -1);
40+
}
41+
3642
@Override
3743
public int getSlots() {
3844
return handlerBySlotIndex.size();

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public class CraftingRecipeLogic extends SyncHandler {
7070
this.strategy);
7171

7272
private final Int2IntMap compactedIndexes = new Int2IntArrayMap(9);
73+
private final Int2IntMap slotMap = new Int2IntArrayMap();
7374

7475
private final Map<Integer, Object2BooleanMap<ItemStack>> replaceAttemptMap = new Int2ObjectArrayMap<>();
7576
private final InventoryCrafting craftingMatrix;
@@ -92,6 +93,14 @@ public InventoryCrafting getCraftingMatrix() {
9293
return this.craftingMatrix;
9394
}
9495

96+
public void updateSlotMap(int offset, int slot) {
97+
slotMap.put(offset + slot, slotMap.size());
98+
}
99+
100+
public void clearSlotMap() {
101+
slotMap.clear();
102+
}
103+
95104
public void updateInventory(IItemHandlerModifiable handler) {
96105
this.availableHandlers = handler;
97106
}
@@ -126,9 +135,8 @@ public boolean isRecipeValid() {
126135
* @return true if all items matched
127136
*/
128137
public boolean attemptMatchRecipe() {
129-
this.requiredItems.clear();
130138
for (CraftingInputSlot slot : this.inputSlots) {
131-
if (!getIngredientEquivalent(slot)) {
139+
if (!slot.hasIngredients) {
132140
return false;
133141
}
134142
}
@@ -244,7 +252,7 @@ public ItemStack findSubstitute(int craftingIndex, ItemStack stack) {
244252

245253
boolean matchedPreviously = false;
246254
if (map.containsKey(itemStack)) {
247-
if (map.get(itemStack)) {
255+
if (map.getBoolean(itemStack)) {
248256
// cant return here before checking if:
249257
// The item is available for extraction
250258
// The recipe output is still the same, as depending on
@@ -253,6 +261,12 @@ public ItemStack findSubstitute(int craftingIndex, ItemStack stack) {
253261
}
254262
}
255263

264+
// this is also every tick
265+
if (itemStack.getItem() instanceof ItemGTToolbelt) {
266+
// we need to do this here because of ingredient apply
267+
ItemGTToolbelt.setCraftingSlot(slotMap.get(i), (EntityPlayerMP) getSyncManager().getPlayer());
268+
}
269+
256270
if (!matchedPreviously) {
257271
// Matching shapeless recipes actually is very bad for performance, as it checks the entire
258272
// recipe ingredients recursively, so we fail early here if none of the recipes ingredients can
@@ -286,6 +300,7 @@ public ItemStack findSubstitute(int craftingIndex, ItemStack stack) {
286300
// update item in slot, and check that recipe matches and output item is equal to the expected one
287301
craftingMatrix.setInventorySlotContents(craftingIndex, itemStack);
288302
var newResult = recipe.getCraftingResult(craftingMatrix);
303+
// this will send packets every tick for the toolbelt, not sure what can be done
289304
if ((cachedRecipeData.matches(craftingMatrix, world) &&
290305
ItemStack.areItemStacksEqual(newResult, previousResult)) ||
291306
recipe instanceof ShapedOreEnergyTransferRecipe) {
@@ -295,7 +310,7 @@ public ItemStack findSubstitute(int craftingIndex, ItemStack stack) {
295310
substitute = itemStack;
296311
break;
297312
}
298-
map.put(itemStack.copy(), false);
313+
map.put(GTUtility.copy(1, itemStack), false);
299314
craftingMatrix.setInventorySlotContents(craftingIndex, stack);
300315
}
301316
return substitute;
@@ -317,9 +332,7 @@ private boolean simulateExtractItem(ItemStack itemStack, int count) {
317332
var slotStack = availableHandlers.extractItem(slot, count, true);
318333
// we are certain the stack map is correct
319334
if (slotStack.getItem() instanceof ItemGTToolbelt) {
320-
// todo this doesn't work correctly
321-
// the slots are different on the client for some reason
322-
ItemGTToolbelt.setCraftingSlot(slot, (EntityPlayerMP) getSyncManager().getPlayer());
335+
ItemGTToolbelt.setCraftingSlot(slotMap.get(slot), (EntityPlayerMP) getSyncManager().getPlayer());
323336
}
324337
extracted += slotStack.getCount();
325338
if (extracted >= count) return true;

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
import com.cleanroommc.modularui.utils.Alignment;
4949
import com.cleanroommc.modularui.value.sync.IntSyncValue;
5050
import com.cleanroommc.modularui.value.sync.PanelSyncManager;
51-
import com.cleanroommc.modularui.value.sync.SyncHandlers;
5251
import com.cleanroommc.modularui.widget.scroll.VerticalScrollData;
5352
import com.cleanroommc.modularui.widgets.ButtonWidget;
5453
import com.cleanroommc.modularui.widgets.ItemSlot;
@@ -57,6 +56,7 @@
5756
import com.cleanroommc.modularui.widgets.SlotGroupWidget;
5857
import com.cleanroommc.modularui.widgets.layout.Flow;
5958
import com.cleanroommc.modularui.widgets.layout.Grid;
59+
import com.cleanroommc.modularui.widgets.slot.ModularSlot;
6060
import com.cleanroommc.modularui.widgets.slot.SlotGroup;
6161
import com.google.common.base.Preconditions;
6262
import org.apache.commons.lang3.ArrayUtils;
@@ -66,6 +66,7 @@
6666

6767
import java.util.ArrayList;
6868
import java.util.Arrays;
69+
import java.util.Collections;
6970
import java.util.List;
7071

7172
public class MetaTileEntityWorkbench extends MetaTileEntity {
@@ -80,8 +81,8 @@ public class MetaTileEntityWorkbench extends MetaTileEntity {
8081
private final ItemStackHandler internalInventory = new GTItemStackHandler(this, 18);
8182
private final ItemStackHandler toolInventory = new ToolItemStackHandler(9);
8283

83-
private IItemHandlerModifiable combinedInventory;
84-
private IItemHandlerModifiable connectedInventory;
84+
private ItemHandlerList combinedInventory;
85+
private ItemHandlerList connectedInventory;
8586

8687
private final CraftingRecipeMemory recipeMemory = new CraftingRecipeMemory(9, this.craftingGrid);
8788
private CraftingRecipeLogic recipeLogic = null;
@@ -202,6 +203,7 @@ public boolean usesMui2() {
202203
@Override
203204
public ModularPanel buildUI(PosGuiData guiData, PanelSyncManager syncManager) {
204205
getCraftingRecipeLogic().updateCurrentRecipe();
206+
this.recipeLogic.clearSlotMap();
205207

206208
syncManager.syncValue("recipe_logic", this.recipeLogic);
207209
syncManager.syncValue("recipe_memory", this.recipeMemory);
@@ -259,6 +261,13 @@ public ModularPanel buildUI(PosGuiData guiData, PanelSyncManager syncManager) {
259261
.bindPlayerInventory();
260262
}
261263

264+
private ModularSlot trackSlot(IItemHandler handler, int slot) {
265+
int offset = combinedInventory.getIndexOffset(handler);
266+
if (offset == -1) throw new NullPointerException("handler cannot be found");
267+
this.recipeLogic.updateSlotMap(offset, slot);
268+
return new ModularSlot(handler, slot);
269+
}
270+
262271
public IWidget createToolInventory(PanelSyncManager syncManager) {
263272
var toolSlots = new SlotGroup("tool_slots", 9, -120, true);
264273
syncManager.registerSlotGroup(toolSlots);
@@ -267,7 +276,7 @@ public IWidget createToolInventory(PanelSyncManager syncManager) {
267276
.row("XXXXXXXXX")
268277
.key('X', i -> new ItemSlot()
269278
.background(GTGuiTextures.SLOT, GTGuiTextures.TOOL_SLOT_OVERLAY)
270-
.slot(SyncHandlers.itemSlot(this.toolInventory, i)
279+
.slot(trackSlot(this.toolInventory, i)
271280
.slotGroup(toolSlots)))
272281
.build().marginTop(2);
273282
}
@@ -280,7 +289,7 @@ public IWidget createInternalInventory(PanelSyncManager syncManager) {
280289
.row("XXXXXXXXX")
281290
.row("XXXXXXXXX")
282291
.key('X', i -> new ItemSlot()
283-
.slot(SyncHandlers.itemSlot(this.internalInventory, i)
292+
.slot(trackSlot(this.internalInventory, i)
284293
.slotGroup(inventory)))
285294
.build().marginTop(2);
286295
}
@@ -364,7 +373,7 @@ public IWidget createInventoryPage(PanelSyncManager syncManager) {
364373
int slot = itemSlot.getSlot().getSlotIndex();
365374
return slot < this.connectedInventory.getSlots();
366375
})
367-
.slot(SyncHandlers.itemSlot(this.connectedInventory, i)
376+
.slot(trackSlot(this.connectedInventory, i)
368377
.slotGroup(connected)));
369378
}
370379

@@ -400,7 +409,7 @@ public void readHandler(PacketBuffer buf) {
400409
int connected = buf.readVarInt();
401410

402411
// set connected inventory
403-
this.connectedInventory = new ItemStackHandler(connected);
412+
this.connectedInventory = new ItemHandlerList(Collections.singletonList(new ItemStackHandler(connected)));
404413

405414
// set combined inventory
406415
this.combinedInventory = new ItemHandlerList(Arrays.asList(

0 commit comments

Comments
 (0)