Enchantment and item spawning cleanup

Should make it a little easier to add itemmeta.
This commit is contained in:
KHobbits 2013-01-06 18:28:24 +00:00
parent c57332be63
commit 3bf36fecf9
5 changed files with 143 additions and 169 deletions

View file

@ -4,6 +4,7 @@ import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.api.IItemDb; import com.earth2me.essentials.api.IItemDb;
import java.util.*; import java.util.*;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -135,18 +136,92 @@ public class ItemDb implements IConf, IItemDb
return retval; return retval;
} }
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;
}
public String names(ItemStack item) public String names(ItemStack item)
{ {
ItemData itemData = new ItemData(item.getTypeId(), item.getDurability()); ItemData itemData = new ItemData(item.getTypeId(), item.getDurability());
List<String> nameList = names.get(itemData); List<String> nameList = names.get(itemData);
if (nameList == null) { if (nameList == null)
itemData = new ItemData(item.getTypeId(), (short) 0); {
itemData = new ItemData(item.getTypeId(), (short)0);
nameList = names.get(itemData); nameList = names.get(itemData);
if (nameList == null) { if (nameList == null)
{
return null; return null;
} }
} }
if (nameList.size() > 15) if (nameList.size() > 15)
{ {
nameList = nameList.subList(0, 14); nameList = nameList.subList(0, 14);
@ -154,6 +229,7 @@ public class ItemDb implements IConf, IItemDb
return Util.joinList(", ", nameList); return Util.joinList(", ", nameList);
} }
class ItemData class ItemData
{ {
final private int itemNo; final private int itemNo;
@ -198,6 +274,7 @@ public class ItemDb implements IConf, IItemDb
} }
} }
class LengthCompare implements java.util.Comparator<String> class LengthCompare implements java.util.Comparator<String>
{ {
public LengthCompare() public LengthCompare()

View file

@ -6,9 +6,7 @@ import com.earth2me.essentials.commands.NoChargeException;
import com.earth2me.essentials.craftbukkit.InventoryWorkaround; import com.earth2me.essentials.craftbukkit.InventoryWorkaround;
import java.util.*; import java.util.*;
import java.util.logging.Level; import java.util.logging.Level;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -34,16 +32,16 @@ public class Kit
{ {
throw new Exception(_("kitError"), ex); throw new Exception(_("kitError"), ex);
} }
} }
public static void checkTime(final User user, final String kitName, final Map<String, Object> els) throws Exception public static void checkTime(final User user, final String kitName, final Map<String, Object> els) throws Exception
{ {
if (user.isAuthorized("essentials.kit.exemptdelay")) if (user.isAuthorized("essentials.kit.exemptdelay"))
{ {
return; return;
} }
final Calendar time = new GregorianCalendar(); final Calendar time = new GregorianCalendar();
// Take the current time, and remove the delay from it. // Take the current time, and remove the delay from it.
@ -56,7 +54,7 @@ public class Kit
// When was the last kit used? // When was the last kit used?
final long lastTime = user.getKitTimestamp(kitName); final long lastTime = user.getKitTimestamp(kitName);
if (lastTime < earliestLong || lastTime == 0L) if (lastTime < earliestLong || lastTime == 0L)
{ {
user.setKitTimestamp(kitName, time.getTimeInMillis()); user.setKitTimestamp(kitName, time.getTimeInMillis());
@ -81,15 +79,14 @@ public class Kit
throw new NoChargeException(); throw new NoChargeException();
} }
} }
public static List<String> getItems(final User user, final Map<String, Object> kit) throws Exception public static List<String> getItems(final User user, final Map<String, Object> kit) throws Exception
{ {
if (kit == null) if (kit == null)
{ {
throw new Exception(_("kitError2")); throw new Exception(_("kitError2"));
} }
try try
{ {
return (List<String>)kit.get("items"); return (List<String>)kit.get("items");
@ -100,69 +97,34 @@ public class Kit
throw new Exception(_("kitErrorHelp"), e); throw new Exception(_("kitErrorHelp"), e);
} }
} }
public static void expandItems(final IEssentials ess, final User user, final List<String> items) throws Exception public static void expandItems(final IEssentials ess, final User user, final List<String> items) throws Exception
{ {
try try
{ {
boolean spew = false; boolean spew = false;
final boolean allowUnsafe = ess.getSettings().allowUnsafeEnchantments();
for (String d : items) for (String d : items)
{ {
if (d.startsWith(ess.getSettings().getCurrencySymbol())) if (d.startsWith(ess.getSettings().getCurrencySymbol()))
{ {
Double value = Double.parseDouble(d.substring(ess.getSettings().getCurrencySymbol().length()).trim()); Double value = Double.parseDouble(d.substring(ess.getSettings().getCurrencySymbol().length()).trim());
Trade t = new Trade(value, ess); Trade t = new Trade(value, ess);
t.pay(user); t.pay(user);
continue; continue;
} }
final String[] parts = d.split(" ");
final String[] item = parts[0].split("[:+',;.]", 2); final String[] parts = d.split(" ", 3);
final int id = Material.getMaterial(Integer.parseInt(item[0])).getId(); final ItemStack stack = ess.getItemDb().get(parts[0], parts.length > 1 ? Integer.parseInt(parts[1]) : 1);
final short data = item.length > 1 ? Short.parseShort(item[1]) : 0;
final int amount = parts.length > 1 ? Integer.parseInt(parts[1]) : 1;
final ItemStack stack = new ItemStack(id, amount, data);
if (parts.length > 2) if (parts.length > 2)
{ {
for (int i = 2; i < parts.length; i++) for (int i = 2; i < parts.length; i++)
{ {
final String[] split = parts[i].split("[:+',;.]", 2); ess.getItemDb().addStringEnchantment(null, allowUnsafe, stack, parts[i]);
if (split.length < 1)
{
continue;
}
final Enchantment enchantment = Enchantments.getByName(split[0]);
if (enchantment == null)
{
throw new Exception("Enchantment not found: " + split[0]);
}
int level;
if (split.length > 1)
{
level = Integer.parseInt(split[1]);
}
else
{
level = enchantment.getMaxLevel();
}
try
{
if (ess.getSettings().allowUnsafeEnchantments())
{
stack.addUnsafeEnchantment(enchantment, level);
}
else
{
stack.addEnchantment(enchantment, level);
}
}
catch (Exception ex)
{
throw new Exception("Enchantment " + enchantment.getName() + ": " + ex.getMessage(), ex);
}
} }
} }
final Map<Integer, ItemStack> overfilled; final Map<Integer, ItemStack> overfilled;
if (user.isAuthorized("essentials.oversizedstacks")) if (user.isAuthorized("essentials.oversizedstacks"))
{ {

View file

@ -43,6 +43,7 @@ public class Commandenchant extends EssentialsCommand
} }
throw new NotEnoughArgumentsException(_("enchantments", Util.joinList(enchantmentslist.toArray()))); throw new NotEnoughArgumentsException(_("enchantments", Util.joinList(enchantmentslist.toArray())));
} }
int level = -1; int level = -1;
if (args.length > 1) if (args.length > 1)
{ {
@ -55,27 +56,12 @@ public class Commandenchant extends EssentialsCommand
level = -1; level = -1;
} }
} }
final Enchantment enchantment = getEnchantment(args[0], user);
final boolean allowUnsafe = ess.getSettings().allowUnsafeEnchantments() && user.isAuthorized("essentials.enchant.allowunsafe"); final boolean allowUnsafe = ess.getSettings().allowUnsafeEnchantments() && user.isAuthorized("essentials.enchant.allowunsafe");
if (level < 0 || (!allowUnsafe && level > enchantment.getMaxLevel())) final Enchantment enchantment = ess.getItemDb().getEnchantment(user, args[0]);
{ ess.getItemDb().addEnchantment(user, allowUnsafe, stack, enchantment, level);
level = enchantment.getMaxLevel();
}
if (level == 0)
{
stack.removeEnchantment(enchantment);
}
else
{
if (allowUnsafe)
{
stack.addUnsafeEnchantment(enchantment, level);
}
else
{
stack.addEnchantment(enchantment, level);
}
}
user.getInventory().setItemInHand(stack); user.getInventory().setItemInHand(stack);
user.updateInventory(); user.updateInventory();
final String enchantmentName = enchantment.getName().toLowerCase(Locale.ENGLISH); final String enchantmentName = enchantment.getName().toLowerCase(Locale.ENGLISH);
@ -88,20 +74,4 @@ public class Commandenchant extends EssentialsCommand
user.sendMessage(_("enchantmentApplied", enchantmentName.replace('_', ' '))); user.sendMessage(_("enchantmentApplied", enchantmentName.replace('_', ' ')));
} }
} }
public static Enchantment getEnchantment(final String name, final User user) throws Exception
{
final Enchantment enchantment = Enchantments.getByName(name);
if (enchantment == null)
{
throw new Exception(_("enchantmentNotFound"));
}
final String enchantmentName = enchantment.getName().toLowerCase(Locale.ENGLISH);
if (user != null && !user.isAuthorized("essentials.enchant." + enchantmentName))
{
throw new Exception(_("enchantmentPerm", enchantmentName));
}
return enchantment;
}
} }

