-
Notifications
You must be signed in to change notification settings - Fork 211
Expand file tree
/
Copy pathCraftingRecipeLogic.java
More file actions
475 lines (412 loc) · 18.4 KB
/
Copy pathCraftingRecipeLogic.java
File metadata and controls
475 lines (412 loc) · 18.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
package gregtech.common.metatileentities.storage;
import gregtech.api.items.toolitem.ItemGTToolbelt;
import gregtech.api.util.DummyContainer;
import gregtech.api.util.GTTransferUtils;
import gregtech.api.util.GTUtility;
import gregtech.api.util.ItemStackHashStrategy;
import gregtech.common.crafting.ShapedOreEnergyTransferRecipe;
import gregtech.common.mui.widget.workbench.CraftingInputSlot;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.InventoryCraftResult;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.network.PacketBuffer;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.common.crafting.IShapedRecipe;
import net.minecraftforge.items.IItemHandlerModifiable;
import com.cleanroommc.modularui.network.NetworkUtils;
import com.cleanroommc.modularui.value.sync.SyncHandler;
import it.unimi.dsi.fastutil.ints.Int2BooleanArrayMap;
import it.unimi.dsi.fastutil.ints.Int2BooleanMap;
import it.unimi.dsi.fastutil.ints.Int2IntArrayMap;
import it.unimi.dsi.fastutil.ints.Int2IntMap;
import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.IntArraySet;
import it.unimi.dsi.fastutil.ints.IntSet;
import it.unimi.dsi.fastutil.objects.Object2BooleanMap;
import it.unimi.dsi.fastutil.objects.Object2BooleanOpenCustomHashMap;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenCustomHashMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenCustomHashMap;
import java.util.Collections;
import java.util.Map;
public class CraftingRecipeLogic extends SyncHandler {
// client only
public static final int UPDATE_INGREDIENTS = 1;
public static final int RESET_INGREDIENTS = 2;
public static final int SYNC_STACK = 3;
// server only
public static final int UPDATE_MATRIX = 0;
private final World world;
private IItemHandlerModifiable availableHandlers;
private final ItemStackHashStrategy strategy = ItemStackHashStrategy.builder()
.compareItem(true)
.compareMetadata(true)
.build();
/**
* Used to lookup a list of slots for a given stack
* filled by {@link CraftingRecipeLogic#refreshStackMap()}
**/
private final Map<ItemStack, IntSet> stackLookupMap = new Object2ObjectOpenCustomHashMap<>(this.strategy);
/**
* List of items needed to complete the crafting recipe, filled by
* {@link CraftingRecipeLogic#detectAndSendChanges(boolean)} )}
**/
private final Object2IntMap<ItemStack> requiredItems = new Object2IntOpenCustomHashMap<>(
this.strategy);
private final Int2IntMap compactedIndexes = new Int2IntArrayMap(9);
private final Int2IntMap slotMap = new Int2IntArrayMap();
private final Int2ObjectMap<Object2BooleanMap<ItemStack>> replaceAttemptMap = new Int2ObjectArrayMap<>();
private final InventoryCrafting craftingMatrix;
private final IInventory craftingResultInventory = new InventoryCraftResult();
private final CachedRecipeData cachedRecipeData;
private final CraftingInputSlot[] inputSlots = new CraftingInputSlot[9];
public CraftingRecipeLogic(World world, IItemHandlerModifiable handlers, IItemHandlerModifiable craftingMatrix) {
this.world = world;
this.availableHandlers = handlers;
this.craftingMatrix = wrapHandler(craftingMatrix);
this.cachedRecipeData = new CachedRecipeData();
}
public IInventory getCraftingResultInventory() {
return craftingResultInventory;
}
public InventoryCrafting getCraftingMatrix() {
return this.craftingMatrix;
}
public void updateSlotMap(int offset, int slot) {
slotMap.put(offset + slot, slotMap.size());
}
public void clearSlotMap() {
slotMap.clear();
}
public void updateInventory(IItemHandlerModifiable handler) {
this.availableHandlers = handler;
}
public void clearCraftingGrid() {
fillCraftingGrid(Collections.emptyMap());
}
public void fillCraftingGrid(Map<Integer, ItemStack> ingredients) {
for (int i = 0; i < craftingMatrix.getSizeInventory(); i++) {
craftingMatrix.setInventorySlotContents(i, ingredients.getOrDefault(i, ItemStack.EMPTY));
}
syncMatrix();
updateCurrentRecipe();
}
public void setInputSlot(CraftingInputSlot slot, int index) {
this.inputSlots[index] = slot;
}
public boolean performRecipe() {
return isRecipeValid() && attemptMatchRecipe() && consumeRecipeItems();
}
public boolean isRecipeValid() {
return cachedRecipeData.getRecipe() != null && cachedRecipeData.matches(craftingMatrix, this.world);
}
/**
* Attempts to match the crafting matrix against all connected inventories
*
* @return true if all items matched
*/
public boolean attemptMatchRecipe() {
for (CraftingInputSlot slot : this.inputSlots) {
if (!slot.hasIngredients) {
return false;
}
}
return true;
}
protected boolean consumeRecipeItems() {
if (requiredItems.isEmpty()) {
return false;
}
Int2IntMap gatheredItems = new Int2IntOpenHashMap();
for (var entry : requiredItems.entrySet()) {
ItemStack stack = entry.getKey();
int requestedAmount = entry.getValue();
var slotList = stackLookupMap.get(stack);
for (int slot : slotList) {
var extracted = availableHandlers.extractItem(slot, requestedAmount, true);
gatheredItems.put(slot, extracted.getCount());
requestedAmount -= extracted.getCount();
}
// not enough to satisfy the recipe, return false
if (requestedAmount > 0) return false;
}
for (var gathered : gatheredItems.entrySet()) {
int slot = gathered.getKey(), amount = gathered.getValue();
var stack = availableHandlers.getStackInSlot(slot);
boolean hasContainer = stack.getItem().hasContainerItem(stack);
if (!hasContainer) {
// not a transmutable item (damagable tool, etc), extract normally
availableHandlers.extractItem(slot, amount, false);
} else if (stack.getCount() > 1) {
// only some stacks are transmuted, try insert non-empty stacks
ItemStack newStack = ForgeHooks.getContainerItem(stack.splitStack(1));
if (!newStack.isEmpty())
GTTransferUtils.insertItem(this.availableHandlers, newStack, false);
} else {
// all stacks are transmuted, just replace
availableHandlers.setStackInSlot(slot, ForgeHooks.getContainerItem(stack));
}
}
// we've checked everything, return true
return true;
}
/**
* <p>
* Searches through all connected inventories for a replacement stack that can be used in the recipe
* </p>
*
* @param craftingIndex Index of the current crafting slot
* @param stack The stack to find a substitute for
* @return a valid replacement stack, or {@link ItemStack#EMPTY} if no valid replacements exist
*/
public ItemStack findSubstitute(int craftingIndex, ItemStack stack) {
Object2BooleanMap<ItemStack> map = replaceAttemptMap.computeIfAbsent(craftingIndex,
(m) -> new Object2BooleanOpenCustomHashMap<>(ItemStackHashStrategy.comparingAllButCount()));
ItemStack substitute = ItemStack.EMPTY;
var recipe = getCachedRecipe();
int index = compactedIndexes.get(craftingIndex);
// iterate stored items to find equivalent
for (int i = 0; i < this.availableHandlers.getSlots(); i++) {
var itemStack = availableHandlers.getStackInSlot(i);
if (itemStack.isEmpty() || this.strategy.equals(itemStack, stack)) continue;
boolean matchedPreviously = false;
if (map.containsKey(itemStack)) {
if (map.getBoolean(itemStack)) {
// cant return here before checking if:
// The item is available for extraction
// The recipe output is still the same, as depending on
// the ingredient, the output NBT may change
matchedPreviously = true;
}
}
// this is also every tick
if (itemStack.getItem() instanceof ItemGTToolbelt) {
// we need to do this here because of ingredient apply
ItemGTToolbelt.setCraftingSlot(slotMap.get(i), (EntityPlayerMP) getSyncManager().getPlayer());
}
if (!matchedPreviously) {
// Matching shapeless recipes actually is very bad for performance, as it checks the entire
// recipe ingredients recursively, so we fail early here if none of the recipes ingredients can
// take the stack
boolean matched = false;
if (!(recipe instanceof IShapedRecipe)) {
for (Ingredient ing : recipe.getIngredients()) {
if (ing.apply(itemStack)) {
matched = true;
break;
}
}
} else {
// for shaped recipes, check the exact ingredient instead
// ingredients should be in the correct order
matched = cachedRecipeData.canIngredientApply(index, itemStack);
}
if (!matched) {
map.put(GTUtility.copy(1, itemStack), false);
continue;
}
}
ItemStack previousResult = recipe.getCraftingResult(craftingMatrix);
// update item in slot, and check that recipe matches and output item is equal to the expected one
craftingMatrix.setInventorySlotContents(craftingIndex, itemStack);
var newResult = recipe.getCraftingResult(craftingMatrix);
// this will send packets every tick for the toolbelt, not sure what can be done
if ((cachedRecipeData.matches(craftingMatrix, world) &&
ItemStack.areItemStacksEqual(newResult, previousResult)) ||
recipe instanceof ShapedOreEnergyTransferRecipe) {
// ingredient matched, return the substitute
craftingMatrix.setInventorySlotContents(craftingIndex, stack);
map.put(GTUtility.copy(1, itemStack), true);
substitute = itemStack;
break;
}
map.put(GTUtility.copy(1, itemStack), false);
craftingMatrix.setInventorySlotContents(craftingIndex, stack);
}
return substitute;
}
/**
* Attempts to extract the given stack from connected inventories
*
* @param craftingIndex current crafting index
* @param itemStack stack from the crafting matrix
* @return true if the stack was successfully extracted or the stack is empty
*/
private boolean simulateExtractItem(int craftingIndex, ItemStack itemStack, int count) {
if (itemStack.isEmpty()) return true;
if (!stackLookupMap.containsKey(itemStack)) return false;
int extracted = 0;
for (int slot : stackLookupMap.get(itemStack)) {
var slotStack = availableHandlers.extractItem(slot, count, true);
// we are certain the stack map is correct
if (slotStack.getItem() instanceof ItemGTToolbelt) {
ItemGTToolbelt.setCraftingSlot(slotMap.get(slot), (EntityPlayerMP) getSyncManager().getPlayer());
}
if (cachedRecipeData.canIngredientApply(compactedIndexes.get(craftingIndex), slotStack)) {
extracted += slotStack.getCount();
if (extracted >= count) return true;
}
}
return false;
}
public void updateCurrentRecipe() {
if (!cachedRecipeData.matches(craftingMatrix, world)) {
IRecipe newRecipe = CraftingManager.findMatchingRecipe(craftingMatrix, world);
ItemStack resultStack = ItemStack.EMPTY;
if (newRecipe != null) {
resultStack = newRecipe.getCraftingResult(craftingMatrix);
}
this.craftingResultInventory.setInventorySlotContents(0, resultStack);
this.cachedRecipeData.setRecipe(newRecipe);
}
}
public IRecipe getCachedRecipe() {
return this.cachedRecipeData.getRecipe();
}
@Override
public void detectAndSendChanges(boolean init) {
var recipe = getCachedRecipe();
if (recipe == null) {
var prevRecipe = cachedRecipeData.getPreviousRecipe();
if (prevRecipe == null) return;
cachedRecipeData.setRecipe(null);
for (CraftingInputSlot inputSlot : this.inputSlots) {
inputSlot.hasIngredients = true;
}
syncToClient(RESET_INGREDIENTS);
return;
}
Int2BooleanMap map = updateInputSlots();
// only sync when something has changed
if (!map.isEmpty()) {
syncToClient(UPDATE_INGREDIENTS, buffer -> {
buffer.writeByte(map.size());
for (var set : map.entrySet()) {
buffer.writeByte(set.getKey());
buffer.writeBoolean(set.getValue());
}
});
}
}
/**
* Updates each input slot for if a valid item exists for that slot
*
* @return a map of slots that has changed since last time, if any
*/
private Int2BooleanMap updateInputSlots() {
compactedIndexes.clear();
requiredItems.clear();
refreshStackMap();
Int2BooleanMap map = new Int2BooleanArrayMap();
int next = 0;
for (CraftingInputSlot slot : this.inputSlots) {
boolean hadIngredients = slot.hasIngredients;
// check if existing stack works
var slotStack = slot.getStack();
if (slotStack.isEmpty()) {
if (!hadIngredients) {
slot.hasIngredients = true;
map.put(slot.getIndex(), slot.hasIngredients);
}
continue;
}
compactedIndexes.put(slot.getIndex(), next++);
int count = requiredItems.getOrDefault(slotStack, 0) + 1;
slot.hasIngredients = simulateExtractItem(slot.getIndex(), slotStack, count);
if (slot.hasIngredients) {
requiredItems.put(GTUtility.copy(1, slotStack), count);
} else {
// check if substitute exists
ItemStack substitute = findSubstitute(slot.getIndex(), slotStack);
if (!substitute.isEmpty()) {
count = requiredItems.getOrDefault(substitute, 0) + 1;
slot.hasIngredients = simulateExtractItem(slot.getIndex(), substitute, count);
requiredItems.put(GTUtility.copy(1, substitute), count);
}
}
if (hadIngredients != slot.hasIngredients)
map.put(slot.getIndex(), slot.hasIngredients);
}
return map;
}
/**
* Searches available handlers and
* adds the stack and slots the stack lookup map
*/
public void refreshStackMap() {
// the stack lookup map is a pain to do "correctly"
// so just clear and reset every tick in detectAndSendChanges()
stackLookupMap.clear();
for (int i = 0; i < this.availableHandlers.getSlots(); i++) {
var curStack = this.availableHandlers.getStackInSlot(i);
if (curStack.isEmpty()) continue;
IntSet slots;
if (stackLookupMap.containsKey(curStack)) {
slots = stackLookupMap.get(curStack);
} else {
stackLookupMap.put(GTUtility.copy(1, curStack), slots = new IntArraySet());
}
slots.add(i);
}
}
public void writeMatrix(PacketBuffer buffer) {
buffer.writeVarInt(craftingMatrix.getSizeInventory());
for (int i = 0; i < craftingMatrix.getSizeInventory(); i++) {
NetworkUtils.writeItemStack(buffer, craftingMatrix.getStackInSlot(i));
}
}
@Override
public void readOnClient(int id, PacketBuffer buf) {
if (id == UPDATE_INGREDIENTS) {
int size = buf.readByte();
for (int i = 0; i < size; i++) {
this.inputSlots[buf.readByte()].hasIngredients = buf.readBoolean();
}
} else if (id == SYNC_STACK) {
getSyncManager().setCursorItem(NetworkUtils.readItemStack(buf));
} else if (id == RESET_INGREDIENTS) {
for (CraftingInputSlot inputSlot : this.inputSlots) {
inputSlot.hasIngredients = true;
}
}
}
@Override
public void readOnServer(int id, PacketBuffer buf) {
if (id == UPDATE_MATRIX) {
int size = buf.readVarInt();
for (int i = 0; i < size; i++) {
this.craftingMatrix.setInventorySlotContents(i, NetworkUtils.readItemStack(buf));
}
this.updateCurrentRecipe();
}
}
public void syncMatrix() {
if (getSyncManager().isClient())
syncToServer(UPDATE_MATRIX, this::writeMatrix);
}
public static InventoryCrafting wrapHandler(IItemHandlerModifiable handler) {
return new InventoryCrafting(new DummyContainer(), 3, 3) {
@Override
public ItemStack getStackInRowAndColumn(int row, int column) {
int index = row + (3 * column);
return handler.getStackInSlot(index);
}
@Override
public ItemStack getStackInSlot(int index) {
return handler.getStackInSlot(index);
}
@Override
public void setInventorySlotContents(int index, ItemStack stack) {
handler.setStackInSlot(index, GTUtility.copy(1, stack));
}
};
}
}