mirror of
https://github.com/EssentialsX/Essentials.git
synced 2024-11-09 20:41:23 +01:00
Enchantment and item spawning cleanup
Should make it a little easier to add itemmeta.
This commit is contained in:
parent
c57332be63
commit
3bf36fecf9
@ -4,6 +4,7 @@ import static com.earth2me.essentials.I18n._;
|
||||
import com.earth2me.essentials.api.IItemDb;
|
||||
import java.util.*;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
|
||||
@ -135,14 +136,88 @@ public class ItemDb implements IConf, IItemDb
|
||||
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)
|
||||
{
|
||||
ItemData itemData = new ItemData(item.getTypeId(), item.getDurability());
|
||||
List<String> nameList = names.get(itemData);
|
||||
if (nameList == null) {
|
||||
itemData = new ItemData(item.getTypeId(), (short) 0);
|
||||
if (nameList == null)
|
||||
{
|
||||
itemData = new ItemData(item.getTypeId(), (short)0);
|
||||
nameList = names.get(itemData);
|
||||
if (nameList == null) {
|
||||
if (nameList == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@ -154,6 +229,7 @@ public class ItemDb implements IConf, IItemDb
|
||||
return Util.joinList(", ", nameList);
|
||||
}
|
||||
|
||||
|
||||
class ItemData
|
||||
{
|
||||
final private int itemNo;
|
||||
@ -198,6 +274,7 @@ public class ItemDb implements IConf, IItemDb
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class LengthCompare implements java.util.Comparator<String>
|
||||
{
|
||||
public LengthCompare()
|
||||
|
@ -6,9 +6,7 @@ import com.earth2me.essentials.commands.NoChargeException;
|
||||
import com.earth2me.essentials.craftbukkit.InventoryWorkaround;
|
||||
import java.util.*;
|
||||
import java.util.logging.Level;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
|
||||
@ -89,7 +87,6 @@ public class Kit
|
||||
throw new Exception(_("kitError2"));
|
||||
}
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
return (List<String>)kit.get("items");
|
||||
@ -106,6 +103,7 @@ public class Kit
|
||||
try
|
||||
{
|
||||
boolean spew = false;
|
||||
final boolean allowUnsafe = ess.getSettings().allowUnsafeEnchantments();
|
||||
for (String d : items)
|
||||
{
|
||||
if (d.startsWith(ess.getSettings().getCurrencySymbol()))
|
||||
@ -115,51 +113,15 @@ public class Kit
|
||||
t.pay(user);
|
||||
continue;
|
||||
}
|
||||
final String[] parts = d.split(" ");
|
||||
final String[] item = parts[0].split("[:+',;.]", 2);
|
||||
final int id = Material.getMaterial(Integer.parseInt(item[0])).getId();
|
||||
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);
|
||||
final String[] parts = d.split(" ", 3);
|
||||
final ItemStack stack = ess.getItemDb().get(parts[0], parts.length > 1 ? Integer.parseInt(parts[1]) : 1);
|
||||
|
||||
if (parts.length > 2)
|
||||
{
|
||||
for (int i = 2; i < parts.length; i++)
|
||||
{
|
||||
final String[] split = parts[i].split("[:+',;.]", 2);
|
||||
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);
|
||||
}
|
||||
ess.getItemDb().addStringEnchantment(null, allowUnsafe, stack, parts[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -43,6 +43,7 @@ public class Commandenchant extends EssentialsCommand
|
||||
}
|
||||
throw new NotEnoughArgumentsException(_("enchantments", Util.joinList(enchantmentslist.toArray())));
|
||||
}
|
||||
|
||||
int level = -1;
|
||||
if (args.length > 1)
|
||||
{
|
||||
@ -55,27 +56,12 @@ public class Commandenchant extends EssentialsCommand
|
||||
level = -1;
|
||||
}
|
||||
}
|
||||
final Enchantment enchantment = getEnchantment(args[0], user);
|
||||
|
||||
final boolean allowUnsafe = ess.getSettings().allowUnsafeEnchantments() && user.isAuthorized("essentials.enchant.allowunsafe");
|
||||
if (level < 0 || (!allowUnsafe && level > enchantment.getMaxLevel()))
|
||||
{
|
||||
level = enchantment.getMaxLevel();
|
||||
}
|
||||
if (level == 0)
|
||||
{
|
||||
stack.removeEnchantment(enchantment);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (allowUnsafe)
|
||||
{
|
||||
stack.addUnsafeEnchantment(enchantment, level);
|
||||
}
|
||||
else
|
||||
{
|
||||
stack.addEnchantment(enchantment, level);
|
||||
}
|
||||
}
|
||||
final Enchantment enchantment = ess.getItemDb().getEnchantment(user, args[0]);
|
||||
ess.getItemDb().addEnchantment(user, allowUnsafe, stack, enchantment, level);
|
||||
|
||||
|
||||
user.getInventory().setItemInHand(stack);
|
||||
user.updateInventory();
|
||||
final String enchantmentName = enchantment.getName().toLowerCase(Locale.ENGLISH);
|
||||
@ -88,20 +74,4 @@ public class Commandenchant extends EssentialsCommand
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,6 @@ import java.util.Locale;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
@ -44,6 +43,8 @@ public class Commandgive extends EssentialsCommand
|
||||
|
||||
final User giveTo = getPlayer(server, args, 0);
|
||||
|
||||
try
|
||||
{
|
||||
if (args.length > 3 && Util.isInt(args[2]) && Util.isInt(args[3]))
|
||||
{
|
||||
stack.setAmount(Integer.parseInt(args[2]));
|
||||
@ -61,39 +62,23 @@ public class Commandgive extends EssentialsCommand
|
||||
{
|
||||
stack.setAmount(ess.getSettings().getOversizedStackSize());
|
||||
}
|
||||
}
|
||||
catch (NumberFormatException e)
|
||||
{
|
||||
throw new NotEnoughArgumentsException();
|
||||
}
|
||||
|
||||
if (args.length > 3)
|
||||
{
|
||||
for (int i = Util.isInt(args[3]) ? 4 : 3; i < args.length; i++)
|
||||
{
|
||||
final String[] split = args[i].split("[:+',;.]", 2);
|
||||
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)
|
||||
|
||||
for (int i = Util.isInt(args[3]) ? 4 : 3; i < args.length; i++)
|
||||
{
|
||||
stack.addUnsafeEnchantment(enchantment, level);
|
||||
}
|
||||
else
|
||||
{
|
||||
stack.addEnchantment(enchantment, level);
|
||||
}
|
||||
ess.getItemDb().addStringEnchantment(null, allowUnsafe, stack, args[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,6 @@ import com.earth2me.essentials.craftbukkit.InventoryWorkaround;
|
||||
import java.util.Locale;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
|
||||
@ -50,40 +49,21 @@ public class Commanditem extends EssentialsCommand
|
||||
{
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user