Search in sources :

Example 1 with MCPlayer

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

the class Script method enforceLabelPermissions.

public void enforceLabelPermissions() {
    String label = CurrentEnv.getEnv(GlobalEnv.class).GetLabel();
    if (label == null || label.equals(Static.GLOBAL_PERMISSION)) {
        return;
    }
    MCPlayer p = CurrentEnv.getEnv(CommandHelperEnvironment.class).GetPlayer();
    if (p == null) {
        // labels only apply to players
        CurrentEnv.getEnv(GlobalEnv.class).SetLabel(Static.GLOBAL_PERMISSION);
    } else if (label.startsWith("~")) {
        // group labels
        String[] groups = label.substring(1).split("/");
        for (String group : groups) {
            if (group.startsWith("-") && p.inGroup(group.substring(1))) {
                // negative permission
                throw new CREInsufficientPermissionException("You do not have permission to use that script.", Target.UNKNOWN);
            } else if (p.inGroup(group)) {
                // they have explicit permission.
                CurrentEnv.getEnv(GlobalEnv.class).SetLabel(Static.GLOBAL_PERMISSION);
                return;
            }
        }
    } else if (label.indexOf('.') != -1) {
        // custom permission label
        if (p.hasPermission(label)) {
            CurrentEnv.getEnv(GlobalEnv.class).SetLabel(Static.GLOBAL_PERMISSION);
        }
    } else if (p.hasPermission("ch.alias." + label) || p.hasPermission("commandhelper.alias." + label)) {
        CurrentEnv.getEnv(GlobalEnv.class).SetLabel(Static.GLOBAL_PERMISSION);
    }
}
Also used : CREInsufficientPermissionException(com.laytonsmith.core.exceptions.CRE.CREInsufficientPermissionException) MCPlayer(com.laytonsmith.abstraction.MCPlayer) CommandHelperEnvironment(com.laytonsmith.core.environments.CommandHelperEnvironment) CString(com.laytonsmith.core.constructs.CString) GlobalEnv(com.laytonsmith.core.environments.GlobalEnv)

Example 2 with MCPlayer

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

the class CommandHelperInterpreterListener method onPlayerChat.

@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerChat(final AsyncPlayerChatEvent event) {
    if (interpreterMode.contains(event.getPlayer().getName())) {
        final MCPlayer p = new BukkitMCPlayer(event.getPlayer());
        event.setCancelled(true);
        StaticLayer.SetFutureRunnable(null, 0, new Runnable() {

            @Override
            public void run() {
                textLine(p, event.getMessage());
            }
        });
    }
}
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 3 with MCPlayer

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

the class AliasCore method alias.

/**
 * This is the workhorse function. It takes a given command, then converts it into the actual command(s). If the
 * command maps to a defined alias, it will run the specified alias. It will search through the global list of
 * aliases, as well as the aliases defined for that specific player. This function doesn't handle the /alias command
 * however.
 *
 * @param command
 * @return
 */
