2013-01-12 14:12:12 +00:00
|
|
|
package com.earth2me.essentials;
|
|
|
|
|
|
|
|
import static com.earth2me.essentials.I18n._;
|
2013-01-13 21:08:24 +00:00
|
|
|
import com.earth2me.essentials.textreader.*;
|
2013-01-13 04:44:28 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2013-01-12 14:12:12 +00:00
|
|
|
import java.util.Locale;
|
|
|
|
import java.util.regex.Pattern;
|
2013-01-13 04:44:28 +00:00
|
|
|
import org.bukkit.Color;
|
2013-01-14 23:11:19 +00:00
|
|
|
import org.bukkit.DyeColor;
|
|
|
|
import org.bukkit.FireworkEffect;
|
|
|
|
import org.bukkit.FireworkEffect.Builder;
|
2013-01-12 14:12:12 +00:00
|
|
|
import org.bukkit.Material;
|
|
|
|
import org.bukkit.enchantments.Enchantment;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
2013-01-13 21:08:24 +00:00
|
|
|
import org.bukkit.inventory.meta.*;
|
2013-01-12 14:12:12 +00:00
|
|
|
|
|
|
|
|
2013-01-12 17:05:05 +00:00
|
|
|
public class MetaItemStack
|
2013-01-12 14:12:12 +00:00
|
|
|
{
|
|
|
|
private final transient Pattern splitPattern = Pattern.compile("[:+',;.]");
|
|
|
|
private final ItemStack stack;
|
2013-01-13 21:08:24 +00:00
|
|
|
|
2013-01-12 14:12:12 +00:00
|
|
|
public MetaItemStack(final ItemStack stack)
|
|
|
|
{
|
2013-01-12 17:05:05 +00:00
|
|
|
this.stack = stack.clone();
|
2013-01-12 14:12:12 +00:00
|
|
|
}
|
2013-01-13 21:08:24 +00:00
|
|
|
|
2013-01-12 17:05:05 +00:00
|
|
|
public ItemStack getItemStack()
|
2013-01-12 14:12:12 +00:00
|
|
|
{
|
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
|
2013-01-13 04:44:28 +00:00
|
|
|
//TODO: TL this
|
2013-01-13 21:08:24 +00:00
|
|
|
public void addStringMeta(final User user, final boolean allowUnsafe, final String string, final IEssentials ess) throws Exception
|
2013-01-12 14:12:12 +00:00
|
|
|
{
|
2013-01-13 04:44:28 +00:00
|
|
|
final String[] split = splitPattern.split(string, 2);
|
|
|
|
if (split.length < 1)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-01-13 21:08:24 +00:00
|
|
|
|
2013-01-13 04:44:28 +00:00
|
|
|
if (split.length > 1 && split[0].equalsIgnoreCase("name"))
|
|
|
|
{
|
2013-01-14 21:34:35 +00:00
|
|
|
final String displayName = Util.replaceFormat(split[1].replace('_', ' '));
|
2013-01-13 04:44:28 +00:00
|
|
|
final ItemMeta meta = stack.getItemMeta();
|
|
|
|
meta.setDisplayName(displayName);
|
|
|
|
stack.setItemMeta(meta);
|
|
|
|
}
|
|
|
|
else if (split.length > 1 && (split[0].equalsIgnoreCase("lore") || split[0].equalsIgnoreCase("desc")))
|
|
|
|
{
|
|
|
|
final List<String> lore = new ArrayList<String>();
|
|
|
|
for (String line : split[1].split("\\|"))
|
|
|
|
{
|
2013-01-14 21:34:35 +00:00
|
|
|
lore.add(Util.replaceFormat(line.replace('_', ' ')));
|
2013-01-13 04:44:28 +00:00
|
|
|
}
|
|
|
|
final ItemMeta meta = stack.getItemMeta();
|
|
|
|
meta.setLore(lore);
|
|
|
|
stack.setItemMeta(meta);
|
|
|
|
}
|
|
|
|
else if (split.length > 1 && (split[0].equalsIgnoreCase("player") || split[0].equalsIgnoreCase("owner")) && stack.getType() == Material.SKULL_ITEM)
|
|
|
|
{
|
|
|
|
if (stack.getDurability() == 3)
|
|
|
|
{
|
|
|
|
final String owner = split[1];
|
|
|
|
final SkullMeta meta = (SkullMeta)stack.getItemMeta();
|
|
|
|
boolean result = meta.setOwner(owner);
|
|
|
|
stack.setItemMeta(meta);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new Exception("You can only set the owner of player skulls (397:3)");
|
|
|
|
}
|
|
|
|
}
|
2013-01-14 08:14:20 +00:00
|
|
|
else if (split.length > 1 && split[0].equalsIgnoreCase("book") && stack.getType() == Material.WRITTEN_BOOK)
|
2013-01-13 21:08:24 +00:00
|
|
|
{
|
|
|
|
final BookMeta meta = (BookMeta)stack.getItemMeta();
|
2013-01-14 08:14:20 +00:00
|
|
|
final IText input = new BookInput("book", true, ess);
|
2013-01-13 21:08:24 +00:00
|
|
|
final BookPager pager = new BookPager(input);
|
|
|
|
|
|
|
|
List<String> pages = pager.getPages(split[1]);
|
|
|
|
meta.setPages(pages);
|
|
|
|
|
|
|
|
stack.setItemMeta(meta);
|
|
|
|
}
|
|
|
|
else if (split.length > 1 && split[0].equalsIgnoreCase("author") && stack.getType() == Material.WRITTEN_BOOK)
|
|
|
|
{
|
|
|
|
final String author = split[1];
|
|
|
|
final BookMeta meta = (BookMeta)stack.getItemMeta();
|
|
|
|
meta.setAuthor(author);
|
|
|
|
stack.setItemMeta(meta);
|
|
|
|
}
|
|
|
|
else if (split.length > 1 && split[0].equalsIgnoreCase("title") && stack.getType() == Material.WRITTEN_BOOK)
|
|
|
|
{
|
2013-01-14 21:34:35 +00:00
|
|
|
final String title = Util.replaceFormat(split[1].replace('_', ' '));
|
2013-01-13 21:08:24 +00:00
|
|
|
final BookMeta meta = (BookMeta)stack.getItemMeta();
|
|
|
|
meta.setTitle(title);
|
|
|
|
stack.setItemMeta(meta);
|
|
|
|
}
|
2013-01-14 23:11:19 +00:00
|
|
|
else if (split.length > 1 && split[0].equalsIgnoreCase("power") && stack.getType() == Material.FIREWORK)
|
|
|
|
{
|
|
|
|
final int power = Integer.parseInt(split[1]);
|
|
|
|
final FireworkMeta meta = (FireworkMeta)stack.getItemMeta();
|
|
|
|
meta.setPower(power);
|
|
|
|
stack.setItemMeta(meta);
|
2013-01-16 16:30:30 +00:00
|
|
|
}
|
|
|
|
// else if (split.length > 1 && split[0].equalsIgnoreCase("effect") && stack.getType() == Material.FIREWORK)
|
|
|
|
// {
|
|
|
|
// //TODO: Add validation messages
|
|
|
|
// final FireworkMeta meta = (FireworkMeta)stack.getItemMeta();
|
|
|
|
// Builder builder = FireworkEffect.builder();
|
|
|
|
//
|
|
|
|
// String[] effectData = split[1].toUpperCase(Locale.ENGLISH).split("\\|");
|
|
|
|
//
|
|
|
|
// builder.with(FireworkEffect.Type.valueOf(effectData[0]));
|
|
|
|
//
|
|
|
|
// String[] primaryColorStrings = effectData[1].split(",");
|
|
|
|
// List<Color> primaryColors = new ArrayList<Color>();
|
|
|
|
//
|
|
|
|
// for (String primaryColorString : primaryColorStrings) {
|
|
|
|
// primaryColors.add(DyeColor.valueOf(primaryColorString).getFireworkColor());
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// builder.withColor(primaryColors);
|
|
|
|
//
|
|
|
|
// final FireworkEffect effect = builder.build();
|
|
|
|
// meta.addEffect(effect);
|
|
|
|
// stack.setItemMeta(meta);
|
|
|
|
// }
|
2013-01-13 04:44:28 +00:00
|
|
|
else if (split.length > 1 && (split[0].equalsIgnoreCase("color") || split[0].equalsIgnoreCase("colour"))
|
|
|
|
&& (stack.getType() == Material.LEATHER_BOOTS
|
|
|
|
|| stack.getType() == Material.LEATHER_CHESTPLATE
|
|
|
|
|| stack.getType() == Material.LEATHER_HELMET
|
|
|
|
|| stack.getType() == Material.LEATHER_LEGGINGS))
|
|
|
|
{
|
2013-01-14 23:11:19 +00:00
|
|
|
final String[] color = split[1].split(",");
|
2013-01-13 04:44:28 +00:00
|
|
|
if (color.length == 3)
|
|
|
|
{
|
|
|
|
final int red = Util.isInt(color[0]) ? Integer.parseInt(color[0]) : 0;
|
|
|
|
final int green = Util.isInt(color[1]) ? Integer.parseInt(color[1]) : 0;
|
|
|
|
final int blue = Util.isInt(color[2]) ? Integer.parseInt(color[2]) : 0;
|
|
|
|
final LeatherArmorMeta meta = (LeatherArmorMeta)stack.getItemMeta();
|
|
|
|
meta.setColor(Color.fromRGB(red, green, blue));
|
|
|
|
stack.setItemMeta(meta);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new Exception("Leather Color Syntax: color:<red>|<green>|<blue> eg: color:255|0|0");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
parseEnchantmentStrings(user, allowUnsafe, split);
|
|
|
|
}
|
2013-01-12 14:12:12 +00:00
|
|
|
}
|
2013-01-13 21:08:24 +00:00
|
|
|
|
2013-01-12 14:12:12 +00:00
|
|
|
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;
|
|
|
|
}
|
2013-01-13 21:08:24 +00:00
|
|
|
|
2013-01-13 04:44:28 +00:00
|
|
|
parseEnchantmentStrings(user, allowUnsafe, split);
|
|
|
|
}
|
2013-01-13 21:08:24 +00:00
|
|
|
|
2013-01-13 04:44:28 +00:00
|
|
|
private void parseEnchantmentStrings(final User user, final boolean allowUnsafe, final String[] split) throws Exception
|
|
|
|
{
|
2013-01-12 14:12:12 +00:00
|
|
|
Enchantment enchantment = getEnchantment(user, split[0]);
|
2013-01-13 21:08:24 +00:00
|
|
|
|
2013-01-12 14:12:12 +00:00
|
|
|
int level = -1;
|
|
|
|
if (split.length > 1)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
level = Integer.parseInt(split[1]);
|
|
|
|
}
|
|
|
|
catch (NumberFormatException ex)
|
|
|
|
{
|
|
|
|
level = -1;
|
|
|
|
}
|
|
|
|
}
|
2013-01-13 21:08:24 +00:00
|
|
|
|
2013-01-12 14:12:12 +00:00
|
|
|
if (level < 0 || (!allowUnsafe && level > enchantment.getMaxLevel()))
|
|
|
|
{
|
|
|
|
level = enchantment.getMaxLevel();
|
|
|
|
}
|
|
|
|
addEnchantment(user, allowUnsafe, enchantment, level);
|
|
|
|
}
|
2013-01-13 21:08:24 +00:00
|
|
|
|
2013-01-12 14:12:12 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|