|
1 | 1 | package com.laytonsmith.core.functions; |
2 | 2 |
|
3 | 3 | import com.laytonsmith.PureUtilities.Version; |
| 4 | +import com.laytonsmith.abstraction.MCCommandSender; |
4 | 5 | import com.laytonsmith.annotations.api; |
5 | 6 | import com.laytonsmith.annotations.breakable; |
6 | 7 | import com.laytonsmith.annotations.core; |
@@ -3217,6 +3218,79 @@ public CHVersion since() { |
3217 | 3218 | } |
3218 | 3219 | } |
3219 | 3220 |
|
| 3221 | + @api |
| 3222 | + @seealso({com.laytonsmith.tools.docgen.templates.Closures.class}) |
| 3223 | + public static class executeas extends AbstractFunction { |
| 3224 | + |
| 3225 | + @Override |
| 3226 | + public String getName() { |
| 3227 | + return "executeas"; |
| 3228 | + } |
| 3229 | + |
| 3230 | + @Override |
| 3231 | + public Integer[] numArgs() { |
| 3232 | + return new Integer[]{Integer.MAX_VALUE}; |
| 3233 | + } |
| 3234 | + |
| 3235 | + @Override |
| 3236 | + public String docs() { |
| 3237 | + return "mixed {player, label, [values...,] closure} Executes the given closure in the context of a given" |
| 3238 | + + " player. A closure that runs player(), for instance, would return the specified player's name." |
| 3239 | + + " The label argument sets the permission label that this closure will use. If null is given," |
| 3240 | + + " the current label will be used, like with execute()."; |
| 3241 | + } |
| 3242 | + |
| 3243 | + @Override |
| 3244 | + public Class<? extends CREThrowable>[] thrown() { |
| 3245 | + return new Class[]{CRECastException.class}; |
| 3246 | + } |
| 3247 | + |
| 3248 | + @Override |
| 3249 | + public Boolean runAsync() { |
| 3250 | + return null; |
| 3251 | + } |
| 3252 | + |
| 3253 | + @Override |
| 3254 | + public boolean isRestricted() { |
| 3255 | + return true; |
| 3256 | + } |
| 3257 | + |
| 3258 | + @Override |
| 3259 | + public Construct exec(Target t, Environment environment, Construct... args) throws ConfigRuntimeException { |
| 3260 | + if(!(args[args.length - 1] instanceof CClosure)) { |
| 3261 | + throw new CRECastException("Only a closure (created from the closure function) can be sent to executeas()", t); |
| 3262 | + } |
| 3263 | + Construct[] vals = new Construct[args.length - 3]; |
| 3264 | + System.arraycopy(args, 2, vals, 0, args.length - 3); |
| 3265 | + CClosure closure = (CClosure) args[args.length - 1]; |
| 3266 | + CommandHelperEnvironment cEnv = closure.getEnv().getEnv(CommandHelperEnvironment.class); |
| 3267 | + GlobalEnv gEnv = closure.getEnv().getEnv(GlobalEnv.class); |
| 3268 | + |
| 3269 | + MCCommandSender originalSender = cEnv.GetCommandSender(); |
| 3270 | + cEnv.SetCommandSender(Static.GetCommandSender(args[0].val(), t)); |
| 3271 | + |
| 3272 | + String originalLabel = gEnv.GetLabel(); |
| 3273 | + if(!(args[1] instanceof CNull)) { |
| 3274 | + gEnv.SetLabel(args[1].val()); |
| 3275 | + } |
| 3276 | + |
| 3277 | + try { |
| 3278 | + closure.execute(vals); |
| 3279 | + } catch (FunctionReturnException e) { |
| 3280 | + return e.getReturn(); |
| 3281 | + } finally { |
| 3282 | + cEnv.SetCommandSender(originalSender); |
| 3283 | + gEnv.SetLabel(originalLabel); |
| 3284 | + } |
| 3285 | + return CVoid.VOID; |
| 3286 | + } |
| 3287 | + |
| 3288 | + @Override |
| 3289 | + public CHVersion since() { |
| 3290 | + return CHVersion.V3_3_2; |
| 3291 | + } |
| 3292 | + } |
| 3293 | + |
3220 | 3294 | @api |
3221 | 3295 | public static class _boolean extends AbstractFunction implements Optimizable { |
3222 | 3296 |
|
|
0 commit comments