Search in sources :

Example 1 with EnchantmentTag

use of com.denizenscript.denizen.objects.EnchantmentTag in project Denizen-For-Bukkit by DenizenScript.

the class ItemEnchantments method getObjectAttribute.

@Override
public ObjectTag getObjectAttribute(Attribute attribute) {
    if (attribute == null) {
        return null;
    }
    // -->
    if (attribute.startsWith("is_enchanted")) {
        return new ElementTag(getEnchantments().size() > 0).getObjectAttribute(attribute.fulfill(1));
    }
    if (attribute.startsWith("enchantments.with_levels")) {
        Deprecations.itemEnchantmentTags.warn(attribute.context);
        Set<Map.Entry<Enchantment, Integer>> enchantments = getEnchantments();
        ListTag enchants = new ListTag();
        for (Map.Entry<Enchantment, Integer> enchantment : enchantments) {
            enchants.add(new EnchantmentTag(enchantment.getKey()).getCleanName() + "," + enchantment.getValue());
        }
        return enchants.getObjectAttribute(attribute.fulfill(2));
    }
    if (attribute.startsWith("enchantments.levels")) {
        Deprecations.itemEnchantmentTags.warn(attribute.context);
        Set<Map.Entry<Enchantment, Integer>> enchantments = getEnchantments();
        ListTag enchants = new ListTag();
        for (Map.Entry<Enchantment, Integer> enchantment : enchantments) {
            enchants.add(String.valueOf(enchantment.getValue()));
        }
        return enchants.getObjectAttribute(attribute.fulfill(2));
    }
    if (attribute.startsWith("enchantments.level") && attribute.hasContext(2)) {
        Deprecations.itemEnchantmentTags.warn(attribute.context);
        Set<Map.Entry<Enchantment, Integer>> enchantments = getEnchantments();
        if (enchantments.size() > 0) {
            for (Map.Entry<Enchantment, Integer> enchantment : enchantments) {
                if (enchantment.getKey().getName().equalsIgnoreCase(attribute.getContext(2)) || new EnchantmentTag(enchantment.getKey()).getCleanName().equalsIgnoreCase(attribute.getContext(2))) {
                    return new ElementTag(enchantment.getValue()).getObjectAttribute(attribute.fulfill(2));
                }
            }
        }
        return new ElementTag(0).getObjectAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("enchantment_types")) {
        Set<Map.Entry<Enchantment, Integer>> enchantments = getEnchantments();
        ListTag enchants = new ListTag();
        for (Map.Entry<Enchantment, Integer> enchantment : enchantments) {
            enchants.addObject(new EnchantmentTag(enchantment.getKey()));
        }
        return enchants.getObjectAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("enchantments")) {
        Deprecations.itemEnchantmentsLegacy.warn(attribute.context);
        Set<Map.Entry<Enchantment, Integer>> enchantments = getEnchantments();
        ListTag enchants = new ListTag();
        for (Map.Entry<Enchantment, Integer> enchantment : enchantments) {
            enchants.add(new EnchantmentTag(enchantment.getKey()).getCleanName());
        }
        return enchants.getObjectAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("enchantment_map")) {
        return getEnchantmentMap().getObjectAttribute(attribute.fulfill(1));
    }
    return null;
}
Also used : ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) EnchantmentTag(com.denizenscript.denizen.objects.EnchantmentTag) Enchantment(org.bukkit.enchantments.Enchantment) ListTag(com.denizenscript.denizencore.objects.core.ListTag)

Example 2 with EnchantmentTag

use of com.denizenscript.denizen.objects.EnchantmentTag in project Denizen-For-Bukkit by DenizenScript.

the class ItemScriptContainer method getItemFrom.

