Search in sources :

Example 1 with MCPlayerCommandEvent

use of com.laytonsmith.abstraction.events.MCPlayerCommandEvent 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)

Aggregations

MCPlayer (com.laytonsmith.abstraction.MCPlayer)1 BukkitMCPlayer (com.laytonsmith.abstraction.bukkit.entities.BukkitMCPlayer)1 MCPlayerCommandEvent (com.laytonsmith.abstraction.events.MCPlayerCommandEvent)1 InternalException (com.laytonsmith.core.InternalException)1 ConfigRuntimeException (com.laytonsmith.core.exceptions.ConfigRuntimeException)1 EventHandler (org.bukkit.event.EventHandler)1