Search in sources :

Example 1 with Banner

use of org.bukkit.block.Banner in project Denizen-For-Bukkit by DenizenScript.

the class ItemPatterns method setPatterns.

private void setPatterns(List<Pattern> patterns) {
    ItemStack itemStack = item.getItemStack();
    ItemMeta itemMeta = itemStack.getItemMeta();
    if (itemMeta instanceof BlockStateMeta) {
        Banner banner = (Banner) ((BlockStateMeta) itemMeta).getBlockState();
        banner.setPatterns(patterns);
        banner.update();
        ((BlockStateMeta) itemMeta).setBlockState(banner);
    } else {
        ((BannerMeta) itemMeta).setPatterns(patterns);
    }
    itemStack.setItemMeta(itemMeta);
}
Also used : BlockStateMeta(org.bukkit.inventory.meta.BlockStateMeta) BannerMeta(org.bukkit.inventory.meta.BannerMeta) Banner(org.bukkit.block.Banner) ItemStack(org.bukkit.inventory.ItemStack) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Example 2 with Banner

use of org.bukkit.block.Banner in project Denizen-For-Bukkit by DenizenScript.

the class ItemBaseColor method setBaseColor.

private void setBaseColor(DyeColor color) {
    ItemStack itemStack = item.getItemStack();
    ItemMeta itemMeta = itemStack.getItemMeta();
    if (itemMeta instanceof BlockStateMeta) {
        Banner banner = (Banner) ((BlockStateMeta) itemMeta).getBlockState();
        banner.setBaseColor(color);
        banner.update();
        ((BlockStateMeta) itemMeta).setBlockState(banner);
    } else {
        ((BannerMeta) itemMeta).setBaseColor(color);
    }
    itemStack.setItemMeta(itemMeta);
}
Also used : BlockStateMeta(org.bukkit.inventory.meta.BlockStateMeta) BannerMeta(org.bukkit.inventory.meta.BannerMeta) Banner(org.bukkit.block.Banner) ItemStack(org.bukkit.inventory.ItemStack) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Example 3 with Banner

use of org.bukkit.block.Banner in project Essentials by drtshock.

the class ItemDb method serialize.

