Search in sources :

Example 1 with StringHolder

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

the class ConstantsTrait method rebuildAssignmentConstants.

public Map<String, String> rebuildAssignmentConstants() {
    // Builds a map of constants inherited from the NPCs current Assignment
    if (!npc.hasTrait(AssignmentTrait.class) || !npc.getTrait(AssignmentTrait.class).hasAssignment()) {
        assignmentConstants.clear();
        return assignmentConstants;
    }
    if (npc.getTrait(AssignmentTrait.class).getAssignment() != null) {
        assignment = npc.getTrait(AssignmentTrait.class).getAssignment().getName();
        assignmentConstants.clear();
    } else {
        return assignmentConstants;
    }
    try {
        if (ScriptRegistry.getScriptContainer(assignment).contains("DEFAULT CONSTANTS")) {
            for (StringHolder constant : ScriptRegistry.getScriptContainer(assignment).getConfigurationSection("DEFAULT CONSTANTS").getKeys(false)) {
                assignmentConstants.put(CoreUtilities.toLowerCase(constant.str), ScriptRegistry.getScriptContainer(assignment).getString("DEFAULT CONSTANTS." + constant.str.toUpperCase(), ""));
            }
        }
    } catch (NullPointerException e) {
        dB.echoError("Constants in assignment script '" + npc.getTrait(AssignmentTrait.class).getAssignment().getName() + "' improperly defined, no constants will be set.");
    }
    return assignmentConstants;
}
Also used : StringHolder(net.aufdemrand.denizencore.utilities.text.StringHolder)

Example 2 with StringHolder

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

the class InteractScriptContainer method getIdMapFor.

/**
     * Gets the available IDs with its trigger value in the form of a Map. The 'key' is
     * the name of the ID and the value is the value of the 'Trigger' key that is owned
     * by the ID key.
     * <p/>
     * <b>Example:</b>
     * <tt>
     * Example Interact Script: <br/>
     * Type: interact <br/>
     * Steps: <br/>
     * Current Step:
     * Click Trigger:           <--- obtained with the Trigger class <br/>
     * <br/>
     * id:                    <--- id of the specific trigger script/script options <br/>
     * Trigger: iron_sword  <--- value of the id key <br/>
     * Script: <br/>
     * - ...
     * - ... <br/>
     * <br/>
     * Script:                <--- since this is an id-less entry for the click trigger, <br/>
     * - ...                       it will be ignored and not in the Map. <br/>
     * - ... <br/>
     * </tt>
     * <p/>
     * <p>Note: This is handled internally with the parse() method in AbstractTrigger, so for
     * basic triggers, you probably don't need to even call this.</p>
     *
     * @param trigger The trigger involved
     * @param player  The Denizen Player object for the player who triggered it
     * @return A map of options in the trigger's script, excluding a plain 'script'
     */
public Map<String, String> getIdMapFor(Class<? extends AbstractTrigger> trigger, dPlayer player) {
    // Get the trigger name
    String triggerName = DenizenAPI.getCurrentInstance().getTriggerRegistry().get(trigger).getName().toUpperCase();
    // Get the step
    String step = InteractScriptHelper.getCurrentStep(player, getName());
    // Check for entries
    if (contains("STEPS." + step + "." + triggerName + " TRIGGER")) {
        // Trigger exists in Player's current step, get ids.
        Map<String, String> idMap = new HashMap<String, String>();
        // Iterate through IDs to build the idMap
        try {
            for (StringHolder id : getConfigurationSection("STEPS." + step + "." + triggerName + " TRIGGER").getKeys(false)) {
                if (!id.str.equalsIgnoreCase("SCRIPT")) {
                    idMap.put(id.str, getString("STEPS." + step + "." + triggerName + " TRIGGER." + id.str + ".TRIGGER", ""));
                }
            }
        } catch (Exception ex) {
            dB.echoError("Warning: improperly defined " + trigger.getName() + " trigger for script '" + getName() + "'!");
        }
        return idMap;
    } else // No entries, so just return an empty list to avoid NPEs
    {
        return Collections.emptyMap();
    }
}
Also used : StringHolder(net.aufdemrand.denizencore.utilities.text.StringHolder)

Example 3 with StringHolder

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

the class InventoryScriptContainer method getInventoryFrom.

