Search in sources :

Example 1 with ScriptEntry

use of net.aufdemrand.denizencore.scripts.ScriptEntry 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 2 with ScriptEntry

use of net.aufdemrand.denizencore.scripts.ScriptEntry in project Denizen-For-Bukkit by DenizenScript.

the class CommandScriptContainer method runTabCompleteProcedure.

public List<String> runTabCompleteProcedure(dPlayer player, dNPC npc, Map<String, dObject> context) {
    // Add the reqId to each of the entries for the determine command
    List<ScriptEntry> entries = getEntries(new BukkitScriptEntryData(player, npc), "TAB COMPLETE");
    long id = DetermineCommand.getNewId();
    ScriptBuilder.addObjectToEntries(entries, "ReqId", id);
    ScriptQueue queue = InstantQueue.getQueue(ScriptQueue.getNextId(getName())).setReqId(id).addEntries(entries);
    if (context != null) {
        OldEventManager.OldEventContextSource oecs = new OldEventManager.OldEventContextSource();
        oecs.contexts = context;
        queue.setContextSource(oecs);
    }
    queue.start();
    if (DetermineCommand.hasOutcome(id)) {
        return dList.valueOf(DetermineCommand.getOutcome(id).get(0));
    } else {
        return new ArrayList<String>();
    }
}
Also used : BukkitScriptEntryData(net.aufdemrand.denizen.BukkitScriptEntryData) OldEventManager(net.aufdemrand.denizencore.events.OldEventManager) ArrayList(java.util.ArrayList) ScriptEntry(net.aufdemrand.denizencore.scripts.ScriptEntry) ScriptQueue(net.aufdemrand.denizencore.scripts.queues.ScriptQueue)

Example 3 with ScriptEntry

use of net.aufdemrand.denizencore.scripts.ScriptEntry in project Denizen-For-Bukkit by DenizenScript.

the class CommandScriptContainer method runAllowedHelpProcedure.

public boolean runAllowedHelpProcedure(dPlayer player, dNPC npc, Map<String, dObject> context) {
    // Add the reqId to each of the entries for the determine command
    List<ScriptEntry> entries = getEntries(new BukkitScriptEntryData(player, npc), "ALLOWED HELP");
    long id = DetermineCommand.getNewId();
    ScriptBuilder.addObjectToEntries(entries, "ReqId", id);
    ScriptQueue queue = InstantQueue.getQueue(ScriptQueue.getNextId(getName())).setReqId(id).addEntries(entries);
    if (context != null) {
        OldEventManager.OldEventContextSource oecs = new OldEventManager.OldEventContextSource();
        oecs.contexts = context;
        queue.setContextSource(oecs);
    }
    queue.start();
    return DetermineCommand.hasOutcome(id) && DetermineCommand.getOutcome(id).get(0).equalsIgnoreCase("true");
}
Also used : BukkitScriptEntryData(net.aufdemrand.denizen.BukkitScriptEntryData) OldEventManager(net.aufdemrand.denizencore.events.OldEventManager) ScriptEntry(net.aufdemrand.denizencore.scripts.ScriptEntry) ScriptQueue(net.aufdemrand.denizencore.scripts.queues.ScriptQueue)

Example 4 with ScriptEntry

use of net.aufdemrand.denizencore.scripts.ScriptEntry in project Denizen-For-Bukkit by DenizenScript.

the class Denizen method onCommand.

