Search in sources :

Example 6 with InstantQueue

use of com.denizenscript.denizencore.scripts.queues.core.InstantQueue in project Denizen-For-Bukkit by DenizenScript.

the class EnchantmentScriptContainer method runSubScript.

public void runSubScript(String pathName, Entity attacker, Entity victim, Entity primary, int level) {
    validateThread();
    List<ScriptEntry> entries = getEntries(new BukkitScriptEntryData(new EntityTag(primary)), pathName);
    if (entries == null || entries.isEmpty()) {
        return;
    }
    InstantQueue queue = new InstantQueue(getName());
    queue.addEntries(entries);
    ContextSource.SimpleMap src = new ContextSource.SimpleMap();
    src.contexts = new HashMap<>();
    if (attacker != null) {
        src.contexts.put("attacker", new EntityTag(attacker));
    }
    if (victim != null) {
        src.contexts.put("victim", new EntityTag(victim));
    }
    src.contexts.put("level", new ElementTag(level));
    queue.contextSource = src;
    queue.start();
}
Also used : BukkitScriptEntryData(com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData) ContextSource(com.denizenscript.denizencore.scripts.queues.ContextSource) ScriptEntry(com.denizenscript.denizencore.scripts.ScriptEntry) EntityTag(com.denizenscript.denizen.objects.EntityTag) InstantQueue(com.denizenscript.denizencore.scripts.queues.core.InstantQueue) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag)

Example 7 with InstantQueue

use of com.denizenscript.denizencore.scripts.queues.core.InstantQueue in project Denizen-For-Bukkit by DenizenScript.

the class InventoryScriptContainer method getInventoryFrom.

