Search in sources :

Example 1 with ItemScriptContainer

use of com.denizenscript.denizen.scripts.containers.core.ItemScriptContainer in project Denizen-For-Bukkit by DenizenScript.

the class ItemTag method valueOf.

@Fetchable("i")
public static ItemTag valueOf(String string, TagContext context) {
    if (string == null || string.equals("")) {
        return null;
    }
    ItemTag stack = null;
    if (ObjectFetcher.isObjectWithProperties(string)) {
        return ObjectFetcher.getObjectFromWithProperties(ItemTag.class, string, context);
    }
    if (string.startsWith("i@")) {
        string = string.substring("i@".length());
    }
    string = CoreUtilities.toLowerCase(string);
    try {
        if (ScriptRegistry.containsScript(string, ItemScriptContainer.class)) {
            ItemScriptContainer isc = ScriptRegistry.getScriptContainerAs(string, ItemScriptContainer.class);
            // TODO: If a script does not contain tags, get the clean reference here.
            stack = isc.getItemFrom(context);
            if (stack == null && (context == null || context.showErrors())) {
                Debug.echoError("Item script '" + isc.getName() + "' returned a null item.");
            }
        } else if (ScriptRegistry.containsScript(string, BookScriptContainer.class)) {
            BookScriptContainer book = ScriptRegistry.getScriptContainerAs(string, BookScriptContainer.class);
            stack = book.getBookFrom(context);
            if (stack == null && (context == null || context.showErrors())) {
                Debug.echoError("Book script '" + book.getName() + "' returned a null item.");
            }
        }
        if (stack != null) {
            return stack;
        }
    } catch (Exception ex) {
        if (Debug.verbose) {
            Debug.echoError(ex);
        }
    }
    try {
        MaterialTag mat = MaterialTag.valueOf(string.toUpperCase(), context);
        if (mat != null) {
            stack = new ItemTag(mat.getMaterial());
        }
        if (stack != null) {
            return stack;
        }
    } catch (Exception ex) {
        if (!string.equalsIgnoreCase("none") && (context == null || context.showErrors())) {
            Debug.log("Does not match a valid item ID or material: " + string);
        }
        if (Debug.verbose) {
            Debug.echoError(ex);
        }
    }
    if (context == null || context.showErrors()) {
        Debug.log("valueOf ItemTag returning null: " + string);
    }
    return null;
}
Also used : ItemScriptContainer(com.denizenscript.denizen.scripts.containers.core.ItemScriptContainer) BookScriptContainer(com.denizenscript.denizen.scripts.containers.core.BookScriptContainer)

Example 2 with ItemScriptContainer

use of com.denizenscript.denizen.scripts.containers.core.ItemScriptContainer 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)

Aggregations

ItemScriptContainer (com.denizenscript.denizen.scripts.containers.core.ItemScriptContainer)2 BookScriptContainer (com.denizenscript.denizen.scripts.containers.core.BookScriptContainer)1 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)1 ListTag (com.denizenscript.denizencore.objects.core.ListTag)1 Keyed (org.bukkit.Keyed)1 NamespacedKey (org.bukkit.NamespacedKey)1 YamlConfiguration (org.bukkit.configuration.file.YamlConfiguration)1 BlockStateMeta (org.bukkit.inventory.meta.BlockStateMeta)1