Search in sources :

Example 16 with BlockStateMeta

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

the class ItemStorage method deserialiseMeta.

private static void deserialiseMeta(DataKey root, ItemStack res) {
    if (root.keyExists("encoded-meta")) {
        byte[] raw = BaseEncoding.base64().decode(root.getString("encoded-meta"));
        try {
            BukkitObjectInputStream inp = new BukkitObjectInputStream(new ByteArrayInputStream(raw));
            ItemMeta meta = (ItemMeta) inp.readObject();
            res.setItemMeta(meta);
            Bukkit.getPluginManager().callEvent(new CitizensDeserialiseMetaEvent(root, res));
            return;
        } catch (IOException e1) {
            e1.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    if (SUPPORTS_CUSTOM_MODEL_DATA) {
        try {
            if (root.keyExists("custommodel")) {
                ItemMeta meta = ensureMeta(res);
                meta.setCustomModelData(root.getInt("custommodel"));
                res.setItemMeta(meta);
            }
        } catch (Throwable t) {
            SUPPORTS_CUSTOM_MODEL_DATA = false;
        }
    }
    if (root.keyExists("flags")) {
        ItemMeta meta = ensureMeta(res);
        for (DataKey key : root.getRelative("flags").getIntegerSubKeys()) {
            meta.addItemFlags(ItemFlag.valueOf(key.getString("")));
        }
        res.setItemMeta(meta);
    }
    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(deserialiseEnchantment(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", ""));
        }
        if (root.keyExists("skull.texture") && !root.getString("skull.texture").isEmpty()) {
            CitizensAPI.getSkullMetaProvider().setTexture(root.getString("skull.texture", ""), meta);
        }
        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);
            }
        }
        res.setItemMeta(meta);
    }
    if (root.keyExists("potion")) {
        PotionMeta meta = ensureMeta(res);
        try {
            PotionData data = new PotionData(PotionType.valueOf(root.getString("potion.data.type")), root.getBoolean("potion.data.extended"), root.getBoolean("potion.data.upgraded"));
            meta.setBasePotionData(data);
        } catch (Throwable t) {
        }
        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("crossbow") && SUPPORTS_1_14_API) {
        CrossbowMeta meta = null;
        try {
            meta = ensureMeta(res);
        } catch (Throwable t) {
            SUPPORTS_1_14_API = false;
        // old MC version
        }
        if (meta != null) {
            for (DataKey key : root.getRelative("crossbow.projectiles").getSubKeys()) {
                meta.addChargedProjectile(ItemStorage.loadItemStack(key));
            }
            res.setItemMeta(meta);
        }
    }
    if (root.keyExists("repaircost") && res.getItemMeta() instanceof Repairable) {
        ItemMeta meta = ensureMeta(res);
        ((Repairable) meta).setRepairCost(root.getInt("repaircost"));
        res.setItemMeta(meta);
    }
    if (root.keyExists("attributes") && SUPPORTS_ATTRIBUTES) {
        ItemMeta meta = ensureMeta(res);
        try {
            for (DataKey attr : root.getRelative("attributes").getSubKeys()) {
                Attribute attribute = Attribute.valueOf(attr.name());
                for (DataKey modifier : attr.getIntegerSubKeys()) {
                    UUID uuid = UUID.fromString(modifier.getString("uuid"));
                    String name = modifier.getString("name");
                    double amount = modifier.getDouble("amount");
                    Operation operation = Operation.valueOf(modifier.getString("operation"));
                    EquipmentSlot slot = modifier.keyExists("slot") ? EquipmentSlot.valueOf(modifier.getString("slot")) : null;
                    meta.addAttributeModifier(attribute, new AttributeModifier(uuid, name, amount, operation, slot));
                }
            }
        } catch (Throwable e) {
            SUPPORTS_ATTRIBUTES = false;
        }
        res.setItemMeta(meta);
    }
    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 : CitizensDeserialiseMetaEvent(net.citizensnpcs.api.event.CitizensDeserialiseMetaEvent) PotionEffect(org.bukkit.potion.PotionEffect) Attribute(org.bukkit.attribute.Attribute) EquipmentSlot(org.bukkit.inventory.EquipmentSlot) SkullMeta(org.bukkit.inventory.meta.SkullMeta) Operation(org.bukkit.attribute.AttributeModifier.Operation) PotionData(org.bukkit.potion.PotionData) UUID(java.util.UUID) ItemMeta(org.bukkit.inventory.meta.ItemMeta) CrossbowMeta(org.bukkit.inventory.meta.CrossbowMeta) Repairable(org.bukkit.inventory.meta.Repairable) BannerMeta(org.bukkit.inventory.meta.BannerMeta) Pattern(org.bukkit.block.banner.Pattern) BukkitObjectInputStream(org.bukkit.util.io.BukkitObjectInputStream) Banner(org.bukkit.block.Banner) PotionEffectType(org.bukkit.potion.PotionEffectType) FireworkMeta(org.bukkit.inventory.meta.FireworkMeta) PotionMeta(org.bukkit.inventory.meta.PotionMeta) IOException(java.io.IOException) AttributeModifier(org.bukkit.attribute.AttributeModifier) BlockStateMeta(org.bukkit.inventory.meta.BlockStateMeta) EnchantmentStorageMeta(org.bukkit.inventory.meta.EnchantmentStorageMeta) ByteArrayInputStream(java.io.ByteArrayInputStream) MapMeta(org.bukkit.inventory.meta.MapMeta) LeatherArmorMeta(org.bukkit.inventory.meta.LeatherArmorMeta) BookMeta(org.bukkit.inventory.meta.BookMeta)