View file

@ -8,7 +8,6 @@ import java.util.Locale;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.Server; import org.bukkit.Server;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -44,56 +43,42 @@ public class Commandgive extends EssentialsCommand
final User giveTo = getPlayer(server, args, 0); final User giveTo = getPlayer(server, args, 0);
if (args.length > 3 && Util.isInt(args[2]) && Util.isInt(args[3])) try
{ {
stack.setAmount(Integer.parseInt(args[2])); if (args.length > 3 && Util.isInt(args[2]) && Util.isInt(args[3]))
stack.setDurability(Short.parseShort(args[3])); {
stack.setAmount(Integer.parseInt(args[2]));
stack.setDurability(Short.parseShort(args[3]));
}
else if (args.length > 2 && Integer.parseInt(args[2]) > 0)
{
stack.setAmount(Integer.parseInt(args[2]));
}
else if (ess.getSettings().getDefaultStackSize() > 0)
{
stack.setAmount(ess.getSettings().getDefaultStackSize());
}
else if (ess.getSettings().getOversizedStackSize() > 0 && giveTo.isAuthorized("essentials.oversizedstacks"))
{
stack.setAmount(ess.getSettings().getOversizedStackSize());
}
} }
else if (args.length > 2 && Integer.parseInt(args[2]) > 0) catch (NumberFormatException e)
{ {
stack.setAmount(Integer.parseInt(args[2])); throw new NotEnoughArgumentsException();
}
else if (ess.getSettings().getDefaultStackSize() > 0)
{
stack.setAmount(ess.getSettings().getDefaultStackSize());
}
else if (ess.getSettings().getOversizedStackSize() > 0 && giveTo.isAuthorized("essentials.oversizedstacks"))
{
stack.setAmount(ess.getSettings().getOversizedStackSize());
} }
if (args.length > 3) if (args.length > 3)
{ {
boolean allowUnsafe = ess.getSettings().allowUnsafeEnchantments();
if (allowUnsafe && sender instanceof Player && !ess.getUser(sender).isAuthorized("essentials.enchant.allowunsafe"))
{
allowUnsafe = false;
}
for (int i = Util.isInt(args[3]) ? 4 : 3; i < args.length; i++) for (int i = Util.isInt(args[3]) ? 4 : 3; i < args.length; i++)
{ {
final String[] split = args[i].split("[:+',;.]", 2); ess.getItemDb().addStringEnchantment(null, allowUnsafe, stack, args[i]);
if (split.length < 1)
{
continue;
}
final Enchantment enchantment = Commandenchant.getEnchantment(split[0], sender instanceof Player ? ess.getUser(sender) : null);
int level;
if (split.length > 1)
{
level = Integer.parseInt(split[1]);
}
else
{
level = enchantment.getMaxLevel();
}
boolean allowUnsafe = ess.getSettings().allowUnsafeEnchantments();
if (allowUnsafe && sender instanceof Player && !ess.getUser(sender).isAuthorized("essentials.enchant.allowunsafe"))
{
allowUnsafe = false;
}
if (allowUnsafe)
{
stack.addUnsafeEnchantment(enchantment, level);
}
else
{
stack.addEnchantment(enchantment, level);
}
} }
} }

