Search in sources :

Example 6 with StringHolder

use of net.aufdemrand.denizencore.utilities.text.StringHolder in project Denizen-For-Bukkit by DenizenScript.

the class YamlCommand method yaml.

@TagManager.TagEvents
public void yaml(ReplaceableTagEvent event) {
    if (!event.matches("yaml")) {
        return;
    }
    Attribute attribute = event.getAttributes();
    // -->
    if (attribute.getAttribute(2).equalsIgnoreCase("list")) {
        dList list = new dList();
        list.addAll(yamls.keySet());
        event.setReplaced(list.getAttribute(attribute.fulfill(2)));
        return;
    }
    // YAML tag requires name context and type context.
    if ((!event.hasNameContext() || !(event.hasTypeContext() || attribute.getAttribute(2).equalsIgnoreCase("to_json"))) && !attribute.hasAlternative()) {
        dB.echoError("YAML tag '" + event.raw_tag + "' is missing required context. Tag replacement aborted.");
        return;
    }
    // Set id (name context) and path (type context)
    String id = event.getNameContext().toUpperCase();
    String path = event.getTypeContext();
    // Check if there is a yaml file loaded with the specified id
    if (!yamls.containsKey(id)) {
        if (!attribute.hasAlternative()) {
            dB.echoError("YAML tag '" + event.raw_tag + "' has specified an invalid ID, or the specified id has already" + " been closed. Tag replacement aborted. ID given: '" + id + "'.");
        }
        return;
    }
    // Catch up with what has already been processed.
    attribute.fulfill(1);
    // -->
    if (attribute.startsWith("contains")) {
        event.setReplaced(new Element(getYaml(id).contains(path)).getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("is_list")) {
        event.setReplaced(new Element(getYaml(id).isList(path)).getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("read")) {
        attribute.fulfill(1);
        if (getYaml(id).isList(path)) {
            List<String> value = getYaml(id).getStringList(path);
            if (value == null) {
                // If value is null, the key at the specified path didn't exist.
                return;
            } else {
                event.setReplaced(new dList(value).getAttribute(attribute));
                return;
            }
        } else {
            String value = getYaml(id).getString(path);
            if (value == null) {
                // If value is null, the key at the specified path didn't exist.
                return;
            } else {
                event.setReplaced(new Element(value).getAttribute(attribute));
                return;
            }
        }
    }
    // -->
    if (attribute.startsWith("list_deep_keys")) {
        Set<StringHolder> keys;
        if (path != null && path.length() > 0) {
            YamlConfiguration section = getYaml(id).getConfigurationSection(path);
            if (section == null) {
                return;
            }
            keys = section.getKeys(true);
        } else {
            keys = getYaml(id).getKeys(true);
        }
        if (keys == null) {
            return;
        } else {
            event.setReplaced(new dList(keys).getAttribute(attribute.fulfill(1)));
            return;
        }
    }
    // -->
    if (attribute.startsWith("list_keys")) {
        Set<StringHolder> keys;
        if (path != null && path.length() > 0) {
            YamlConfiguration section = getYaml(id).getConfigurationSection(path);
            if (section == null) {
                return;
            }
            keys = section.getKeys(false);
        } else {
            keys = getYaml(id).getKeys(false);
        }
        if (keys == null) {
            return;
        } else {
            event.setReplaced(new dList(keys).getAttribute(attribute.fulfill(1)));
            return;
        }
    }
    // -->
    if (attribute.startsWith("to_json")) {
        JSONObject jsobj = new JSONObject(getYaml(id).getMap());
        event.setReplaced(new Element(jsobj.toString()).getAttribute(attribute.fulfill(1)));
        return;
    }
}
Also used : StringHolder(net.aufdemrand.denizencore.utilities.text.StringHolder) JSONObject(org.json.JSONObject) Attribute(net.aufdemrand.denizencore.tags.Attribute) net.aufdemrand.denizencore.objects.dList(net.aufdemrand.denizencore.objects.dList) Element(net.aufdemrand.denizencore.objects.Element) YamlConfiguration(net.aufdemrand.denizencore.utilities.YamlConfiguration)

Example 7 with StringHolder

use of net.aufdemrand.denizencore.utilities.text.StringHolder in project Denizen-For-Bukkit by DenizenScript.

the class AssignmentTrait method describe.

public void describe(CommandSender sender, int page) throws CommandException {
    AssignmentScriptContainer assignmentScript = ScriptRegistry.getScriptContainer(assignment);
    Paginator paginator = new Paginator().header("Assignment");
    paginator.addLine("<e>Current assignment: " + (hasAssignment() ? this.assignment : "None.") + "");
    paginator.addLine("");
    if (!hasAssignment()) {
        paginator.sendPage(sender, page);
        return;
    }
    // Interact Scripts
    boolean entriesPresent = false;
    paginator.addLine(ChatColor.GRAY + "Interact Scripts:");
    paginator.addLine("<e>Key: <a>Priority  <b>Name");
    if (assignmentScript.contains("INTERACT SCRIPTS")) {
        entriesPresent = true;
        for (String scriptEntry : assignmentScript.getStringList("INTERACT SCRIPTS")) {
            String[] split = scriptEntry.split(" ", 2);
            if (split.length == 2 && aH.matchesInteger(split[0])) {
                paginator.addLine("<a>" + split[0] + "<b> " + split[1]);
            } else {
                paginator.addLine("<b>" + scriptEntry);
            }
        }
    }
    if (!entriesPresent) {
        paginator.addLine("<c>No Interact Scripts assigned.");
    }
    paginator.addLine("");
    if (!entriesPresent) {
        if (!paginator.sendPage(sender, page)) {
            throw new CommandException(Messages.COMMAND_PAGE_MISSING);
        }
        return;
    }
    // Scheduled Activities
    entriesPresent = false;
    paginator.addLine(ChatColor.GRAY + "Scheduled Scripts:");
    paginator.addLine("<e>Key: <a>Time  <b>Name");
    if (assignmentScript.contains("SCHEDULED ACTIVITIES")) {
        entriesPresent = true;
        for (String scriptEntry : assignmentScript.getStringList("SCHEDULED ACTIVITIES")) {
            paginator.addLine("<a>" + scriptEntry.split(" ")[0] + "<b> " + scriptEntry.split(" ", 2)[1]);
        }
    }
    if (!entriesPresent) {
        paginator.addLine("<c>No scheduled scripts activities.");
    }
    paginator.addLine("");
    // Actions
    entriesPresent = false;
    paginator.addLine(ChatColor.GRAY + "Actions:");
    paginator.addLine("<e>Key: <a>Action name  <b>Script Size");
    if (assignmentScript.contains("ACTIONS")) {
        entriesPresent = true;
    }
    if (entriesPresent) {
        for (StringHolder action : assignmentScript.getConfigurationSection("ACTIONS").getKeys(false)) {
            paginator.addLine("<a>" + action.str + " <b>" + assignmentScript.getStringList("ACTIONS." + action.str).size());
        }
    } else {
        paginator.addLine("<c>No actions defined in the assignment.");
    }
    paginator.addLine("");
    if (!paginator.sendPage(sender, page)) {
        throw new CommandException(Messages.COMMAND_PAGE_MISSING, page);
    }
}
Also used : StringHolder(net.aufdemrand.denizencore.utilities.text.StringHolder) AssignmentScriptContainer(net.aufdemrand.denizen.scripts.containers.core.AssignmentScriptContainer) CommandException(net.citizensnpcs.api.command.exception.CommandException) Paginator(net.citizensnpcs.api.util.Paginator)

Aggregations

StringHolder (net.aufdemrand.denizencore.utilities.text.StringHolder)7 YamlConfiguration (net.aufdemrand.denizencore.utilities.YamlConfiguration)3 BukkitTagContext (net.aufdemrand.denizen.tags.BukkitTagContext)2 Element (net.aufdemrand.denizencore.objects.Element)2 net.aufdemrand.denizencore.objects.dList (net.aufdemrand.denizencore.objects.dList)2 net.aufdemrand.denizencore.objects.dScript (net.aufdemrand.denizencore.objects.dScript)2 ArrayList (java.util.ArrayList)1 BukkitScriptEntryData (net.aufdemrand.denizen.BukkitScriptEntryData)1 net.aufdemrand.denizen.objects.dEntity (net.aufdemrand.denizen.objects.dEntity)1 net.aufdemrand.denizen.objects.dInventory (net.aufdemrand.denizen.objects.dInventory)1 net.aufdemrand.denizen.objects.dItem (net.aufdemrand.denizen.objects.dItem)1 AssignmentScriptContainer (net.aufdemrand.denizen.scripts.containers.core.AssignmentScriptContainer)1 Mechanism (net.aufdemrand.denizencore.objects.Mechanism)1 ScriptEntry (net.aufdemrand.denizencore.scripts.ScriptEntry)1 InstantQueue (net.aufdemrand.denizencore.scripts.queues.core.InstantQueue)1 Attribute (net.aufdemrand.denizencore.tags.Attribute)1 NaturalOrderComparator (net.aufdemrand.denizencore.utilities.NaturalOrderComparator)1 CommandException (net.citizensnpcs.api.command.exception.CommandException)1 Paginator (net.citizensnpcs.api.util.Paginator)1 ItemStack (org.bukkit.inventory.ItemStack)1