Search in sources :

Example 6 with BannerMeta

use of org.bukkit.inventory.meta.BannerMeta in project Denizen-For-Bukkit by DenizenScript.

the class ItemPatterns method setPatterns.

private void setPatterns(List<Pattern> patterns) {
    ItemMeta itemMeta = item.getItemMeta();
    if (itemMeta instanceof BannerMeta) {
        ((BannerMeta) itemMeta).setPatterns(patterns);
    } else if (itemMeta instanceof BlockStateMeta) {
        try {
            Banner banner = (Banner) ((BlockStateMeta) itemMeta).getBlockState();
            banner.setPatterns(patterns);
            banner.update();
            ((BlockStateMeta) itemMeta).setBlockState(banner);
        } catch (Exception ex) {
            Debug.echoError("Banner setPatterns failed!");
            Debug.echoError(ex);
        }
    } else {
    // ...???
    }
    item.setItemMeta(itemMeta);
}
Also used : BlockStateMeta(org.bukkit.inventory.meta.BlockStateMeta) BannerMeta(org.bukkit.inventory.meta.BannerMeta) Banner(org.bukkit.block.Banner) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Example 7 with BannerMeta

use of org.bukkit.inventory.meta.BannerMeta in project Prism-Bukkit by prism.

the class ItemStackAction method deserialize.

@Override
public void deserialize(String data) {
    if (data == null || !data.startsWith("{")) {
        return;
    }
    actionData = gson().fromJson(data, ItemStackActionData.class);
    item = new ItemStack(getMaterial(), actionData.amt);
    MaterialState.setItemDamage(item, actionData.durability);
    // Restore enchantment
    if (actionData.enchs != null && actionData.enchs.length > 0) {
        for (final String ench : actionData.enchs) {
            final String[] enchArgs = ench.split(":");
            Enchantment enchantment = Enchantment.getByKey(NamespacedKey.minecraft(enchArgs[0]));
            // Restore book enchantment
            if (enchantment != null) {
                if (item.getType() == Material.ENCHANTED_BOOK) {
                    final EnchantmentStorageMeta bookEnchantments = (EnchantmentStorageMeta) item.getItemMeta();
                    bookEnchantments.addStoredEnchant(enchantment, Integer.parseInt(enchArgs[1]), false);
                    item.setItemMeta(bookEnchantments);
                } else {
                    item.addUnsafeEnchantment(enchantment, Integer.parseInt(enchArgs[1]));
                }
            }
        }
    }
    ItemMeta meta = item.getItemMeta();
    // Leather color
    if (meta instanceof LeatherArmorMeta && actionData.color > 0) {
        final LeatherArmorMeta lam = (LeatherArmorMeta) meta;
        lam.setColor(Color.fromRGB(actionData.color));
        item.setItemMeta(lam);
    } else if (meta instanceof SkullMeta && actionData.owner != null) {
        final SkullMeta skull = (SkullMeta) meta;
        skull.setOwningPlayer(Bukkit.getOfflinePlayer(EntityUtils.uuidOf(actionData.owner)));
        item.setItemMeta(skull);
    } else if (meta instanceof BookMeta) {
        final BookMeta bookMeta = (BookMeta) meta;
        bookMeta.setAuthor(actionData.by);
        bookMeta.setTitle(actionData.title);
        bookMeta.setPages(actionData.content);
        item.setItemMeta(bookMeta);
    } else if (meta instanceof PotionMeta) {
        final PotionType potionType = PotionType.valueOf(actionData.potionType.toUpperCase());
        final PotionMeta potionMeta = (PotionMeta) meta;
        potionMeta.setBasePotionData(new PotionData(potionType, actionData.potionExtended, actionData.potionUpgraded));
    }
    if (meta instanceof FireworkEffectMeta && actionData.effectColors != null && actionData.effectColors.length > 0) {
        item = deserializeFireWorksMeta(item, meta, actionData);
    }
    if (meta instanceof BannerMeta && actionData.bannerMeta != null) {
        Map<String, String> stringStringMap = actionData.bannerMeta;
        List<Pattern> patterns = new ArrayList<>();
        stringStringMap.forEach((patternIdentifier, dyeName) -> {
            PatternType type = PatternType.getByIdentifier(patternIdentifier);
            DyeColor color = DyeColor.valueOf(dyeName);
            if (type != null && color != null) {
                Pattern p = new Pattern(color, type);
                patterns.add(p);
            }
        });
        ((BannerMeta) meta).setPatterns(patterns);
    }
    if (actionData.name != null) {
        if (meta == null) {
            meta = item.getItemMeta();
        }
        if (meta != null) {
            meta.setDisplayName(actionData.name);
        }
    }
    if (actionData.lore != null) {
        if (meta == null) {
            meta = item.getItemMeta();
        }
        if (meta != null) {
            meta.setLore(Arrays.asList(actionData.lore));
        }
    }
    if (meta != null) {
        item.setItemMeta(meta);
    }
}
Also used : PatternType(org.bukkit.block.banner.PatternType) BannerMeta(org.bukkit.inventory.meta.BannerMeta) Pattern(org.bukkit.block.banner.Pattern) ArrayList(java.util.ArrayList) SkullMeta(org.bukkit.inventory.meta.SkullMeta) PotionMeta(org.bukkit.inventory.meta.PotionMeta) DyeColor(org.bukkit.DyeColor) EnchantmentStorageMeta(org.bukkit.inventory.meta.EnchantmentStorageMeta) PotionData(org.bukkit.potion.PotionData) LeatherArmorMeta(org.bukkit.inventory.meta.LeatherArmorMeta) FireworkEffectMeta(org.bukkit.inventory.meta.FireworkEffectMeta) PotionType(org.bukkit.potion.PotionType) ItemStack(org.bukkit.inventory.ItemStack) Enchantment(org.bukkit.enchantments.Enchantment) BookMeta(org.bukkit.inventory.meta.BookMeta) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Example 8 with BannerMeta