@Override
public String serialize(ItemStack is) {
    String mat = is.getType().name();
    if (is.getData().getData() != 0) {
        mat = mat + ":" + is.getData().getData();
    }
    int quantity = is.getAmount();
    // Add space AFTER you add something. We can trim at end.
    StringBuilder sb = new StringBuilder();
    sb.append(mat).append(" ").append(quantity).append(" ");
    // ItemMeta applies to anything.
    if (is.hasItemMeta()) {
        ItemMeta meta = is.getItemMeta();
        if (meta.hasDisplayName()) {
            sb.append("name:").append(meta.getDisplayName().replaceAll(" ", "_")).append(" ");
        }
        if (meta.hasLore()) {
            sb.append("lore:");
            boolean first = true;
            for (String s : meta.getLore()) {
                // to do this since we need each line separated by |
                if (!first) {
                    sb.append("|");
                }
                first = false;
                sb.append(s.replaceAll(" ", "_"));
            }
            sb.append(" ");
        }
        if (meta.hasEnchants()) {
            for (Enchantment e : meta.getEnchants().keySet()) {
                sb.append(e.getName().toLowerCase()).append(":").append(meta.getEnchantLevel(e)).append(" ");
            }
        }
    }
    switch(is.getType()) {
        case WRITTEN_BOOK:
            // Everything from http://wiki.ess3.net/wiki/Item_Meta#Books in that order.
            // Interesting as I didn't see a way to do pages or chapters.
            BookMeta bookMeta = (BookMeta) is.getItemMeta();
            if (bookMeta.hasTitle()) {
                sb.append("title:").append(bookMeta.getTitle()).append(" ");
            }
            if (bookMeta.hasAuthor()) {
                sb.append("author:").append(bookMeta.getAuthor()).append(" ");
            }
            // Only other thing it could have is lore but that's done up there ^^^
            break;
        case ENCHANTED_BOOK:
            EnchantmentStorageMeta enchantmentStorageMeta = (EnchantmentStorageMeta) is.getItemMeta();
            for (Enchantment e : enchantmentStorageMeta.getStoredEnchants().keySet()) {
                sb.append(e.getName().toLowerCase()).append(":").append(enchantmentStorageMeta.getStoredEnchantLevel(e)).append(" ");
            }
            break;
        case FIREWORK:
            // Everything from http://wiki.ess3.net/wiki/Item_Meta#Fireworks in that order.
            FireworkMeta fireworkMeta = (FireworkMeta) is.getItemMeta();
            if (fireworkMeta.hasEffects()) {
                for (FireworkEffect effect : fireworkMeta.getEffects()) {
                    if (effect.getColors() != null && !effect.getColors().isEmpty()) {
                        sb.append("color:");
                        boolean first = true;
                        for (Color c : effect.getColors()) {
                            if (!first) {
                                // same thing as above.
                                sb.append(",");
                            }
                            sb.append(c.toString());
                            first = false;
                        }
                        sb.append(" ");
                    }
                    sb.append("shape: ").append(effect.getType().name()).append(" ");
                    if (effect.getFadeColors() != null && !effect.getFadeColors().isEmpty()) {
                        sb.append("fade:");
                        boolean first = true;
                        for (Color c : effect.getFadeColors()) {
                            if (!first) {
                                // same thing as above.
                                sb.append(",");
                            }
                            sb.append(c.toString());
                            first = false;
                        }
                        sb.append(" ");
                    }
                }
                sb.append("power: ").append(fireworkMeta.getPower()).append(" ");
            }
            break;
        case POTION:
            Potion potion = Potion.fromItemStack(is);
            for (PotionEffect e : potion.getEffects()) {
                // long but needs to be effect:speed power:2 duration:120 in that order.
                sb.append("splash:").append(potion.isSplash()).append(" ").append("effect:").append(e.getType().getName().toLowerCase()).append(" ").append("power:").append(e.getAmplifier()).append(" ").append("duration:").append(e.getDuration() / 20).append(" ");
            }
            break;
        case SKULL_ITEM:
            // item stack with meta
            SkullMeta skullMeta = (SkullMeta) is.getItemMeta();
            if (skullMeta != null && skullMeta.hasOwner()) {
                sb.append("player:").append(skullMeta.getOwner()).append(" ");
            }
            break;
        case LEATHER_HELMET:
        case LEATHER_CHESTPLATE:
        case LEATHER_LEGGINGS:
        case LEATHER_BOOTS:
            LeatherArmorMeta leatherArmorMeta = (LeatherArmorMeta) is.getItemMeta();
            int rgb = leatherArmorMeta.getColor().asRGB();
            sb.append("color:").append(rgb).append(" ");
            break;
        case BANNER:
            BannerMeta bannerMeta = (BannerMeta) is.getItemMeta();
            if (bannerMeta != null) {
                int basecolor = bannerMeta.getBaseColor().getColor().asRGB();
                sb.append("basecolor:").append(basecolor).append(" ");
                for (org.bukkit.block.banner.Pattern p : bannerMeta.getPatterns()) {
                    String type = p.getPattern().getIdentifier();
                    int color = p.getColor().getColor().asRGB();
                    sb.append(type).append(",").append(color).append(" ");
                }
            }
            break;
        case SHIELD:
            // Hacky fix for accessing Shield meta - https://github.com/drtshock/Essentials/pull/745#issuecomment-234843795
            BlockStateMeta shieldMeta = (BlockStateMeta) is.getItemMeta();
            Banner shieldBannerMeta = (Banner) shieldMeta.getBlockState();
            int basecolor = shieldBannerMeta.getBaseColor().getColor().asRGB();
            sb.append("basecolor:").append(basecolor).append(" ");
            for (org.bukkit.block.banner.Pattern p : shieldBannerMeta.getPatterns()) {
                String type = p.getPattern().getIdentifier();
                int color = p.getColor().getColor().asRGB();
                sb.append(type).append(",").append(color).append(" ");
            }
            break;
    }
    return sb.toString().trim().replaceAll("ยง", "&");
}
Also used : Potion(org.bukkit.potion.Potion) PotionEffect(org.bukkit.potion.PotionEffect) Banner(org.bukkit.block.Banner) Color(org.bukkit.Color) FireworkEffect(org.bukkit.FireworkEffect) Enchantment(org.bukkit.enchantments.Enchantment)

