Search in sources :

Example 1 with Repairable

use of org.bukkit.inventory.meta.Repairable 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)

Example 2 with Repairable

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

the class ItemStorage method deserialiseMeta.

private static void deserialiseMeta(DataKey root, ItemStack res) {
    if (root.keyExists("flags")) {
        ItemMeta meta = ensureMeta(res);
        for (DataKey key : root.getRelative("flags").getIntegerSubKeys()) {
            meta.addItemFlags(ItemFlag.valueOf(key.getString("")));
        }
    }
    if (root.keyExists("lore")) {
        ItemMeta meta = ensureMeta(res);
        List<String> lore = Lists.newArrayList();
        for (DataKey key : root.getRelative("lore").getIntegerSubKeys()) lore.add(key.getString(""));
        meta.setLore(lore);
        res.setItemMeta(meta);
    }
    if (root.keyExists("displayname")) {
        ItemMeta meta = ensureMeta(res);
        meta.setDisplayName(root.getString("displayname"));
        res.setItemMeta(meta);
    }
    if (root.keyExists("firework")) {
        FireworkMeta meta = ensureMeta(res);
        for (DataKey sub : root.getRelative("firework.effects").getIntegerSubKeys()) {
            meta.addEffect(deserialiseFireworkEffect(sub));
        }
        meta.setPower(root.getInt("firework.power"));
        res.setItemMeta(meta);
    }
    if (root.keyExists("book")) {
        BookMeta meta = ensureMeta(res);
        for (DataKey sub : root.getRelative("book.pages").getIntegerSubKeys()) {
            meta.addPage(sub.getString(""));
        }
        meta.setTitle(root.getString("book.title"));
        meta.setAuthor(root.getString("book.author"));
        res.setItemMeta(meta);
    }
    if (root.keyExists("armor")) {
        LeatherArmorMeta meta = ensureMeta(res);
        meta.setColor(Color.fromRGB(root.getInt("armor.color")));
        res.setItemMeta(meta);
    }
    if (root.keyExists("map")) {
        MapMeta meta = ensureMeta(res);
        meta.setScaling(root.getBoolean("map.scaling"));
        res.setItemMeta(meta);
    }
    if (root.keyExists("blockstate")) {
        BlockStateMeta meta = ensureMeta(res);
        if (root.keyExists("blockstate.banner")) {
            Banner banner = (Banner) meta.getBlockState();
            deserialiseBanner(root.getRelative("blockstate"), banner);
            banner.update(true);
            meta.setBlockState(banner);
        }
        res.setItemMeta(meta);
    }
    if (root.keyExists("enchantmentstorage")) {
        EnchantmentStorageMeta meta = ensureMeta(res);
        for (DataKey key : root.getRelative("enchantmentstorage").getSubKeys()) {
            meta.addStoredEnchant(Enchantment.getByName(key.name()), key.getInt(""), true);
        }
        res.setItemMeta(meta);
    }
    if (root.keyExists("skull")) {
        SkullMeta meta = ensureMeta(res);
        if (root.keyExists("skull.owner") && !root.getString("skull.owner").isEmpty()) {
            meta.setOwner(root.getString("skull.owner", ""));
        }
        res.setItemMeta(meta);
    }
    if (root.keyExists("banner")) {
        BannerMeta meta = ensureMeta(res);
        if (root.keyExists("banner.basecolor")) {
            meta.setBaseColor(DyeColor.valueOf(root.getString("banner.basecolor")));
        }
        if (root.keyExists("banner.patterns")) {
            for (DataKey sub : root.getRelative("banner.patterns").getIntegerSubKeys()) {
                Pattern pattern = new Pattern(DyeColor.valueOf(sub.getString("color")), PatternType.getByIdentifier(sub.getString("type")));
                meta.addPattern(pattern);
            }
        }
    }
    if (root.keyExists("potion")) {
        PotionMeta meta = ensureMeta(res);
        PotionData data = new PotionData(PotionType.valueOf(root.getString("potion.data.type")), root.getBoolean("potion.data.extended"), root.getBoolean("potion.data.upgraded"));
        meta.setBasePotionData(data);
        for (DataKey sub : root.getRelative("potion.effects").getIntegerSubKeys()) {
            int duration = sub.getInt("duration");
            int amplifier = sub.getInt("amplifier");
            PotionEffectType type = PotionEffectType.getByName(sub.getString("type"));
            boolean ambient = sub.getBoolean("ambient");
            meta.addCustomEffect(new PotionEffect(type, duration, amplifier, ambient), true);
        }
        res.setItemMeta(meta);
    }
    if (root.keyExists("repaircost") && res.getItemMeta() instanceof Repairable) {
        ((Repairable) res.getItemMeta()).setRepairCost(root.getInt("repaircost"));
    }
    ItemMeta meta = res.getItemMeta();
    if (meta != null) {
        try {
            meta.setUnbreakable(root.getBoolean("unbreakable", false));
        } catch (Throwable t) {
        // probably backwards-compat issue, don't log
        }
        res.setItemMeta(meta);
    }
    Bukkit.getPluginManager().callEvent(new CitizensDeserialiseMetaEvent(root, res));
}
Also used : Repairable(org.bukkit.inventory.meta.Repairable) BannerMeta(org.bukkit.inventory.meta.BannerMeta) Pattern(org.bukkit.block.banner.Pattern) CitizensDeserialiseMetaEvent(net.citizensnpcs.api.event.CitizensDeserialiseMetaEvent) PotionEffect(org.bukkit.potion.PotionEffect) Banner(org.bukkit.block.Banner) PotionEffectType(org.bukkit.potion.PotionEffectType) FireworkMeta(org.bukkit.inventory.meta.FireworkMeta) SkullMeta(org.bukkit.inventory.meta.SkullMeta) PotionMeta(org.bukkit.inventory.meta.PotionMeta) BlockStateMeta(org.bukkit.inventory.meta.BlockStateMeta) EnchantmentStorageMeta(org.bukkit.inventory.meta.EnchantmentStorageMeta) PotionData(org.bukkit.potion.PotionData) MapMeta(org.bukkit.inventory.meta.MapMeta) LeatherArmorMeta(org.bukkit.inventory.meta.LeatherArmorMeta) BookMeta(org.bukkit.inventory.meta.BookMeta) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Example 3 with Repairable

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