Example 17 with BlockStateMeta

use of org.bukkit.inventory.meta.BlockStateMeta in project Essentials by EssentialsX.

the class BlockMetaSpawnerProvider method setEntityType.

@Override
public ItemStack setEntityType(ItemStack is, EntityType type) {
    BlockStateMeta bsm = (BlockStateMeta) is.getItemMeta();
    BlockState bs = bsm.getBlockState();
    ((CreatureSpawner) bs).setSpawnedType(type);
    bsm.setBlockState(bs);
    is.setItemMeta(bsm);
    return setDisplayName(is, type);
}
Also used : BlockStateMeta(org.bukkit.inventory.meta.BlockStateMeta) BlockState(org.bukkit.block.BlockState) CreatureSpawner(org.bukkit.block.CreatureSpawner)

Example 18 with BlockStateMeta

use of org.bukkit.inventory.meta.BlockStateMeta in project Essentials by EssentialsX.

the class BlockMetaSpawnerProvider method getEntityType.

@Override
public EntityType getEntityType(ItemStack is) {
    BlockStateMeta bsm = (BlockStateMeta) is.getItemMeta();
    CreatureSpawner bs = (CreatureSpawner) bsm.getBlockState();
    return bs.getSpawnedType();
}
Also used : BlockStateMeta(org.bukkit.inventory.meta.BlockStateMeta) CreatureSpawner(org.bukkit.block.CreatureSpawner)

Example 19 with BlockStateMeta

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

the class ItemTag method registerTags.

