Skip to content

Commit

Permalink
Add FUNCTION placeholder
Browse files Browse the repository at this point in the history
  • Loading branch information
imDaniX committed Feb 12, 2024
1 parent b4194e7 commit e5e656e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ public void onDisable(@NotNull ReActions.Platform platform) {
new ActivatorNamePlaceholder(),
new PersistentVarPlaceholders(),
new LocalVarPlaceholder(),
new EnderChestPlaceholder()
new EnderChestPlaceholder(),
new FunctionPlaceholder()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public boolean proceed(@NotNull Environment env, @NotNull String paramsStr) {
} catch (StackOverflowError error) {
env.getPlatform().logger().error(
"RUN_FUNCTION action failed in '" + env.getActivatorName() + "' due to stack overflow. " +
"Consider limiting the usage of looped FUNCTION actions or try using EXECUTE actions when possible."
"Consider limiting the usage of looped RUN_FUNCTION actions and FUNCTION placeholders " +
"or try using EXECUTE actions."
);
return false;
}
Expand Down
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";
}
}

0 comments on commit e5e656e

Please sign in to comment.