the class ItemRepairCost method adjust.

@Override
public void adjust(Mechanism mechanism) {
    // -->
    if (mechanism.matches("repair_cost") && mechanism.requireInteger()) {
        Repairable meta = ((Repairable) item.getItemMeta());
        meta.setRepairCost(mechanism.getValue().asInt());
        item.setItemMeta((ItemMeta) meta);
    }
}
Also used : Repairable(org.bukkit.inventory.meta.Repairable)

Aggregations

Repairable (org.bukkit.inventory.meta.Repairable)3 Banner (org.bukkit.block.Banner)2 Pattern (org.bukkit.block.banner.Pattern)2 BannerMeta (org.bukkit.inventory.meta.BannerMeta)2 BlockStateMeta (org.bukkit.inventory.meta.BlockStateMeta)2 BookMeta (org.bukkit.inventory.meta.BookMeta)2 EnchantmentStorageMeta (org.bukkit.inventory.meta.EnchantmentStorageMeta)2 FireworkMeta (org.bukkit.inventory.meta.FireworkMeta)2 LeatherArmorMeta (org.bukkit.inventory.meta.LeatherArmorMeta)2 MapMeta (org.bukkit.inventory.meta.MapMeta)2 PotionMeta (org.bukkit.inventory.meta.PotionMeta)2 SkullMeta (org.bukkit.inventory.meta.SkullMeta)2 PotionData (org.bukkit.potion.PotionData)2 PotionEffect (org.bukkit.potion.PotionEffect)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CitizensDeserialiseMetaEvent (net.citizensnpcs.api.event.CitizensDeserialiseMetaEvent)1 CitizensSerialiseMetaEvent (net.citizensnpcs.api.event.CitizensSerialiseMetaEvent)1 Color (org.bukkit.Color)1 DyeColor (org.bukkit.DyeColor)1