public static void registerTags() {
    AbstractFlagTracker.registerFlagHandlers(tagProcessor);
    PropertyParser.registerPropertyTagHandlers(ItemTag.class, tagProcessor);
    // <--[tag]
    // @attribute <ItemTag.repairable>
    // @returns ElementTag(Boolean)
    // @group properties
    // @description
    // Returns whether the item can be repaired.
    // If this returns true, it will enable access to:
    // <@link mechanism ItemTag.durability>,
    // <@link tag ItemTag.max_durability>, and <@link tag ItemTag.durability>.
    // Note that due to odd design choices in Spigot, this is effectively true for all items, even though the durability value of most items is locked at zero.
    // -->
    tagProcessor.registerTag(ElementTag.class, "repairable", (attribute, object) -> {
        return new ElementTag(ItemDurability.describes(object));
    });
    // <--[tag]
    // @attribute <ItemTag.is_book>
    // @returns ElementTag(Boolean)
    // @group properties
    // @description
    // Returns whether the item is considered an editable book.
    // If this returns true, it will enable access to:
    // <@link mechanism ItemTag.book>,
    // <@link tag ItemTag.book_author>, <@link tag ItemTag.book_title>, and <@link tag ItemTag.book_pages>.
    // -->
    tagProcessor.registerTag(ElementTag.class, "is_book", (attribute, object) -> {
        return new ElementTag(ItemBook.describes(object));
    });
    // <--[tag]
    // @attribute <ItemTag.is_colorable>
    // @returns ElementTag(Boolean)
    // @group properties
    // @description
    // Returns whether the item can have a custom color.
    // If this returns true, it will enable access to:
    // <@link mechanism ItemTag.color>, and <@link tag ItemTag.color>.
    // -->
    tagProcessor.registerTag(ElementTag.class, "is_colorable", (attribute, object) -> {
        return new ElementTag(ItemColor.describes(object));
    });
    tagProcessor.registerTag(ElementTag.class, "is_dyeable", (attribute, object) -> {
        return new ElementTag(ItemColor.describes(object));
    });
    // <--[tag]
    // @attribute <ItemTag.is_firework>
    // @returns ElementTag(Boolean)
    // @group properties
    // @description
    // Returns whether the item is a firework.
    // If this returns true, it will enable access to:
    // <@link mechanism ItemTag.firework>, and <@link tag ItemTag.firework>.
    // -->
    tagProcessor.registerTag(ElementTag.class, "is_firework", (attribute, object) -> {
        return new ElementTag(ItemFirework.describes(object));
    });
    // <--[tag]
    // @attribute <ItemTag.has_inventory>
    // @returns ElementTag(Boolean)
    // @group properties
    // @description
    // Returns whether the item has an inventory.
    // If this returns true, it will enable access to:
    // <@link mechanism ItemTag.inventory_contents>, and <@link tag ItemTag.inventory_contents>.
    // -->
    tagProcessor.registerTag(ElementTag.class, "has_inventory", (attribute, object) -> {
        return new ElementTag(ItemInventory.describes(object));
    });
    // <--[tag]
    // @attribute <ItemTag.is_lockable>
    // @returns ElementTag(Boolean)
    // @group properties
    // @description
    // Returns whether the item is lockable.
    // If this returns true, it will enable access to:
    // <@link mechanism ItemTag.lock>, and <@link tag ItemTag.lock>.
    // -->
    tagProcessor.registerTag(ElementTag.class, "is_lockable", (attribute, object) -> {
        return new ElementTag(ItemLock.describes(object));
    });
    // <--[tag]
    // @attribute <ItemTag.material>
    // @returns MaterialTag
    // @mechanism ItemTag.material
    // @group conversion
    // @description
    // Returns the MaterialTag that is the basis of the item.
    // EG, a stone with lore and a display name, etc. will return only "m@stone".
    // -->
    tagProcessor.registerTag(ObjectTag.class, "material", (attribute, object) -> {
        if (attribute.getAttribute(2).equals("formatted")) {
            return object;
        }
        if (object.getItemMeta() instanceof BlockStateMeta) {
            if (object.getBukkitMaterial() == Material.SHIELD) {
                MaterialTag material = new MaterialTag(Material.SHIELD);
                material.setModernData(((BlockStateMeta) object.getItemMeta()).getBlockState().getBlockData());
                return material;
            }
            return new MaterialTag(((BlockStateMeta) object.getItemMeta()).getBlockState());
        }
        return object.getMaterial();
    });
    // <--[tag]
    // @attribute <ItemTag.json>
    // @returns ElementTag
    // @group conversion
    // @description
    // Returns the item converted to a raw JSON object with one layer of escaping for network transmission.
    // EG, via /tellraw.
    // Generally, prefer tags like <@link tag ElementTag.on_hover.type> with type 'show_item'.
    // -->
    tagProcessor.registerTag(ElementTag.class, "json", (attribute, object) -> {
        return new ElementTag(NMSHandler.getItemHelper().getJsonString(object.item));
    });
    // <--[tag]
    // @attribute <ItemTag.meta_type>
    // @returns ElementTag
    // @group conversion
    // @description
    // Returns the name of the Bukkit item meta type that applies to this item.
    // This is for debugging purposes.
    // -->
    tagProcessor.registerTag(ElementTag.class, "meta_type", (attribute, object) -> {
        return new ElementTag(object.getItemMeta().getClass().getName());
    });
    // <--[tag]
    // @attribute <ItemTag.bukkit_serial>
    // @returns ElementTag
    // @group conversion
    // @description
    // Returns a YAML text section representing the Bukkit serialization of the item, under subkey "item".
    // -->
    tagProcessor.registerTag(ElementTag.class, "bukkit_serial", (attribute, object) -> {
        YamlConfiguration config = new YamlConfiguration();
        config.set("item", object.getItemStack());
        return new ElementTag(config.saveToString());
    });
    // <--[tag]
    // @attribute <ItemTag.simple>
    // @returns ElementTag
    // @group conversion
    // @description
    // Returns a simple reusable item identification for this item, with minimal extra data.
    // -->
    tagProcessor.registerTag(ElementTag.class, "simple", (attribute, object) -> {
        return new ElementTag(object.identifySimple());
    });
    // <--[tag]
    // @attribute <ItemTag.recipe_ids[(<type>)]>
    // @returns ListTag
    // @description
    // If the item is a scripted item, returns a list of all recipe IDs created by the item script.
    // Others, returns a list of all recipe IDs that the server lists as capable of crafting the item.
    // Returns a list in the Namespace:Key format, for example "minecraft:gold_nugget".
    // Optionally, specify a recipe type (CRAFTING, FURNACE, COOKING, BLASTING, SHAPED, SHAPELESS, SMOKING, STONECUTTING)
    // to limit to just recipes of that type.
    // -->
    tagProcessor.registerTag(ListTag.class, "recipe_ids", (attribute, object) -> {
        String type = attribute.hasParam() ? CoreUtilities.toLowerCase(attribute.getParam()) : null;
        ItemScriptContainer container = object.isItemscript() ? ItemScriptHelper.getItemScriptContainer(object.getItemStack()) : null;
        ListTag list = new ListTag();
        for (Recipe recipe : Bukkit.getRecipesFor(object.getItemStack())) {
            if (!Utilities.isRecipeOfType(recipe, type)) {
                continue;
            }
            if (recipe instanceof Keyed) {
                NamespacedKey key = ((Keyed) recipe).getKey();
                if (key.getNamespace().equalsIgnoreCase("denizen")) {
                    if (container != ItemScriptHelper.recipeIdToItemScript.get(key.toString())) {
                        continue;
                    }
                } else if (container != null) {
                    continue;
                }
                list.add(key.toString());
            }
        }
        return list;
    });
    // <--[tag]
    // @attribute <ItemTag.formatted>
    // @returns ElementTag
    // @group formatting
    // @description
    // Returns the formatted material name of the item to be used in a sentence.
    // Correctly uses singular and plural forms of item names, among other things.
    // -->
    tagProcessor.registerTag(ElementTag.class, "formatted", (attribute, object) -> {
        return new ElementTag(object.formattedName());
    });
    // <--[tag]
    // @attribute <ItemTag.advanced_matches[<matcher>]>
    // @returns ElementTag(Boolean)
    // @group element checking
    // @description
    // Returns whether the item matches some matcher text, using the system behind <@link language Advanced Script Event Matching>.
    // -->
    tagProcessor.registerTag(ElementTag.class, "advanced_matches", (attribute, object) -> {
        if (!attribute.hasParam()) {
            return null;
        }
        return new ElementTag(BukkitScriptEvent.tryItem(object, attribute.getParam()));
    });
}
Also used : BlockStateMeta(org.bukkit.inventory.meta.BlockStateMeta) ItemScriptContainer(com.denizenscript.denizen.scripts.containers.core.ItemScriptContainer) NamespacedKey(org.bukkit.NamespacedKey) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) ListTag(com.denizenscript.denizencore.objects.core.ListTag) Keyed(org.bukkit.Keyed)

