Package net.aufdemrand.denizencore.utilities

Examples of net.aufdemrand.denizencore.utilities.YamlConfiguration


                    continue;
                }

                // ...and through each event inside the script.
                if (script.contains("EVENTS")) {
                    YamlConfiguration configSection = script.getConfigurationSection("EVENTS");
                    if (configSection == null) {
                        dB.echoError("Script '" + script.getName() + "' has an invalid events block!");
                        break;
                    }
                    Set<String> keys = configSection.getKeys(false);
                    if (keys == null) {
                        dB.echoError("Script '" + script.getName() + "' has an empty events block!");
                        break;
                    }
                    for (String eventName : keys) {
View Full Code Here


        // Returns the value of the constant as either an Element or dList.
        // -->
        if (attribute.startsWith("cons")) {
            if (!attribute.hasContext(1)) return null;

            YamlConfiguration section = getContainer().getConfigurationSection("constants");
            if (section == null) return null;
            Object obj = section.get(attribute.getContext(1).toUpperCase());
            if (obj == null) return null;

            if (obj instanceof List) {
                dList list = new dList();
                for (Object each : (List<Object>) obj)
                    list.add(TagManager.tag(attribute.getScriptEntry() == null ? null: attribute.getScriptEntry().getPlayer(),
                            attribute.getScriptEntry() == null ? null: attribute.getScriptEntry().getNPC(), each.toString(), false, attribute.getScriptEntry()));
                return list.getAttribute(attribute.fulfill(1));

            }
            else return new Element(TagManager.tag(attribute.getScriptEntry() == null ? null: attribute.getScriptEntry().getPlayer(),
                    attribute.getScriptEntry() == null ? null: attribute.getScriptEntry().getNPC(), obj.toString(), false, attribute.getScriptEntry()))
                    .getAttribute(attribute.fulfill(1));
        }

        // <--[tag]
        // @attribute <s@script.yaml_key[<constant_name>]>
        // @returns Element or dList
        // @description
        // Returns the value of the script's YAML as either an Element or dList.
        // -->
        if (attribute.startsWith("yaml_key")
                && attribute.hasContext(1)) {
            ScriptContainer container = getContainer();
            if (container == null) {
                dB.echoError("Missing script container?!");
                return new Element(identify()).getAttribute(attribute);
            }
            YamlConfiguration section = container.getConfigurationSection("");
            if (section == null) {
                dB.echoError("Missing YAML section?!");
                return new Element(identify()).getAttribute(attribute);
            }
            Object obj = section.get(attribute.getContext(1).toUpperCase());
            if (obj == null) return null;

            if (obj instanceof List) {
                dList list = new dList();
                for (Object each : (List<Object>) obj)
View Full Code Here

        Element split = scriptEntry.getElement("split");
        YAML_Action yaml_action = (YAML_Action) scriptEntry.getObject("yaml_action");
        Element actionElement = scriptEntry.getElement("action");
        Element idElement = scriptEntry.getElement("id");

        YamlConfiguration yamlConfiguration;

        dB.report(scriptEntry, getName(),
                        idElement.debug()
                        + actionElement.debug()
                        + (filename != null ? filename.debug() : "")
                        + (yaml_action != null ? aH.debugObj("yaml_action", yaml_action.name()): "")
                        + (key != null ? key.debug() : "")
                        + (value != null ? value.debug() : "")
                        + (split != null ? split.debug() : ""));

        // Do action
        Action action = Action.valueOf(actionElement.asString().toUpperCase());
        String id = idElement.asString().toUpperCase();

        switch (action) {

            case LOAD:
                File file = new File(DenizenAPI.getCurrentInstance().getDataFolder(), filename.asString());
                if (!file.exists()) {
                    dB.echoError("File cannot be found!");
                    return;
                }
                try {
                    FileInputStream fis = new FileInputStream(file);
                    yamlConfiguration = YamlConfiguration.load(ScriptHelper.convertStreamToString(fis));
                    fis.close();
                }
                catch (Exception e) {
                    dB.echoError(e);
                    return;
                }
                if (yamls.containsKey(id))
                    yamls.remove(id);
                if (yamlConfiguration != null)
                    yamls.put(id, yamlConfiguration);
                break;

            case UNLOAD:
                if (yamls.containsKey(id))
                    yamls.remove(id);
                else
                    dB.echoError("Unknown YAML ID '" + id + "'");
                break;

            case SAVE:
                if (yamls.containsKey(id)) {
                    try {
                        if (!Settings.allowStrangeYAMLSaves()) {
                            File fileObj = new File(DenizenAPI.getCurrentInstance().
                                    getDataFolder().getAbsolutePath() + "/" + filename.asString());
                            String directory = URLDecoder.decode(System.getProperty("user.dir"));
                            if (!fileObj.getCanonicalPath().startsWith(directory)) {
                                dB.echoError("Outside-the-main-folder YAML saves disabled by administrator.");
                                return;
                            }
                        }
                        File fileObj = new File(DenizenAPI.getCurrentInstance().
                                getDataFolder().getAbsolutePath() + "/" + filename.asString());
                        fileObj.getParentFile().mkdirs();
                        FileWriter fw = new FileWriter(DenizenAPI.getCurrentInstance()
                                .getDataFolder().getAbsolutePath() + "/" + filename.asString());
                        BufferedWriter writer = new BufferedWriter(fw);
                        writer.write(yamls.get(id).saveToString());
                        writer.close();
                        fw.close();
                    } catch (IOException e) {
                        dB.echoError(e);
                    }
                }
                else
                    dB.echoError("Unknown YAML ID '" + id + "'");
                break;

            case WRITE:
                if (yamls.containsKey(id)) {
                    if (value instanceof Element)
                        yamls.get(id).set(key.asString(), ((Element) value).asString());
                    else if (split != null && split.asBoolean())
                        yamls.get(id).set(key.asString(), value);
                    else
                        yamls.get(id).set(key.asString(), value.identify());
                }
                else
                    dB.echoError("Unknown YAML ID '" + id + "'");
                break;

            case SET:
                if (yamls.containsKey(id)) {
                    if (yaml_action == null || key == null || value == null) {
                        dB.echoError("Must specify a YAML action and value!");
                        return;
                    }
                    YamlConfiguration yaml = yamls.get(id);

                    int index = -1;
                    if (key.asString().contains("[")) {
                        try {
                            index = Integer.valueOf(key.asString().split("\\[")[1].replace("]", "")) - 1;
                        } catch (Exception e) { index = -1; }
                        key = Element.valueOf(key.asString().split("\\[")[0]);
                    }

                    String keyStr = key.asString();
                    String valueStr = value.identify();

                    switch (yaml_action) {
                        case INCREASE:
                            Set(yaml, index, keyStr, String.valueOf(aH.getIntegerFrom(Get(yaml, index, keyStr, "0")) + aH.getIntegerFrom(valueStr)));
                            break;
                        case DECREASE:
                            Set(yaml, index, keyStr, String.valueOf(aH.getIntegerFrom(Get(yaml, index, keyStr, "0")) - aH.getIntegerFrom(valueStr)));
                            break;
                        case MULTIPLY:
                            Set(yaml, index, keyStr, String.valueOf(aH.getIntegerFrom(Get(yaml, index, keyStr, "1")) * aH.getIntegerFrom(valueStr)));
                            break;
                        case DIVIDE:
                            Set(yaml, index, keyStr, String.valueOf(aH.getIntegerFrom(Get(yaml, index, keyStr, "1")) / aH.getIntegerFrom(valueStr)));
                            break;
                        case DELETE:
                            yaml.set(keyStr, null);
                            break;
                        case SET_VALUE:
                            Set(yaml, index, keyStr, valueStr);
                            break;
                        case INSERT:
                        {
                            List<String> list = yaml.getStringList(keyStr);
                            if (list == null)
                                list = new ArrayList<String>();
                            list.add(valueStr);
                            yaml.set(keyStr, list);
                            break;
                        }
                        case REMOVE:
                        {
                            List<String> list = yaml.getStringList(keyStr);
                            if (list == null)
                                break;
                            if (index > -1 && index < list.size()) {
                                list.remove(index);
                            }
                            else {
                                for (int i = 0; i < list.size(); i++) {
                                    if (list.get(i).equalsIgnoreCase(valueStr)) {
                                        list.remove(i);
                                        break;
                                    }
                                }
                                yaml.set(keyStr, list);
                                break;
                            }
                        }
                        case SPLIT:
                        {
                            List<String> list = yaml.getStringList(keyStr);
                            if (list == null)
                                list = new ArrayList<String>();
                            list.addAll(dList.valueOf(valueStr));
                            yaml.set(keyStr, list);
                            break;
                        }
                    }
                }
                else
                    dB.echoError("Unknown YAML ID '" + id + "'");
                break;

            case CREATE:
                if (yamls.containsKey(id))
                    yamls.remove(id);
                yamlConfiguration = new YamlConfiguration();
                yamls.put(id.toUpperCase(), yamlConfiguration);
                break;
        }

    }
View Full Code Here

        // Returns a dList of all the keys at the path and all subpaths.
        // -->
        if (attribute.startsWith("list_deep_keys")) {
            Set<String> 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 {
                ArrayList<String> list = new ArrayList<String>();
                list.addAll(keys);
                event.setReplaced(new dList(list).getAttribute(attribute.fulfill(1)));
                return;
            }
        }

        // <--[tag]
        // @attribute <yaml[<id>].list_keys[<path>]>
        // @returns dList
        // @description
        // Returns a dList of all the keys at the path.
        // -->
        if (attribute.startsWith("list_keys")) {
            Set<String> 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) {
View Full Code Here

            if (keys.isEmpty())
                throw new ExceptionInInitializerError("Could not find any STEPS in " + getName() + "! Is the type on this script correct?");

            for (String step : keys) {
                if (step.contains("*")) {
                    YamlConfiguration defaultStepSection = getConfigurationSection("STEPS." + step);
                    step = step.replace("*", "");
                    set("STEPS." + step, defaultStepSection);
                    set("STEPS." + step + "*", null);
                    defaultStep = step;
                }
View Full Code Here

TOP

Related Classes of net.aufdemrand.denizencore.utilities.YamlConfiguration

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.