Skip to content
Permalink
Browse files
Get CommandSender for executeas() and scriptas().
This allows for injected players that are currently offline, which is already a possibility for closures. Also now checks if executeas() has the minimum number of arguments and updates the docs. (Fixes 1194)
  • Loading branch information
PseudoKnight committed May 24, 2020
1 parent f111a7a commit 309df09d3c965137d7ee3caebaa290506ffcb7d9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 30 deletions.
@@ -2324,7 +2324,7 @@ public MSVersion since() {

@api
@seealso({com.laytonsmith.tools.docgen.templates.Closures.class})
public static class executeas extends AbstractFunction {
public static class executeas extends AbstractFunction implements Optimizable {

@Override
public String getName() {
@@ -2338,11 +2338,12 @@ public Integer[] numArgs() {

@Override
public String docs() {
return "mixed {player, label, [values...], closure} Executes the given closure in the context of a given"
return "mixed {sender, label, [values...], closure} Executes the given closure in the context of a given"
+ " player or " + Static.getConsoleName() + ". A closure that runs player(), for instance,"
+ " would return the specified player's name."
+ " If null is given, it will execute with the current sender context instead of the closure's."
+ " The label argument sets the permission label that this closure will use. If null is given,"
+ " the current label will be used, like with execute().";
+ " the closure's label will be used, like with execute().";
}

@Override
@@ -2373,12 +2374,10 @@ public Mixed exec(Target t, Environment environment, Mixed... args) throws Confi

MCCommandSender originalSender = cEnv.GetCommandSender();
MCCommandSender sender;
if(args[0].val().equals(Static.getConsoleName())) {
sender = Static.getServer().getConsole();
} else if(args[0] instanceof CNull) {
if(args[0] instanceof CNull) {
sender = environment.getEnv(CommandHelperEnvironment.class).GetCommandSender();
} else {
sender = Static.GetPlayer(args[0].val(), t);
sender = Static.GetCommandSender(args[0].val(), t);
}
cEnv.SetCommandSender(sender);

@@ -2399,6 +2398,21 @@ public Mixed exec(Target t, Environment environment, Mixed... args) throws Confi
public MSVersion since() {
return MSVersion.V3_3_2;
}

@Override
public Set<OptimizationOption> optimizationOptions() {
return EnumSet.of(OptimizationOption.OPTIMIZE_DYNAMIC);
}

@Override
public ParseTree optimizeDynamic(Target t, Environment env,
Set<Class<? extends Environment.EnvironmentImpl>> envs, List<ParseTree> children,
FileOptions fileOptions) throws ConfigCompileException, ConfigRuntimeException {
if(children.size() < 3) {
throw new ConfigCompileException(getName() + " must have 3 or more arguments", t);
}
return null;
}
}

@api
@@ -151,25 +151,21 @@ public Mixed exec(Target t, final Environment env, Mixed... args) throws CancelC
Static.getServer().runasConsole(cmd);
} else {
MCPlayer m = Static.GetPlayer(args[0], t);
if(m != null && m.isOnline()) {
MCPlayer p = env.getEnv(CommandHelperEnvironment.class).GetPlayer();
String name;

if(p != null) {
name = p.getName();
} else {
name = "Unknown player";
}

MSLog.GetLogger().Log(MSLog.Tags.META, LogLevel.INFO, "Executing command on " + name + " (running as " + args[0].val() + "): " + args[1].val().trim(), t);
if(Prefs.DebugMode()) {
Static.getLogger().log(Level.INFO, "Executing command on " + name + " (running as " + args[0].val() + "): " + args[1].val().trim());
}
//m.chat(cmd);
Static.getServer().dispatchCommand(m, cmd);
MCPlayer p = env.getEnv(CommandHelperEnvironment.class).GetPlayer();
String name;
if(p != null) {
name = p.getName();
} else {
throw new CREPlayerOfflineException("The player " + args[0].val() + " is not online", t);
name = "Unknown player";
}

MSLog.GetLogger().Log(MSLog.Tags.META, LogLevel.INFO, "Executing command on " + name + " (running as " + args[0].val() + "): " + args[1].val().trim(), t);
if(Prefs.DebugMode()) {
Static.getLogger().log(Level.INFO, "Executing command on " + name + " (running as " + args[0].val() + "): " + args[1].val().trim());
}

Static.getServer().dispatchCommand(m, cmd);
}
return CVoid.VOID;
}
@@ -621,7 +617,7 @@ public Integer[] numArgs() {

@Override
public String docs() {
return "void {player, [label], script} Runs the specified script in the context of a given player or "
return "void {sender, [label], script} Runs the specified script in the context of a given player or "
+ Static.getConsoleName() + ". A script that runs player(), for instance,"
+ " would return the specified player's name, not the player running the command."
+ " Setting the label allows you to dynamically set the label"
@@ -661,12 +657,7 @@ public Mixed exec(Target t, Environment environment, Mixed... args) {
@Override
public Mixed execs(Target t, Environment environment, Script parent, ParseTree... nodes) throws ConfigRuntimeException {
String senderName = parent.seval(nodes[0], environment).val();
MCCommandSender sender;
if(senderName.equals(Static.getConsoleName())) {
sender = Static.getServer().getConsole();
} else {
sender = Static.GetPlayer(senderName, t);
}
MCCommandSender sender = Static.GetCommandSender(senderName, t);
MCCommandSender originalSender = environment.getEnv(CommandHelperEnvironment.class).GetCommandSender();
int offset = 0;
String originalLabel = environment.getEnv(GlobalEnv.class).GetLabel();

0 comments on commit 309df09

Please sign in to comment.