Search in sources :

Example 1 with MCCommandSender

use of com.laytonsmith.abstraction.MCCommandSender in project CommandHelper by EngineHub.

the class Script method run.

public void run(final List<Variable> vars, Environment myEnv, final MethodScriptComplete done) {
    // Some things, such as the label are determined at compile time
    this.CurrentEnv = myEnv;
    this.CurrentEnv.getEnv(GlobalEnv.class).SetLabel(this.label);
    MCCommandSender p = myEnv.getEnv(CommandHelperEnvironment.class).GetCommandSender();
    if (!hasBeenCompiled || compilerError) {
        Target target = Target.UNKNOWN;
        if (left.size() >= 1) {
            try {
                target = new Target(left.get(0).line_num, left.get(0).file, left.get(0).column);
            } catch (NullPointerException e) {
            // Oh well, we tried to get more information
            }
        }
        throw ConfigRuntimeException.CreateUncatchableException("Unable to run command, script not yet compiled," + " or a compiler error occurred for that command. To see the compile error, run /reloadaliases", target);
    }
    enforceLabelPermissions();
    try {
        for (ParseTree rootNode : cright) {
            if (rootNode == null) {
                continue;
            }
            for (Construct tempNode : rootNode.getAllData()) {
                if (tempNode instanceof Variable) {
                    if (left_vars == null) {
                        throw ConfigRuntimeException.CreateUncatchableException("$variables may not be used in this context." + " Only @variables may be.", tempNode.getTarget());
                    }
                    Construct c = Static.resolveDollarVar(left_vars.get(((Variable) tempNode).getVariableName()), vars);
                    ((Variable) tempNode).setVal(new CString(c.toString(), tempNode.getTarget()));
                }
            }
            MethodScriptCompiler.registerAutoIncludes(CurrentEnv, this);
            MethodScriptCompiler.execute(rootNode, CurrentEnv, done, this);
        }
    } catch (ConfigRuntimeException ex) {
        // We don't know how to handle this really, so let's pass it up the chain.
        throw ex;
    } catch (CancelCommandException e) {
    // p.sendMessage(e.getMessage());
    // The message in the exception is actually empty
    } catch (LoopBreakException e) {
        if (p != null) {
            p.sendMessage("The break() function must be used inside a for() or foreach() loop");
        }
        StreamUtils.GetSystemOut().println("The break() function must be used inside a for() or foreach() loop");
    } catch (LoopContinueException e) {
        if (p != null) {
            p.sendMessage("The continue() function must be used inside a for() or foreach() loop");
        }
        StreamUtils.GetSystemOut().println("The continue() function must be used inside a for() or foreach() loop");
    } catch (FunctionReturnException e) {
        if (myEnv.getEnv(GlobalEnv.class).GetEvent() != null) {
            // Oh, we're running in an event handler. Those know how to catch it too.
            throw e;
        }
        if (p != null) {
            p.sendMessage("The return() function must be used inside a procedure.");
        }
        StreamUtils.GetSystemOut().println("The return() function must be used inside a procedure.");
    } catch (Throwable t) {
        StreamUtils.GetSystemOut().println("An unexpected exception occurred during the execution of a script.");
        t.printStackTrace();
        if (p != null) {
            p.sendMessage("An unexpected exception occurred during the execution of your script." + " Please check the console for more information.");
        }
    }
    if (done != null) {
        done.done(null);
    }
}
Also used : IVariable(com.laytonsmith.core.constructs.IVariable) Variable(com.laytonsmith.core.constructs.Variable) ConfigRuntimeException(com.laytonsmith.core.exceptions.ConfigRuntimeException) MCCommandSender(com.laytonsmith.abstraction.MCCommandSender) CString(com.laytonsmith.core.constructs.CString) Target(com.laytonsmith.core.constructs.Target) LoopContinueException(com.laytonsmith.core.exceptions.LoopContinueException) CancelCommandException(com.laytonsmith.core.exceptions.CancelCommandException) CommandHelperEnvironment(com.laytonsmith.core.environments.CommandHelperEnvironment) Construct(com.laytonsmith.core.constructs.Construct) LoopBreakException(com.laytonsmith.core.exceptions.LoopBreakException) GlobalEnv(com.laytonsmith.core.environments.GlobalEnv) FunctionReturnException(com.laytonsmith.core.exceptions.FunctionReturnException)

