Search in sources :

Example 1 with CREPlayerOfflineException

use of com.laytonsmith.core.exceptions.CRE.CREPlayerOfflineException in project CommandHelper by EngineHub.

the class AbstractEvent method execute.

/**
 * This function is run when the actual event occurs.
 *
 * @param tree The compiled parse tree
 * @param b The bound event
 * @param env The operating environment
 * @param activeEvent The active event being executed
 */
@Override
public final void execute(ParseTree tree, BoundEvent b, Environment env, BoundEvent.ActiveEvent activeEvent) throws ConfigRuntimeException {
    preExecution(env, activeEvent);
    // Various events have a player to put into the env.
    // Do this after preExcecution() in case the particular event needs to inject the player first.
    Construct c = activeEvent.getParsedEvent().get("player");
    if (c != null) {
        try {
            MCPlayer p = Static.GetPlayer(c, Target.UNKNOWN);
            env.getEnv(CommandHelperEnvironment.class).SetPlayer(p);
        } catch (CREPlayerOfflineException e) {
            // Set env CommandSender to prevent incorrect inherited player from being used in a player event.
            if (env.getEnv(CommandHelperEnvironment.class).GetPlayer() != null) {
                env.getEnv(CommandHelperEnvironment.class).SetCommandSender(Static.getServer().getConsole());
            }
        }
    }
    ProfilePoint event = null;
    if (env.getEnv(GlobalEnv.class).GetProfiler() != null) {
        event = env.getEnv(GlobalEnv.class).GetProfiler().start("Event " + b.getEventName() + " (defined at " + b.getTarget().toString() + ")", LogLevel.ERROR);
    }
    try {
        try {
            // Get the label from the bind time environment, and put it in the current environment.
            String label = b.getEnvironment().getEnv(GlobalEnv.class).GetLabel();
            if (label == null) {
                // Set the permission to global if it's null, since that means
                // it wasn't set, and so we aren't in a secured environment anyways.
                label = Static.GLOBAL_PERMISSION;
            }
            env.getEnv(GlobalEnv.class).SetLabel(label);
            MethodScriptCompiler.execute(tree, env, null, null);
        } catch (CancelCommandException ex) {
            if (ex.getMessage() != null && !ex.getMessage().isEmpty()) {
                StreamUtils.GetSystemOut().println(ex.getMessage());
            }
        } catch (FunctionReturnException ex) {
        // We simply allow this to end the event execution
        } catch (ProgramFlowManipulationException ex) {
            ConfigRuntimeException.HandleUncaughtException(new CREFormatException("Unexpected control flow operation used.", ex.getTarget()), env);
        }
    } finally {
        if (event != null) {
            event.stop();
        }
        // Finally, among other things, we need to clean-up injected players and entities
        postExecution(env, activeEvent);
    }
}
Also used : CREPlayerOfflineException(com.laytonsmith.core.exceptions.CRE.CREPlayerOfflineException) CancelCommandException(com.laytonsmith.core.exceptions.CancelCommandException) MCPlayer(com.laytonsmith.abstraction.MCPlayer) Construct(com.laytonsmith.core.constructs.Construct) CommandHelperEnvironment(com.laytonsmith.core.environments.CommandHelperEnvironment) ProgramFlowManipulationException(com.laytonsmith.core.exceptions.ProgramFlowManipulationException) GlobalEnv(com.laytonsmith.core.environments.GlobalEnv) FunctionReturnException(com.laytonsmith.core.exceptions.FunctionReturnException) CREFormatException(com.laytonsmith.core.exceptions.CRE.CREFormatException) ProfilePoint(com.laytonsmith.core.profiler.ProfilePoint)

Example 2 with CREPlayerOfflineException

use of com.laytonsmith.core.exceptions.CRE.CREPlayerOfflineException in project CommandHelper by EngineHub.

the class Static method GetPlayer.

/**
 * Returns the player specified by name. Injected players also are returned in this list. If provided a string
 * between 1 and 16 characters, the lookup will be name-based. If provided a string that is 32 or 36 characters, the
 * lookup will be uuid-based.
 *
 * @param player
 * @param t
 * @return
 * @throws ConfigRuntimeException
 */