use of org.bukkit.inventory.meta.BannerMeta 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 9 with BannerMeta

use of org.bukkit.inventory.meta.BannerMeta in project MagicPlugin by elBukkit.

the class MaterialAndData method applyToItem.

@Override
public ItemStack applyToItem(ItemStack stack) {
    stack.setType(material);
    if (data != null) {
        stack.setDurability(data);
    }
    if (material == Material.SKULL_ITEM) {
        ItemMeta meta = stack.getItemMeta();
        if (meta != null && meta instanceof SkullMeta && extraData != null && extraData instanceof BlockSkull) {
            BlockSkull skullData = (BlockSkull) extraData;
            if (skullData.skullType == SkullType.PLAYER && skullData.profile != null) {
                SkullMeta skullMeta = (SkullMeta) meta;
                InventoryUtils.setSkullProfile(skullMeta, ((BlockSkull) extraData).profile);
                stack.setItemMeta(meta);
            } else if (skullData.skullType == SkullType.PLAYER && skullData.playerName != null) {
                SkullMeta skullMeta = (SkullMeta) meta;
                skullMeta.setOwner(skullData.playerName);
                stack.setItemMeta(meta);
            }
        }
    } else if (material == Material.STANDING_BANNER || material == Material.WALL_BANNER || material == Material.BANNER) {
        ItemMeta meta = stack.getItemMeta();
        if (meta != null && meta instanceof BannerMeta && extraData != null && extraData instanceof BlockBanner) {
            BannerMeta banner = (BannerMeta) meta;
            BlockBanner bannerData = (BlockBanner) extraData;
            if (bannerData.patterns != null) {
                banner.setPatterns(bannerData.patterns);
            }
            if (bannerData.baseColor != null) {
                banner.setBaseColor(bannerData.baseColor);
            }
            stack.setItemMeta(meta);
        }
    } else if (this.material == Material.LEATHER_BOOTS || this.material == Material.LEATHER_CHESTPLATE || this.material == Material.LEATHER_HELMET || this.material == Material.LEATHER_LEGGINGS) {
        ItemMeta meta = stack.getItemMeta();
        if (extraData != null && extraData instanceof LeatherArmorData && meta != null && meta instanceof LeatherArmorMeta) {
            ((LeatherArmorMeta) meta).setColor(((LeatherArmorData) extraData).getColor());
            stack.setItemMeta(meta);
        }
    }
    return stack;
}
Also used : BannerMeta(org.bukkit.inventory.meta.BannerMeta) LeatherArmorMeta(org.bukkit.inventory.meta.LeatherArmorMeta) SkullMeta(org.bukkit.inventory.meta.SkullMeta) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Example 10 with BannerMeta

