Skip to content

Commit e5e656e

Browse files
committed
Add FUNCTION placeholder
1 parent b4194e7 commit e5e656e

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

reactions/src/main/java/fun/reactions/module/basic/BasicModule.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,8 @@ public void onDisable(@NotNull ReActions.Platform platform) {
230230
new ActivatorNamePlaceholder(),
231231
new PersistentVarPlaceholders(),
232232
new LocalVarPlaceholder(),
233-
new EnderChestPlaceholder()
233+
new EnderChestPlaceholder(),
234+
new FunctionPlaceholder()
234235
);
235236
}
236237

reactions/src/main/java/fun/reactions/module/basic/actions/RunFunctionAction.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public boolean proceed(@NotNull Environment env, @NotNull String paramsStr) {
3838
} catch (StackOverflowError error) {
3939
env.getPlatform().logger().error(
4040
"RUN_FUNCTION action failed in '" + env.getActivatorName() + "' due to stack overflow. " +
41-
"Consider limiting the usage of looped FUNCTION actions or try using EXECUTE actions when possible."
41+
"Consider limiting the usage of looped RUN_FUNCTION actions and FUNCTION placeholders " +
42+
"or try using EXECUTE actions."
4243
);
4344
return false;
4445
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package fun.reactions.module.basic.placeholders;
2+
3+
import fun.reactions.model.activators.Activator;
4+
import fun.reactions.model.environment.Environment;
5+
import fun.reactions.model.environment.Variables;
6+
import fun.reactions.module.basic.activators.FunctionActivator;
7+
import fun.reactions.placeholders.Placeholder;
8+
import fun.reactions.util.naming.Aliased;
9+
import org.jetbrains.annotations.NotNull;
10+
import org.jetbrains.annotations.Nullable;
11+
12+
@Aliased.Names("funct")
13+
public class FunctionPlaceholder implements Placeholder {
14+
@Override
15+
public @Nullable String resolve(@NotNull Environment env, @NotNull String key, @NotNull String params) {
16+
Activator activator = env.getPlatform().getActivators().getActivator(params);
17+
if (activator == null || activator.getClass() != FunctionActivator.class) {
18+
return null;
19+
}
20+
String id = activator.getLogic().getName();
21+
Variables vars = new Variables();
22+
try {
23+
activator.getLogic().execute(new Environment(
24+
env.getPlatform(),
25+
id,
26+
vars,
27+
env.getPlayer(),
28+
env.isAsync()
29+
));
30+
} catch (StackOverflowError error) {
31+
env.getPlatform().logger().error(
32+
"FUNCTION placeholder (" + params + ") failed in '" + env.getActivatorName() + "' due to stack overflow. " +
33+
"Consider limiting the usage of looped FUNCTION placeholders and RUN_FUNCTION actions."
34+
);
35+
}
36+
return vars.getStringUnsafe("return");
37+
}
38+
39+
@Override
40+
public @NotNull String getName() {
41+
return "function";
42+
}
43+
}

0 commit comments

Comments
 (0)