-
Notifications
You must be signed in to change notification settings - Fork 753
/
ToolBuilder.java
434 lines (370 loc) · 14.9 KB
/
ToolBuilder.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
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
package tconstruct.library.crafting;
/** Once upon a time, too many tools to count. Let's put them together automatically */
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.MinecraftForge;
import tconstruct.library.TConstructRegistry;
import tconstruct.library.event.ToolBuildEvent;
import tconstruct.library.event.ToolCraftEvent;
import tconstruct.library.modifier.ItemModifier;
import tconstruct.library.tools.ToolCore;
import tconstruct.library.tools.ToolMaterial;
import tconstruct.library.util.IToolPart;
import cpw.mods.fml.common.eventhandler.Event.Result;
public class ToolBuilder
{
public static ToolBuilder instance = new ToolBuilder();
public HashMap<String, ToolRecipe> recipeList = new HashMap<String, ToolRecipe>();
public List<ToolRecipe> combos = new ArrayList<ToolRecipe>();
public HashMap<String, String> modifiers = new HashMap<String, String>();
/* Build tools */
public static void addNormalToolRecipe (ToolCore output, Item head, Item handle)
{
ToolRecipe recipe = instance.recipeList.get(output.getToolName());
if (recipe != null)
{
recipe.addHeadItem(head);
recipe.addHandleItem(handle);
}
else
{
recipe = new ToolRecipe(head, handle, output);
instance.combos.add(recipe);
instance.recipeList.put(output.getToolName(), recipe);
}
}
public static void addNormalToolRecipe (ToolCore output, Item head, Item handle, Item accessory)
{
//instance.combos.add(new ToolRecipe(head, accessory, output));
ToolRecipe recipe = instance.recipeList.get(output.getToolName());
if (recipe != null)
{
recipe.addHeadItem(head);
recipe.addHandleItem(handle);
recipe.addAccessoryItem(accessory);
}
else
{
recipe = new ToolRecipe(head, handle, accessory, output);
instance.combos.add(recipe);
instance.recipeList.put(output.getToolName(), recipe);
}
}
public static void addNormalToolRecipe (ToolCore output, Item head, Item handle, Item accessory, Item extra)
{
ToolRecipe recipe = instance.recipeList.get(output.getToolName());
if (recipe != null)
{
recipe.addHeadItem(head);
recipe.addHandleItem(handle);
recipe.addAccessoryItem(accessory);
recipe.addExtraItem(extra);
}
else
{
recipe = new ToolRecipe(head, handle, accessory, extra, output);
instance.combos.add(recipe);
instance.recipeList.put(output.getToolName(), recipe);
}
}
public static void addCustomToolRecipe (ToolRecipe recipe)
{
instance.combos.add(recipe);
}
public static void addToolRecipe (ToolCore output, Item... items)
{
if (items.length == 2)
addNormalToolRecipe(output, items[0], items[1]);
if (items.length == 3)
addNormalToolRecipe(output, items[0], items[1], items[2]);
if (items.length == 4)
addNormalToolRecipe(output, items[0], items[1], items[2], items[3]);
}
public ToolCore getMatchingRecipe (Item head, Item handle, Item accessory, Item extra)
{
for (ToolRecipe recipe : combos)
{
if (recipe.validHead(head) && recipe.validHandle(handle) && recipe.validAccessory(accessory) && recipe.validExtra(extra))
return recipe.getType();
}
return null;
}
//Builds a tool from the parts given
public ItemStack buildTool (ItemStack headStack, ItemStack handleStack, ItemStack accessoryStack, String name)
{
return buildTool(headStack, handleStack, accessoryStack, null, name);
}
public int getMaterialID (ItemStack stack)
{
if (stack == null)
return -1;
Item item = stack.getItem();
if (item instanceof IToolPart)
return ((IToolPart) item).getMaterialID(stack);
return -1;
}
public ItemStack buildTool (ItemStack headStack, ItemStack handleStack, ItemStack accessoryStack, ItemStack extraStack, String name)
{
/*if (headStack != null && headStack.getItem() instanceof ToolCore)
return modifyTool(headStack, new ItemStack[] { handleStack, accessoryStack, extraStack }, name);*/
if (headStack == null || handleStack == null) //Nothing to build without these. All tools need at least two parts!
return null;
if(name == null)
name = "";
// fire the ToolBuild event to get the correct items
ToolBuildEvent buildEvent = new ToolBuildEvent(headStack, handleStack, accessoryStack, extraStack, name);
MinecraftForge.EVENT_BUS.post(buildEvent);
// copy back the items
headStack = buildEvent.headStack;
handleStack = buildEvent.handleStack;
accessoryStack = buildEvent.accessoryStack;
extraStack = buildEvent.extraStack;
name = buildEvent.name;
ToolCore item;
boolean validMaterials = true;
int head = -1, handle = -1, accessory = -1, extra = -1;
head = getMaterialID(headStack);
if (head == -1)
validMaterials = false;
handle = getMaterialID(handleStack);
if (handle == -1)
validMaterials = false;
if (!validMaterials)
return null;
if (accessoryStack == null)
{
item = getMatchingRecipe(headStack.getItem(), handleStack.getItem(), null, null);
}
else
{
accessory = getMaterialID(accessoryStack);
if (accessory == -1)
return null;
if (extraStack != null)
{
extra = getMaterialID(extraStack);
if (extra == -1)
return null;
item = getMatchingRecipe(headStack.getItem(), handleStack.getItem(), accessoryStack.getItem(), extraStack.getItem());
}
else
{
item = getMatchingRecipe(headStack.getItem(), handleStack.getItem(), accessoryStack.getItem(), null);
}
}
if (item == null)
return null;
ToolMaterial headMat = null, handleMat = null, accessoryMat = null, extraMat = null;
headMat = TConstructRegistry.getMaterial(head);
handleMat = TConstructRegistry.getMaterial(handle);
if (accessory != -1)
accessoryMat = TConstructRegistry.getMaterial(accessory);
if (extra != -1)
extraMat = TConstructRegistry.getMaterial(extra);
int durability = headMat.durability();
int heads = 1;
int handles = 0;
float modifier = 1f;
int attack = headMat.attack();
if (item.durabilityTypeHandle() == 2)
{
heads++;
durability += handleMat.durability();
attack += handleMat.attack();
}
else if (item.durabilityTypeHandle() == 1)
{
handles++;
modifier = handleMat.handleDurability();
}
if (accessory != -1)
{
if (item.durabilityTypeAccessory() == 2)
{
heads++;
durability += accessoryMat.durability();
attack += accessoryMat.attack();
}
else if (item.durabilityTypeAccessory() == 1)
{
handles++;
modifier += accessoryMat.handleDurability();
}
}
if (extra != -1)
{
if (item.durabilityTypeExtra() == 2)
{
heads++;
durability += extraMat.durability();
attack += extraMat.attack();
}
else if (item.durabilityTypeExtra() == 1)
{
handles++;
modifier += extraMat.handleDurability();
}
}
if (handles > 0)
{
modifier *= (0.5 + handles * 0.5);
modifier /= handles;
}
durability = (int) (durability / heads * (0.5 + heads * 0.5) * modifier * item.getDurabilityModifier());
attack = attack / heads + item.getDamageVsEntity(null);
if (attack % heads != 0)
attack++;
ItemStack tool = new ItemStack(item);
NBTTagCompound compound = new NBTTagCompound();
compound.setTag("InfiTool", new NBTTagCompound());
compound.getCompoundTag("InfiTool").setInteger("Head", head);
compound.getCompoundTag("InfiTool").setInteger("RenderHead", head);
compound.getCompoundTag("InfiTool").setInteger("Handle", handle);
compound.getCompoundTag("InfiTool").setInteger("RenderHandle", handle);
if (accessory != -1)
{
compound.getCompoundTag("InfiTool").setInteger("Accessory", accessory);
compound.getCompoundTag("InfiTool").setInteger("RenderAccessory", accessory);
}
if (extra != -1)
{
compound.getCompoundTag("InfiTool").setInteger("Extra", extra);
compound.getCompoundTag("InfiTool").setInteger("RenderExtra", extra);
}
compound.getCompoundTag("InfiTool").setInteger("Damage", 0); //Damage is damage to the tool
compound.getCompoundTag("InfiTool").setInteger("TotalDurability", durability);
compound.getCompoundTag("InfiTool").setInteger("BaseDurability", durability);
compound.getCompoundTag("InfiTool").setInteger("BonusDurability", 0); //Modifier
compound.getCompoundTag("InfiTool").setFloat("ModDurability", 0f); //Modifier
compound.getCompoundTag("InfiTool").setBoolean("Broken", false);
compound.getCompoundTag("InfiTool").setInteger("Attack", attack);
compound.getCompoundTag("InfiTool").setInteger("MiningSpeed", headMat.toolSpeed());
compound.getCompoundTag("InfiTool").setInteger("HarvestLevel", headMat.harvestLevel());
if (item.durabilityTypeHandle() == 2)
{
compound.getCompoundTag("InfiTool").setInteger("MiningSpeedHandle", handleMat.toolSpeed());
compound.getCompoundTag("InfiTool").setInteger("HarvestLevelHandle", handleMat.harvestLevel());
}
if (accessory != -1 && item.durabilityTypeAccessory() == 2)
{
compound.getCompoundTag("InfiTool").setInteger("MiningSpeed2", accessoryMat.toolSpeed());
compound.getCompoundTag("InfiTool").setInteger("HarvestLevel2", accessoryMat.harvestLevel());
}
if (extra != -1 && item.durabilityTypeExtra() == 2)
{
compound.getCompoundTag("InfiTool").setInteger("MiningSpeedExtra", extraMat.toolSpeed());
compound.getCompoundTag("InfiTool").setInteger("HarvestLevelExtra", extraMat.harvestLevel());
}
compound.getCompoundTag("InfiTool").setInteger("Unbreaking", buildReinforced(headMat, handleMat, accessoryMat, extraMat));
compound.getCompoundTag("InfiTool").setFloat("Shoddy", buildShoddy(headMat, handleMat, accessoryMat, extraMat));
int modifiers = item.getModifierAmount();
if (accessory == -1)
modifiers += (head == 9 ? 2 : 0);
else
modifiers += (head == 9 ? 1 : 0) + (accessory == 9 ? 1 : 0);
modifiers += +(handle == 9 ? 1 : 0);
modifiers += +(extra == 9 ? 1 : 0);
compound.getCompoundTag("InfiTool").setInteger("Modifiers", modifiers);
if (name != null && !name.equals(""))
{
compound.setTag("display", new NBTTagCompound());
compound.getCompoundTag("display").setString("Name", "\u00A7f" + name);
}
ToolCraftEvent.NormalTool event = new ToolCraftEvent.NormalTool(item, compound, new ToolMaterial[] { headMat, handleMat, accessoryMat, extraMat });
MinecraftForge.EVENT_BUS.post(event);
if (event.getResult() == Result.DEFAULT)
{
tool.setTagCompound(compound);
}
else if (event.getResult() == Result.ALLOW)
{
tool = event.getResultStack();
}
else
{
tool = null;
}
return tool;
}
/*@Deprecated
public ItemStack modifyTool (ItemStack input, ItemStack topSlot, ItemStack bottomSlot, ItemStack extraStack, String name)
{
if (extraStack != null)
return null;
ItemStack tool = input.copy();
NBTTagCompound tags = tool.getTagCompound().getCompoundTag("InfiTool");
tags.removeTag("Built");
if (topSlot == null && bottomSlot == null)
return tool;
boolean built = false;
for (ToolMod mod : toolMods)
{
ItemStack[] slots = new ItemStack[] { topSlot, bottomSlot };
if (mod.matches(slots, tool))
{
built = true;
mod.addMatchingEffect(tool);
mod.modify(slots, tool);
}
}
tags = tool.getTagCompound();
if (name != null && !name.equals("") && !tags.hasKey("display"))
{
tags.setCompoundTag("display", new NBTTagCompound());
tags.getCompoundTag("display").setString("Name", "\u00A7f" + name);
}
if (built)
return tool;
else
return null;
}*/
int buildReinforced (ToolMaterial headMat, ToolMaterial handleMat, ToolMaterial accessoryMat, ToolMaterial extraMat)
{
int reinforced = 0;
int dHead = headMat.reinforced();
int dHandle = handleMat.reinforced();
int dAccessory = 0;
if (accessoryMat != null)
dAccessory = accessoryMat.reinforced();
int dExtra = 0;
if (extraMat != null)
dExtra = extraMat.reinforced();
if (dHead > reinforced)
reinforced = dHead;
if (dHandle > reinforced)
reinforced = dHandle;
if (dAccessory > reinforced)
reinforced = dAccessory;
if (dExtra > reinforced)
reinforced = dExtra;
return reinforced;
}
float buildShoddy (ToolMaterial headMat, ToolMaterial handleMat, ToolMaterial accessoryMat, ToolMaterial extraMat)
{
float sHead = headMat.shoddy();
float sHandle = handleMat.shoddy();
if (extraMat != null)
{
float sAccessory = accessoryMat.shoddy();
float sExtra = extraMat.shoddy();
return (sHead + sHandle + sAccessory + sExtra) / 4f;
}
if (accessoryMat != null)
{
float sAccessory = accessoryMat.shoddy();
return (sHead + sHandle + sAccessory) / 3f;
}
return (sHead + sHandle) / 2f;
}
//Passthrough for now
@Deprecated
public static void registerToolMod(ItemModifier mod)
{
ModifyBuilder.registerModifier(mod);
}
}