/
RecipeManager.java
238 lines (192 loc) · 8.13 KB
/
RecipeManager.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
package com.sk89q.craftbook.mech.crafting;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import com.sk89q.craftbook.LocalConfiguration;
import com.sk89q.craftbook.bukkit.util.BukkitUtil;
import com.sk89q.craftbook.util.RegexUtil;
import com.sk89q.util.yaml.YAMLProcessor;
public class RecipeManager extends LocalConfiguration {
public static RecipeManager INSTANCE;
private Collection<Recipe> recipes;
protected final YAMLProcessor config;
protected final Logger logger;
public RecipeManager(YAMLProcessor config, Logger logger) {
INSTANCE = this;
this.config = config;
this.logger = logger;
load();
}
@Override
public void load() {
recipes = new ArrayList<Recipe>();
if (config == null) {
Bukkit.getLogger().severe("Failure loading recipes! Config is null!");
return; // If the config is null, it can't continue.
}
try {
config.load();
} catch (IOException e) {
e.printStackTrace();
}
List<String> keys = config.getKeys("crafting-recipes");
if (keys != null) {
for (String key : keys) {
try {
recipes.add(new Recipe(key, config));
} catch (InvalidCraftingException e) {
logger.warning(e.getMessage());
}
}
}
}
public Collection<Recipe> getRecipes() {
return recipes;
}
public static final class Recipe {
private final String id;
private final YAMLProcessor config;
private RecipeType type;
private Collection<CraftingItemStack> ingredients;
private HashMap<CraftingItemStack, Character> items;
private CraftingItemStack result;
private List<String> shape;
public boolean hasAdvancedData() {
for(CraftingItemStack stack : ingredients)
if(!stack.hasAdvancedData())
return true;
for(CraftingItemStack stack : items.keySet())
if(!stack.hasAdvancedData())
return true;
if(!result.hasAdvancedData())
return true;
return false;
}
private Recipe(String id, YAMLProcessor config) throws InvalidCraftingException {
this.id = id;
this.config = config;
ingredients = new ArrayList<CraftingItemStack>();
items = new HashMap<CraftingItemStack, Character>();
load();
}
private void load() throws InvalidCraftingException {
type = RecipeType.getTypeFromName(config.getString("crafting-recipes." + id + ".type"));
if (type != RecipeType.SHAPED2X2 && type != RecipeType.SHAPED3X3) {
ingredients = getItems("crafting-recipes." + id + ".ingredients");
} else {
items = getHashItems("crafting-recipes." + id + ".ingredients");
shape = config.getStringList("crafting-recipes." + id + ".shape", Arrays.asList(""));
}
Iterator<CraftingItemStack> iterator = getItems("crafting-recipes." + id + ".results").iterator();
if(iterator.hasNext())
result = iterator.next();
else
throw new InvalidCraftingException("Result is invalid in recipe: "+ id);
}
private HashMap<CraftingItemStack, Character> getHashItems(String path) {
HashMap<CraftingItemStack, Character> items = new HashMap<CraftingItemStack, Character>();
try {
for (Object oitem : config.getKeys(path)) {
String item = String.valueOf(oitem);
if (item == null || item.isEmpty()) continue;
String[] split = RegexUtil.COLON_PATTERN.split(item);
Material material;
try {
material = Material.getMaterial(Integer.parseInt(split[0]));
} catch (NumberFormatException e) {
// use the name
material = Material.getMaterial(split[0].toUpperCase());
}
if (material != null) {
CraftingItemStack itemStack = new CraftingItemStack(material);
if (split.length > 1) {
itemStack.setData(Short.parseShort(split[1]));
} else {
itemStack.setData((short) 0);
}
itemStack.setAmount(1);
items.put(itemStack, config.getString(path + "." + item, "a").charAt(0));
}
}
} catch (Exception e) {
Bukkit.getLogger().severe("An error occured generating ingredients for recipe: " + id);
BukkitUtil.printStacktrace(e);
}
return items;
}
private Collection<CraftingItemStack> getItems(String path) {
Collection<CraftingItemStack> items = new ArrayList<CraftingItemStack>();
try {
for (Object oitem : config.getKeys(path)) {
String item = String.valueOf(oitem);
if (item == null || item.isEmpty()) continue;
item = RegexUtil.PIPE_PATTERN.split(item)[0];
String[] split = RegexUtil.COLON_PATTERN.split(item);
Material material;
try {
material = Material.getMaterial(Integer.parseInt(split[0]));
} catch (NumberFormatException e) {
// use the name
material = Material.getMaterial(split[0].toUpperCase());
}
if (material != null) {
CraftingItemStack itemStack = new CraftingItemStack(material);
if (split.length > 1) {
itemStack.setData(Short.parseShort(split[1]));
} else {
itemStack.setData((short) 0);
}
itemStack.setAmount(config.getInt(path + "." + item, 1));
if(RegexUtil.PIPE_PATTERN.split(String.valueOf(oitem)).length > 1) {
itemStack.addAdvancedData("name", RegexUtil.PIPE_PATTERN.split(String.valueOf(oitem))[1]);
}
items.add(itemStack);
}
}
} catch (Exception e) {
Bukkit.getLogger().severe("An error occured generating ingredients for recipe: " + id);
BukkitUtil.printStacktrace(e);
}
return items;
}
public String getId() {
return id;
}
public RecipeType getType() {
return type;
}
public Collection<CraftingItemStack> getIngredients() {
return ingredients;
}
public String[] getShape() {
return shape.toArray(new String[shape.size()]);
}
public HashMap<CraftingItemStack, Character> getShapedIngredients() {
return items;
}
public CraftingItemStack getResult() {
return result;
}
public enum RecipeType {
SHAPELESS("Shapeless"), SHAPED3X3("Shaped3x3"), SHAPED2X2("Shaped2x2"), FURNACE("Furnace"), SHAPED("Shaped");
private String name;
private RecipeType(String name) {
this.name = name;
}
public String getName() {
return name;
}
public static RecipeType getTypeFromName(String name) {
for (RecipeType t : RecipeType.values()) { if (t.getName().equalsIgnoreCase(name)) return t; }
return SHAPELESS; // Default to shapeless
}
}
}
}