View file

@ -6,7 +6,6 @@ import com.earth2me.essentials.craftbukkit.InventoryWorkaround;
import java.util.Locale; import java.util.Locale;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.Server; import org.bukkit.Server;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -16,7 +15,7 @@ public class Commanditem extends EssentialsCommand
{ {
super("item"); super("item");
} }
@Override @Override
public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception
{ {
@ -25,7 +24,7 @@ public class Commanditem extends EssentialsCommand
throw new NotEnoughArgumentsException(); throw new NotEnoughArgumentsException();
} }
final ItemStack stack = ess.getItemDb().get(args[0]); final ItemStack stack = ess.getItemDb().get(args[0]);
final String itemname = stack.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", ""); final String itemname = stack.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", "");
if (ess.getSettings().permissionBasedItemSpawn() if (ess.getSettings().permissionBasedItemSpawn()
? (!user.isAuthorized("essentials.itemspawn.item-all") ? (!user.isAuthorized("essentials.itemspawn.item-all")
@ -50,46 +49,27 @@ public class Commanditem extends EssentialsCommand
{ {
stack.setAmount(ess.getSettings().getOversizedStackSize()); stack.setAmount(ess.getSettings().getOversizedStackSize());
} }
if (args.length > 2)
{
for (int i = 2; i < args.length; i++)
{
final String[] split = args[i].split("[:+',;.]", 2);
if (split.length < 1)
{
continue;
}
final Enchantment enchantment = Commandenchant.getEnchantment(split[0], user);
int level;
if (split.length > 1)
{
level = Integer.parseInt(split[1]);
}
else
{
level = enchantment.getMaxLevel();
}
if (ess.getSettings().allowUnsafeEnchantments() && user.isAuthorized("essentials.enchant.allowunsafe"))
{
stack.addUnsafeEnchantment(enchantment, level);
}
else
{
stack.addEnchantment(enchantment, level);
}
}
}
} }
catch (NumberFormatException e) catch (NumberFormatException e)
{ {
throw new NotEnoughArgumentsException(); throw new NotEnoughArgumentsException();
} }
if (args.length > 2)
{
final boolean allowUnsafe = ess.getSettings().allowUnsafeEnchantments() && user.isAuthorized("essentials.enchant.allowunsafe");
for (int i = 2; i < args.length; i++)
{
ess.getItemDb().addStringEnchantment(null, allowUnsafe, stack, args[i]);
}
}
if (stack.getType() == Material.AIR) if (stack.getType() == Material.AIR)
{ {
throw new Exception(_("cantSpawnItem", "Air")); throw new Exception(_("cantSpawnItem", "Air"));
} }
final String displayName = stack.getType().toString().toLowerCase(Locale.ENGLISH).replace('_', ' '); final String displayName = stack.getType().toString().toLowerCase(Locale.ENGLISH).replace('_', ' ');
user.sendMessage(_("itemSpawn", stack.getAmount(), displayName)); user.sendMessage(_("itemSpawn", stack.getAmount(), displayName));
if (user.isAuthorized("essentials.oversizedstacks")) if (user.isAuthorized("essentials.oversizedstacks"))