Example 2 with MCCommandSender

use of com.laytonsmith.abstraction.MCCommandSender in project CommandHelper by EngineHub.

the class PlayerManangementTest method testPlayer3.

@Test(timeout = 10000)
public void testPlayer3() throws Exception {
    MCCommandSender c = GetFakeConsoleCommandSender();
    assertEquals("~console", SRun("player()", c));
}
Also used : MCCommandSender(com.laytonsmith.abstraction.MCCommandSender) Test(org.junit.Test) StaticTest(com.laytonsmith.testing.StaticTest)

Example 3 with MCCommandSender

use of com.laytonsmith.abstraction.MCCommandSender in project CommandHelper by EngineHub.

the class BukkitMCServer method dispatchAndCaptureCommand.

@Override
public String dispatchAndCaptureCommand(MCCommandSender commandSender, String cmd) {
    // Grab the CommandSender object from the abstraction layer
    CommandSender sender = (CommandSender) commandSender.getHandle();
    // Create the interceptor
    CommandSenderInterceptor interceptor = new CommandSenderInterceptor(sender);
    // Create a new proxy and abstraction layer wrapper around the proxy
    CommandSender newCommandSender = (CommandSender) Proxy.newProxyInstance(BukkitMCServer.class.getClassLoader(), new Class[] { CommandSender.class }, interceptor);
    BukkitMCCommandSender aCommandSender = new BukkitMCCommandSender(newCommandSender);
    MCCommandSender oldSender = Static.UninjectPlayer(commandSender);
    // Inject our new wrapped object
    Static.InjectPlayer(aCommandSender);
    // Dispatch the command now
    try {
        s.dispatchCommand(newCommandSender, cmd);
    } finally {
        // Clean up
        Static.UninjectPlayer(aCommandSender);
        if (oldSender != null) {
            Static.InjectPlayer(oldSender);
        }
    }
    // Return the buffered text (if any)
    return interceptor.getBuffer();
}
Also used : MCConsoleCommandSender(com.laytonsmith.abstraction.MCConsoleCommandSender) CommandSender(org.bukkit.command.CommandSender) MCCommandSender(com.laytonsmith.abstraction.MCCommandSender) MCCommandSender(com.laytonsmith.abstraction.MCCommandSender)

Example 4 with MCCommandSender

use of com.laytonsmith.abstraction.MCCommandSender in project CommandHelper by EngineHub.

the class ConfigRuntimeException method GetReaction.

/**
 * If a exception bubbles all the way up to the top, this should be called first, to see what reaction the plugin
 * should take. Generally speaking, you'll want to use {@link #HandleUncaughtException} instead of this, though if
 * you need to take custom action, you can determine the user's preferred reaction with this method.
 *
 * @param e
 * @return
 */
