Search in sources :

Example 1 with ServerCommandEvent

use of org.bukkit.event.server.ServerCommandEvent 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)

Aggregations

BukkitScriptEntryData (net.aufdemrand.denizen.BukkitScriptEntryData)1 PlayerHelper (net.aufdemrand.denizen.nms.interfaces.PlayerHelper)1 Element (net.aufdemrand.denizencore.objects.Element)1 net.aufdemrand.denizencore.objects.dList (net.aufdemrand.denizencore.objects.dList)1 Player (org.bukkit.entity.Player)1 PlayerCommandPreprocessEvent (org.bukkit.event.player.PlayerCommandPreprocessEvent)1 ServerCommandEvent (org.bukkit.event.server.ServerCommandEvent)1