mirror of
https://github.com/EssentialsX/Essentials.git
synced 2024-11-16 23:55:28 +01:00
Pull item meta from itemdb class
This commit is contained in:
parent
0b581580c7
commit
7fdb2ad7d3
@ -5,9 +5,7 @@ import com.earth2me.essentials.api.IItemDb;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.enchantments.Enchantment;
|
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
|
|
||||||
|
|
||||||
|
|
||||||
public class ItemDb implements IConf, IItemDb
|
public class ItemDb implements IConf, IItemDb
|
||||||
@ -140,94 +138,6 @@ 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 = splitPattern.split(string, 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
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
if (stack.getType().equals(Material.ENCHANTED_BOOK))
|
|
||||||
{
|
|
||||||
EnchantmentStorageMeta meta = (EnchantmentStorageMeta)stack.getItemMeta();
|
|
||||||
if (level == 0)
|
|
||||||
{
|
|
||||||
meta.removeStoredEnchant(enchantment);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
meta.addStoredEnchant(enchantment, level, allowUnsafe);
|
|
||||||
}
|
|
||||||
stack.setItemMeta(meta);
|
|
||||||
}
|
|
||||||
else // all other material types besides ENCHANTED_BOOK
|
|
||||||
{
|
|
||||||
if (level == 0)
|
|
||||||
{
|
|
||||||
stack.removeEnchantment(enchantment);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
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());
|
||||||
|
@ -115,24 +115,25 @@ public class Kit
|
|||||||
}
|
}
|
||||||
|
|
||||||
final String[] parts = d.split(" ");
|
final String[] parts = d.split(" ");
|
||||||
final ItemStack stack = ess.getItemDb().get(parts[0], parts.length > 1 ? Integer.parseInt(parts[1]) : 1);
|
final ItemStack parseStack = ess.getItemDb().get(parts[0], parts.length > 1 ? Integer.parseInt(parts[1]) : 1);
|
||||||
|
final MetaItemStack stack = new MetaItemStack(parseStack);
|
||||||
|
|
||||||
if (parts.length > 2)
|
if (parts.length > 2)
|
||||||
{
|
{
|
||||||
for (int i = 2; i < parts.length; i++)
|
for (int i = 2; i < parts.length; i++)
|
||||||
{
|
{
|
||||||
ess.getItemDb().addStringEnchantment(null, allowUnsafe, stack, parts[i]);
|
stack.addStringEnchantment(null, allowUnsafe, parts[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
final Map<Integer, ItemStack> overfilled;
|
final Map<Integer, ItemStack> overfilled;
|
||||||
if (user.isAuthorized("essentials.oversizedstacks"))
|
if (user.isAuthorized("essentials.oversizedstacks"))
|
||||||
{
|
{
|
||||||
overfilled = InventoryWorkaround.addOversizedItems(user.getInventory(), ess.getSettings().getOversizedStackSize(), stack);
|
overfilled = InventoryWorkaround.addOversizedItems(user.getInventory(), ess.getSettings().getOversizedStackSize(), stack.getBase());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
overfilled = InventoryWorkaround.addItems(user.getInventory(), stack);
|
overfilled = InventoryWorkaround.addItems(user.getInventory(), stack.getBase());
|
||||||
}
|
}
|
||||||
for (ItemStack itemStack : overfilled.values())
|
for (ItemStack itemStack : overfilled.values())
|
||||||
{
|
{
|
||||||
|
121
Essentials/src/com/earth2me/essentials/MetaItemStack.java
Normal file
121
Essentials/src/com/earth2me/essentials/MetaItemStack.java
Normal file
@ -0,0 +1,121 @@
|
|||||||
|
package com.earth2me.essentials;
|
||||||
|
|
||||||
|
import static com.earth2me.essentials.I18n._;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.enchantments.Enchantment;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
|
||||||
|
|
||||||
|
|
||||||
|
public class MetaItemStack extends ItemStack
|
||||||
|
{
|
||||||
|
private final transient Pattern splitPattern = Pattern.compile("[:+',;.]");
|
||||||
|
private final ItemStack stack;
|
||||||
|
|
||||||
|
public MetaItemStack(final ItemStack stack)
|
||||||
|
{
|
||||||
|
this.stack = stack;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemStack getBase()
|
||||||
|
{
|
||||||
|
return stack;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addStringMeta(final User user, final boolean allowUnsafe, final String string) throws Exception
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addStringEnchantment(final User user, final boolean allowUnsafe, final String string) throws Exception
|
||||||
|
{
|
||||||
|
final String[] split = splitPattern.split(string, 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, enchantment, level);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addEnchantment(final User user, final boolean allowUnsafe, final Enchantment enchantment, final int level) throws Exception
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (stack.getType().equals(Material.ENCHANTED_BOOK))
|
||||||
|
{
|
||||||
|
EnchantmentStorageMeta meta = (EnchantmentStorageMeta)stack.getItemMeta();
|
||||||
|
if (level == 0)
|
||||||
|
{
|
||||||
|
meta.removeStoredEnchant(enchantment);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
meta.addStoredEnchant(enchantment, level, allowUnsafe);
|
||||||
|
}
|
||||||
|
stack.setItemMeta(meta);
|
||||||
|
}
|
||||||
|
else // all other material types besides ENCHANTED_BOOK
|
||||||
|
{
|
||||||
|
if (level == 0)
|
||||||
|
{
|
||||||
|
stack.removeEnchantment(enchantment);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -2,6 +2,7 @@ package com.earth2me.essentials.commands;
|
|||||||
|
|
||||||
import com.earth2me.essentials.Enchantments;
|
import com.earth2me.essentials.Enchantments;
|
||||||
import static com.earth2me.essentials.I18n._;
|
import static com.earth2me.essentials.I18n._;
|
||||||
|
import com.earth2me.essentials.MetaItemStack;
|
||||||
import com.earth2me.essentials.User;
|
import com.earth2me.essentials.User;
|
||||||
import com.earth2me.essentials.Util;
|
import com.earth2me.essentials.Util;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
@ -10,7 +11,6 @@ import java.util.Set;
|
|||||||
import java.util.TreeSet;
|
import java.util.TreeSet;
|
||||||
import org.bukkit.Server;
|
import org.bukkit.Server;
|
||||||
import org.bukkit.enchantments.Enchantment;
|
import org.bukkit.enchantments.Enchantment;
|
||||||
import org.bukkit.inventory.ItemStack;
|
|
||||||
|
|
||||||
|
|
||||||
public class Commandenchant extends EssentialsCommand
|
public class Commandenchant extends EssentialsCommand
|
||||||
@ -24,7 +24,7 @@ public class Commandenchant extends EssentialsCommand
|
|||||||
@Override
|
@Override
|
||||||
protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception
|
protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception
|
||||||
{
|
{
|
||||||
final ItemStack stack = user.getItemInHand();
|
final MetaItemStack stack = new MetaItemStack(user.getItemInHand());
|
||||||
if (stack == null)
|
if (stack == null)
|
||||||
{
|
{
|
||||||
throw new Exception(_("nothingInHand"));
|
throw new Exception(_("nothingInHand"));
|
||||||
@ -58,11 +58,11 @@ public class Commandenchant extends EssentialsCommand
|
|||||||
}
|
}
|
||||||
|
|
||||||
final boolean allowUnsafe = ess.getSettings().allowUnsafeEnchantments() && user.isAuthorized("essentials.enchant.allowunsafe");
|
final boolean allowUnsafe = ess.getSettings().allowUnsafeEnchantments() && user.isAuthorized("essentials.enchant.allowunsafe");
|
||||||
final Enchantment enchantment = ess.getItemDb().getEnchantment(user, args[0]);
|
final Enchantment enchantment = stack.getEnchantment(user, args[0]);
|
||||||
ess.getItemDb().addEnchantment(user, allowUnsafe, stack, enchantment, level);
|
stack.addEnchantment(user, allowUnsafe, enchantment, level);
|
||||||
|
|
||||||
|
|
||||||
user.getInventory().setItemInHand(stack);
|
user.getInventory().setItemInHand(stack.getBase());
|
||||||
user.updateInventory();
|
user.updateInventory();
|
||||||
final String enchantmentName = enchantment.getName().toLowerCase(Locale.ENGLISH);
|
final String enchantmentName = enchantment.getName().toLowerCase(Locale.ENGLISH);
|
||||||
if (level == 0)
|
if (level == 0)
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.earth2me.essentials.commands;
|
package com.earth2me.essentials.commands;
|
||||||
|
|
||||||
import static com.earth2me.essentials.I18n._;
|
import static com.earth2me.essentials.I18n._;
|
||||||
|
import com.earth2me.essentials.MetaItemStack;
|
||||||
import com.earth2me.essentials.User;
|
import com.earth2me.essentials.User;
|
||||||
import com.earth2me.essentials.Util;
|
import com.earth2me.essentials.Util;
|
||||||
import com.earth2me.essentials.craftbukkit.InventoryWorkaround;
|
import com.earth2me.essentials.craftbukkit.InventoryWorkaround;
|
||||||
@ -9,7 +10,6 @@ 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.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.inventory.ItemStack;
|
|
||||||
|
|
||||||
|
|
||||||
public class Commandgive extends EssentialsCommand
|
public class Commandgive extends EssentialsCommand
|
||||||
@ -27,7 +27,7 @@ public class Commandgive extends EssentialsCommand
|
|||||||
throw new NotEnoughArgumentsException();
|
throw new NotEnoughArgumentsException();
|
||||||
}
|
}
|
||||||
|
|
||||||
final ItemStack stack = ess.getItemDb().get(args[1]);
|
final MetaItemStack stack = new MetaItemStack(ess.getItemDb().get(args[1]));
|
||||||
|
|
||||||
final String itemname = stack.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", "");
|
final String itemname = stack.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", "");
|
||||||
if (sender instanceof Player
|
if (sender instanceof Player
|
||||||
@ -78,7 +78,7 @@ public class Commandgive extends EssentialsCommand
|
|||||||
|
|
||||||
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++)
|
||||||
{
|
{
|
||||||
ess.getItemDb().addStringEnchantment(null, allowUnsafe, stack, args[i]);
|
stack.addStringEnchantment(null, allowUnsafe, args[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,11 +91,11 @@ public class Commandgive extends EssentialsCommand
|
|||||||
sender.sendMessage(_("giveSpawn", stack.getAmount(), itemName, giveTo.getDisplayName()));
|
sender.sendMessage(_("giveSpawn", stack.getAmount(), itemName, giveTo.getDisplayName()));
|
||||||
if (giveTo.isAuthorized("essentials.oversizedstacks"))
|
if (giveTo.isAuthorized("essentials.oversizedstacks"))
|
||||||
{
|
{
|
||||||
InventoryWorkaround.addOversizedItems(giveTo.getInventory(), ess.getSettings().getOversizedStackSize(), stack);
|
InventoryWorkaround.addOversizedItems(giveTo.getInventory(), ess.getSettings().getOversizedStackSize(), stack.getBase());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
InventoryWorkaround.addItems(giveTo.getInventory(), stack);
|
InventoryWorkaround.addItems(giveTo.getInventory(), stack.getBase());
|
||||||
}
|
}
|
||||||
giveTo.updateInventory();
|
giveTo.updateInventory();
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
package com.earth2me.essentials.commands;
|
package com.earth2me.essentials.commands;
|
||||||
|
|
||||||
import static com.earth2me.essentials.I18n._;
|
import static com.earth2me.essentials.I18n._;
|
||||||
|
import com.earth2me.essentials.MetaItemStack;
|
||||||
import com.earth2me.essentials.User;
|
import com.earth2me.essentials.User;
|
||||||
import com.earth2me.essentials.craftbukkit.InventoryWorkaround;
|
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.inventory.ItemStack;
|
|
||||||
|
|
||||||
|
|
||||||
public class Commanditem extends EssentialsCommand
|
public class Commanditem extends EssentialsCommand
|
||||||
@ -23,7 +23,7 @@ public class Commanditem extends EssentialsCommand
|
|||||||
{
|
{
|
||||||
throw new NotEnoughArgumentsException();
|
throw new NotEnoughArgumentsException();
|
||||||
}
|
}
|
||||||
final ItemStack stack = ess.getItemDb().get(args[0]);
|
final MetaItemStack stack = new MetaItemStack(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()
|
||||||
@ -60,7 +60,7 @@ public class Commanditem extends EssentialsCommand
|
|||||||
|
|
||||||
for (int i = 2; i < args.length; i++)
|
for (int i = 2; i < args.length; i++)
|
||||||
{
|
{
|
||||||
ess.getItemDb().addStringEnchantment(null, allowUnsafe, stack, args[i]);
|
stack.addStringEnchantment(null, allowUnsafe, args[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,11 +74,11 @@ public class Commanditem extends EssentialsCommand
|
|||||||
user.sendMessage(_("itemSpawn", stack.getAmount(), displayName));
|
user.sendMessage(_("itemSpawn", stack.getAmount(), displayName));
|
||||||
if (user.isAuthorized("essentials.oversizedstacks"))
|
if (user.isAuthorized("essentials.oversizedstacks"))
|
||||||
{
|
{
|
||||||
InventoryWorkaround.addOversizedItems(user.getInventory(), ess.getSettings().getOversizedStackSize(), stack);
|
InventoryWorkaround.addOversizedItems(user.getInventory(), ess.getSettings().getOversizedStackSize(), stack.getBase());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
InventoryWorkaround.addItems(user.getInventory(), stack);
|
InventoryWorkaround.addItems(user.getInventory(), stack.getBase());
|
||||||
}
|
}
|
||||||
user.updateInventory();
|
user.updateInventory();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user