Example 4 with Banner

use of org.bukkit.block.Banner in project Essentials by drtshock.

the class MetaItemStack method addBannerMeta.

public void addBannerMeta(final CommandSource sender, final boolean allowShortName, final String string, final IEssentials ess) throws Exception {
    if (stack.getType() == Material.BANNER && string != null) {
        final String[] split = splitPattern.split(string, 2);
        if (split.length < 2) {
            throw new Exception(tl("invalidBanner", split[1]));
        }
        PatternType patternType = null;
        try {
            patternType = PatternType.valueOf(split[0]);
        } catch (Exception ignored) {
        }
        final BannerMeta meta = (BannerMeta) stack.getItemMeta();
        if (split[0].equalsIgnoreCase("basecolor")) {
            Color color = Color.fromRGB(Integer.valueOf(split[1]));
            meta.setBaseColor(DyeColor.getByColor(color));
        } else if (patternType != null) {
            PatternType type = PatternType.valueOf(split[0]);
            DyeColor color = DyeColor.getByColor(Color.fromRGB(Integer.valueOf(split[1])));
            org.bukkit.block.banner.Pattern pattern = new org.bukkit.block.banner.Pattern(color, type);
            meta.addPattern(pattern);
        }
        stack.setItemMeta(meta);
    } else if (stack.getType() == Material.SHIELD && string != null) {
        final String[] split = splitPattern.split(string, 2);
        if (split.length < 2) {
            throw new Exception(tl("invalidBanner", split[1]));
        }
        PatternType patternType = null;
        try {
            patternType = PatternType.valueOf(split[0]);
        } catch (Exception ignored) {
        }
        // Hacky fix for accessing Shield meta - https://github.com/drtshock/Essentials/pull/745#issuecomment-234843795
        BlockStateMeta meta = (BlockStateMeta) stack.getItemMeta();
        Banner banner = (Banner) meta.getBlockState();
        if (split[0].equalsIgnoreCase("basecolor")) {
            Color color = Color.fromRGB(Integer.valueOf(split[1]));
            banner.setBaseColor(DyeColor.getByColor(color));
        } else if (patternType != null) {
            PatternType type = PatternType.valueOf(split[0]);
            DyeColor color = DyeColor.getByColor(Color.fromRGB(Integer.valueOf(split[1])));
            org.bukkit.block.banner.Pattern pattern = new org.bukkit.block.banner.Pattern(color, type);
            banner.addPattern(pattern);
        }
        banner.update();
        meta.setBlockState(banner);
        stack.setItemMeta(meta);
    }
}
Also used : PatternType(org.bukkit.block.banner.PatternType) Pattern(java.util.regex.Pattern) Banner(org.bukkit.block.Banner) DyeColor(org.bukkit.DyeColor) Color(org.bukkit.Color) DyeColor(org.bukkit.DyeColor)

Aggregations

Banner (org.bukkit.block.Banner)4 Color (org.bukkit.Color)2 ItemStack (org.bukkit.inventory.ItemStack)2 BannerMeta (org.bukkit.inventory.meta.BannerMeta)2 BlockStateMeta (org.bukkit.inventory.meta.BlockStateMeta)2 ItemMeta (org.bukkit.inventory.meta.ItemMeta)2 Pattern (java.util.regex.Pattern)1 DyeColor (org.bukkit.DyeColor)1 FireworkEffect (org.bukkit.FireworkEffect)1 PatternType (org.bukkit.block.banner.PatternType)1 Enchantment (org.bukkit.enchantments.Enchantment)1 Potion (org.bukkit.potion.Potion)1 PotionEffect (org.bukkit.potion.PotionEffect)1