public dInventory getInventoryFrom(dPlayer player, dNPC npc) {
    // TODO: Clean all this code!
    dInventory inventory = null;
    BukkitTagContext context = new BukkitTagContext(player, npc, false, null, shouldDebug(), new dScript(this));
    try {
        if (contains("INVENTORY")) {
            if (InventoryType.valueOf(getString("INVENTORY").toUpperCase()) != null) {
                inventory = new dInventory(InventoryType.valueOf(getString("INVENTORY").toUpperCase()));
                if (contains("TITLE")) {
                    inventory.setTitle(TagManager.tag(getString("TITLE"), context));
                }
                inventory.setIdentifiers("script", getName());
            } else {
                dB.echoError("Invalid inventory type specified. Assuming \"CHEST\"");
            }
        }
        int size = 0;
        if (contains("SIZE")) {
            if (inventory != null && !getInventoryType().name().equalsIgnoreCase("CHEST")) {
                dB.echoError("You can only set the size of chest inventories!");
            } else {
                size = aH.getIntegerFrom(TagManager.tag(getString("SIZE"), context));
                if (size == 0) {
                    dB.echoError("Inventory size can't be 0. Assuming default of inventory type...");
                }
                if (size % 9 != 0) {
                    size = (int) Math.ceil(size / 9) * 9;
                    dB.echoError("Inventory size must be a multiple of 9! Rounding up to " + size + "...");
                }
                if (size < 0) {
                    size = size * -1;
                    dB.echoError("Inventory size must be a positive number! Inverting to " + size + "...");
                }
                inventory = new dInventory(size, contains("TITLE") ? TagManager.tag(getString("TITLE"), context) : "Chest");
                inventory.setIdentifiers("script", getName());
            }
        }
        if (size == 0) {
            size = getInventoryType().getDefaultSize();
        }
        boolean[] filledSlots = new boolean[size];
        if (contains("SLOTS")) {
            ItemStack[] finalItems = new ItemStack[size];
            int itemsAdded = 0;
            for (String items : getStringList("SLOTS")) {
                items = TagManager.tag(items, context).trim();
                if (items.isEmpty()) {
                    continue;
                }
                if (!items.startsWith("[") || !items.endsWith("]")) {
                    dB.echoError("Inventory script \"" + getName() + "\" has an invalid slots line: [" + items + "]... Ignoring it");
                    continue;
                }
                String[] itemsInLine = items.substring(1, items.length() - 1).split("\\[?\\]?\\s+\\[", -1);
                for (String item : itemsInLine) {
                    if (contains("DEFINITIONS." + item)) {
                        dItem def = dItem.valueOf(TagManager.tag(getString("DEFINITIONS." + item), context), player, npc);
                        if (def == null) {
                            dB.echoError("Invalid definition '" + item + "' in inventory script '" + getName() + "'" + "... Ignoring it and assuming \"AIR\"");
                            finalItems[itemsAdded] = new ItemStack(Material.AIR);
                        } else {
                            finalItems[itemsAdded] = def.getItemStack();
                        }
                    } else if (dItem.matches(item)) {
                        finalItems[itemsAdded] = dItem.valueOf(item, player, npc).getItemStack();
                    } else {
                        finalItems[itemsAdded] = new ItemStack(Material.AIR);
                        if (!item.isEmpty()) {
                            dB.echoError("Inventory script \"" + getName() + "\" has an invalid slot item: [" + item + "]... Ignoring it and assuming \"AIR\"");
                        }
                    }
                    filledSlots[itemsAdded] = !item.isEmpty();
                    itemsAdded++;
                }
            }
            if (inventory == null) {
                size = finalItems.length % 9 == 0 ? finalItems.length : Math.round(finalItems.length / 9) * 9;
                inventory = new dInventory(size == 0 ? 9 : size, contains("TITLE") ? TagManager.tag(getString("TITLE"), context) : "Chest");
            }
            inventory.setContents(finalItems);
        }
        if (contains("PROCEDURAL ITEMS")) {
            // TODO: Document this feature!
            if (inventory == null) {
                size = InventoryType.CHEST.getDefaultSize();
                inventory = new dInventory(size, contains("TITLE") ? TagManager.tag(getString("TITLE"), context) : "Chest");
            }
            List<ScriptEntry> entries = getEntries(new BukkitScriptEntryData(player, npc), "PROCEDURAL ITEMS");
            if (!entries.isEmpty()) {
                long id = DetermineCommand.getNewId();
                ScriptBuilder.addObjectToEntries(entries, "ReqId", id);
                InstantQueue queue = InstantQueue.getQueue(ScriptQueue.getNextId("INV_SCRIPT_ITEM_PROC"));
                queue.addEntries(entries);
                queue.setReqId(id);
                if (contains("DEFINITIONS")) {
                    YamlConfiguration section = getConfigurationSection("DEFINITIONS");
                    for (StringHolder string : section.getKeys(false)) {
                        String definition = string.str;
                        queue.addDefinition(definition, section.getString(definition));
                    }
                }
                queue.start();
                if (DetermineCommand.hasOutcome(id)) {
                    dList list = dList.valueOf(DetermineCommand.getOutcome(id).get(0));
                    if (list != null) {
                        int x = 0;
                        for (dItem item : list.filter(dItem.class)) {
                            while (x < filledSlots.length && filledSlots[x]) {
                                x++;
                            }
                            if (x >= filledSlots.length || filledSlots[x]) {
                                break;
                            }
                            inventory.setSlots(x, item.getItemStack());
                            filledSlots[x] = true;
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        dB.echoError("Woah! An exception has been called with this inventory script!");
        dB.echoError(e);
        inventory = null;
    }
    if (inventory != null) {
        InventoryScriptHelper.tempInventoryScripts.put(inventory.getInventory(), getName());
    }
    return inventory;
}
Also used : net.aufdemrand.denizencore.objects.dList(net.aufdemrand.denizencore.objects.dList) net.aufdemrand.denizen.objects.dInventory(net.aufdemrand.denizen.objects.dInventory) ScriptEntry(net.aufdemrand.denizencore.scripts.ScriptEntry) YamlConfiguration(net.aufdemrand.denizencore.utilities.YamlConfiguration) net.aufdemrand.denizen.objects.dItem(net.aufdemrand.denizen.objects.dItem) StringHolder(net.aufdemrand.denizencore.utilities.text.StringHolder) BukkitTagContext(net.aufdemrand.denizen.tags.BukkitTagContext) BukkitScriptEntryData(net.aufdemrand.denizen.BukkitScriptEntryData) net.aufdemrand.denizencore.objects.dScript(net.aufdemrand.denizencore.objects.dScript) InstantQueue(net.aufdemrand.denizencore.scripts.queues.core.InstantQueue) ItemStack(org.bukkit.inventory.ItemStack)

Example 4 with StringHolder

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

the class MapScriptContainer method applyTo.

public void applyTo(MapView mapView) {
    DenizenMapRenderer renderer = new DenizenMapRenderer(mapView.getRenderers(), aH.getBooleanFrom(getString("AUTO UPDATE", "true")));
    boolean debug = true;
    if (contains("DEBUG")) {
        debug = aH.getBooleanFrom(getString("DEBUG"));
    }
    if (contains("OBJECTS")) {
        YamlConfiguration objectsSection = getConfigurationSection("OBJECTS");
        List<StringHolder> objectKeys1 = new ArrayList<StringHolder>(objectsSection.getKeys(false));
        List<String> objectKeys = new ArrayList<String>(objectKeys1.size());
        for (StringHolder sh : objectKeys1) {
            objectKeys.add(sh.str);
        }
        Collections.sort(objectKeys, new NaturalOrderComparator());
        for (String objectKey : objectKeys) {
            YamlConfiguration objectSection = objectsSection.getConfigurationSection(objectKey);
            if (!objectSection.contains("TYPE")) {
                dB.echoError("Map script '" + getName() + "' has an object without a specified type!");
                return;
            }
            String type = objectSection.getString("TYPE").toUpperCase();
            String x = objectSection.getString("X", "0");
            String y = objectSection.getString("Y", "0");
            String visible = objectSection.getString("VISIBLE", "true");
            if (type.equals("IMAGE")) {
                if (!objectSection.contains("IMAGE")) {
                    dB.echoError("Map script '" + getName() + "'s image '" + objectKey + "' has no specified image location!");
                    return;
                }
                String image = objectSection.getString("IMAGE");
                int width = aH.getIntegerFrom(objectSection.getString("WIDTH", "0"));
                int height = aH.getIntegerFrom(objectSection.getString("HEIGHT", "0"));
                if (CoreUtilities.toLowerCase(image).endsWith(".gif")) {
                    renderer.addObject(new MapAnimatedImage(x, y, visible, debug, image, width, height));
                } else {
                    renderer.addObject(new MapImage(x, y, visible, debug, image, width, height));
                }
            } else if (type.equals("TEXT")) {
                if (!objectSection.contains("TEXT")) {
                    dB.echoError("Map script '" + getName() + "'s text object '" + objectKey + "' has no specified text!");
                    return;
                }
                String text = objectSection.getString("TEXT");
                renderer.addObject(new MapText(x, y, visible, debug, text));
            } else if (type.equals("CURSOR")) {
                if (!objectSection.contains("CURSOR")) {
                    dB.echoError("Map script '" + getName() + "'s cursor '" + objectKey + "' has no specified cursor type!");
                    return;
                }
                String cursor = objectSection.getString("CURSOR");
                if (cursor == null) {
                    dB.echoError("Map script '" + getName() + "'s cursor '" + objectKey + "' is missing a cursor type!");
                    return;
                }
                renderer.addObject(new MapCursor(x, y, visible, debug, objectSection.getString("DIRECTION", "0"), cursor));
            } else if (type.equals("DOT")) {
                renderer.addObject(new MapDot(x, y, visible, debug, objectSection.getString("RADIUS", "1"), objectSection.getString("COLOR", "black")));
            }
        }
    }
    DenizenMapManager.setMap(mapView, renderer);
}
Also used : ArrayList(java.util.ArrayList) YamlConfiguration(net.aufdemrand.denizencore.utilities.YamlConfiguration) NaturalOrderComparator(net.aufdemrand.denizencore.utilities.NaturalOrderComparator) StringHolder(net.aufdemrand.denizencore.utilities.text.StringHolder)

Example 5 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)

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