@Override
public boolean onCommand(CommandSender sender, Command cmd, String cmdName, String[] args) {
    if (cmdName.equalsIgnoreCase("ex")) {
        List<Object> entries = new ArrayList<Object>();
        String entry = "";
        for (String arg : args) {
            entry = entry + arg + " ";
        }
        if (entry.length() < 2) {
            sender.sendMessage("/ex <dCommand> (arguments)");
            return true;
        }
        if (Settings.showExHelp()) {
            if (dB.showDebug) {
                sender.sendMessage(ChatColor.YELLOW + "Executing dCommand... check the console for debug output!");
            } else {
                sender.sendMessage(ChatColor.YELLOW + "Executing dCommand... to see debug, use /denizen debug");
            }
        }
        entries.add(entry);
        InstantQueue queue = InstantQueue.getQueue(ScriptQueue.getNextId("EXCOMMAND"));
        dNPC npc = null;
        if (Depends.citizens != null && Depends.citizens.getNPCSelector().getSelected(sender) != null) {
            npc = new dNPC(Depends.citizens.getNPCSelector().getSelected(sender));
        }
        List<ScriptEntry> scriptEntries = ScriptBuilder.buildScriptEntries(entries, null, new BukkitScriptEntryData(sender instanceof Player ? new dPlayer((Player) sender) : null, npc));
        queue.addEntries(scriptEntries);
        queue.start();
        return true;
    }
    String modifier = args.length > 0 ? args[0] : "";
    if (!commandManager.hasCommand(cmd, modifier) && !modifier.isEmpty()) {
        return suggestClosestModifier(sender, cmd.getName(), modifier);
    }
    Object[] methodArgs = { sender };
    return commandManager.executeSafe(cmd, args, sender, methodArgs);
}
Also used : OfflinePlayer(org.bukkit.OfflinePlayer) Player(org.bukkit.entity.Player) FakePlayer(net.aufdemrand.denizen.nms.interfaces.FakePlayer) ArrayList(java.util.ArrayList) net.aufdemrand.denizencore.objects.dObject(net.aufdemrand.denizencore.objects.dObject) InstantQueue(net.aufdemrand.denizencore.scripts.queues.core.InstantQueue) ScriptEntry(net.aufdemrand.denizencore.scripts.ScriptEntry)

Example 5 with ScriptEntry

use of net.aufdemrand.denizencore.scripts.ScriptEntry in project Denizen-For-Bukkit by DenizenScript.

the class WalkCommand method checkHeld.

public void checkHeld(dEntity entity) {
    for (int i = 0; i < held.size(); i++) {
        ScriptEntry entry = held.get(i);
        List<dEntity> waitForEntities = (List<dEntity>) entry.getObject("entities");
        if (waitForEntities == null) {
            continue;
        }
        waitForEntities.remove(entity);
        if (waitForEntities.isEmpty()) {
            if (!entry.hasObject("tally") || ((List<dNPC>) entry.getObject("tally")).isEmpty()) {
                entry.setFinished(true);
                held.remove(i);
                i--;
            }
        }
    }
}
Also used : net.aufdemrand.denizen.objects.dNPC(net.aufdemrand.denizen.objects.dNPC) net.aufdemrand.denizen.objects.dEntity(net.aufdemrand.denizen.objects.dEntity) ScriptEntry(net.aufdemrand.denizencore.scripts.ScriptEntry) ArrayList(java.util.ArrayList) List(java.util.List) net.aufdemrand.denizencore.objects.dList(net.aufdemrand.denizencore.objects.dList)

Aggregations

ScriptEntry (net.aufdemrand.denizencore.scripts.ScriptEntry)20 Element (net.aufdemrand.denizencore.objects.Element)9 ScriptQueue (net.aufdemrand.denizencore.scripts.queues.ScriptQueue)8 List (java.util.List)7 BukkitScriptEntryData (net.aufdemrand.denizen.BukkitScriptEntryData)6 net.aufdemrand.denizencore.objects.dObject (net.aufdemrand.denizencore.objects.dObject)5 net.aufdemrand.denizencore.objects.dScript (net.aufdemrand.denizencore.objects.dScript)5 DebugElement (net.aufdemrand.denizencore.utilities.debugging.dB.DebugElement)5 net.aufdemrand.denizen.objects.dEntity (net.aufdemrand.denizen.objects.dEntity)4 OldEventManager (net.aufdemrand.denizencore.events.OldEventManager)4 CommandExecutionException (net.aufdemrand.denizencore.exceptions.CommandExecutionException)4 InvalidArgumentsException (net.aufdemrand.denizencore.exceptions.InvalidArgumentsException)4 net.aufdemrand.denizencore.objects.dList (net.aufdemrand.denizencore.objects.dList)4 ArrayList (java.util.ArrayList)3 net.aufdemrand.denizen.objects.dLocation (net.aufdemrand.denizen.objects.dLocation)3 net.aufdemrand.denizen.objects.dNPC (net.aufdemrand.denizen.objects.dNPC)3 BukkitTagContext (net.aufdemrand.denizen.tags.BukkitTagContext)2 ScriptEntryCreationException (net.aufdemrand.denizencore.exceptions.ScriptEntryCreationException)2 InstantQueue (net.aufdemrand.denizencore.scripts.queues.core.InstantQueue)2 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)2