2011-03-19 22:39:51 +00:00
|
|
|
package com.earth2me.essentials;
|
|
|
|
|
2013-07-13 15:14:39 +00:00
|
|
|
import net.ess3.api.IEssentials;
|
2013-06-08 21:31:19 +00:00
|
|
|
import com.earth2me.essentials.utils.StringUtil;
|
2011-11-21 01:55:26 +00:00
|
|
|
import static com.earth2me.essentials.I18n._;
|
2013-06-08 21:31:19 +00:00
|
|
|
import com.earth2me.essentials.utils.NumberUtil;
|
2012-09-08 17:33:06 +00:00
|
|
|
import java.util.*;
|
2013-01-07 15:26:09 +00:00
|
|
|
import java.util.regex.Pattern;
|
2011-05-01 19:09:38 +00:00
|
|
|
import org.bukkit.Material;
|
2011-03-19 22:39:51 +00:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
|
|
|
|
|
2013-07-13 16:52:08 +00:00
|
|
|
public class ItemDb implements IConf, net.ess3.api.IItemDb
|
2011-03-19 22:39:51 +00:00
|
|
|
{
|
2011-07-15 23:33:22 +00:00
|
|
|
private final transient IEssentials ess;
|
|
|
|
|
2011-09-17 22:49:34 +00:00
|
|
|
public ItemDb(final IEssentials ess)
|
2011-06-01 10:40:12 +00:00
|
|
|
{
|
2011-07-15 23:33:22 +00:00
|
|
|
this.ess = ess;
|
2011-09-17 22:49:34 +00:00
|
|
|
file = new ManagedFile("items.csv", ess);
|
2011-06-01 10:40:12 +00:00
|
|
|
}
|
2011-07-15 23:33:22 +00:00
|
|
|
private final transient Map<String, Integer> items = new HashMap<String, Integer>();
|
2012-09-08 17:33:06 +00:00
|
|
|
private final transient Map<ItemData, List<String>> names = new HashMap<ItemData, List<String>>();
|
2011-07-15 23:33:22 +00:00
|
|
|
private final transient Map<String, Short> durabilities = new HashMap<String, Short>();
|
2011-09-17 22:49:34 +00:00
|
|
|
private final transient ManagedFile file;
|
2013-01-07 15:26:09 +00:00
|
|
|
private final transient Pattern splitPattern = Pattern.compile("[:+',;.]");
|
2011-03-19 22:39:51 +00:00
|
|
|
|
2011-09-17 22:49:34 +00:00
|
|
|
@Override
|
2011-07-15 23:33:22 +00:00
|
|
|
public void reloadConfig()
|
2011-03-19 22:39:51 +00:00
|
|
|
{
|
2011-09-17 22:49:34 +00:00
|
|
|
final List<String> lines = file.getLines();
|
2011-11-21 01:55:26 +00:00
|
|
|
|
|
|
|
if (lines.isEmpty())
|
|
|
|
{
|
2011-09-17 22:49:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
durabilities.clear();
|
|
|
|
items.clear();
|
2012-09-08 17:33:06 +00:00
|
|
|
names.clear();
|
2011-03-19 22:39:51 +00:00
|
|
|
|
2011-09-17 22:49:34 +00:00
|
|
|
for (String line : lines)
|
2011-03-19 22:39:51 +00:00
|
|
|
{
|
2011-11-21 01:55:26 +00:00
|
|
|
line = line.trim().toLowerCase(Locale.ENGLISH);
|
2011-09-17 22:49:34 +00:00
|
|
|
if (line.length() > 0 && line.charAt(0) == '#')
|
2011-03-19 22:39:51 +00:00
|
|
|
{
|
2011-09-17 22:49:34 +00:00
|
|
|
continue;
|
2011-03-19 22:39:51 +00:00
|
|
|
}
|
|
|
|
|
2011-09-17 22:49:34 +00:00
|
|
|
final String[] parts = line.split("[^a-z0-9]");
|
|
|
|
if (parts.length < 2)
|
2011-03-19 22:39:51 +00:00
|
|
|
{
|
2011-09-17 22:49:34 +00:00
|
|
|
continue;
|
|
|
|
}
|
2011-05-09 00:22:45 +00:00
|
|
|
|
2011-09-17 22:49:34 +00:00
|
|
|
final int numeric = Integer.parseInt(parts[1]);
|
2012-09-08 17:33:06 +00:00
|
|
|
final short data = parts.length > 2 && !parts[2].equals("0") ? Short.parseShort(parts[2]) : 0;
|
|
|
|
String itemName = parts[0].toLowerCase(Locale.ENGLISH);
|
2011-05-09 00:22:45 +00:00
|
|
|
|
2012-09-08 17:33:06 +00:00
|
|
|
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);
|
|
|
|
}
|
2011-03-19 22:39:51 +00:00
|
|
|
}
|
|
|
|
}
|
2011-05-09 00:22:45 +00:00
|
|
|
|
2013-04-30 16:25:25 +00:00
|
|
|
@Override
|
2011-07-15 23:33:22 +00:00
|
|
|
public ItemStack get(final String id, final int quantity) throws Exception
|
2011-05-09 00:22:45 +00:00
|
|
|
{
|
2011-11-21 01:55:26 +00:00
|
|
|
final ItemStack retval = get(id.toLowerCase(Locale.ENGLISH));
|
2011-03-19 22:39:51 +00:00
|
|
|
retval.setAmount(quantity);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2013-04-30 16:25:25 +00:00
|
|
|
@Override
|
2011-07-15 23:33:22 +00:00
|
|
|
public ItemStack get(final String id) throws Exception
|
2011-03-19 22:39:51 +00:00
|
|
|
{
|
2011-05-09 00:48:21 +00:00
|
|
|
int itemid = 0;
|
|
|
|
String itemname = null;
|
2011-05-09 00:22:45 +00:00
|
|
|
short metaData = 0;
|
2013-04-30 16:25:25 +00:00
|
|
|
String[] parts = splitPattern.split(id);
|
2011-05-09 00:48:21 +00:00
|
|
|
if (id.matches("^\\d+[:+',;.]\\d+$"))
|
2011-05-07 13:03:45 +00:00
|
|
|
{
|
2013-01-07 15:26:09 +00:00
|
|
|
itemid = Integer.parseInt(parts[0]);
|
|
|
|
metaData = Short.parseShort(parts[1]);
|
2011-05-09 00:22:45 +00:00
|
|
|
}
|
2013-06-08 21:31:19 +00:00
|
|
|
else if (NumberUtil.isInt(id))
|
2011-05-09 00:22:45 +00:00
|
|
|
{
|
|
|
|
itemid = Integer.parseInt(id);
|
|
|
|
}
|
2011-05-09 00:48:21 +00:00
|
|
|
else if (id.matches("^[^:+',;.]+[:+',;.]\\d+$"))
|
2011-05-09 00:22:45 +00:00
|
|
|
{
|
2013-01-07 15:26:09 +00:00
|
|
|
itemname = parts[0].toLowerCase(Locale.ENGLISH);
|
|
|
|
metaData = Short.parseShort(parts[1]);
|
2011-05-07 13:03:45 +00:00
|
|
|
}
|
2011-07-15 23:33:22 +00:00
|
|
|
else
|
2011-05-07 13:03:45 +00:00
|
|
|
{
|
2011-11-21 01:55:26 +00:00
|
|
|
itemname = id.toLowerCase(Locale.ENGLISH);
|
2011-05-09 00:48:21 +00:00
|
|
|
}
|
2011-07-15 23:33:22 +00:00
|
|
|
|
2011-05-09 00:48:21 +00:00
|
|
|
if (itemname != null)
|
|
|
|
{
|
|
|
|
if (items.containsKey(itemname))
|
|
|
|
{
|
|
|
|
itemid = items.get(itemname);
|
|
|
|
if (durabilities.containsKey(itemname) && metaData == 0)
|
|
|
|
{
|
|
|
|
metaData = durabilities.get(itemname);
|
|
|
|
}
|
|
|
|
}
|
2013-03-16 12:11:03 +00:00
|
|
|
else if (Material.getMaterial(itemname.toUpperCase(Locale.ENGLISH)) != null)
|
2011-08-10 09:45:24 +00:00
|
|
|
{
|
2013-03-16 12:11:03 +00:00
|
|
|
itemid = Material.getMaterial(itemname.toUpperCase(Locale.ENGLISH)).getId();
|
2011-08-10 09:45:24 +00:00
|
|
|
metaData = 0;
|
|
|
|
}
|
2011-05-09 00:48:21 +00:00
|
|
|
else
|
|
|
|
{
|
2011-11-21 01:55:26 +00:00
|
|
|
throw new Exception(_("unknownItemName", id));
|
2011-05-09 00:48:21 +00:00
|
|
|
}
|
2011-05-07 13:03:45 +00:00
|
|
|
}
|
2011-05-09 00:22:45 +00:00
|
|
|
|
2011-07-15 23:33:22 +00:00
|
|
|
final Material mat = Material.getMaterial(itemid);
|
2011-05-09 00:22:45 +00:00
|
|
|
if (mat == null)
|
|
|
|
{
|
2011-11-21 01:55:26 +00:00
|
|
|
throw new Exception(_("unknownItemId", itemid));
|
2011-05-01 19:09:38 +00:00
|
|
|
}
|
2011-07-15 23:33:22 +00:00
|
|
|
final ItemStack retval = new ItemStack(mat);
|
2011-11-28 18:55:51 +00:00
|
|
|
retval.setAmount(mat.getMaxStackSize());
|
2011-05-09 00:22:45 +00:00
|
|
|
retval.setDurability(metaData);
|
2011-05-01 19:09:38 +00:00
|
|
|
return retval;
|
2011-03-19 22:39:51 +00:00
|
|
|
}
|
2013-04-30 16:25:25 +00:00
|
|
|
|
2012-09-08 17:33:06 +00:00
|
|
|
public String names(ItemStack item)
|
|
|
|
{
|
|
|
|
ItemData itemData = new ItemData(item.getTypeId(), item.getDurability());
|
|
|
|
List<String> nameList = names.get(itemData);
|
2013-01-06 18:28:24 +00:00
|
|
|
if (nameList == null)
|
|
|
|
{
|
|
|
|
itemData = new ItemData(item.getTypeId(), (short)0);
|
2012-09-08 17:41:21 +00:00
|
|
|
nameList = names.get(itemData);
|
2013-01-06 18:28:24 +00:00
|
|
|
if (nameList == null)
|
|
|
|
{
|
2012-09-08 17:41:21 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2013-01-06 18:28:24 +00:00
|
|
|
|
2012-09-08 17:33:06 +00:00
|
|
|
if (nameList.size() > 15)
|
|
|
|
{
|
|
|
|
nameList = nameList.subList(0, 14);
|
|
|
|
}
|
2013-06-08 21:31:19 +00:00
|
|
|
return StringUtil.joinList(", ", nameList);
|
2012-09-08 17:33:06 +00:00
|
|
|
}
|
|
|
|
|
2013-01-06 18:28:24 +00:00
|
|
|
|
2013-05-07 21:53:37 +00:00
|
|
|
static class ItemData
|
2012-09-08 17:33:06 +00:00
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-06 18:28:24 +00:00
|
|
|
|
2012-09-08 17:33:06 +00:00
|
|
|
class LengthCompare implements java.util.Comparator<String>
|
|
|
|
{
|
|
|
|
public LengthCompare()
|
|
|
|
{
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int compare(String s1, String s2)
|
|
|
|
{
|
|
|
|
return s1.length() - s2.length();
|
|
|
|
}
|
|
|
|
}
|
2011-03-19 22:39:51 +00:00
|
|
|
}
|