summaryrefslogtreecommitdiff
path: root/src/main/java/darkknight/jewelrycraft/util/JewelryNBT.java
blob: 7b74403bdbee96d7c878942cf45d12b993792c84 (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
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
package darkknight.jewelrycraft.util;

import java.util.ArrayList;

import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;

public class JewelryNBT {
	// TODO NBT Tag Adding
	public static void addItem(ItemStack item, ItemStack target) {
		if (target != null) {
			NBTTagCompound itemStackData;
			if (item.hasTagCompound())
				itemStackData = item.getTagCompound();
			else {
				itemStackData = new NBTTagCompound();
				item.setTagCompound(itemStackData);
			}
			NBTTagCompound targetNBT = new NBTTagCompound();
			target.writeToNBT(targetNBT);
			itemStackData.setTag("target", targetNBT);
		}
	}

	/**
	 * @param item
	 *            The item you want to add the NBT data on
	 * @param metal
	 *            The metal you want to add on the item
	 */
	public static void addMetal(ItemStack item, ItemStack metal) {
		NBTTagCompound itemStackData;
		if (item.hasTagCompound())
			itemStackData = item.getTagCompound();
		else {
			itemStackData = new NBTTagCompound();
			item.setTagCompound(itemStackData);
		}
		if (metal != null) {
			NBTTagCompound ingotNBT = new NBTTagCompound();
			metal.writeToNBT(ingotNBT);
			itemStackData.setTag("ingot", ingotNBT);
		}
	}

	/**
	 * @param item
	 *            The item you want to add the NBT data on
	 * @param gem
	 *            The gem you want to add on the item
	 */
	public static void addGem(ItemStack item, ItemStack gem) {
		if (gem != null) {
			NBTTagCompound itemStackData;
			if (item.hasTagCompound())
				itemStackData = item.getTagCompound();
			else {
				itemStackData = new NBTTagCompound();
				item.setTagCompound(itemStackData);
			}
			NBTTagCompound gemNBT = new NBTTagCompound();
			gem.writeToNBT(gemNBT);
			itemStackData.setTag("gem", gemNBT);
		}
	}

	/**
	 * @param item
	 *            The item you want to add the NBT data on
	 * @param modifier
	 *            The modifier you want to add on the item
	 */
	public static void addModifiers(ItemStack item,
			ArrayList<ItemStack> modifier) {
		if (modifier != null) {
			NBTTagCompound itemStackData;
			if (item.hasTagCompound())
				itemStackData = item.getTagCompound();
			else {
				itemStackData = new NBTTagCompound();
				item.setTagCompound(itemStackData);
			}
			for (int i = 0; i < modifier.size(); i++) {
				NBTTagCompound modifierNBT = new NBTTagCompound();
				modifier.get(i).writeToNBT(modifierNBT);
				itemStackData.setTag("modifier" + i, modifierNBT);
			}
			itemStackData.setInteger("modifierSize", modifier.size());
		}
	}

	/**
	 * @param item
	 * @param color
	 */
	public static void addIngotColor(ItemStack item, int color) {
		NBTTagCompound itemStackData;
		if (item.hasTagCompound())
			itemStackData = item.getTagCompound();
		else {
			itemStackData = new NBTTagCompound();
			item.setTagCompound(itemStackData);
		}
		itemStackData.setInteger("ingotColor", color);
	}

	// TODO
	/**
	 * @param item
	 * @param color
	 */
	public static void addGemColor(ItemStack item, int color) {
		NBTTagCompound itemStackData;
		if (item.hasTagCompound())
			itemStackData = item.getTagCompound();
		else {
			itemStackData = new NBTTagCompound();
			item.setTagCompound(itemStackData);
		}
		itemStackData.setInteger("gemColor", color);
	}

	// TODO NTB Tag Checking
	/**
	 * @param item
	 * @param tag
	 * @return
	 */
	public static boolean hasTag(ItemStack item, String tag) {
		NBTTagCompound itemStackData;
		if (item.hasTagCompound())
			itemStackData = item.getTagCompound();
		else {
			itemStackData = new NBTTagCompound();
			item.setTagCompound(itemStackData);
		}
		if (itemStackData.hasKey(tag))
			return true;
		return false;
	}

	/**
	 * @param stack
	 * @param gem
	 * @return
	 */
	public static boolean isGemX(ItemStack stack, ItemStack gem) {
		if (gem(stack) != null && gem(stack).getItem() == gem.getItem()
				&& gem(stack).getItemDamage() == gem.getItemDamage())
			return true;
		return false;
	}

	/**
	 * @param stack
	 * @param modifier
	 * @return
	 */
	public static boolean doesModifierExist(ItemStack stack,
			ItemStack modifier) {
		if (modifier(stack) != null) {
			ArrayList<ItemStack> list = modifier(stack);
			for (int i = 0; i < list.size(); i++)
				if (list.get(i).getItem() == modifier.getItem()
						&& list.get(i).getItemDamage() == modifier
								.getItemDamage())
					return true;
		}
		return false;
	}

	public static int modifierSize(ItemStack stack, ItemStack modifier) {
		if (modifier(stack) != null) {
			ArrayList<ItemStack> list = modifier(stack);
			for (int i = 0; i < list.size(); i++)
				if (list.get(i).getItem() == modifier.getItem()
						&& list.get(i).getItemDamage() == modifier
								.getItemDamage())
					return list.get(i).stackSize;
		}
		return -1;
	}

	public static int numberOfModifiers(ItemStack stack) {
		if (modifier(stack) != null)
			return modifier(stack).size();
		return -1;
	}

	/**
	 * @param stack
	 * @param ingot
	 * @return
	 */
	public static boolean isIngotX(ItemStack stack, ItemStack ingot) {
		if (ingot(stack) != null
				&& ingot(stack).getItem() == ingot.getItem()
				&& ingot(stack).getItemDamage() == ingot.getItemDamage())
			return true;
		return false;
	}

	// TODO Return components based on NBT
	public static ItemStack item(ItemStack stack) {
		if (stack != null
				&& stack != new ItemStack(Item.getItemById(0), 0, 0)
				&& stack.hasTagCompound()
				&& stack.getTagCompound().hasKey("target")) {
			NBTTagCompound itemNBT = (NBTTagCompound) stack
					.getTagCompound().getTag("target");
			ItemStack target = new ItemStack(Item.getItemById(0), 0, 0);
			target.readFromNBT(itemNBT);
			return target;
		}
		return null;
	}

	/**
	 * @param stack
	 * @return
	 */
	public static ItemStack gem(ItemStack stack) {
		if (stack != null
				&& stack != new ItemStack(Item.getItemById(0), 0, 0)
				&& stack.hasTagCompound()
				&& stack.getTagCompound().hasKey("gem")) {
			NBTTagCompound jewelNBT =
					(NBTTagCompound) stack.getTagCompound().getTag("gem");
			ItemStack gem = new ItemStack(Item.getItemById(0), 0, 0);
			gem.readFromNBT(jewelNBT);
			return gem;
		}
		return null;
	}

	/**
	 * @param stack
	 * @return
	 */
	public static ArrayList<ItemStack> modifier(ItemStack stack) {
		if (stack != null
				&& stack != new ItemStack(Item.getItemById(0), 0, 0)
				&& stack.hasTagCompound()) {
			int size = stack.getTagCompound().getInteger("modifierSize");
			ArrayList<ItemStack> list = new ArrayList<>();
			for (int i = 0; i < size; i++) {
				ItemStack modifier =
						new ItemStack(Item.getItemById(0), 0, 0);
				NBTTagCompound modifierNBT = (NBTTagCompound) stack
						.getTagCompound().getTag("modifier" + i);
				modifier.readFromNBT(modifierNBT);
				list.add(modifier);
			}
			return list;
		}
		return null;
	}

	/**
	 * @param stack
	 * @return
	 */
	public static ItemStack ingot(ItemStack stack) {
		if (stack != null
				&& stack != new ItemStack(Item.getItemById(0), 0, 0)
				&& stack.hasTagCompound()
				&& stack.getTagCompound().hasKey("ingot")) {
			NBTTagCompound ingotNBT = (NBTTagCompound) stack
					.getTagCompound().getTag("ingot");
			ItemStack ingot = new ItemStack(Item.getItemById(0), 0, 0);
			ingot.readFromNBT(ingotNBT);
			return ingot;
		}
		return null;
	}

	/**
	 * @param stack
	 * @return
	 */
	public static int ingotColor(ItemStack stack) {
		if (stack != null
				&& stack != new ItemStack(Item.getItemById(0), 0, 0)
				&& stack.hasTagCompound()
				&& stack.getTagCompound().hasKey("ingotColor"))
			return stack.getTagCompound().getInteger("ingotColor");
		return 16777215;
	}

	// TODO
	/**
	 * @param stack
	 * @return
	 */
	public static int gemColor(ItemStack stack) {
		if (stack != null
				&& stack != new ItemStack(Item.getItemById(0), 0, 0)
				&& stack.hasTagCompound()
				&& stack.getTagCompound().hasKey("gemColor"))
			return stack.getTagCompound().getInteger("gemColor");
		return 16777215;
	}

}