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
|
package com.earth2me.essentials;
import static com.earth2me.essentials.I18n.tl;
import com.earth2me.essentials.utils.NumberUtil;
import com.earth2me.essentials.utils.StringUtil;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.ess3.api.IEssentials;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
public class ItemDb implements IConf, net.ess3.api.IItemDb
{
private final transient IEssentials ess;
private final transient Map<String, Integer> items = new HashMap<String, Integer>();
private final transient Map<ItemData, List<String>> names = new HashMap<ItemData, List<String>>();
private final transient Map<ItemData, String> primaryName = new HashMap<ItemData, String>();
private final transient Map<String, Short> durabilities = new HashMap<String, Short>();
private final transient ManagedFile file;
private final transient Pattern splitPattern = Pattern.compile("((.*)[:+',;.](\\d+))");
public ItemDb(final IEssentials ess)
{
this.ess = ess;
file = new ManagedFile("items.csv", ess);
}
@Override
public void reloadConfig()
{
final List<String> lines = file.getLines();
if (lines.isEmpty())
{
return;
}
durabilities.clear();
items.clear();
names.clear();
primaryName.clear();
for (String line : lines)
{
line = line.trim().toLowerCase(Locale.ENGLISH);
if (line.length() > 0 && line.charAt(0) == '#')
{
continue;
}
final String[] parts = line.split("[^a-z0-9]");
if (parts.length < 2)
{
continue;
}
final int numeric = Integer.parseInt(parts[1]);
final short data = parts.length > 2 && !parts[2].equals("0") ? Short.parseShort(parts[2]) : 0;
String itemName = parts[0].toLowerCase(Locale.ENGLISH);
durabilities.put(itemName, data);
items.put(itemName, numeric);
ItemData itemData = new ItemData(numeric, data);
if (names.containsKey(itemData))
{
List<String> nameList = names.get(itemData);
nameList.add(itemName);
Collections.sort(nameList, new LengthCompare());
}
else
{
List<String> nameList = new ArrayList<String>();
nameList.add(itemName);
names.put(itemData, nameList);
primaryName.put(itemData, itemName);
}
}
}
@Override
public ItemStack get(final String id, final int quantity) throws Exception
{
final ItemStack retval = get(id.toLowerCase(Locale.ENGLISH));
retval.setAmount(quantity);
return retval;
}
@Override
public ItemStack get(final String id) throws Exception
{
int itemid = 0;
String itemname = null;
short metaData = 0;
Matcher parts = splitPattern.matcher(id);
if (parts.matches())
{
itemname = parts.group(2);
metaData = Short.parseShort(parts.group(3));
}
else
{
itemname = id;
}
if (NumberUtil.isInt(itemname))
{
itemid = Integer.parseInt(itemname);
}
else if (NumberUtil.isInt(id))
{
itemid = Integer.parseInt(id);
}
else
{
itemname = itemname.toLowerCase(Locale.ENGLISH);
}
if (itemid < 1)
{
if (items.containsKey(itemname))
{
itemid = items.get(itemname);
if (durabilities.containsKey(itemname) && metaData == 0)
{
metaData = durabilities.get(itemname);
}
}
else if (Material.getMaterial(itemname.toUpperCase(Locale.ENGLISH)) != null)
{
Material bMaterial = Material.getMaterial(itemname.toUpperCase(Locale.ENGLISH));
itemid = bMaterial.getId();
}
else
{
try
{
Material bMaterial = Bukkit.getUnsafe().getMaterialFromInternalName(itemname.toLowerCase(Locale.ENGLISH));
itemid = bMaterial.getId();
}
catch (Throwable throwable)
{
throw new Exception(tl("unknownItemName", itemname), throwable);
}
}
}
if (itemid < 1)
{
throw new Exception(tl("unknownItemName", itemname));
}
final Material mat = Material.getMaterial(itemid);
if (mat == null)
{
throw new Exception(tl("unknownItemId", itemid));
}
final ItemStack retval = new ItemStack(mat);
retval.setAmount(mat.getMaxStackSize());
retval.setDurability(metaData);
return retval;
}
@Override
public List<ItemStack> getMatching(User user, String[] args) throws Exception
{
List<ItemStack> is = new ArrayList<ItemStack>();
if (args.length < 1)
{
is.add(user.getBase().getItemInHand());
}
else if (args[0].equalsIgnoreCase("hand"))
{
is.add(user.getBase().getItemInHand());
}
else if (args[0].equalsIgnoreCase("inventory") || args[0].equalsIgnoreCase("invent") || args[0].equalsIgnoreCase("all"))
{
for (ItemStack stack : user.getBase().getInventory().getContents())
{
if (stack == null || stack.getType() == Material.AIR)
{
continue;
}
is.add(stack);
}
}
else if (args[0].equalsIgnoreCase("blocks"))
{
for (ItemStack stack : user.getBase().getInventory().getContents())
{
if (stack == null || stack.getTypeId() > 255 || stack.getType() == Material.AIR)
{
continue;
}
is.add(stack);
}
}
else
{
is.add(get(args[0]));
}
if (is.isEmpty() || is.get(0).getType() == Material.AIR)
{
throw new Exception(tl("itemSellAir"));
}
return is;
}
@Override
public String names(ItemStack item)
{
ItemData itemData = new ItemData(item.getTypeId(), item.getDurability());
List<String> nameList = names.get(itemData);
if (nameList == null)
{
itemData = new ItemData(item.getTypeId(), (short)0);
nameList = names.get(itemData);
if (nameList == null)
{
return null;
}
}
if (nameList.size() > 15)
{
nameList = nameList.subList(0, 14);
}
return StringUtil.joinList(", ", nameList);
}
@Override
public String name(ItemStack item)
{
ItemData itemData = new ItemData(item.getTypeId(), item.getDurability());
String name = primaryName.get(itemData);
if (name == null)
{
itemData = new ItemData(item.getTypeId(), (short)0);
name = primaryName.get(itemData);
if (name == null)
{
return null;
}
}
return name;
}
static class ItemData
{
final private int itemNo;
final private short itemData;
ItemData(final int itemNo, final short itemData)
{
this.itemNo = itemNo;
this.itemData = itemData;
}
public int getItemNo()
{
return itemNo;
}
public short getItemData()
{
return itemData;
}
@Override
public int hashCode()
{
return (31 * itemNo) ^ itemData;
}
@Override
public boolean equals(Object o)
{
if (o == null)
{
return false;
}
if (!(o instanceof ItemData))
{
return false;
}
ItemData pairo = (ItemData)o;
return this.itemNo == pairo.getItemNo()
&& this.itemData == pairo.getItemData();
}
}
class LengthCompare implements java.util.Comparator<String>
{
public LengthCompare()
{
super();
}
@Override
public int compare(String s1, String s2)
{
return s1.length() - s2.length();
}
}
}
|