summaryrefslogtreecommitdiff
path: root/TF2 Crates/src/main/java/tf2crates/crate/RandomTinkersTool.java
blob: c79ea863deefcadce90482ea78fa537d0e755515 (plain)
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
package tf2crates.crate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import tconstruct.library.TConstructRegistry;
import tconstruct.library.crafting.ModifyBuilder;
import tconstruct.library.crafting.ToolBuilder;
import tconstruct.library.crafting.ToolRecipe;
import tconstruct.library.modifier.ItemModifier;
import tconstruct.library.tools.ToolCore;
import tconstruct.library.tools.ToolMaterial;
import tconstruct.library.util.IToolPart;
import tlhpoeCore.util.MathUtil;

public class RandomTinkersTool extends RandomLoot {
	private boolean				isLegendary;

	private ToolRecipe			toolType;

	private ArrayList<Integer>	materialIDs;

	public RandomTinkersTool(boolean isLegendary, ToolRecipe toolType) {
		super(null, isLegendary);

		this.isLegendary = isLegendary;
		this.toolType = toolType;
	}

	@Override
	public String getDisplayName() {
		return "A Random " + (isLegendary ? " Legendary" : "")
				+ " Tinker's " + toolType.getType().getLocalizedToolName();
	}

	@Override
	public ItemStack getLoot() {
		if (materialIDs == null) {
			prepareMaterials();
		}

		// determine type
		ToolRecipe recipe = toolType;
		ToolCore type = recipe.getType();

		ItemStack tool = null;
		int tries = 0;
		// try to build the tool
		do {
			tries++;
			// get components
			ItemStack[] parts = new ItemStack[] { null, null, null, null };

			Item[] items = new Item[4];
			items[0] = type.getHeadItem();
			items[1] = type.getHandleItem();
			items[2] = type.getAccessoryItem();
			items[3] = type.getExtraItem();

			for (int i = 0; i < 4; i++) {
				if (items[i] == null) {
					continue;
				}

				do {
					// get a material
					Integer matId = materialIDs
							.get(MathUtil.nextInt(materialIDs.size()));

					parts[i] = new ItemStack(items[i], 1, matId);
				} while (((IToolPart) items[i])
						.getMaterialID(parts[i]) == -1);
			}
			// build the tool
			tool = ToolBuilder.instance.buildTool(parts[0], parts[1],
					parts[2], parts[3], "");
		} while (tool == null && tries < 200);

		if (tool == null) {
			ItemStack oops = new ItemStack(Items.stick);

			oops.setStackDisplayName("Crate Malfunction");

			return oops;
		}

		int modCount = MathUtil.nextInt(3);

		for (int i = modCount; i > 0; i--) {
			doModifyWeapon(tool);
		}

		if (isLegendary) {
			EnchantmentHelper.addRandomEnchantment(MathUtil.getRandom(),
					tool, MathUtil.getRandomIntegerBetween(25, 30));
		}

		return tool;
	}

	private void doModifyWeapon(ItemStack tool) {
		List<ItemModifier> modifiers = ModifyBuilder.instance.itemModifiers;

		ItemModifier mod = modifiers
				.get(MathUtil.nextInt(modifiers.size()));

		ItemStack[] stacks = stackFromModifier(mod);

		if (!mod.matches(stacks, tool)) {
			return;
		}

		mod.addMatchingEffect(tool);

		mod.modify(stacks, tool);
	}

	private static ItemStack[] stackFromModifier(ItemModifier modifier) {
		ItemStack[] stack = new ItemStack[modifier.stacks.size()];
		int i = 0;
		for (Object s : modifier.stacks)
			stack[i++] = (ItemStack) s;

		return stack;
	}

	private void prepareMaterials() {
		materialIDs = new ArrayList<Integer>(); // converted to list for
												// indexed access for
												// random

		for (Map.Entry<Integer,
				ToolMaterial> entry : TConstructRegistry.toolMaterials
						.entrySet()) {
			materialIDs.add(entry.getKey());
		}
	}
}