Search in sources :

Example 1 with BookInput

use of com.earth2me.essentials.textreader.BookInput in project Essentials by drtshock.

the class MetaItemStack method addStringMeta.

public void addStringMeta(final CommandSource sender, final boolean allowUnsafe, final String string, final IEssentials ess) throws Exception {
    final String[] split = splitPattern.split(string, 2);
    if (split.length < 1) {
        return;
    }
    Material banner = null;
    Material shield = null;
    try {
        // 1.8
        banner = Material.valueOf("BANNER");
        // 1.9
        shield = Material.valueOf("SHIELD");
    } catch (IllegalArgumentException ignored) {
    }
    if (split.length > 1 && split[0].equalsIgnoreCase("name") && hasMetaPermission(sender, "name", false, true, ess)) {
        final String displayName = FormatUtil.replaceFormat(split[1].replace('_', ' '));
        final ItemMeta meta = stack.getItemMeta();
        meta.setDisplayName(displayName);
        stack.setItemMeta(meta);
    } else if (split.length > 1 && (split[0].equalsIgnoreCase("lore") || split[0].equalsIgnoreCase("desc")) && hasMetaPermission(sender, "lore", false, true, ess)) {
        final List<String> lore = new ArrayList<String>();
        for (String line : split[1].split("\\|")) {
            lore.add(FormatUtil.replaceFormat(line.replace('_', ' ')));
        }
        final ItemMeta meta = stack.getItemMeta();
        meta.setLore(lore);
        stack.setItemMeta(meta);
    } else if (split.length > 1 && (split[0].equalsIgnoreCase("unbreakable") && hasMetaPermission(sender, "unbreakable", false, true, ess))) {
        setUnbreakable(stack, Boolean.valueOf(split[1]));
    } else if (split.length > 1 && (split[0].equalsIgnoreCase("player") || split[0].equalsIgnoreCase("owner")) && stack.getType() == Material.SKULL_ITEM && hasMetaPermission(sender, "head", false, true, ess)) {
        if (stack.getDurability() == 3) {
            final String owner = split[1];
            final SkullMeta meta = (SkullMeta) stack.getItemMeta();
            meta.setOwner(owner);
            stack.setItemMeta(meta);
        } else {
            throw new Exception(tl("onlyPlayerSkulls"));
        }
    } else if (split.length > 1 && split[0].equalsIgnoreCase("book") && stack.getType() == Material.WRITTEN_BOOK && (hasMetaPermission(sender, "book", true, true, ess) || hasMetaPermission(sender, "chapter-" + split[1].toLowerCase(Locale.ENGLISH), true, true, ess))) {
        final BookMeta meta = (BookMeta) stack.getItemMeta();
        final IText input = new BookInput("book", true, ess);
        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 && hasMetaPermission(sender, "author", false, true, ess)) {
        final String author = FormatUtil.replaceFormat(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 && hasMetaPermission(sender, "title", false, true, ess)) {
        final String title = FormatUtil.replaceFormat(split[1].replace('_', ' '));
        final BookMeta meta = (BookMeta) stack.getItemMeta();
        meta.setTitle(title);
        stack.setItemMeta(meta);
    } else if (split.length > 1 && split[0].equalsIgnoreCase("power") && stack.getType() == Material.FIREWORK && hasMetaPermission(sender, "firework-power", false, true, ess)) {
        final int power = NumberUtil.isInt(split[1]) ? Integer.parseInt(split[1]) : 0;
        final FireworkMeta meta = (FireworkMeta) stack.getItemMeta();
        meta.setPower(power > 3 ? 4 : power);
        stack.setItemMeta(meta);
    } else if (stack.getType() == Material.FIREWORK) {
        //WARNING - Meta for fireworks will be ignored after this point.
        addFireworkMeta(sender, false, string, ess);
    } else if (isPotion(stack.getType())) {
        //WARNING - Meta for potions will be ignored after this point.
        addPotionMeta(sender, false, string, ess);
    } else if (banner != null && stack.getType() == banner) {
        //WARNING - Meta for banners will be ignored after this point.
        addBannerMeta(sender, false, string, ess);
    } else if (shield != null && stack.getType() == shield) {
        //WARNING - Meta for shields will be ignored after this point.
        addBannerMeta(sender, false, string, ess);
    } 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)) {
        final String[] color = split[1].split("(\\||,)");
        if (color.length == 1 && (NumberUtil.isInt(color[0]) || color[0].startsWith("#"))) {
            // int rgb and hex
            final LeatherArmorMeta meta = (LeatherArmorMeta) stack.getItemMeta();
            String input = color[0];
            if (input.startsWith("#")) {
                meta.setColor(Color.fromRGB(Integer.valueOf(input.substring(1, 3), 16), Integer.valueOf(input.substring(3, 5), 16), Integer.valueOf(input.substring(5, 7), 16)));
            } else {
                meta.setColor(Color.fromRGB(Integer.parseInt(input)));
            }
            stack.setItemMeta(meta);
        } else if (color.length == 3) {
            // r,g,b
            final int red = NumberUtil.isInt(color[0]) ? Integer.parseInt(color[0]) : 0;
            final int green = NumberUtil.isInt(color[1]) ? Integer.parseInt(color[1]) : 0;
            final int blue = NumberUtil.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(tl("leatherSyntax"));
        }
    } else {
        parseEnchantmentStrings(sender, allowUnsafe, split, ess);
    }
}
Also used : BookInput(com.earth2me.essentials.textreader.BookInput) Material(org.bukkit.Material) IText(com.earth2me.essentials.textreader.IText) BookPager(com.earth2me.essentials.textreader.BookPager)

Aggregations

BookInput (com.earth2me.essentials.textreader.BookInput)1 BookPager (com.earth2me.essentials.textreader.BookPager)1 IText (com.earth2me.essentials.textreader.IText)1 Material (org.bukkit.Material)1