Example 20 with BlockStateMeta

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

the class ItemSpawnerCount method getPropertyString.

@Override
public String getPropertyString() {
    BlockStateMeta meta = (BlockStateMeta) item.getItemMeta();
    CreatureSpawner state = (CreatureSpawner) meta.getBlockState();
    return String.valueOf(state.getSpawnCount());
}
Also used : BlockStateMeta(org.bukkit.inventory.meta.BlockStateMeta) CreatureSpawner(org.bukkit.block.CreatureSpawner)

Aggregations

BlockStateMeta (org.bukkit.inventory.meta.BlockStateMeta)32 CreatureSpawner (org.bukkit.block.CreatureSpawner)22 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)6 Banner (org.bukkit.block.Banner)5 BannerMeta (org.bukkit.inventory.meta.BannerMeta)5 ItemMeta (org.bukkit.inventory.meta.ItemMeta)5 ListTag (com.denizenscript.denizencore.objects.core.ListTag)4 ItemStack (org.bukkit.inventory.ItemStack)3 EntityTag (com.denizenscript.denizen.objects.EntityTag)2 BlockState (org.bukkit.block.BlockState)2 InventoryHolder (org.bukkit.inventory.InventoryHolder)2 InventoryTag (com.denizenscript.denizen.objects.InventoryTag)1 ItemTag (com.denizenscript.denizen.objects.ItemTag)1 ItemScriptContainer (com.denizenscript.denizen.scripts.containers.core.ItemScriptContainer)1 Argument (com.denizenscript.denizencore.objects.Argument)1 MapTag (com.denizenscript.denizencore.objects.core.MapTag)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1