2011-03-19 22:39:51 +00:00
|
|
|
package com.earth2me.essentials;
|
|
|
|
|
2011-11-21 01:55:26 +00:00
|
|
|
import static com.earth2me.essentials.I18n._;
|
2012-09-08 13:55:37 +00:00
|
|
|
import com.earth2me.essentials.api.IItemDb;
|
2012-09-08 17:33:06 +00:00
|
|
|
import java.util.*;
|
2011-05-01 19:09:38 +00:00
|
|
|
import org.bukkit.Material;
|
2013-01-06 18:28:24 +00:00
|
|
|
import org.bukkit.enchantments.Enchantment;
|
2011-03-19 22:39:51 +00:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
|
|
|
|
|
2011-12-06 15:32:06 +00:00
|
|
|
public class ItemDb implements IConf, 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;
|
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
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
2011-05-09 00:48:21 +00:00
|
|
|
if (id.matches("^\\d+[:+',;.]\\d+$"))
|
2011-05-07 13:03:45 +00:00
|
|
|
{
|
2011-05-09 00:48:21 +00:00
|
|
|
itemid = Integer.parseInt(id.split("[:+',;.]")[0]);
|
|
|
|
metaData = Short.parseShort(id.split("[:+',;.]")[1]);
|
2011-05-09 00:22:45 +00:00
|
|
|
}
|
|
|
|
else if (id.matches("^\\d+$"))
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2011-11-21 01:55:26 +00:00
|
|
|
itemname = id.split("[:+',;.]")[0].toLowerCase(Locale.ENGLISH);
|
2011-05-09 00:48:21 +00:00
|
|
|
metaData = Short.parseShort(id.split("[:+',;.]")[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);
|
|
|
|
}
|
|
|
|
}
|
2011-09-17 22:49:34 +00:00
|
|
|
else if (Material.getMaterial(itemname) != null)
|
2011-08-10 09:45:24 +00:00
|
|
|
{
|
|
|
|
itemid = Material.getMaterial(itemname).getId();
|
|
|
|
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
|
|
|
}
|
2012-09-08 17:33:06 +00:00
|
|
|
|
2013-01-06 18:28:24 +00:00
|
|
|
public void addStringEnchantment(final User user, final boolean allowUnsafe, final ItemStack stack, final String string) throws Exception
|
|
|
|
{
|
|
|
|
final String[] split = string.split("[:+',;.]", 2);
|
|
|
|
if (split.length < 1)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Enchantment enchantment = getEnchantment(user, split[0]);
|
|
|
|
|
|
|
|
int level = -1;
|
|
|
|
if (split.length > 1)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
level = Integer.parseInt(split[1]);
|
|
|
|
}
|
|
|
|
catch (NumberFormatException ex)
|
|
|
|
{
|
|
|
|
level = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (level < 0 || (!allowUnsafe && level > enchantment.getMaxLevel()))
|
|
|
|
{
|
|
|
|
level = enchantment.getMaxLevel();
|
|
|
|
}
|
|
|
|
addEnchantment(user, allowUnsafe, stack, enchantment, level);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addEnchantment(final User user, final boolean allowUnsafe, final ItemStack stack, final Enchantment enchantment, final int level) throws Exception
|
|
|
|
{
|
|
|
|
if (level == 0)
|
|
|
|
{
|
|
|
|
stack.removeEnchantment(enchantment);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (allowUnsafe)
|
|
|
|
{
|
|
|
|
stack.addUnsafeEnchantment(enchantment, level);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stack.addEnchantment(enchantment, level);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
throw new Exception("Enchantment " + enchantment.getName() + ": " + ex.getMessage(), ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: Properly TL this
|
|
|
|
public Enchantment getEnchantment(final User user, final String name) throws Exception
|
|
|
|
{
|
|
|
|
final Enchantment enchantment = Enchantments.getByName(name);
|
|
|
|
if (enchantment == null)
|
|
|
|
{
|
|
|
|
throw new Exception(_("enchantmentNotFound") + ": " + name);
|
|
|
|
}
|
|
|
|
final String enchantmentName = enchantment.getName().toLowerCase(Locale.ENGLISH);
|
|
|
|
if (user != null && !user.isAuthorized("essentials.enchant." + enchantmentName))
|
|
|
|
{
|
|
|
|
throw new Exception(_("enchantmentPerm", enchantmentName));
|
|
|
|
}
|
|
|
|
return enchantment;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
return Util.joinList(", ", nameList);
|
|
|
|
}
|
|
|
|
|
2013-01-06 18:28:24 +00:00
|
|
|
|
2012-09-08 17:33:06 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|