Search in sources :

Example 1 with InternalException

use of com.laytonsmith.core.InternalException in project CommandHelper by EngineHub.

the class CommandHelperListener method onPlayerCommandPreprocess.

/**
 * Called when a player attempts to use a command
 *
 * @param event Relevant event details
 */
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
    if (CommandHelperPlugin.self.interpreterListener.isInInterpreterMode(event.getPlayer().getName())) {
        // They are in interpreter mode, so we want it to handle this, not everything else.
        return;
    }
    MCPlayerCommandEvent mpce = new BukkitPlayerEvents.BukkitMCPlayerCommandEvent(event);
    EventUtils.TriggerListener(Driver.PLAYER_COMMAND, "player_command", mpce);
    if (mpce.isCancelled()) {
        return;
    }
    String cmd = event.getMessage();
    MCPlayer player = new BukkitMCPlayer(event.getPlayer());
    BukkitDirtyRegisteredListener.PlayDirty();
    if (!Prefs.PlayDirty()) {
        if (event.isCancelled()) {
            return;
        }
    }
    try {
        if (runAlias(event.getMessage(), player)) {
            event.setCancelled(true);
            if (Prefs.PlayDirty()) {
                // Super cancel the event
                BukkitDirtyRegisteredListener.setCancelled(event);
            }
        }
    } catch (InternalException e) {
        logger.log(Level.SEVERE, e.getMessage());
    } catch (ConfigRuntimeException e) {
        logger.log(Level.WARNING, e.getMessage());
    } catch (Throwable e) {
        player.sendMessage(MCChatColor.RED + "Command failed with following reason: " + e.getMessage());
        // Obviously the command is registered, but it somehow failed. Cancel the event.
        event.setCancelled(true);
        e.printStackTrace();
    }
}
Also used : MCPlayerCommandEvent(com.laytonsmith.abstraction.events.MCPlayerCommandEvent) MCPlayer(com.laytonsmith.abstraction.MCPlayer) BukkitMCPlayer(com.laytonsmith.abstraction.bukkit.entities.BukkitMCPlayer) ConfigRuntimeException(com.laytonsmith.core.exceptions.ConfigRuntimeException) BukkitMCPlayer(com.laytonsmith.abstraction.bukkit.entities.BukkitMCPlayer) InternalException(com.laytonsmith.core.InternalException) EventHandler(org.bukkit.event.EventHandler)

Example 2 with InternalException

use of com.laytonsmith.core.InternalException 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

InternalException (com.laytonsmith.core.InternalException)2 ConfigRuntimeException (com.laytonsmith.core.exceptions.ConfigRuntimeException)2 EventHandler (org.bukkit.event.EventHandler)2 MCCommandSender (com.laytonsmith.abstraction.MCCommandSender)1 MCPlayer (com.laytonsmith.abstraction.MCPlayer)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 MCPlayerCommandEvent (com.laytonsmith.abstraction.events.MCPlayerCommandEvent)1 BlockCommandSender (org.bukkit.command.BlockCommandSender)1 ConsoleCommandSender (org.bukkit.command.ConsoleCommandSender)1 CommandMinecart (org.bukkit.entity.minecart.CommandMinecart)1