Search in sources :

Example 16 with BukkitTagContext

use of net.aufdemrand.denizen.tags.BukkitTagContext in project Denizen-For-Bukkit by DenizenScript.

the class ConstantTags method constantTags.

@TagManager.TagEvents
public void constantTags(ReplaceableTagEvent event) {
    if (!event.matches("cons")) {
        return;
    }
    if (!event.hasValue()) {
        dB.echoError("Constant tag '" + event.raw_tag + " does not contain a valid constant! " + "Replacement has been aborted...");
        return;
    }
    dB.echoError(event.getAttributes().getScriptEntry().getResidingQueue(), "constant: tags are deprecated! Use <npc.constant[]>!");
    NPC npc = null;
    if (event.getType() != null && event.getType().matches("\\d+")) {
        npc = CitizensAPI.getNPCRegistry().getById(Integer.valueOf(event.getType()));
    } else if (((BukkitTagContext) event.getContext()).npc != null) {
        npc = ((BukkitTagContext) event.getContext()).npc.getCitizen();
    }
    if (npc == null) {
        dB.echoError("Constant tag '" + event.raw_tag + " does not contain a valid NPC! " + "Has the NPC been removed, or is there no NPC list available? " + "Replacement has been aborted...");
        return;
    }
    Attribute attribute = event.getAttributes();
    if (npc.hasTrait(ConstantsTrait.class) && npc.getTrait(ConstantsTrait.class).getConstant(event.getValue()) != null) {
        event.setReplaced(new Element(npc.getTrait(ConstantsTrait.class).getConstant(event.getValue())).getAttribute(attribute.fulfill(1)));
    }
}
Also used : NPC(net.citizensnpcs.api.npc.NPC) BukkitTagContext(net.aufdemrand.denizen.tags.BukkitTagContext) Attribute(net.aufdemrand.denizencore.tags.Attribute) ConstantsTrait(net.aufdemrand.denizen.npc.traits.ConstantsTrait) Element(net.aufdemrand.denizencore.objects.Element)

Example 17 with BukkitTagContext

use of net.aufdemrand.denizen.tags.BukkitTagContext in project Denizen-For-Bukkit by DenizenScript.

the class FlagTags method flagTag.

/**
     * Replaces FLAG TAGs. Called automatically by the dScript ScriptBuilder and Executer.
     *
     * @param event ReplaceableTagEvent
     */
@TagManager.TagEvents
public void flagTag(ReplaceableTagEvent event) {
    if (!event.matches("FLAG")) {
        return;
    }
    if (!event.hasValue()) {
        return;
    }
    dB.echoError(event.getAttributes().getScriptEntry().getResidingQueue(), "flag.x: tags are deprecated! Use <x.flag[]>, EG, <global.flag[name]>!");
    // Replace <FLAG...> TAGs.
    String flagName = event.getValue().split(":").length > 1 ? event.getValue().split(":")[0].toUpperCase() : event.getValue().toUpperCase();
    String flagFallback = event.hasAlternative() ? event.getAlternative() : "EMPTY";
    int index = -1;
    ReplaceType replaceType = ReplaceType.ASSTRING;
    // Get format, if specified
    if (flagName.contains(".")) {
        try {
            int replaceTypeIndex = flagName.split("\\.").length - 1;
            replaceType = ReplaceType.valueOf(flagName.split("\\.")[replaceTypeIndex].replace("_", "").toUpperCase());
            flagName = flagName.replace("." + flagName.split("\\.")[replaceTypeIndex], "");
        } catch (Exception e) {
            replaceType = ReplaceType.ASSTRING;
        }
    }
    // Check flag replacement type
    if (event.getType().toUpperCase().startsWith("G")) {
        if (denizen.flagManager().getGlobalFlag(flagName).get(index).isEmpty()) {
        // dB.echoDebug(ChatColor.YELLOW + "//REPLACED//" + ChatColor.WHITE + " '%s' flag not found, using fallback!", flagName);
        } else {
            FlagManager.Flag flag = denizen.flagManager().getGlobalFlag(flagName);
            event.setReplaced(getReplaceable(flag, flag.get(index), replaceType));
        // dB.echoDebug(ChatColor.YELLOW + "//REPLACED//" + ChatColor.WHITE + " '%s' with flag value '" + event.getReplaced() + "'.", flagName);
        }
    } else if (event.getType().toUpperCase().startsWith("D") || event.getType().toUpperCase().startsWith("N")) {
        if (denizen.flagManager().getNPCFlag(((BukkitTagContext) event.getContext()).npc.getId(), flagName).get(index).isEmpty()) {
        // dB.echoDebug(ChatColor.YELLOW + "//REPLACED//" + ChatColor.WHITE + " '%s' flag not found, using fallback!", flagName);
        } else {
            FlagManager.Flag flag = denizen.flagManager().getNPCFlag(((BukkitTagContext) event.getContext()).npc.getId(), flagName);
            event.setReplaced(getReplaceable(flag, flag.get(index), replaceType));
        // dB.echoDebug(ChatColor.YELLOW + "//REPLACED//" + ChatColor.WHITE + " '%s' with flag value '" + event.getReplaced() + "'.", flagName);
        }
    } else if (event.getType().toUpperCase().startsWith("P")) {
        // Separate name since subType context may specify a different (or offline) player
        dPlayer player = ((BukkitTagContext) event.getContext()).player;
        // No name? No flag replacement!
        if (player == null) {
            return;
        }
        if (denizen.flagManager().getPlayerFlag(player, flagName).get(index).isEmpty()) {
            if (replaceType.toString().equals("ISEXPIRED")) {
                event.setReplaced("true");
            }
        } else {
            FlagManager.Flag flag = denizen.flagManager().getPlayerFlag(player, flagName);
            event.setReplaced(getReplaceable(flag, flag.get(index), replaceType));
        // dB.echoDebug(ChatColor.YELLOW + "//REPLACED//" + ChatColor.WHITE + " '%s' with flag value '" + event.getReplaced() + "'.", flagName);
        }
    }
}
Also used : BukkitTagContext(net.aufdemrand.denizen.tags.BukkitTagContext) net.aufdemrand.denizen.objects.dPlayer(net.aufdemrand.denizen.objects.dPlayer) FlagManager(net.aufdemrand.denizen.flags.FlagManager)

