/
AutomaticCrafter.java
347 lines (291 loc) · 12.5 KB
/
AutomaticCrafter.java
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
package com.sk89q.craftbook.mechanics.ic.gates.world.items;
import com.sk89q.craftbook.ChangedSign;
import com.sk89q.craftbook.bukkit.CraftBookPlugin;
import com.sk89q.craftbook.bukkit.util.BukkitUtil;
import com.sk89q.craftbook.mechanics.crafting.CustomCrafting;
import com.sk89q.craftbook.mechanics.ic.*;
import com.sk89q.craftbook.mechanics.pipe.PipePutEvent;
import com.sk89q.craftbook.mechanics.pipe.PipeRequestEvent;
import com.sk89q.craftbook.util.ItemUtil;
import com.sk89q.craftbook.util.VerifyUtil;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Dispenser;
import org.bukkit.block.Dropper;
import org.bukkit.entity.Item;
import org.bukkit.inventory.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class AutomaticCrafter extends AbstractSelfTriggeredIC implements PipeInputIC {
private static boolean hasWarned = false;
public AutomaticCrafter(Server server, ChangedSign block, ICFactory factory) {
super(server, block, factory);
}
@Override
public String getTitle() {
return "Automatic Crafter";
}
@Override
public String getSignTitle() {
return "AUTO CRAFT";
}
// Cache the recipe - makes it faster
private Recipe recipe;
@Override
public void trigger(ChipState chip) {
if (chip.getInput(0)) {
chip.setOutput(0, doStuff(true, true));
}
}
@Override
public void think(ChipState state) {
state.setOutput(0, doStuff(true, true));
}
public boolean craft(InventoryHolder disp) {
Inventory inv = disp.getInventory();
for (ItemStack it : inv.getContents()) {
if (!ItemUtil.isStackValid(it))
continue;
if (it.getAmount() < 2) return false;
}
if (recipe == null) {
Iterator<Recipe> recipes = Bukkit.recipeIterator();
try {
while (recipes.hasNext()) {
Recipe temprecipe = recipes.next();
if (isValidRecipe(temprecipe, inv)) {
recipe = temprecipe;
break; //There should only be 1 valid recipe.
}
}
} catch (Exception e) {
BukkitUtil.printStacktrace(e);
disp.getInventory().setContents(inv.getContents());
}
}
if (recipe == null) return false;
if (!isValidRecipe(recipe, inv)) {
recipe = null;
return craft(disp);
}
ItemStack result = CustomCrafting.craftItem(recipe);
if(!ItemUtil.isStackValid(result)) {
CraftBookPlugin.inst().getLogger().warning("An Automatic Crafter IC had a valid recipe, but there was no result! This means Bukkit has an invalid recipe!");
return false;
}
List<ItemStack> items = new ArrayList<ItemStack>();
ItemStack[] replace = new ItemStack[9];
for (int i = 0; i < disp.getInventory().getContents().length; i++) {
if (disp.getInventory().getContents()[i] == null) {
continue;
}
replace[i] = new ItemStack(disp.getInventory().getContents()[i]);
if(replace[i].getType() == Material.WATER_BUCKET || replace[i].getType() == Material.LAVA_BUCKET || replace[i].getType() == Material.MILK_BUCKET)
items.add(new ItemStack(Material.BUCKET, 1));
replace[i].setAmount(replace[i].getAmount() - 1);
}
disp.getInventory().clear();
CraftBookPlugin.logDebugMessage("AutoCrafter is dispensing a " + result.getType().name() + " with data: " + result.getDurability() + " and amount: " + result.getAmount(), "ic-mc1219");
items.add(result);
Block pipe = ((BlockState) disp).getBlock().getRelative(((org.bukkit.material.Directional) ((BlockState) disp).getData()).getFacing());
Block base = ((BlockState) disp).getBlock();
PipeRequestEvent event = new PipeRequestEvent(pipe, items, base);
Bukkit.getPluginManager().callEvent(event);
items = event.getItems();
if(!items.isEmpty()) {
for(ItemStack stack : items) {
if(disp.getInventory().addItem(stack).isEmpty())
for(int i = 0; i < stack.getAmount(); i++)
if(disp instanceof Dispenser)
((Dispenser) disp).dispense();
else if(disp instanceof Dropper)
((Dropper) disp).drop();
}
}
disp.getInventory().setContents(replace);
return true;
}
private boolean collect(InventoryHolder disp) {
for (Item item : ItemUtil.getItemsAtBlock(BukkitUtil.toSign(getSign()).getBlock())) {
boolean delete = true;
ItemStack stack = item.getItemStack();
int newAmount = stack.getAmount();
for (int i = 0; i < stack.getAmount(); i++) {
ItemStack it = ItemUtil.getSmallestStackOfType(disp.getInventory().getContents(), stack);
if (it == null) break;
if (it.getAmount() < 64) {
it.setAmount(it.getAmount() + 1);
newAmount -= 1;
} else if (newAmount > 0) {
delete = false;
break;
}
}
if (newAmount > 0) delete = false;
if (delete) {
item.remove();
} else {
stack.setAmount(newAmount);
item.setItemStack(stack);
}
}
return false;
}
/**
* @param craft Whether to craft.
* @param collect Whether to collect.
*
* @return If it performed an action
*/
private boolean doStuff(boolean craft, boolean collect) {
boolean ret = false;
Block crafter = getBackBlock().getRelative(0, 1, 0);
if (crafter.getType() == Material.DISPENSER || crafter.getType() == Material.DROPPER) {
if (collect)
ret = collect((InventoryHolder) crafter.getState());
if (craft)
ret = craft((InventoryHolder) crafter.getState());
}
return ret;
}
private boolean isValidRecipe(Recipe r, Inventory inv) {
if (r instanceof ShapedRecipe && (recipe == null || recipe instanceof ShapedRecipe)) {
ShapedRecipe shape = (ShapedRecipe) r;
Map<Character, ItemStack> ingredientMap = shape.getIngredientMap();
String[] shapeArr = shape.getShape();
if (shape.getShape().length != shapeArr.length || shapeArr[0].length() != shape.getShape()[0].length()) return false;
int c = -1, in = 0;
int validRecipeItems = 0;
for (int slot = 0; slot < 9; slot++) {
ItemStack stack = inv.getItem(slot);
try {
c++;
if (c >= 3) {
c = 0;
in++;
if (in >= 3) {
break;
}
}
String shapeSection;
if(in < shapeArr.length)
shapeSection = shapeArr[in];
else
shapeSection = " ";
ItemStack require = null;
try {
Character item;
if(c < shapeSection.length())
item = shapeSection.charAt(c);
else
item = ' ';
if(item == ' ')
require = null;
else
require = ingredientMap.get(item);
}
catch(Exception e){
BukkitUtil.printStacktrace(e);
}
if (require != null && require.getType() != Material.AIR) {
validRecipeItems ++;
}
if (!ItemUtil.areItemsIdentical(require, stack))
return false;
} catch (Exception e) {
BukkitUtil.printStacktrace(e);
return false;
}
}
if (validRecipeItems == 0 && !hasWarned) {
CraftBookPlugin.logger().warning("Found invalid recipe! This is an issue with Bukkit/Spigot/etc, please report to them. All recipe ingredients are air. Recipe result: " + recipe.getResult().toString());
hasWarned = true;
return false;
}
return true;
} else if (r instanceof ShapelessRecipe && (recipe == null || recipe instanceof ShapelessRecipe)) {
ShapelessRecipe shape = (ShapelessRecipe) r;
List<ItemStack> ing = new ArrayList<ItemStack>(VerifyUtil.withoutNulls(shape.getIngredientList()));
if (ing.isEmpty()) {
return false; // If it's empty already, something is wrong with the recipe.
}
for (ItemStack it : inv.getContents()) {
if (!ItemUtil.isStackValid(it)) continue;
if(ing.isEmpty())
return false;
Iterator<ItemStack> ingIterator = ing.iterator();
while (ingIterator.hasNext()) {
if(ing.isEmpty())
break;
ItemStack stack = ingIterator.next();
if (!ItemUtil.isStackValid(stack)) {
ingIterator.remove();
continue;
}
if (ItemUtil.areItemsIdentical(it, stack)) {
ingIterator.remove();
break;
}
}
}
return ing.isEmpty();
} else
return false;
}
public static class Factory extends AbstractICFactory {
public Factory(Server server) {
super(server);
}
@Override
public IC create(ChangedSign sign) {
return new AutomaticCrafter(getServer(), sign, this);
}
@Override
public String getShortDescription() {
return "Auto-crafts recipes in the above dispenser/dropper.";
}
@Override
public String[] getLineHelp() {
return new String[] {null, null};
}
}
@Override
public void onPipeTransfer(PipePutEvent event) {
Block crafter = getBackBlock().getRelative(0, 1, 0);
if (crafter.getType() == Material.DISPENSER || crafter.getType() == Material.DROPPER) {
InventoryHolder disp = (InventoryHolder) crafter.getState();
boolean delete = true;
List<ItemStack> newItems = new ArrayList<ItemStack>(event.getItems());
for (ItemStack ite : event.getItems()) {
if (!ItemUtil.isStackValid(ite)) continue;
int iteind = newItems.indexOf(ite);
int newAmount = ite.getAmount();
for (int i = 0; i < ite.getAmount(); i++) {
ItemStack it = ItemUtil.getSmallestStackOfType(disp.getInventory().getContents(), ite);
if (!ItemUtil.isStackValid(it) || !ItemUtil.areItemsIdentical(ite, it)) continue;
if (it.getAmount() < 64) {
it.setAmount(it.getAmount() + 1);
newAmount -= 1;
} else {
if (newAmount > 0) {
delete = false;
break;
}
}
}
if (newAmount > 0) delete = false;
if(newAmount != ite.getAmount())
ite.setAmount(newAmount);
if (delete) newItems.remove(iteind);
else newItems.set(iteind, ite);
}
event.getItems().clear();
event.setItems(newItems);
}
}
}