use of org.bukkit.inventory.meta.BannerMeta in project CitizensAPI by CitizensDev.

the class ItemStorage method serialiseMeta.

private static void serialiseMeta(DataKey key, ItemMeta meta) {
    key.removeKey("flags");
    try {
        key.setBoolean("unbreakable", meta.isUnbreakable());
    } catch (Throwable t) {
    // probably backwards-compat issue, don't log
    }
    int j = 0;
    for (ItemFlag flag : ItemFlag.values()) {
        if (meta.hasItemFlag(flag)) {
            key.setString("flags." + j++, flag.name());
        }
    }
    if (meta instanceof Repairable) {
        Repairable rep = (Repairable) meta;
        key.setInt("repaircost", rep.getRepairCost());
    } else {
        key.removeKey("repaircost");
    }
    if (meta.hasLore()) {
        List<String> lore = meta.getLore();
        DataKey root = key.getRelative("lore");
        for (int i = 0; i < lore.size(); i++) {
            root.setString(Integer.toString(i), lore.get(i));
        }
    } else {
        key.removeKey("lore");
    }
    if (meta.hasDisplayName()) {
        key.setString("displayname", meta.getDisplayName());
    } else {
        key.removeKey("displayname");
    }
    if (meta instanceof BookMeta) {
        BookMeta book = (BookMeta) meta;
        DataKey pages = key.getRelative("book.pages");
        for (int i = 1; i <= book.getPageCount(); i++) {
            pages.setString(Integer.toString(i), book.getPage(i));
        }
        key.setString("book.title", book.getTitle());
        key.setString("book.author", book.getAuthor());
        serialiseEnchantments(key.getRelative("book.enchantments"), book.getEnchants());
    } else {
        key.removeKey("book");
    }
    if (meta instanceof SkullMeta) {
        SkullMeta skull = (SkullMeta) meta;
        key.setString("skull.owner", skull.getOwner());
    } else {
        key.removeKey("skull");
    }
    if (meta instanceof FireworkMeta) {
        FireworkMeta firework = (FireworkMeta) meta;
        int i = 0;
        for (FireworkEffect effect : firework.getEffects()) {
            serialiseFireworkEffect(key.getRelative("firework.effects." + i), effect);
            i++;
        }
        key.setInt("firework.power", firework.getPower());
    } else {
        key.removeKey("firework");
    }
    if (meta instanceof MapMeta) {
        MapMeta map = (MapMeta) meta;
        key.setBoolean("map.scaling", map.isScaling());
    } else {
        key.removeKey("map");
    }
    if (meta instanceof LeatherArmorMeta) {
        LeatherArmorMeta armor = (LeatherArmorMeta) meta;
        Color color = armor.getColor();
        key.setInt("armor.color", color.asRGB());
    } else {
        key.removeKey("armor");
    }
    if (meta instanceof EnchantmentStorageMeta) {
        EnchantmentStorageMeta ench = (EnchantmentStorageMeta) meta;
        for (Map.Entry<Enchantment, Integer> e : ench.getStoredEnchants().entrySet()) {
            key.getRelative("enchantmentstorage").setInt(e.getKey().getName(), e.getValue());
        }
    } else {
        key.removeKey("enchantmentstorage");
    }
    if (meta instanceof PotionMeta) {
        PotionMeta potion = (PotionMeta) meta;
        PotionData data = potion.getBasePotionData();
        List<PotionEffect> effects = potion.getCustomEffects();
        key.setBoolean("potion.data.extended", data.isExtended());
        key.setBoolean("potion.data.upgraded", data.isUpgraded());
        key.setString("potion.data.type", data.getType().name());
        key.removeKey("potion.effects");
        DataKey effectKey = key.getRelative("potion.effects");
        for (int i = 0; i < effects.size(); i++) {
            PotionEffect effect = effects.get(i);
            DataKey sub = effectKey.getRelative(Integer.toString(i));
            sub.setBoolean("ambient", effect.isAmbient());
            sub.setInt("amplifier", effect.getAmplifier());
            sub.setInt("duration", effect.getDuration());
            sub.setString("type", effect.getType().getName());
        }
    } else {
        key.removeKey("potion");
    }
    key.removeKey("blockstate");
    if (meta instanceof BlockStateMeta) {
        BlockStateMeta state = (BlockStateMeta) meta;
        if (state.hasBlockState()) {
            DataKey root = key.getRelative("blockstate");
            BlockState blockstate = state.getBlockState();
            if (blockstate instanceof Banner) {
                Banner banner = (Banner) blockstate;
                serialiseBanner(root.getRelative("banner"), banner);
            } else {
                root.removeKey("banner");
            }
        }
    }
    if (meta instanceof BannerMeta) {
        BannerMeta banner = (BannerMeta) meta;
        DataKey root = key.getRelative("banner");
        if (banner.getBaseColor() != null) {
            root.setString("basecolor", banner.getBaseColor().name());
        } else {
            root.removeKey("basecolor");
        }
        List<org.bukkit.block.banner.Pattern> patterns = banner.getPatterns();
        root.removeKey("patterns");
        for (int i = 0; i < patterns.size(); i++) {
            org.bukkit.block.banner.Pattern pattern = patterns.get(i);
            DataKey sub = root.getRelative("patterns." + i);
            sub.setString("color", pattern.getColor().name());
            sub.setString("type", pattern.getPattern().getIdentifier());
        }
    } else {
        key.removeKey("banner");
    }
    Bukkit.getPluginManager().callEvent(new CitizensSerialiseMetaEvent(key, meta));
}
Also used : PotionEffect(org.bukkit.potion.PotionEffect) Pattern(org.bukkit.block.banner.Pattern) SkullMeta(org.bukkit.inventory.meta.SkullMeta) FireworkEffect(org.bukkit.FireworkEffect) CitizensSerialiseMetaEvent(net.citizensnpcs.api.event.CitizensSerialiseMetaEvent) PotionData(org.bukkit.potion.PotionData) Repairable(org.bukkit.inventory.meta.Repairable) BannerMeta(org.bukkit.inventory.meta.BannerMeta) Pattern(org.bukkit.block.banner.Pattern) Banner(org.bukkit.block.Banner) Color(org.bukkit.Color) DyeColor(org.bukkit.DyeColor) FireworkMeta(org.bukkit.inventory.meta.FireworkMeta) PotionMeta(org.bukkit.inventory.meta.PotionMeta) BlockStateMeta(org.bukkit.inventory.meta.BlockStateMeta) EnchantmentStorageMeta(org.bukkit.inventory.meta.EnchantmentStorageMeta) BlockState(org.bukkit.block.BlockState) MapMeta(org.bukkit.inventory.meta.MapMeta) LeatherArmorMeta(org.bukkit.inventory.meta.LeatherArmorMeta) ItemFlag(org.bukkit.inventory.ItemFlag) Enchantment(org.bukkit.enchantments.Enchantment) BookMeta(org.bukkit.inventory.meta.BookMeta) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