public ItemTag getItemFrom(TagContext context) {
    if (isProcessing) {
        Debug.echoError("Item script contains (or chains to) a reference to itself. Cannot process.");
        return null;
    }
    if (context == null) {
        context = new BukkitTagContext(null, null, new ScriptTag(this));
    } else {
        context = new BukkitTagContext((BukkitTagContext) context);
        context.script = new ScriptTag(this);
    }
    // Try to use this script to make an item.
    ItemTag stack;
    isProcessing = true;
    try {
        if (!contains("material", String.class)) {
            Debug.echoError("Item script '" + getName() + "' does not contain a material. Script cannot function.");
            return null;
        }
        // Check validity of material
        String material = TagManager.tag(getString("material"), context);
        if (material.startsWith("m@")) {
            material = material.substring(2);
        }
        stack = ItemTag.valueOf(material, this);
        // Make sure we're working with a valid base ItemStack
        if (stack == null) {
            Debug.echoError("Item script '" + getName() + "' contains an invalid or incorrect material '" + material + "' (did you spell the material name wrong?). Script cannot function.");
            return null;
        }
        // Handle listed mechanisms
        if (contains("mechanisms", Map.class)) {
            YamlConfiguration mechs = getConfigurationSection("mechanisms");
            for (StringHolder key : mechs.getKeys(false)) {
                ObjectTag obj = CoreUtilities.objectToTagForm(mechs.get(key.low), context, true, true);
                stack.safeAdjust(new Mechanism(key.low, obj, context));
            }
        }
        // Set Display Name
        if (contains("display name", String.class)) {
            String displayName = TagManager.tag(getString("display name"), context);
            NMSHandler.getItemHelper().setDisplayName(stack, displayName);
        }
        // Set if the object is bound to the player
        if (contains("bound", String.class)) {
            Deprecations.boundWarning.warn(context);
        }
        // Set Lore
        if (contains("lore", List.class)) {
            List<String> lore = NMSHandler.getItemHelper().getLore(stack);
            if (lore == null) {
                lore = new ArrayList<>();
            }
            for (String line : getStringList("lore")) {
                line = TagManager.tag(line, context);
                lore.add(line);
            }
            CoreUtilities.fixNewLinesToListSeparation(lore);
            NMSHandler.getItemHelper().setLore(stack, lore);
        }
        // Set Durability
        if (contains("durability", String.class)) {
            short durability = Short.valueOf(getString("durability"));
            stack.setDurability(durability);
        }
        // Set Enchantments
        if (contains("enchantments", List.class)) {
            for (String enchantment : getStringList("enchantments")) {
                enchantment = TagManager.tag(enchantment, context);
                try {
                    // Build enchantment context
                    int level = 1;
                    int colon = enchantment.lastIndexOf(':');
                    if (colon == -1) {
                        Debug.echoError("Item script '" + getName() + "' has enchantment '" + enchantment + "' without a level.");
                    } else {
                        level = Integer.valueOf(enchantment.substring(colon + 1).replace(" ", ""));
                        enchantment = enchantment.substring(0, colon).replace(" ", "");
                    }
                    // Add enchantment
                    EnchantmentTag ench = EnchantmentTag.valueOf(enchantment, context);
                    if (ench == null) {
                        Debug.echoError("Item script '" + getName() + "' specifies enchantment '" + enchantment + "' which is invalid.");
                        continue;
                    }
                    if (stack.getBukkitMaterial() == Material.ENCHANTED_BOOK) {
                        EnchantmentStorageMeta meta = (EnchantmentStorageMeta) stack.getItemMeta();
                        meta.addStoredEnchant(ench.enchantment, level, true);
                        stack.setItemMeta(meta);
                    } else {
                        stack.getItemStack().addUnsafeEnchantment(ench.enchantment, level);
                        stack.resetCache();
                    }
                } catch (Exception ex) {
                    Debug.echoError("While constructing item script '" + getName() + "', encountered error while applying enchantment '" + enchantment + "':");
                    Debug.echoError(ex);
                }
            }
        }
        // Set Color
        if (contains("color", String.class)) {
            Deprecations.itemScriptColor.warn(context);
            String color = TagManager.tag(getString("color"), context);
            LeatherColorer.colorArmor(stack, color);
        }
        // Set Book
        if (contains("book", String.class)) {
            BookScriptContainer book = ScriptRegistry.getScriptContainer(TagManager.tag(getString("book"), context).replace("s@", ""));
            stack = book.writeBookTo(stack, context);
        }
        if (contains("flags", Map.class)) {
            YamlConfiguration flagSection = getConfigurationSection("flags");
            AbstractFlagTracker tracker = stack.getFlagTracker();
            for (StringHolder key : flagSection.getKeys(false)) {
                tracker.setFlag(key.str, CoreUtilities.objectToTagForm(flagSection.get(key.str), context, true, true), null);
            }
            stack.reapplyTracker(tracker);
        }
        stack.setItemScript(this);
    } catch (Exception e) {
        Debug.echoError("Woah! An exception has been called with this item script!");
        Debug.echoError(e);
        stack = null;
    } finally {
        isProcessing = false;
    }
    return stack;
}
Also used : YamlConfiguration(com.denizenscript.denizencore.utilities.YamlConfiguration) Mechanism(com.denizenscript.denizencore.objects.Mechanism) StringHolder(com.denizenscript.denizencore.utilities.text.StringHolder) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) EnchantmentStorageMeta(org.bukkit.inventory.meta.EnchantmentStorageMeta) BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) ScriptTag(com.denizenscript.denizencore.objects.core.ScriptTag) AbstractFlagTracker(com.denizenscript.denizencore.flags.AbstractFlagTracker) EnchantmentTag(com.denizenscript.denizen.objects.EnchantmentTag) ItemTag(com.denizenscript.denizen.objects.ItemTag)

Example 3 with EnchantmentTag

use of com.denizenscript.denizen.objects.EnchantmentTag in project Denizen-For-Bukkit by DenizenScript.

the class ItemEnchantments method adjust.

