summaryrefslogtreecommitdiff
path: root/src/main/java/com/redstoner/utils/ItemProperties.java
blob: 13dc6203bdcb6813ca6730739a2b15f889016c3c (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
package com.redstoner.utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.BiConsumer;

import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

/** Save and load {@link ItemStack} in json format
 * Any additional NBT data not included by {@link ItemMeta} is discarded. */
public class ItemProperties
{
	private int id = 0;
	private Material type;
	private byte data = 0;
	private int amount = 1;
	private Map<Enchantment, Integer> enchantments;
	private List<String> lore;
	private String displayName;
	private boolean unbreakable = false;
	
	public ItemProperties()
	{}
	
	@SuppressWarnings("deprecation")
	public ItemProperties(ItemStack item)
	{
		if (item == null)
			return;
		id = item.getType().getId();
		type = item.getType();
		data = item.getData().getData();
		amount = item.getAmount();
		enchantments = new HashMap<>();
		ItemMeta meta = item.getItemMeta();
		if (meta == null)
			return;
		if (meta.hasEnchants())
		{
			enchantments.putAll(meta.getEnchants());
		}
		if (meta.hasLore())
		{
			lore = meta.getLore();
		}
		if (meta.hasDisplayName())
		{
			displayName = meta.getDisplayName();
		}
		unbreakable = meta.isUnbreakable();
	}
	
	@SuppressWarnings("deprecation")
	public ItemStack toItemStack()
	{
		ItemStack result = new ItemStack(type, amount, data);
		ItemMeta meta = result.getItemMeta();
		if (meta == null)
			return result;
		if (enchantments != null)
		{
			enchantments.forEach(new BiConsumer<Enchantment, Integer>()
			{
				@Override
				public void accept(Enchantment ench, Integer level)
				{
					meta.addEnchant(ench, level, true);
				}
			});
		}
		if (lore != null)
		{
			meta.setLore(lore);
		}
		if (displayName != null)
		{
			meta.setDisplayName(displayName);
		}
		meta.setUnbreakable(unbreakable);
		result.setItemMeta(meta);
		return result;
	}
	
	@SuppressWarnings({ "unchecked", "deprecation" })
	public JSONObject toJSONObject()
	{
		JSONObject object = new JSONObject();
		object.put("id", id + "");
		object.put("data", data + "");
		object.put("amount", amount + "");
		if (displayName != null)
		{
			object.put("displayName", displayName);
		}
		if (enchantments != null)
		{
			Map<Enchantment, Integer> enchantments = this.enchantments;
			JSONObject stringKeys = new JSONObject();
			for (Map.Entry<Enchantment, Integer> entry : enchantments.entrySet())
			{
				stringKeys.put(entry.getKey().getName(), entry.getValue());
			}
			object.put("enchantments", stringKeys);
		}
		if (lore != null)
		{
			object.put("lore", JSONArray.toJSONString(lore));
		}
		if (unbreakable)
		{
			object.put("unbreakable", true);
		}
		return object;
	}
	
	@Override
	public String toString()
	{
		return toJSONObject().toString();
	}
	
	@SuppressWarnings({ "unchecked", "deprecation" })
	public ItemProperties loadFrom(JSONObject object)
	{
		for (Object obj : object.entrySet())
		{
			Entry<String, Object> entry = (Entry<String, Object>) obj;
			final String key = entry.getKey();
			switch (key)
			{
				case "id":
					id = Integer.parseInt((String) entry.getValue());
					break;
				case "data":
					data = Byte.parseByte((String) entry.getValue());
					break;
				case "amount":
					amount = Integer.parseInt((String) entry.getValue());
					break;
				case "unbreakable":
					unbreakable = (boolean) entry.getValue();
					break;
				case "enchantments":
				{
					if (enchantments == null)
					{
						enchantments = new HashMap<>();
					}
					else if (!enchantments.isEmpty())
					{
						enchantments.clear();
					}
					JSONObject read = (JSONObject) entry.getValue();
					if (read != null)
					{
						for (Object obj2 : read.entrySet())
						{
							Entry<String, Integer> entry2 = (Entry<String, Integer>) obj2;
							Enchantment ench = Enchantment.getByName(entry2.getKey());
							if (ench != null)
							{
								enchantments.put(ench, entry2.getValue());
							}
						}
					}
					break;
				}
				case "lore":
					JSONParser parser = new JSONParser();
					Object rawObject;
					try
					{
						rawObject = parser.parse((String) entry.getValue());
					}
					catch (ParseException e)
					{
						rawObject = new JSONArray();
					}
					JSONArray jsonArray = (JSONArray) rawObject;
					lore = jsonArray;
					break;
				case "displayName":
					displayName = (String) entry.getValue();
				default:
			}
		}
		return this;
	}
	
	public int getId()
	{
		return id;
	}
	
	public byte getData()
	{
		return data;
	}
	
	public int getAmount()
	{
		return amount;
	}
	
	public Map<Enchantment, Integer> getEnchantments()
	{
		return enchantments;
	}
	
	public List<String> getLore()
	{
		return lore;
	}
	
	public String getDisplayName()
	{
		return displayName;
	}
	
	public boolean isUnbreakable()
	{
		return unbreakable;
	}
	
	public ItemProperties setId(int id)
	{
		this.id = id;
		return this;
	}
	
	public ItemProperties setData(byte data)
	{
		this.data = data;
		return this;
	}
	
	public ItemProperties setAmount(int amount)
	{
		this.amount = amount;
		return this;
	}
	
	public ItemProperties setEnchantments(Map<Enchantment, Integer> enchantments)
	{
		this.enchantments = enchantments;
		return this;
	}
	
	public ItemProperties setLore(List<String> lore)
	{
		this.lore = lore;
		return this;
	}
	
	public ItemProperties setDisplayName(String displayName)
	{
		this.displayName = displayName;
		return this;
	}
	
	public ItemProperties setUnbreakable(boolean unbreakable)
	{
		this.unbreakable = unbreakable;
		return this;
	}
}