public static Reaction GetReaction(ConfigRuntimeException e, Environment env) {
    // If there is an exception handler, call it to see what it says.
    Reaction reaction = Reaction.REPORT;
    if (env.getEnv(GlobalEnv.class).GetExceptionHandler() != null) {
        CClosure c = env.getEnv(GlobalEnv.class).GetExceptionHandler();
        CArray ex = ObjectGenerator.GetGenerator().exception(e, env, Target.UNKNOWN);
        if (e.getEnv() != null) {
            MCCommandSender sender = e.getEnv().getEnv(CommandHelperEnvironment.class).GetCommandSender();
            c.getEnv().getEnv(CommandHelperEnvironment.class).SetCommandSender(sender);
        }
        Construct ret = CNull.NULL;
        try {
            c.execute(new Construct[] { ex });
        } catch (FunctionReturnException retException) {
            ret = retException.getReturn();
        }
        if (ret instanceof CNull || Prefs.ScreamErrors()) {
            reaction = Reaction.REPORT;
        } else {
            if (Static.getBoolean(ret, Target.UNKNOWN)) {
                reaction = Reaction.IGNORE;
            } else {
                reaction = Reaction.FATAL;
            }
        }
    }
    return reaction;
}
Also used : CClosure(com.laytonsmith.core.constructs.CClosure) CArray(com.laytonsmith.core.constructs.CArray) CommandHelperEnvironment(com.laytonsmith.core.environments.CommandHelperEnvironment) Construct(com.laytonsmith.core.constructs.Construct) GlobalEnv(com.laytonsmith.core.environments.GlobalEnv) MCCommandSender(com.laytonsmith.abstraction.MCCommandSender) CNull(com.laytonsmith.core.constructs.CNull)

Example 5 with MCCommandSender

use of com.laytonsmith.abstraction.MCCommandSender in project CommandHelper by EngineHub.

the class CommandHelperPlugin method onCommand.

/**
 * Called when a command registered by this plugin is received.
 *
 * @param sender
 * @param cmd
 * @param commandLabel
 * @param args
 * @return
 */
@Override
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    String cmdName = cmd.getName().toLowerCase();
    if ((sender.isOp() || (sender instanceof Player && (sender.hasPermission("commandhelper.reloadaliases") || sender.hasPermission("ch.reloadaliases")))) && (cmdName.equals("reloadaliases") || cmdName.equals("reloadalias") || cmdName.equals("recompile"))) {
        MCPlayer player = null;
        if (sender instanceof Player) {
            player = new BukkitMCPlayer((Player) sender);
        }
        ac.reload(player, args, false);
        return true;
    } else if (cmdName.equalsIgnoreCase("commandhelper")) {
        return args.length >= 1 && args[0].equalsIgnoreCase("null");
    } else if (cmdName.equals("runalias")) {
        // Hardcoded alias rebroadcast
        if (args.length == 0) {
            return false;
        }
        String command = StringUtils.Join(args, " ");
        if (sender instanceof Player) {
            PlayerCommandPreprocessEvent pcpe = new PlayerCommandPreprocessEvent((Player) sender, command);
            playerListener.onPlayerCommandPreprocess(pcpe);
        } else if (sender instanceof ConsoleCommandSender || sender instanceof BlockCommandSender || sender instanceof CommandMinecart) {
            // event handler that would get them if they would not have started with "/runalias".
            if (command.startsWith("/")) {
                command = command.substring(1);
            }
            ServerCommandEvent sce = new ServerCommandEvent(sender, command);
            serverListener.onServerCommand(sce);
        }
        return true;
    } else if (cmdName.equalsIgnoreCase("interpreter-on")) {
        if (sender instanceof ConsoleCommandSender) {
            int interpreterTimeout = Prefs.InterpreterTimeout();
            if (interpreterTimeout != 0) {
                interpreterUnlockedUntil = (interpreterTimeout * 60 * 1000) + System.currentTimeMillis();
                sender.sendMessage("Interpreter mode unlocked for " + interpreterTimeout + " minute" + (interpreterTimeout == 1 ? "" : "s"));
            }
        } else {
            sender.sendMessage("This command can only be run from console.");
        }
        return true;
    } else if (sender instanceof Player && cmdName.equalsIgnoreCase("interpreter")) {
        if (!sender.hasPermission("commandhelper.interpreter")) {
            sender.sendMessage(MCChatColor.RED + "You do not have permission to run that command");
        } else if (!Prefs.EnableInterpreter()) {
            sender.sendMessage(MCChatColor.RED + "The interpreter is currently disabled." + " Check your preferences file.");
        } else if (Prefs.InterpreterTimeout() != 0 && interpreterUnlockedUntil < System.currentTimeMillis()) {
            sender.sendMessage(MCChatColor.RED + "Interpreter mode is currently locked. Run \"interpreter-on\"" + " console to unlock it. If you want to turn this off entirely, set the interpreter-timeout" + " option to 0 in " + CommandHelperFileLocations.getDefault().getPreferencesFile().getName());
        } else {
            interpreterListener.startInterpret(sender.getName());
            sender.sendMessage(MCChatColor.YELLOW + "You are now in interpreter mode. Type a dash (-) on a" + " line by itself to exit, and >>> to enter multiline mode.");
        }
        return true;
    } else {
        MCCommandSender mcsender = BukkitConvertor.BukkitGetCorrectSender(sender);
        MCCommand mccmd = new BukkitMCCommand(cmd);
        return mccmd.handleCustomCommand(mcsender, commandLabel, args);
    }
}
Also used : PlayerCommandPreprocessEvent(org.bukkit.event.player.PlayerCommandPreprocessEvent) Player(org.bukkit.entity.Player) BukkitMCPlayer(com.laytonsmith.abstraction.bukkit.entities.BukkitMCPlayer) MCPlayer(com.laytonsmith.abstraction.MCPlayer) BukkitMCCommand(com.laytonsmith.abstraction.bukkit.BukkitMCCommand) MCCommand(com.laytonsmith.abstraction.MCCommand) BukkitMCPlayer(com.laytonsmith.abstraction.bukkit.entities.BukkitMCPlayer) MCPlayer(com.laytonsmith.abstraction.MCPlayer) ServerCommandEvent(org.bukkit.event.server.ServerCommandEvent) BukkitMCPlayer(com.laytonsmith.abstraction.bukkit.entities.BukkitMCPlayer) ConsoleCommandSender(org.bukkit.command.ConsoleCommandSender) MCCommandSender(com.laytonsmith.abstraction.MCCommandSender) BukkitMCCommand(com.laytonsmith.abstraction.bukkit.BukkitMCCommand) BlockCommandSender(org.bukkit.command.BlockCommandSender) CommandMinecart(org.bukkit.entity.minecart.CommandMinecart)

