Search in sources :

Example 21 with ConfigRuntimeException

use of com.laytonsmith.core.exceptions.ConfigRuntimeException 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 22 with ConfigRuntimeException

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

the class CommandHelperServerListener method onServerCommand.

@EventHandler(priority = EventPriority.LOWEST)
public void onServerCommand(ServerCommandEvent event) {
    // Select the proper CommandSender wrapper.
    MCCommandSender sender;
    if (event.getSender() instanceof ConsoleCommandSender) {
        // Console.
        sender = new BukkitMCConsoleCommandSender((ConsoleCommandSender) event.getSender());
    } else if (event.getSender() instanceof BlockCommandSender) {
        // Commandblock blocks.
        sender = new BukkitMCBlockCommandSender((BlockCommandSender) event.getSender());
    } else if (event.getSender() instanceof CommandMinecart) {
        // Commandblock minecarts.
        sender = new BukkitMCCommandMinecart((CommandMinecart) event.getSender());
    } else {
        // other CommandSenders.
        sender = new BukkitMCCommandSender(event.getSender());
    }
    BukkitMiscEvents.BukkitMCServerCommandEvent cce = new BukkitMiscEvents.BukkitMCServerCommandEvent(event, sender);
    EventUtils.TriggerListener(Driver.SERVER_COMMAND, "server_command", cce);
    try {
        if (event.isCancelled()) {
            return;
        }
    } catch (NoSuchMethodError ex) {
    // not cancellable before 1.8.8
    }
    boolean match = false;
    try {
        match = Static.getAliasCore().alias("/" + event.getCommand(), sender);
    } catch (InternalException e) {
        Static.getLogger().log(Level.SEVERE, e.getMessage());
    } catch (ConfigRuntimeException e) {
        Static.getLogger().log(Level.WARNING, e.getMessage());
    } catch (Throwable e) {
        sender.sendMessage(MCChatColor.RED + "Command failed with following reason: " + e.getMessage());
        // Obviously the command is registered, but it somehow failed. Cancel the event.
        e.printStackTrace();
        return;
    }
    // commandhelper null, which just returns true.
    if (match) {
        event.setCommand("commandhelper null");
    }
}
Also used : BukkitMCConsoleCommandSender(com.laytonsmith.abstraction.bukkit.BukkitMCConsoleCommandSender) BukkitMCCommandMinecart(com.laytonsmith.abstraction.bukkit.entities.BukkitMCCommandMinecart) ConfigRuntimeException(com.laytonsmith.core.exceptions.ConfigRuntimeException) BukkitMCCommandSender(com.laytonsmith.abstraction.bukkit.BukkitMCCommandSender) BukkitMCConsoleCommandSender(com.laytonsmith.abstraction.bukkit.BukkitMCConsoleCommandSender) ConsoleCommandSender(org.bukkit.command.ConsoleCommandSender) BukkitMCCommandSender(com.laytonsmith.abstraction.bukkit.BukkitMCCommandSender) MCCommandSender(com.laytonsmith.abstraction.MCCommandSender) CommandMinecart(org.bukkit.entity.minecart.CommandMinecart) BukkitMCCommandMinecart(com.laytonsmith.abstraction.bukkit.entities.BukkitMCCommandMinecart) InternalException(com.laytonsmith.core.InternalException) BukkitMCBlockCommandSender(com.laytonsmith.abstraction.bukkit.BukkitMCBlockCommandSender) BukkitMiscEvents(com.laytonsmith.abstraction.bukkit.events.BukkitMiscEvents) BlockCommandSender(org.bukkit.command.BlockCommandSender) BukkitMCBlockCommandSender(com.laytonsmith.abstraction.bukkit.BukkitMCBlockCommandSender) EventHandler(org.bukkit.event.EventHandler)

Aggregations

ConfigRuntimeException (com.laytonsmith.core.exceptions.ConfigRuntimeException)22 CString (com.laytonsmith.core.constructs.CString)11 GlobalEnv (com.laytonsmith.core.environments.GlobalEnv)10 FunctionReturnException (com.laytonsmith.core.exceptions.FunctionReturnException)10 Construct (com.laytonsmith.core.constructs.Construct)9 Environment (com.laytonsmith.core.environments.Environment)9 ArrayList (java.util.ArrayList)9 CancelCommandException (com.laytonsmith.core.exceptions.CancelCommandException)8 CArray (com.laytonsmith.core.constructs.CArray)7 CommandHelperEnvironment (com.laytonsmith.core.environments.CommandHelperEnvironment)7 IVariable (com.laytonsmith.core.constructs.IVariable)6 AbstractCREException (com.laytonsmith.core.exceptions.CRE.AbstractCREException)6 ConfigCompileException (com.laytonsmith.core.exceptions.ConfigCompileException)6 Target (com.laytonsmith.core.constructs.Target)5 CRECastException (com.laytonsmith.core.exceptions.CRE.CRECastException)5 ProgramFlowManipulationException (com.laytonsmith.core.exceptions.ProgramFlowManipulationException)5 ParseTree (com.laytonsmith.core.ParseTree)4 ConfigCompileGroupException (com.laytonsmith.core.exceptions.ConfigCompileGroupException)4 StackTraceManager (com.laytonsmith.core.exceptions.StackTraceManager)4 ProfilePoint (com.laytonsmith.core.profiler.ProfilePoint)4