Example 18 with BukkitTagContext

use of net.aufdemrand.denizen.tags.BukkitTagContext in project Denizen-For-Bukkit by DenizenScript.

the class BookScriptContainer method writeBookTo.

public dItem writeBookTo(dItem book, dPlayer player, dNPC npc) {
    // Get current ItemMeta from the book
    BookMeta bookInfo = (BookMeta) book.getItemStack().getItemMeta();
    if (contains("TITLE")) {
        String title = getString("TITLE");
        title = TagManager.tag(title, new BukkitTagContext(player, npc, false, null, shouldDebug(), new dScript(this)));
        bookInfo.setTitle(title);
    }
    if (contains("SIGNED")) {
        if (getString("SIGNED").equalsIgnoreCase("false")) {
            book.getItemStack().setType(Material.BOOK_AND_QUILL);
        }
    }
    if (contains("AUTHOR")) {
        String author = getString("AUTHOR");
        author = TagManager.tag(author, new BukkitTagContext(player, npc, false, null, shouldDebug(), new dScript(this)));
        bookInfo.setAuthor(author);
    }
    if (contains("TEXT")) {
        List<String> pages = getStringList("TEXT");
        for (String page : pages) {
            page = TagManager.tag(page, new BukkitTagContext(player, npc, false, null, shouldDebug(), new dScript(this)));
            bookInfo.addPage(page);
        }
    }
    book.getItemStack().setItemMeta(bookInfo);
    return book;
}
Also used : BukkitTagContext(net.aufdemrand.denizen.tags.BukkitTagContext) net.aufdemrand.denizencore.objects.dScript(net.aufdemrand.denizencore.objects.dScript) BookMeta(org.bukkit.inventory.meta.BookMeta)

Example 19 with BukkitTagContext

use of net.aufdemrand.denizen.tags.BukkitTagContext in project Denizen-For-Bukkit by DenizenScript.

the class EntityScriptContainer method getEntityFrom.

public dEntity getEntityFrom(dPlayer player, dNPC npc) {
    dEntity entity = null;
    try {
        if (contains("ENTITY_TYPE")) {
            String entityType = TagManager.tag((getString("ENTITY_TYPE", "")), new BukkitTagContext(player, npc, false, null, shouldDebug(), new dScript(this)));
            entity = dEntity.valueOf(entityType);
        } else {
            throw new Exception("Missing entity_type argument!");
        }
        Set<StringHolder> strings = getConfigurationSection("").getKeys(false);
        for (StringHolder string : strings) {
            if (!string.low.equals("entity_type") && !string.low.equals("type")) {
                String value = TagManager.tag((getString(string.low, "")), new BukkitTagContext(player, npc, false, null, shouldDebug(), new dScript(this)));
                entity.adjust(new Mechanism(new Element(string.low), new Element(value)));
            }
        }
        if (entity == null || entity.isUnique()) {
            return null;
        }
        entity.setEntityScript(getName());
    } catch (Exception e) {
        dB.echoError("Woah! An exception has been called with this entity script!");
        dB.echoError(e);
        entity = null;
    }
    return entity;
}
Also used : StringHolder(net.aufdemrand.denizencore.utilities.text.StringHolder) BukkitTagContext(net.aufdemrand.denizen.tags.BukkitTagContext) net.aufdemrand.denizen.objects.dEntity(net.aufdemrand.denizen.objects.dEntity) net.aufdemrand.denizencore.objects.dScript(net.aufdemrand.denizencore.objects.dScript) Element(net.aufdemrand.denizencore.objects.Element) Mechanism(net.aufdemrand.denizencore.objects.Mechanism)

Example 20 with BukkitTagContext