Aggregations

MCCommandSender (com.laytonsmith.abstraction.MCCommandSender)8 ConfigRuntimeException (com.laytonsmith.core.exceptions.ConfigRuntimeException)3 MCCommand (com.laytonsmith.abstraction.MCCommand)2 MCPlayer (com.laytonsmith.abstraction.MCPlayer)2 BukkitMCCommand (com.laytonsmith.abstraction.bukkit.BukkitMCCommand)2 Construct (com.laytonsmith.core.constructs.Construct)2 CommandHelperEnvironment (com.laytonsmith.core.environments.CommandHelperEnvironment)2 GlobalEnv (com.laytonsmith.core.environments.GlobalEnv)2 BlockCommandSender (org.bukkit.command.BlockCommandSender)2 ConsoleCommandSender (org.bukkit.command.ConsoleCommandSender)2 CommandMinecart (org.bukkit.entity.minecart.CommandMinecart)2 MCConsoleCommandSender (com.laytonsmith.abstraction.MCConsoleCommandSender)1 BukkitMCBlockCommandSender (com.laytonsmith.abstraction.bukkit.BukkitMCBlockCommandSender)1 BukkitMCCommandSender (com.laytonsmith.abstraction.bukkit.BukkitMCCommandSender)1 BukkitMCConsoleCommandSender (com.laytonsmith.abstraction.bukkit.BukkitMCConsoleCommandSender)1 BukkitMCCommandMinecart (com.laytonsmith.abstraction.bukkit.entities.BukkitMCCommandMinecart)1 BukkitMCPlayer (com.laytonsmith.abstraction.bukkit.entities.BukkitMCPlayer)1 BukkitMiscEvents (com.laytonsmith.abstraction.bukkit.events.BukkitMiscEvents)1 InternalException (com.laytonsmith.core.InternalException)1 CArray (com.laytonsmith.core.constructs.CArray)1