forked from Redolith/ReActions
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
47 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
reactions/src/main/java/fun/reactions/module/basic/placeholders/FunctionPlaceholder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package fun.reactions.module.basic.placeholders; | ||
|
||
import fun.reactions.model.activators.Activator; | ||
import fun.reactions.model.environment.Environment; | ||
import fun.reactions.model.environment.Variables; | ||
import fun.reactions.module.basic.activators.FunctionActivator; | ||
import fun.reactions.placeholders.Placeholder; | ||
import fun.reactions.util.naming.Aliased; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
@Aliased.Names("funct") | ||
public class FunctionPlaceholder implements Placeholder { | ||
@Override | ||
public @Nullable String resolve(@NotNull Environment env, @NotNull String key, @NotNull String params) { | ||
Activator activator = env.getPlatform().getActivators().getActivator(params); | ||
if (activator == null || activator.getClass() != FunctionActivator.class) { | ||
return null; | ||
} | ||
String id = activator.getLogic().getName(); | ||
Variables vars = new Variables(); | ||
try { | ||
activator.getLogic().execute(new Environment( | ||
env.getPlatform(), | ||
id, | ||
vars, | ||
env.getPlayer(), | ||
env.isAsync() | ||
)); | ||
} catch (StackOverflowError error) { | ||
env.getPlatform().logger().error( | ||
"FUNCTION placeholder (" + params + ") failed in '" + env.getActivatorName() + "' due to stack overflow. " + | ||
"Consider limiting the usage of looped FUNCTION placeholders and RUN_FUNCTION actions." | ||
); | ||
} | ||
return vars.getStringUnsafe("return"); | ||
} | ||
|
||
@Override | ||
public @NotNull String getName() { | ||
return "function"; | ||
} | ||
} |