@Override
public void adjust(Mechanism mechanism) {
    // -->
    if (mechanism.matches("remove_enchantments")) {
        if (mechanism.hasValue()) {
            List<EnchantmentTag> toRemove = mechanism.valueAsType(ListTag.class).filter(EnchantmentTag.class, mechanism.context);
            if (item.getBukkitMaterial() == Material.ENCHANTED_BOOK) {
                EnchantmentStorageMeta meta = (EnchantmentStorageMeta) item.getItemMeta();
                for (EnchantmentTag ench : toRemove) {
                    meta.removeStoredEnchant(ench.enchantment);
                }
                item.setItemMeta(meta);
            } else {
                for (EnchantmentTag ench : toRemove) {
                    item.getItemStack().removeEnchantment(ench.enchantment);
                }
                item.resetCache();
            }
        } else {
            if (item.getBukkitMaterial() == Material.ENCHANTED_BOOK) {
                EnchantmentStorageMeta meta = (EnchantmentStorageMeta) item.getItemMeta();
                for (Enchantment ench : meta.getStoredEnchants().keySet()) {
                    meta.removeStoredEnchant(ench);
                }
                item.setItemMeta(meta);
            } else {
                for (Enchantment ench : item.getItemStack().getEnchantments().keySet()) {
                    item.getItemStack().removeEnchantment(ench);
                }
                item.resetCache();
            }
        }
    }
    // -->
    if (mechanism.matches("enchantments")) {
        String val = mechanism.getValue().asString();
        if (val.startsWith("map@") || val.startsWith("[") || (val.contains("=") && !val.contains(","))) {
            MapTag map = mechanism.valueAsType(MapTag.class);
            for (Map.Entry<StringHolder, ObjectTag> enchantments : map.map.entrySet()) {
                Enchantment ench = EnchantmentTag.valueOf(enchantments.getKey().low, mechanism.context).enchantment;
                int level = enchantments.getValue().asElement().asInt();
                if (ench != null) {
                    if (item.getBukkitMaterial() == Material.ENCHANTED_BOOK) {
                        EnchantmentStorageMeta meta = (EnchantmentStorageMeta) item.getItemMeta();
                        meta.addStoredEnchant(ench, level, true);
                        item.setItemMeta(meta);
                    } else {
                        item.getItemStack().addUnsafeEnchantment(ench, level);
                        item.resetCache();
                    }
                } else {
                    mechanism.echoError("Unknown enchantment '" + enchantments.getKey().str + "'");
                }
            }
        } else {
            for (String enchant : mechanism.valueAsType(ListTag.class)) {
                if (!enchant.contains(",")) {
                    mechanism.echoError("Invalid enchantment format, use name,level|...");
                } else {
                    String[] data = enchant.split(",", 2);
                    try {
                        Enchantment ench = EnchantmentTag.valueOf(data[0], mechanism.context).enchantment;
                        if (ench != null) {
                            if (item.getBukkitMaterial() == Material.ENCHANTED_BOOK) {
                                EnchantmentStorageMeta meta = (EnchantmentStorageMeta) item.getItemMeta();
                                meta.addStoredEnchant(ench, Integer.valueOf(data[1]), true);
                                item.setItemMeta(meta);
                            } else {
                                item.getItemStack().addUnsafeEnchantment(ench, Integer.valueOf(data[1]));
                                item.resetCache();
                            }
                        } else {
                            mechanism.echoError("Unknown enchantment '" + data[0] + "'");
                        }
                    } catch (NullPointerException e) {
                        mechanism.echoError("Unknown enchantment '" + data[0] + "'");
                    } catch (NumberFormatException ex) {
                        mechanism.echoError("Cannot apply enchantment '" + data[0] + "': '" + data[1] + "' is not a valid integer!");
                        if (Debug.verbose) {
                            Debug.echoError(ex);
                        }
                    }
                }
            }
        }
    }
}
Also used : ListTag(com.denizenscript.denizencore.objects.core.ListTag) MapTag(com.denizenscript.denizencore.objects.core.MapTag) EnchantmentStorageMeta(org.bukkit.inventory.meta.EnchantmentStorageMeta) StringHolder(com.denizenscript.denizencore.utilities.text.StringHolder) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) EnchantmentTag(com.denizenscript.denizen.objects.EnchantmentTag) Enchantment(org.bukkit.enchantments.Enchantment)

Aggregations

EnchantmentTag (com.denizenscript.denizen.objects.EnchantmentTag)3 ObjectTag (com.denizenscript.denizencore.objects.ObjectTag)2 ListTag (com.denizenscript.denizencore.objects.core.ListTag)2 StringHolder (com.denizenscript.denizencore.utilities.text.StringHolder)2 Enchantment (org.bukkit.enchantments.Enchantment)2 EnchantmentStorageMeta (org.bukkit.inventory.meta.EnchantmentStorageMeta)2 ItemTag (com.denizenscript.denizen.objects.ItemTag)1 BukkitTagContext (com.denizenscript.denizen.tags.BukkitTagContext)1 AbstractFlagTracker (com.denizenscript.denizencore.flags.AbstractFlagTracker)1 Mechanism (com.denizenscript.denizencore.objects.Mechanism)1 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)1 MapTag (com.denizenscript.denizencore.objects.core.MapTag)1 ScriptTag (com.denizenscript.denizencore.objects.core.ScriptTag)1 YamlConfiguration (com.denizenscript.denizencore.utilities.YamlConfiguration)1