public static MCPlayer GetPlayer(String player, Target t) throws ConfigRuntimeException {
    MCCommandSender m;
    if (player == null) {
        throw new CREPlayerOfflineException("No player was specified!", t);
    }
    if (player.length() > 0 && player.length() <= 16) {
        m = GetCommandSender(player, t);
    } else {
        try {
            m = getServer().getPlayer(GetUUID(player, t));
        } catch (ConfigRuntimeException cre) {
            if (cre instanceof CRELengthException) {
                throw new CRELengthException("The given string was the wrong size to identify a player." + " A player name is expected to be between 1 and 16 characters. " + cre.getMessage(), t);
            } else {
                throw cre;
            }
        }
    }
    if (m == null) {
        throw new CREPlayerOfflineException("The specified player (" + player + ") is not online", t);
    }
    if (!(m instanceof MCPlayer)) {
        throw new CREPlayerOfflineException("Expecting a player name, but \"" + player + "\" was found.", t);
    }
    MCPlayer p = (MCPlayer) m;
    if (!p.isOnline()) {
        throw new CREPlayerOfflineException("The specified player (" + player + ") is not online", t);
    }
    return p;
}
Also used : CREPlayerOfflineException(com.laytonsmith.core.exceptions.CRE.CREPlayerOfflineException) CRELengthException(com.laytonsmith.core.exceptions.CRE.CRELengthException) MCPlayer(com.laytonsmith.abstraction.MCPlayer) ConfigRuntimeException(com.laytonsmith.core.exceptions.ConfigRuntimeException) MCCommandSender(com.laytonsmith.abstraction.MCCommandSender)

Example 3 with CREPlayerOfflineException

use of com.laytonsmith.core.exceptions.CRE.CREPlayerOfflineException in project CommandHelper by EngineHub.

the class Static method SendMessage.

/**
 * This function sends a message to the player. If the player is not online, a CRE is thrown.
 *
 * @param m
 * @param msg
 */
public static void SendMessage(final MCCommandSender m, String msg, final Target t) {
    if (m != null && !(m instanceof MCConsoleCommandSender)) {
        if (m instanceof MCPlayer) {
            MCPlayer p = (MCPlayer) m;
            if (!p.isOnline()) {
                throw new CREPlayerOfflineException("The player " + p.getName() + " is not online", t);
            }
        }
        m.sendMessage(msg);
    } else {
        msg = Static.MCToANSIColors(msg);
        if (msg.contains("\033")) {
            // We have terminal colors, we need to reset them at the end
            msg += TermColors.reset();
        }
        StreamUtils.GetSystemOut().println(msg);
    }
}
Also used : CREPlayerOfflineException(com.laytonsmith.core.exceptions.CRE.CREPlayerOfflineException) MCPlayer(com.laytonsmith.abstraction.MCPlayer) MCConsoleCommandSender(com.laytonsmith.abstraction.MCConsoleCommandSender)

Aggregations

MCPlayer (com.laytonsmith.abstraction.MCPlayer)3 CREPlayerOfflineException (com.laytonsmith.core.exceptions.CRE.CREPlayerOfflineException)3 MCCommandSender (com.laytonsmith.abstraction.MCCommandSender)1 MCConsoleCommandSender (com.laytonsmith.abstraction.MCConsoleCommandSender)1 Construct (com.laytonsmith.core.constructs.Construct)1 CommandHelperEnvironment (com.laytonsmith.core.environments.CommandHelperEnvironment)1 GlobalEnv (com.laytonsmith.core.environments.GlobalEnv)1 CREFormatException (com.laytonsmith.core.exceptions.CRE.CREFormatException)1 CRELengthException (com.laytonsmith.core.exceptions.CRE.CRELengthException)1 CancelCommandException (com.laytonsmith.core.exceptions.CancelCommandException)1 ConfigRuntimeException (com.laytonsmith.core.exceptions.ConfigRuntimeException)1 FunctionReturnException (com.laytonsmith.core.exceptions.FunctionReturnException)1 ProgramFlowManipulationException (com.laytonsmith.core.exceptions.ProgramFlowManipulationException)1 ProfilePoint (com.laytonsmith.core.profiler.ProfilePoint)1