Search in sources :

Example 6 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)

Example 7 with MCPlayer

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

the class MetaTest method testScriptas.

@Test
public void testScriptas() throws Exception {
    String script = "scriptas('Player02', 'newlabel', msg(reflect_pull('label'))); msg(reflect_pull('label'))";
    MCPlayer fakePlayer2 = GetOnlinePlayer("Player02", fakeServer);
    when(fakeServer.getPlayer("Player02")).thenReturn(fakePlayer2);
    MethodScriptCompiler.execute(MethodScriptCompiler.compile(MethodScriptCompiler.lex(script, null, true)), env, null, null);
    verify(fakePlayer2).sendMessage("newlabel");
    verify(fakePlayer).sendMessage("*");
}
Also used : MCPlayer(com.laytonsmith.abstraction.MCPlayer) Test(org.junit.Test)

Example 8 with MCPlayer

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

the class AbstractEvent method execute.

/**
 * This function is run when the actual event occurs.
 *
 * @param tree The compiled parse tree
 * @param b The bound event
 * @param env The operating environment
 * @param activeEvent The active event being executed
 */
@Override
public final void execute(ParseTree tree, BoundEvent b, Environment env, BoundEvent.ActiveEvent activeEvent) throws ConfigRuntimeException {
    preExecution(env, activeEvent);
    // Various events have a player to put into the env.
    // Do this after preExcecution() in case the particular event needs to inject the player first.
    Construct c = activeEvent.getParsedEvent().get("player");
    if (c != null) {
        try {
            MCPlayer p = Static.GetPlayer(c, Target.UNKNOWN);
            env.getEnv(CommandHelperEnvironment.class).SetPlayer(p);
        } catch (CREPlayerOfflineException e) {
            // Set env CommandSender to prevent incorrect inherited player from being used in a player event.
            if (env.getEnv(CommandHelperEnvironment.class).GetPlayer() != null) {
                env.getEnv(CommandHelperEnvironment.class).SetCommandSender(Static.getServer().getConsole());
            }
        }
    }
    ProfilePoint event = null;
    if (env.getEnv(GlobalEnv.class).GetProfiler() != null) {
        event = env.getEnv(GlobalEnv.class).GetProfiler().start("Event " + b.getEventName() + " (defined at " + b.getTarget().toString() + ")", LogLevel.ERROR);
    }
    try {
        try {
            // Get the label from the bind time environment, and put it in the current environment.
            String label = b.getEnvironment().getEnv(GlobalEnv.class).GetLabel();
            if (label == null) {
                // Set the permission to global if it's null, since that means
                // it wasn't set, and so we aren't in a secured environment anyways.
                label = Static.GLOBAL_PERMISSION;
            }
            env.getEnv(GlobalEnv.class).SetLabel(label);
            MethodScriptCompiler.execute(tree, env, null, null);
        } catch (CancelCommandException ex) {
            if (ex.getMessage() != null && !ex.getMessage().isEmpty()) {
                StreamUtils.GetSystemOut().println(ex.getMessage());
            }
        } catch (FunctionReturnException ex) {
        // We simply allow this to end the event execution
        } catch (ProgramFlowManipulationException ex) {
            ConfigRuntimeException.HandleUncaughtException(new CREFormatException("Unexpected control flow operation used.", ex.getTarget()), env);
        }
    } finally {
        if (event != null) {
            event.stop();
        }
        // Finally, among other things, we need to clean-up injected players and entities
        postExecution(env, activeEvent);
    }
}
Also used : CREPlayerOfflineException(com.laytonsmith.core.exceptions.CRE.CREPlayerOfflineException) CancelCommandException(com.laytonsmith.core.exceptions.CancelCommandException) MCPlayer(com.laytonsmith.abstraction.MCPlayer) Construct(com.laytonsmith.core.constructs.Construct) CommandHelperEnvironment(com.laytonsmith.core.environments.CommandHelperEnvironment) ProgramFlowManipulationException(com.laytonsmith.core.exceptions.ProgramFlowManipulationException) GlobalEnv(com.laytonsmith.core.environments.GlobalEnv) FunctionReturnException(com.laytonsmith.core.exceptions.FunctionReturnException) CREFormatException(com.laytonsmith.core.exceptions.CRE.CREFormatException) ProfilePoint(com.laytonsmith.core.profiler.ProfilePoint)

Example 9 with MCPlayer

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

the class BukkitMCServer method getOnlinePlayers.

@Override
public Collection<MCPlayer> getOnlinePlayers() {
    Collection<? extends Player> players = s.getOnlinePlayers();
    Set<MCPlayer> mcpa = new HashSet<>();
    for (Player p : players) {
        mcpa.add(new BukkitMCPlayer(p));
    }
    return mcpa;
}
Also used : Player(org.bukkit.entity.Player) OfflinePlayer(org.bukkit.OfflinePlayer) BukkitMCPlayer(com.laytonsmith.abstraction.bukkit.entities.BukkitMCPlayer) MCPlayer(com.laytonsmith.abstraction.MCPlayer) MCOfflinePlayer(com.laytonsmith.abstraction.MCOfflinePlayer) BukkitMCPlayer(com.laytonsmith.abstraction.bukkit.entities.BukkitMCPlayer) MCPlayer(com.laytonsmith.abstraction.MCPlayer) BukkitMCPlayer(com.laytonsmith.abstraction.bukkit.entities.BukkitMCPlayer) HashSet(java.util.HashSet)

Example 10 with MCPlayer

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

the class MetaTest method testRunas1.

@Test(timeout = 10000)
public void testRunas1() throws Exception {
    String script = "runas('Player02', '/cmd yay')";
    MCPlayer fakePlayer2 = GetOnlinePlayer("Player02", fakeServer);
    when(fakeServer.getPlayer("Player02")).thenReturn(fakePlayer2);
    when(fakePlayer.isOp()).thenReturn(true);
    MethodScriptCompiler.execute(MethodScriptCompiler.compile(MethodScriptCompiler.lex(script, null, true)), env, null, null);
    // verify(fakePlayer2).performCommand("cmd yay");
    verify(fakeServer).dispatchCommand(fakePlayer2, "cmd yay");
}
Also used : MCPlayer(com.laytonsmith.abstraction.MCPlayer) Test(org.junit.Test)

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