public boolean alias(String command, final MCCommandSender player) {
    if (scripts == null) {
        throw ConfigRuntimeException.CreateUncatchableException("Cannot run alias commands, no config file is loaded", Target.UNKNOWN);
    }
    boolean match = false;
    try {
        // actually add the player to the array.
        if (player != null && player instanceof MCPlayer && echoCommand.contains(((MCPlayer) player).getName())) {
            // we are running one of the expanded commands, so exit with false
            return false;
        }
        for (Script s : scripts) {
            try {
                if (s.match(command)) {
                    this.addPlayerReference(player);
                    if (Prefs.ConsoleLogCommands() && s.doLog()) {
                        StringBuilder b = new StringBuilder("CH: Running original command ");
                        if (player instanceof MCPlayer) {
                            b.append("on player ").append(((MCPlayer) player).getName());
                        } else {
                            b.append("from a MCCommandSender");
                        }
                        b.append(" ----> ").append(command);
                        Static.getLogger().log(Level.INFO, b.toString());
                    }
                    GlobalEnv gEnv = new GlobalEnv(parent.executionQueue, parent.profiler, parent.persistenceNetwork, MethodScriptFileLocations.getDefault().getConfigDirectory(), parent.profiles, new TaskManager());
                    CommandHelperEnvironment cEnv = new CommandHelperEnvironment();
                    cEnv.SetCommandSender(player);
                    Environment env = Environment.createEnvironment(gEnv, cEnv);
                    try {
                        env.getEnv(CommandHelperEnvironment.class).SetCommand(command);
                        ProfilePoint alias = env.getEnv(GlobalEnv.class).GetProfiler().start("Global Alias - \"" + command + "\"", LogLevel.ERROR);
                        try {
                            s.run(s.getVariables(command), env, new MethodScriptComplete() {

                                @Override
                                public void done(String output) {
                                    try {
                                        if (output != null) {
                                            if (!output.trim().isEmpty() && output.trim().startsWith("/")) {
                                                if (Prefs.DebugMode()) {
                                                    if (player instanceof MCPlayer) {
                                                        Static.getLogger().log(Level.INFO, "[CommandHelper]: Executing command on " + ((MCPlayer) player).getName() + ": " + output.trim());
                                                    } else {
                                                        Static.getLogger().log(Level.INFO, "[CommandHelper]: Executing command from console equivalent: " + output.trim());
                                                    }
                                                }
                                                if (player instanceof MCPlayer) {
                                                    ((MCPlayer) player).chat(output.trim());
                                                } else {
                                                    Static.getServer().dispatchCommand(player, output.trim().substring(1));
                                                }
                                            }
                                        }
                                    } catch (Throwable e) {
                                        StreamUtils.GetSystemErr().println(e.getMessage());
                                        player.sendMessage(MCChatColor.RED + e.getMessage());
                                    } finally {
                                        Static.getAliasCore().removePlayerReference(player);
                                    }
                                }
                            });
                        } finally {
                            alias.stop();
                        }
                    } catch (ConfigRuntimeException ex) {
                        ex.setEnv(env);
                        ConfigRuntimeException.HandleUncaughtException(ex, env);
                    } catch (Throwable e) {
                        // This is not a simple user script error, this is a deeper problem, so we always handle this.
                        StreamUtils.GetSystemErr().println("An unexpected exception occured: " + e.getClass().getSimpleName());
                        player.sendMessage("An unexpected exception occured: " + MCChatColor.RED + e.getClass().getSimpleName());
                        e.printStackTrace();
                    } finally {
                        Static.getAliasCore().removePlayerReference(player);
                    }
                    match = true;
                    break;
                }
            } catch (Exception e) {
                StreamUtils.GetSystemErr().println("An unexpected exception occured inside the command " + s.toString());
                e.printStackTrace();
            }
        }
    } catch (Throwable e) {
        // Not only did an error happen, an error happened in our error handler
        throw new InternalException(TermColors.RED + "An unexpected error occured in the CommandHelper plugin. " + "Further, this is likely an error with the error handler, so it may be caused by your script, " + "however, there is no more information at this point. Check your script, but also report this " + "as a bug in CommandHelper. Also, it's possible that some commands will no longer work. As a temporary " + "workaround, restart the server, and avoid doing whatever it is you did to make this happen.\nThe error is as follows: " + e.toString() + "\n" + TermColors.reset() + "Stack Trace:\n" + StringUtils.Join(Arrays.asList(e.getStackTrace()), "\n"));
    }
    return match;
}
Also used : MCPlayer(com.laytonsmith.abstraction.MCPlayer) ConfigRuntimeException(com.laytonsmith.core.exceptions.ConfigRuntimeException) ProgramFlowManipulationException(com.laytonsmith.core.exceptions.ProgramFlowManipulationException) CancelCommandException(com.laytonsmith.core.exceptions.CancelCommandException) ConfigCompileException(com.laytonsmith.core.exceptions.ConfigCompileException) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) ConfigRuntimeException(com.laytonsmith.core.exceptions.ConfigRuntimeException) ConfigCompileGroupException(com.laytonsmith.core.exceptions.ConfigCompileGroupException) ProfilePoint(com.laytonsmith.core.profiler.ProfilePoint) TaskManager(com.laytonsmith.core.taskmanager.TaskManager) CommandHelperEnvironment(com.laytonsmith.core.environments.CommandHelperEnvironment) CommandHelperEnvironment(com.laytonsmith.core.environments.CommandHelperEnvironment) Environment(com.laytonsmith.core.environments.Environment) GlobalEnv(com.laytonsmith.core.environments.GlobalEnv)

Example 4 with MCPlayer

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

the class BukkitEntityListener method onTargetLiving.

@EventIdentifier(event = Driver.TARGET_ENTITY, className = "org.bukkit.event.entity.EntityTargetEvent")
public void onTargetLiving(Event event) {
    BukkitMCTargetEvent ete = new BukkitMCTargetEvent(event);
    MCEntity target = ete.getTarget();
    if (target == null || !(target instanceof MCPlayer)) {
        return;
    }
    EventUtils.TriggerListener(Driver.TARGET_ENTITY, "target_player", ete);
}
Also used : MCEntity(com.laytonsmith.abstraction.MCEntity) MCPlayer(com.laytonsmith.abstraction.MCPlayer) EventIdentifier(com.laytonsmith.annotations.EventIdentifier)

Example 5 with MCPlayer

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

the class BukkitMCPluginMeta method sendIncomingMessage0.

@Override
protected void sendIncomingMessage0(MCPlayer player, String channel, byte[] message) {
    Player p = ((BukkitMCPlayer) player)._Player();
    Bukkit.getMessenger().dispatchIncomingMessage(p, channel, message);
}
Also used : MCPlayer(com.laytonsmith.abstraction.MCPlayer) BukkitMCPlayer(com.laytonsmith.abstraction.bukkit.entities.BukkitMCPlayer) Player(org.bukkit.entity.Player) BukkitMCPlayer(com.laytonsmith.abstraction.bukkit.entities.BukkitMCPlayer)

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