Search in sources :

Example 21 with BukkitScriptEntryData

use of net.aufdemrand.denizen.BukkitScriptEntryData 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 22 with BukkitScriptEntryData

use of net.aufdemrand.denizen.BukkitScriptEntryData in project Denizen-For-Bukkit by DenizenScript.

the class ExecuteCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
    Element cmd = scriptEntry.getElement("command");
    Element type = scriptEntry.getElement("type");
    Element silent = scriptEntry.getElement("silent");
    // Report to dB
    dB.report(scriptEntry, getName(), type.debug() + cmd.debug() + silent.debug());
    String command = cmd.asString();
    switch(Type.valueOf(type.asString())) {
        case AS_PLAYER:
            try {
                PlayerCommandPreprocessEvent pcpe = new PlayerCommandPreprocessEvent(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getPlayerEntity(), "/" + command);
                Bukkit.getPluginManager().callEvent(pcpe);
                if (!pcpe.isCancelled()) {
                    boolean silentBool = silent.asBoolean();
                    Player player = ((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getPlayerEntity();
                    if (silentBool) {
                        silencedPlayers.add(player.getUniqueId());
                    }
                    player.performCommand(pcpe.getMessage().startsWith("/") ? pcpe.getMessage().substring(1) : pcpe.getMessage());
                    if (silentBool) {
                        silencedPlayers.remove(player.getUniqueId());
                    }
                }
            } catch (Throwable e) {
                dB.echoError(scriptEntry.getResidingQueue(), "Exception while executing command as player.");
                dB.echoError(scriptEntry.getResidingQueue(), e);
            }
            break;
        case AS_OP:
            if (CoreUtilities.toLowerCase(command).equals("stop")) {
                dB.echoError("Please use as_server to execute 'stop'.");
                return;
            }
            Player player = ((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getPlayerEntity();
            PlayerHelper playerHelper = NMSHandler.getInstance().getPlayerHelper();
            boolean isOp = player.isOp();
            if (!isOp) {
                playerHelper.setTemporaryOp(player, true);
            }
            try {
                PlayerCommandPreprocessEvent pcpe = new PlayerCommandPreprocessEvent(player, "/" + command);
                Bukkit.getPluginManager().callEvent(pcpe);
                if (!pcpe.isCancelled()) {
                    boolean silentBool = silent.asBoolean();
                    if (silentBool) {
                        silencedPlayers.add(player.getUniqueId());
                    }
                    player.performCommand(pcpe.getMessage().startsWith("/") ? pcpe.getMessage().substring(1) : pcpe.getMessage());
                    if (silentBool) {
                        silencedPlayers.remove(player.getUniqueId());
                    }
                }
            } catch (Throwable e) {
                dB.echoError(scriptEntry.getResidingQueue(), "Exception while executing command as OP.");
                dB.echoError(scriptEntry.getResidingQueue(), e);
            }
            if (!isOp) {
                playerHelper.setTemporaryOp(player, false);
            }
            break;
        case AS_NPC:
            if (!((BukkitScriptEntryData) scriptEntry.entryData).getNPC().isSpawned()) {
                dB.echoError(scriptEntry.getResidingQueue(), "Cannot EXECUTE AS_NPC unless the NPC is Spawned.");
                return;
            }
            if (((BukkitScriptEntryData) scriptEntry.entryData).getNPC().getEntity().getType() != EntityType.PLAYER) {
                dB.echoError(scriptEntry.getResidingQueue(), "Cannot EXECUTE AS_NPC unless the NPC is Player-Type.");
                return;
            }
            ((Player) ((BukkitScriptEntryData) scriptEntry.entryData).getNPC().getEntity()).setOp(true);
            try {
                ((Player) ((BukkitScriptEntryData) scriptEntry.entryData).getNPC().getEntity()).performCommand(command);
            } catch (Throwable e) {
                dB.echoError(scriptEntry.getResidingQueue(), "Exception while executing command as NPC-OP.");
                dB.echoError(scriptEntry.getResidingQueue(), e);
            }
            ((Player) ((BukkitScriptEntryData) scriptEntry.entryData).getNPC().getEntity()).setOp(false);
            break;
        case AS_SERVER:
            dcs.clearOutput();
            dcs.silent = silent.asBoolean();
            ServerCommandEvent sce = new ServerCommandEvent(dcs, command);
            Bukkit.getPluginManager().callEvent(sce);
            DenizenAPI.getCurrentInstance().getServer().dispatchCommand(dcs, sce.getCommand());
            scriptEntry.addObject("output", new dList(dcs.getOutput()));
            break;
    }
}
Also used : PlayerCommandPreprocessEvent(org.bukkit.event.player.PlayerCommandPreprocessEvent) Player(org.bukkit.entity.Player) BukkitScriptEntryData(net.aufdemrand.denizen.BukkitScriptEntryData) Element(net.aufdemrand.denizencore.objects.Element) net.aufdemrand.denizencore.objects.dList(net.aufdemrand.denizencore.objects.dList) PlayerHelper(net.aufdemrand.denizen.nms.interfaces.PlayerHelper) ServerCommandEvent(org.bukkit.event.server.ServerCommandEvent)

Example 23 with BukkitScriptEntryData

use of net.aufdemrand.denizen.BukkitScriptEntryData in project Denizen-For-Bukkit by DenizenScript.

the class TimeCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
    // Fetch objects
    Duration value = (Duration) scriptEntry.getObject("value");
    dWorld world = (dWorld) scriptEntry.getObject("world");
    Element type_element = scriptEntry.getElement("type");
    Type type = Type.valueOf(type_element.asString().toUpperCase());
    // Report to dB
    dB.report(scriptEntry, getName(), type_element.debug() + value.debug() + world.debug());
    if (type.equals(Type.GLOBAL)) {
        world.getWorld().setTime(value.getTicks());
    } else {
        if (!((BukkitScriptEntryData) scriptEntry.entryData).hasPlayer()) {
            dB.echoError("Must have a valid player link!");
        } else {
            ((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getPlayerEntity().setPlayerTime(value.getTicks(), true);
        }
    }
}
Also used : BukkitScriptEntryData(net.aufdemrand.denizen.BukkitScriptEntryData) Element(net.aufdemrand.denizencore.objects.Element) Duration(net.aufdemrand.denizencore.objects.Duration) net.aufdemrand.denizen.objects.dWorld(net.aufdemrand.denizen.objects.dWorld)

Example 24 with BukkitScriptEntryData

use of net.aufdemrand.denizen.BukkitScriptEntryData in project Denizen-For-Bukkit by DenizenScript.

the class ViewerCommand method execute.

@Override
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {
    dB.echoError(scriptEntry.getResidingQueue(), "This command is deprecated! If you use this, please " + "contact Morphan1 or mcmonkey on irc.esper.net#denizen-dev");
    // Get objects
    String direction = scriptEntry.hasObject("direction") ? ((Element) scriptEntry.getObject("direction")).asString() : null;
    Action action = (Action) scriptEntry.getObject("action");
    Type type = scriptEntry.hasObject("type") ? (Type) scriptEntry.getObject("type") : null;
    Display display = scriptEntry.hasObject("display") ? (Display) scriptEntry.getObject("display") : null;
    final String id = scriptEntry.getObject("id").toString();
    if (viewers.containsKey(id)) {
        ((BukkitScriptEntryData) scriptEntry.entryData).setPlayer(dPlayer.valueOf(viewers.get(id).getContent().split("; ")[1]));
    }
    dLocation location = scriptEntry.hasObject("location") ? (dLocation) scriptEntry.getObject("location") : null;
    String content = scriptEntry.hasObject("display") ? display.toString() + "; " + ((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getOfflinePlayer().getUniqueId() : null;
    switch(action) {
        case CREATE:
            if (viewers.containsKey(id)) {
                dB.echoDebug(scriptEntry, "Viewer ID " + id + " already exists!");
                return;
            }
            Viewer viewer = new Viewer(id, content, location);
            viewers.put(id, viewer);
            final Block sign = location.getBlock();
            sign.setType(Material.valueOf(type.name()));
            if (direction != null) {
                Utilities.setSignRotation(sign.getState(), direction);
            } else {
                Utilities.setSignRotation(sign.getState());
            }
            int task = Bukkit.getScheduler().scheduleSyncRepeatingTask(DenizenAPI.getCurrentInstance(), new Runnable() {

                public void run() {
                    Player player = Bukkit.getPlayer(UUID.fromString(viewers.get(id).getContent().split("; ")[1]));
                    if (player == null) {
                        Utilities.setSignLines((Sign) viewers.get(id).getLocation().getBlock().getState(), new String[] { "", viewers.get(id).getContent().split("; ")[1], "is offline.", "" });
                    } else {
                        Utilities.setSignLines((Sign) viewers.get(id).getLocation().getBlock().getState(), new String[] { String.valueOf((int) player.getLocation().getX()), String.valueOf((int) player.getLocation().getY()), String.valueOf((int) player.getLocation().getZ()), player.getWorld().getName() });
                    }
                }
            }, 0, 20);
            viewer.setTask(task);
            viewer.save();
            break;
        case MODIFY:
            if (!viewers.containsKey(id)) {
                dB.echoDebug(scriptEntry, "Viewer ID " + id + " doesn't exist!");
                return;
            }
            if (content != null) {
                viewers.get(id).setContent(content);
            }
            if (location != null) {
                if (type == null) {
                    type = Type.valueOf(viewers.get(id).getLocation().getBlock().getType().name());
                }
                Bukkit.getScheduler().cancelTask(viewers.get(id).getTask());
                int newTask = Bukkit.getScheduler().scheduleSyncRepeatingTask(DenizenAPI.getCurrentInstance(), new Runnable() {

                    public void run() {
                        Player player = Bukkit.getPlayer(UUID.fromString(viewers.get(id).getContent().split("; ")[1]));
                        if (player == null) {
                            Utilities.setSignLines((Sign) viewers.get(id).getLocation().getBlock().getState(), new String[] { "", viewers.get(id).getContent().split("; ")[1], "is offline.", "" });
                        } else {
                            Utilities.setSignLines((Sign) viewers.get(id).getLocation().getBlock().getState(), new String[] { String.valueOf((int) player.getLocation().getX()), String.valueOf((int) player.getLocation().getY()), String.valueOf((int) player.getLocation().getZ()), player.getWorld().getName() });
                        }
                    }
                }, 0, 20);
                viewers.get(id).getLocation().getBlock().setType(Material.AIR);
                viewers.get(id).setLocation(location);
                viewers.get(id).setTask(newTask);
                location.getBlock().setType(Material.valueOf(type.name()));
            }
            break;
        case REMOVE:
            if (!viewers.containsKey(id)) {
                dB.echoDebug(scriptEntry, "Viewer ID " + id + " doesn't exist!");
                return;
            }
            Block block = viewers.get(id).getLocation().getBlock();
            block.setType(Material.AIR);
            Bukkit.getScheduler().cancelTask(viewers.get(id).getTask());
            viewers.get(id).remove();
            viewers.remove(id);
    }
}
Also used : Player(org.bukkit.entity.Player) net.aufdemrand.denizen.objects.dPlayer(net.aufdemrand.denizen.objects.dPlayer) BukkitScriptEntryData(net.aufdemrand.denizen.BukkitScriptEntryData) Block(org.bukkit.block.Block) Sign(org.bukkit.block.Sign) net.aufdemrand.denizen.objects.dLocation(net.aufdemrand.denizen.objects.dLocation)

Example 25 with BukkitScriptEntryData

use of net.aufdemrand.denizen.BukkitScriptEntryData 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)

Aggregations

BukkitScriptEntryData (net.aufdemrand.denizen.BukkitScriptEntryData)61 Element (net.aufdemrand.denizencore.objects.Element)38 InvalidArgumentsException (net.aufdemrand.denizencore.exceptions.InvalidArgumentsException)19 net.aufdemrand.denizencore.objects.aH (net.aufdemrand.denizencore.objects.aH)18 net.aufdemrand.denizencore.objects.dList (net.aufdemrand.denizencore.objects.dList)18 net.aufdemrand.denizen.objects.dLocation (net.aufdemrand.denizen.objects.dLocation)13 net.aufdemrand.denizen.objects.dNPC (net.aufdemrand.denizen.objects.dNPC)13 Duration (net.aufdemrand.denizencore.objects.Duration)9 net.aufdemrand.denizen.objects.dEntity (net.aufdemrand.denizen.objects.dEntity)8 net.aufdemrand.denizencore.objects.dScript (net.aufdemrand.denizencore.objects.dScript)8 net.aufdemrand.denizen.objects.dPlayer (net.aufdemrand.denizen.objects.dPlayer)6 ScriptEntry (net.aufdemrand.denizencore.scripts.ScriptEntry)6 Player (org.bukkit.entity.Player)6 ArrayList (java.util.ArrayList)5 AssignmentTrait (net.aufdemrand.denizen.npc.traits.AssignmentTrait)5 net.aufdemrand.denizen.objects.dInventory (net.aufdemrand.denizen.objects.dInventory)5 net.aufdemrand.denizen.objects.dItem (net.aufdemrand.denizen.objects.dItem)5 CommandExecutionException (net.aufdemrand.denizencore.exceptions.CommandExecutionException)5 net.aufdemrand.denizencore.objects.dObject (net.aufdemrand.denizencore.objects.dObject)5 EventHandler (org.bukkit.event.EventHandler)5