BannerMeta (org.bukkit.inventory.meta.BannerMeta)15 ItemMeta (org.bukkit.inventory.meta.ItemMeta)8 ItemStack (org.bukkit.inventory.ItemStack)7 Banner (org.bukkit.block.Banner)6 Pattern (org.bukkit.block.banner.Pattern)6 BlockStateMeta (org.bukkit.inventory.meta.BlockStateMeta)6 LeatherArmorMeta (org.bukkit.inventory.meta.LeatherArmorMeta)5 SkullMeta (org.bukkit.inventory.meta.SkullMeta)5 BookMeta (org.bukkit.inventory.meta.BookMeta)4 EnchantmentStorageMeta (org.bukkit.inventory.meta.EnchantmentStorageMeta)4 PotionMeta (org.bukkit.inventory.meta.PotionMeta)4 DyeColor (org.bukkit.DyeColor)3 Enchantment (org.bukkit.enchantments.Enchantment)3 PotionData (org.bukkit.potion.PotionData)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 GlowBanner (net.glowstone.block.entity.state.GlowBanner)2 FireworkEffectMeta (org.bukkit.inventory.meta.FireworkEffectMeta)2 FireworkMeta (org.bukkit.inventory.meta.FireworkMeta)2 MapMeta (org.bukkit.inventory.meta.MapMeta)2