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
|
/*
* Roughly Enough Items by Danielshe.
* Licensed under the MIT License.
*/
package me.shedaniel.rei.server;
import com.google.common.collect.Lists;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntList;
import it.unimi.dsi.fastutil.ints.IntListIterator;
import net.minecraft.container.Container;
import net.minecraft.container.Slot;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.recipe.Ingredient;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.DefaultedList;
import net.minecraft.util.Identifier;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class InputSlotCrafter<C extends Inventory> implements RecipeGridAligner<Integer> {
protected Container craftingContainer;
protected ContainerInfo containerInfo;
protected PlayerInventory inventory;
private InputSlotCrafter(Container craftingContainer, ContainerInfo containerInfo) {
this.craftingContainer = craftingContainer;
this.containerInfo = containerInfo;
}
public static <C extends Inventory> void start(Identifier category, Container craftingContainer_1, ServerPlayerEntity player, Map<Integer, List<ItemStack>> map, boolean hasShift) {
ContainerInfo containerInfo = ContainerInfoHandler.getContainerInfo(category, craftingContainer_1.getClass());
new InputSlotCrafter<C>(craftingContainer_1, containerInfo).fillInputSlots(player, map, hasShift);
}
private void fillInputSlots(ServerPlayerEntity player, Map<Integer, List<ItemStack>> map, boolean hasShift) {
this.inventory = player.inventory;
if (this.canReturnInputs() || player.isCreative()) {
// Return the already placed items on the grid
this.returnInputs();
RecipeFinder recipeFinder = new RecipeFinder();
recipeFinder.clear();
for (ItemStack stack : player.inventory.main) {
recipeFinder.addNormalItem(stack);
}
this.containerInfo.populateRecipeFinder(craftingContainer, recipeFinder);
DefaultedList<Ingredient> ingredients = DefaultedList.of();
map.entrySet().stream().sorted(Comparator.comparingInt(Map.Entry::getKey)).forEach(entry -> {
ingredients.add(Ingredient.ofItems(entry.getValue().stream().map(ItemStack::getItem).collect(Collectors.toList()).toArray(new Item[0])));
});
if (recipeFinder.findRecipe(ingredients, (IntList) null)) {
this.fillInputSlots(recipeFinder, ingredients, hasShift);
} else {
this.returnInputs();
player.inventory.markDirty();
throw new NotEnoughMaterialsException();
}
player.inventory.markDirty();
}
}
@Override
public void acceptAlignedInput(Iterator<Integer> iterator_1, int int_1, int int_2, int int_3, int int_4) {
Slot slot_1 = this.craftingContainer.getSlot(int_1);
ItemStack itemStack_1 = RecipeFinder.getStackFromId(iterator_1.next());
if (!itemStack_1.isEmpty()) {
for (int int_5 = 0; int_5 < int_2; ++int_5) {
this.fillInputSlot(slot_1, itemStack_1);
}
}
}
protected void fillInputSlot(Slot slot_1, ItemStack itemStack_1) {
int int_1 = this.inventory.method_7371(itemStack_1);
if (int_1 != -1) {
ItemStack itemStack_2 = this.inventory.getInvStack(int_1).copy();
if (!itemStack_2.isEmpty()) {
if (itemStack_2.getCount() > 1) {
this.inventory.takeInvStack(int_1, 1);
} else {
this.inventory.removeInvStack(int_1);
}
itemStack_2.setCount(1);
if (slot_1.getStack().isEmpty()) {
slot_1.setStack(itemStack_2);
} else {
slot_1.getStack().increment(1);
}
}
}
}
@SuppressWarnings("deprecation")
protected void fillInputSlots(RecipeFinder recipeFinder, DefaultedList<Ingredient> ingredients, boolean hasShift) {
// boolean boolean_2 = this.craftingContainer.matches(recipe_1);
boolean boolean_2 = false;
int int_1 = recipeFinder.countRecipeCrafts(ingredients, (IntList) null);
int int_2;
if (boolean_2) {
for (int_2 = 0; int_2 < this.containerInfo.getCraftingHeight(craftingContainer) * this.containerInfo.getCraftingWidth(craftingContainer) + 1; ++int_2) {
if (int_2 != this.containerInfo.getCraftingResultSlotIndex(craftingContainer)) {
ItemStack itemStack_1 = this.craftingContainer.getSlot(int_2).getStack();
if (!itemStack_1.isEmpty() && Math.min(int_1, itemStack_1.getMaxCount()) < itemStack_1.getCount() + 1) {
return;
}
}
}
}
int_2 = this.getAmountToFill(hasShift, int_1, boolean_2);
IntList intList_1 = new IntArrayList();
if (recipeFinder.findRecipe(ingredients, intList_1, int_2)) {
int int_4 = int_2;
IntListIterator var8 = intList_1.iterator();
while (var8.hasNext()) {
int int_5 = (Integer) var8.next();
int int_6 = RecipeFinder.getStackFromId(int_5).getMaxCount();
if (int_6 < int_4) {
int_4 = int_6;
}
}
if (recipeFinder.findRecipe(ingredients, intList_1, int_4)) {
this.returnInputs();
this.alignRecipeToGrid(this.containerInfo.getCraftingWidth(craftingContainer), this.containerInfo.getCraftingHeight(craftingContainer), this.containerInfo.getCraftingResultSlotIndex(craftingContainer), ingredients, intList_1.iterator(), int_4);
}
}
}
protected int getAmountToFill(boolean hasShift, int int_1, boolean boolean_2) {
int int_2 = 1;
if (hasShift) {
int_2 = int_1;
} else if (boolean_2) {
int_2 = 64;
for (int int_3 = 0; int_3 < this.containerInfo.getCraftingWidth(craftingContainer) * this.containerInfo.getCraftingHeight(craftingContainer) + 1; ++int_3) {
if (int_3 != this.containerInfo.getCraftingResultSlotIndex(craftingContainer)) {
ItemStack itemStack_1 = this.craftingContainer.getSlot(int_3).getStack();
if (!itemStack_1.isEmpty() && int_2 > itemStack_1.getCount()) {
int_2 = itemStack_1.getCount();
}
}
}
if (int_2 < 64) {
++int_2;
}
}
return int_2;
}
protected void returnInputs() {
for (int int_1 = 0; int_1 < this.containerInfo.getCraftingWidth(craftingContainer) * this.containerInfo.getCraftingHeight(craftingContainer) + 1; ++int_1) {
if (int_1 != this.containerInfo.getCraftingResultSlotIndex(craftingContainer)) {
this.returnSlot(int_1);
}
}
this.containerInfo.clearCraftingSlots(craftingContainer);
}
protected void returnSlot(int int_1) {
ItemStack itemStack_1 = this.craftingContainer.getSlot(int_1).getStack();
if (!itemStack_1.isEmpty()) {
for (; itemStack_1.getCount() > 0; this.craftingContainer.getSlot(int_1).takeStack(1)) {
int int_2 = this.inventory.getOccupiedSlotWithRoomForStack(itemStack_1);
if (int_2 == -
|