public InventoryTag getInventoryFrom(TagContext context) {
    InventoryTag inventory;
    context = (context == null ? CoreUtilities.basicContext : context).clone();
    ScriptTag thisScript = new ScriptTag(this);
    context.script = thisScript;
    context.debug = context.debug && shouldDebug();
    try {
        InventoryType type = InventoryType.CHEST;
        if (contains("inventory", String.class)) {
            try {
                type = InventoryType.valueOf(getString("inventory").toUpperCase());
            } catch (IllegalArgumentException ex) {
                Debug.echoError(this, "Invalid inventory type specified. Assuming \"CHEST\" (" + ex.getMessage() + ")");
            }
        } else {
            Debug.echoError(this, "Inventory script '" + getName() + "' does not specify an inventory type. Assuming \"CHEST\".");
        }
        if (type == InventoryType.PLAYER) {
            Debug.echoError(this, "Inventory type 'player' is not valid for inventory scripts - defaulting to 'CHEST'.");
            type = InventoryType.CHEST;
        }
        int size = 0;
        if (contains("size", String.class)) {
            if (type != InventoryType.CHEST) {
                Debug.echoError(this, "You can only set the size of chest inventories!");
            } else {
                String sizeText = TagManager.tag(getString("size"), context);
                if (!ArgumentHelper.matchesInteger(sizeText)) {
                    Debug.echoError(this, "Invalid (not-a-number) size value.");
                } else {
                    size = Integer.parseInt(sizeText);
                }
                if (size == 0) {
                    Debug.echoError(this, "Inventory size can't be 0. Assuming default of inventory type...");
                }
                if (size % 9 != 0) {
                    size = (int) Math.ceil(size / 9.0) * 9;
                    Debug.echoError(this, "Inventory size must be a multiple of 9! Rounding up to " + size + "...");
                }
                if (size < 0) {
                    size = size * -1;
                    Debug.echoError(this, "Inventory size must be a positive number! Inverting to " + size + "...");
                }
            }
        }
        if (size == 0) {
            if (contains("slots", List.class) && type == InventoryType.CHEST) {
                size = getStringList("slots").size() * 9;
            } else {
                size = type.getDefaultSize();
            }
        }
        String title = contains("title", String.class) ? TagManager.tag(getString("title"), context) : null;
        if (type == InventoryType.CHEST) {
            inventory = new InventoryTag(size, title != null ? title : "Chest");
        } else {
            if (title == null) {
                inventory = new InventoryTag(type);
            } else {
                inventory = new InventoryTag(type, title);
            }
        }
        inventory.idType = "script";
        inventory.idHolder = thisScript;
        boolean[] filledSlots = new boolean[size];
        if (contains("slots", List.class)) {
            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("]")) {
                    Debug.echoError(this, "Invalid slots line: [" + items + "]... Ignoring it");
                    continue;
                }
                String[] itemsInLine = items.substring(1, items.length() - 1).split("\\[?\\]?\\s+\\[", -1);
                for (String item : itemsInLine) {
                    if (item.isEmpty()) {
                        finalItems[itemsAdded++] = new ItemStack(Material.AIR);
                        continue;
                    }
                    filledSlots[itemsAdded] = true;
                    if (contains("definitions." + item, String.class)) {
                        ItemTag def = ItemTag.valueOf(TagManager.tag(getString("definitions." + item), context), context);
                        if (def == null) {
                            Debug.echoError(this, "Invalid definition '" + item + "'... Ignoring it and assuming 'AIR'");
                            finalItems[itemsAdded] = new ItemStack(Material.AIR);
                        } else {
                            finalItems[itemsAdded] = def.getItemStack();
                        }
                    } else {
                        try {
                            ItemTag itemTag = ItemTag.valueOf(item, context);
                            if (itemTag == null) {
                                finalItems[itemsAdded] = new ItemStack(Material.AIR);
                                Debug.echoError(this, "Invalid slot item: [" + item + "]... ignoring it and assuming 'AIR'");
                            } else {
                                finalItems[itemsAdded] = itemTag.getItemStack();
                            }
                        } catch (Exception ex) {
                            Debug.echoError(this, "Invalid slot item: [" + item + "]...");
                            Debug.echoError(ex);
                        }
                    }
                    itemsAdded++;
                }
            }
            inventory.setContents(finalItems);
        }
        if (containsScriptSection("procedural items")) {
            List<ScriptEntry> entries = getEntries(context.getScriptEntryData(), "procedural items");
            if (!entries.isEmpty()) {
                InstantQueue queue = new InstantQueue("INV_SCRIPT_ITEM_PROC");
                queue.addEntries(entries);
                if (contains("definitions", Map.class)) {
                    YamlConfiguration section = getConfigurationSection("definitions");
                    for (StringHolder string : section.getKeys(false)) {
                        String definition = string.str;
                        queue.addDefinition(definition, section.getString(definition));
                    }
                }
                queue.procedural = true;
                queue.start();
                if (queue.determinations != null) {
                    ListTag list = ListTag.getListFor(queue.determinations.getObject(0), context);
                    if (list != null) {
                        int x = 0;
                        for (ItemTag item : list.filter(ItemTag.class, context, true)) {
                            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) {
        Debug.echoError(this, "Woah! An exception has been called while building this inventory script!");
        Debug.echoError(e);
        inventory = null;
    }
    if (inventory != null) {
        InventoryTag.trackTemporaryInventory(inventory);
    }
    return inventory;
}
Also used : InventoryType(org.bukkit.event.inventory.InventoryType) ScriptEntry(com.denizenscript.denizencore.scripts.ScriptEntry) YamlConfiguration(com.denizenscript.denizencore.utilities.YamlConfiguration) ListTag(com.denizenscript.denizencore.objects.core.ListTag) StringHolder(com.denizenscript.denizencore.utilities.text.StringHolder) ScriptTag(com.denizenscript.denizencore.objects.core.ScriptTag) List(java.util.List) InstantQueue(com.denizenscript.denizencore.scripts.queues.core.InstantQueue) ItemStack(org.bukkit.inventory.ItemStack) ItemTag(com.denizenscript.denizen.objects.ItemTag) InventoryTag(com.denizenscript.denizen.objects.InventoryTag)

Example 8 with InstantQueue

use of com.denizenscript.denizencore.scripts.queues.core.InstantQueue in project Denizen-For-Bukkit by DenizenScript.

the class CommandScriptContainer method runCommandScript.

public ScriptQueue runCommandScript(PlayerTag player, NPCTag npc, Map<String, ObjectTag> context) {
    ScriptQueue queue = new InstantQueue(getName());
    queue.addEntries(getBaseEntries(new BukkitScriptEntryData(player, npc)));
    if (context != null) {
        ContextSource.SimpleMap src = new ContextSource.SimpleMap();
        src.contexts = context;
        queue.setContextSource(src);
    }
    queue.start();
    return queue;
}
Also used : BukkitScriptEntryData(com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData) ContextSource(com.denizenscript.denizencore.scripts.queues.ContextSource) InstantQueue(com.denizenscript.denizencore.scripts.queues.core.InstantQueue) ScriptQueue(com.denizenscript.denizencore.scripts.queues.ScriptQueue)

Aggregations

InstantQueue (com.denizenscript.denizencore.scripts.queues.core.InstantQueue)8 ScriptEntry (com.denizenscript.denizencore.scripts.ScriptEntry)7 BukkitScriptEntryData (com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData)6 ContextSource (com.denizenscript.denizencore.scripts.queues.ContextSource)6 ScriptQueue (com.denizenscript.denizencore.scripts.queues.ScriptQueue)5 BukkitTagContext (com.denizenscript.denizen.tags.BukkitTagContext)2 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)2 ScriptTag (com.denizenscript.denizencore.objects.core.ScriptTag)2 ArrayList (java.util.ArrayList)2 EntityTag (com.denizenscript.denizen.objects.EntityTag)1 InventoryTag (com.denizenscript.denizen.objects.InventoryTag)1 ItemTag (com.denizenscript.denizen.objects.ItemTag)1 NPCTag (com.denizenscript.denizen.objects.NPCTag)1 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)1 ListTag (com.denizenscript.denizencore.objects.core.ListTag)1 TimedQueue (com.denizenscript.denizencore.scripts.queues.core.TimedQueue)1 YamlConfiguration (com.denizenscript.denizencore.utilities.YamlConfiguration)1 StringHolder (com.denizenscript.denizencore.utilities.text.StringHolder)1 List (java.util.List)1 Player (org.bukkit.entity.Player)1