Search in sources :

Example 16 with MCPlayer

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

the class EntityEvents method parseEntityDamageEvent.

public static Map<String, Construct> parseEntityDamageEvent(MCEntityDamageEvent event, Map<String, Construct> map) {
    if (event != null) {
        MCEntity victim = event.getEntity();
        map.put("type", new CString(victim.getType().name(), Target.UNKNOWN));
        map.put("id", new CString(victim.getUniqueId().toString(), Target.UNKNOWN));
        map.put("cause", new CString(event.getCause().name(), Target.UNKNOWN));
        map.put("amount", new CDouble(event.getDamage(), Target.UNKNOWN));
        map.put("finalamount", new CDouble(event.getFinalDamage(), Target.UNKNOWN));
        map.put("world", new CString(event.getEntity().getWorld().getName(), Target.UNKNOWN));
        map.put("location", ObjectGenerator.GetGenerator().location(event.getEntity().getLocation()));
        if (event instanceof MCEntityDamageByEntityEvent) {
            MCEntity damager = ((MCEntityDamageByEntityEvent) event).getDamager();
            if (damager instanceof MCPlayer) {
                map.put("damager", new CString(((MCPlayer) damager).getName(), Target.UNKNOWN));
            } else {
                map.put("damager", new CString(damager.getUniqueId().toString(), Target.UNKNOWN));
            }
            if (damager instanceof MCProjectile) {
                MCProjectileSource shooter = ((MCProjectile) damager).getShooter();
                if (shooter instanceof MCPlayer) {
                    map.put("shooter", new CString(((MCPlayer) shooter).getName(), Target.UNKNOWN));
                } else if (shooter instanceof MCEntity) {
                    map.put("shooter", new CString(((MCEntity) shooter).getUniqueId().toString(), Target.UNKNOWN));
                } else if (shooter instanceof MCBlockProjectileSource) {
                    map.put("shooter", ObjectGenerator.GetGenerator().location(((MCBlockProjectileSource) shooter).getBlock().getLocation()));
                }
            }
        }
    }
    return map;
}
Also used : MCBlockProjectileSource(com.laytonsmith.abstraction.blocks.MCBlockProjectileSource) MCEntityDamageByEntityEvent(com.laytonsmith.abstraction.events.MCEntityDamageByEntityEvent) MCEntity(com.laytonsmith.abstraction.MCEntity) MCPlayer(com.laytonsmith.abstraction.MCPlayer) CDouble(com.laytonsmith.core.constructs.CDouble) CString(com.laytonsmith.core.constructs.CString) MCProjectile(com.laytonsmith.abstraction.MCProjectile) MCProjectileSource(com.laytonsmith.abstraction.MCProjectileSource)

Example 17 with MCPlayer

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

the class CommandHelperInterpreterListener method onPlayerCommandPreprocess.

@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
    if (event.isCancelled()) {
        return;
    }
    if (interpreterMode.contains(event.getPlayer().getName())) {
        MCPlayer p = new BukkitMCPlayer(event.getPlayer());
        textLine(p, event.getMessage());
        event.setCancelled(true);
    }
}
Also used : MCPlayer(com.laytonsmith.abstraction.MCPlayer) BukkitMCPlayer(com.laytonsmith.abstraction.bukkit.entities.BukkitMCPlayer) BukkitMCPlayer(com.laytonsmith.abstraction.bukkit.entities.BukkitMCPlayer) EventHandler(org.bukkit.event.EventHandler)

Example 18 with MCPlayer

use of com.laytonsmith.abstraction.MCPlayer 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 19 with MCPlayer

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

Example 20 with MCPlayer

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

Aggregations

MCPlayer (com.laytonsmith.abstraction.MCPlayer)21 BukkitMCPlayer (com.laytonsmith.abstraction.bukkit.entities.BukkitMCPlayer)6 CommandHelperEnvironment (com.laytonsmith.core.environments.CommandHelperEnvironment)5 ConfigRuntimeException (com.laytonsmith.core.exceptions.ConfigRuntimeException)4 CString (com.laytonsmith.core.constructs.CString)3 GlobalEnv (com.laytonsmith.core.environments.GlobalEnv)3 CREPlayerOfflineException (com.laytonsmith.core.exceptions.CRE.CREPlayerOfflineException)3 ArrayList (java.util.ArrayList)3 Player (org.bukkit.entity.Player)3 EventHandler (org.bukkit.event.EventHandler)3 MCCommandSender (com.laytonsmith.abstraction.MCCommandSender)2 MCEntity (com.laytonsmith.abstraction.MCEntity)2 MCServer (com.laytonsmith.abstraction.MCServer)2 Environment (com.laytonsmith.core.environments.Environment)2 AbstractCREException (com.laytonsmith.core.exceptions.CRE.AbstractCREException)2 IOException (java.io.IOException)2 Method (java.lang.reflect.Method)2 MCCommand (com.laytonsmith.abstraction.MCCommand)1 MCConsoleCommandSender (com.laytonsmith.abstraction.MCConsoleCommandSender)1 MCItemStack (com.laytonsmith.abstraction.MCItemStack)1