use of net.aufdemrand.denizen.tags.BukkitTagContext in project Denizen-For-Bukkit by DenizenScript.

the class dItem method valueOf.

/**
     * Gets a Item Object from a string form.
     *
     * @param string The string or dScript argument String
     * @param player The dPlayer to be used for player contexts
     *               where applicable.
     * @param npc    The dNPC to be used for NPC contexts
     *               where applicable.
     * @return an Item, or null if incorrectly formatted
     */
public static dItem valueOf(String string, dPlayer player, dNPC npc) {
    if (string == null || string.equals("")) {
        return null;
    }
    Matcher m;
    dItem stack = null;
    ///////
    // Handle objects with properties through the object fetcher
    m = ObjectFetcher.DESCRIBED_PATTERN.matcher(string);
    if (m.matches()) {
        return ObjectFetcher.getObjectFrom(dItem.class, string, new BukkitTagContext(player, npc, false, null, true, null));
    }
    ////////
    // Match @object format for saved dItems
    m = item_by_saved.matcher(string);
    if (m.matches() && NotableManager.isSaved(m.group(2)) && NotableManager.isType(m.group(2), dItem.class)) {
        stack = (dItem) NotableManager.getSavedObject(m.group(2));
        if (m.group(3) != null) {
            stack.setAmount(Integer.valueOf(m.group(3)));
        }
        return stack;
    }
    string = string.replace("i@", "");
    m = ITEM_PATTERN.matcher(string);
    if (m.matches()) {
        try {
            if (ScriptRegistry.containsScript(m.group(1), ItemScriptContainer.class)) {
                // Get item from script
                stack = ScriptRegistry.getScriptContainerAs(m.group(1), ItemScriptContainer.class).getItemFrom(player, npc);
            } else if (ScriptRegistry.containsScript(m.group(1), BookScriptContainer.class)) {
                // Get book from script
                stack = ScriptRegistry.getScriptContainerAs(m.group(1), BookScriptContainer.class).getBookFrom(player, npc);
            }
            if (stack != null) {
                if (m.group(3) != null) {
                    stack.setAmount(Integer.valueOf(m.group(3)));
                }
                return stack;
            }
        } catch (Exception e) {
        // Just a catch, might be a regular item...
        }
        try {
            String material = m.group(1).toUpperCase();
            if (aH.matchesInteger(material)) {
                stack = new dItem(Integer.valueOf(material));
            } else {
                dMaterial mat = dMaterial.valueOf(material);
                stack = new dItem(mat.getMaterial());
                if (mat.hasData()) {
                    stack.setDurability(mat.getData());
                }
            }
            if (m.group(2) != null) {
                stack.setDurability(Short.valueOf(m.group(2)));
            }
            if (m.group(3) != null) {
                stack.setAmount(Integer.valueOf(m.group(3)));
            }
            return stack;
        } catch (Exception e) {
            if (!string.equalsIgnoreCase("none") && !nope) {
                dB.log("Does not match a valid item ID or material: " + string);
            }
        }
    }
    if (!nope) {
        dB.log("valueOf dItem returning null: " + string);
    }
    // No match! Return null.
    return null;
}
Also used : BukkitTagContext(net.aufdemrand.denizen.tags.BukkitTagContext) Matcher(java.util.regex.Matcher) BookScriptContainer(net.aufdemrand.denizen.scripts.containers.core.BookScriptContainer)

Aggregations

BukkitTagContext (net.aufdemrand.denizen.tags.BukkitTagContext)23 net.aufdemrand.denizen.objects.dPlayer (net.aufdemrand.denizen.objects.dPlayer)9 Element (net.aufdemrand.denizencore.objects.Element)7 net.aufdemrand.denizencore.objects.dScript (net.aufdemrand.denizencore.objects.dScript)6 net.aufdemrand.denizen.objects.dEntity (net.aufdemrand.denizen.objects.dEntity)5 net.aufdemrand.denizen.objects.dNPC (net.aufdemrand.denizen.objects.dNPC)5 Map (java.util.Map)4 net.aufdemrand.denizen.objects.dItem (net.aufdemrand.denizen.objects.dItem)4 net.aufdemrand.denizencore.objects.dObject (net.aufdemrand.denizencore.objects.dObject)4 Attribute (net.aufdemrand.denizencore.tags.Attribute)4 EventHandler (org.bukkit.event.EventHandler)4 HashMap (java.util.HashMap)3 Matcher (java.util.regex.Matcher)3 TriggerTrait (net.aufdemrand.denizen.npc.traits.TriggerTrait)3 InteractScriptContainer (net.aufdemrand.denizen.scripts.containers.core.InteractScriptContainer)3 net.aufdemrand.denizencore.objects.dList (net.aufdemrand.denizencore.objects.dList)3 Player (org.bukkit.entity.Player)3 Iterator (java.util.Iterator)2 Pattern (java.util.regex.Pattern)2 BukkitScriptEntryData (net.aufdemrand.denizen.BukkitScriptEntryData)2