Skip to content

Commit

Permalink
Add ability to pass arguments to functions called by scheduled tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmq committed Apr 1, 2024
1 parent 8c0182d commit 2e94374
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
14 changes: 11 additions & 3 deletions src/main/java/dev/magicmq/pyspigot/manager/task/RepeatingTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@

import dev.magicmq.pyspigot.manager.script.Script;
import dev.magicmq.pyspigot.manager.script.ScriptManager;
import org.python.core.Py;
import org.python.core.PyException;
import org.python.core.PyFunction;
import org.python.core.PyObject;

/**
* Represents a repeating task defined by a script.
Expand All @@ -30,9 +32,10 @@ public class RepeatingTask extends Task {
*
* @param script The script associated with this repeating task
* @param function The script function that should be called every time the repeating task executes
* @param functionArgs Any arguments that should be passed to the function
*/
public RepeatingTask(Script script, PyFunction function) {
super(script, function);
public RepeatingTask(Script script, PyFunction function, Object[] functionArgs) {
super(script, function, functionArgs);
}

/**
Expand All @@ -41,7 +44,12 @@ public RepeatingTask(Script script, PyFunction function) {
@Override
public void run() {
try {
function.__call__();
if (functionArgs != null) {
PyObject[] pyObjects = Py.javas2pys(functionArgs);
function.__call__(pyObjects);
} else {
function.__call__();
}
} catch (PyException e) {
ScriptManager.get().handleScriptException(script, e, "Error when executing task #" + getTaskId());
}
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/dev/magicmq/pyspigot/manager/task/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import dev.magicmq.pyspigot.manager.script.Script;
import dev.magicmq.pyspigot.manager.script.ScriptManager;
import org.bukkit.scheduler.BukkitRunnable;
import org.python.core.Py;
import org.python.core.PyException;
import org.python.core.PyFunction;
import org.python.core.PyObject;

/**
* Represents a task defined by a script.
Expand All @@ -29,15 +31,18 @@ public class Task extends BukkitRunnable {

protected final Script script;
protected final PyFunction function;
protected final Object[] functionArgs;

/**
*
* @param script The script associated with this task
* @param function The script function that should be called when the task executes
* @param functionArgs Any arguments that should be passed to the function
*/
public Task(Script script, PyFunction function) {
public Task(Script script, PyFunction function, Object[] functionArgs) {
this.script = script;
this.function = function;
this.functionArgs = functionArgs;
}

/**
Expand All @@ -46,7 +51,12 @@ public Task(Script script, PyFunction function) {
@Override
public void run() {
try {
function.__call__();
if (functionArgs != null) {
PyObject[] pyObjects = Py.javas2pys(functionArgs);
function.__call__(pyObjects);
} else {
function.__call__();
}
} catch (PyException e) {
ScriptManager.get().handleScriptException(script, e, "Error when executing task #" + getTaskId());
} finally {
Expand Down
30 changes: 18 additions & 12 deletions src/main/java/dev/magicmq/pyspigot/manager/task/TaskManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ private TaskManager() {
* <p>
* <b>Note:</b> This should be called from scripts only!
* @param function The function that should be called when the synchronous task executes
* @param functionArgs Any arguments that should be passed to the function
* @return An ID representing the synchronous task that was scheduled
*/
public synchronized int runTask(PyFunction function) {
public synchronized int runTask(PyFunction function, Object... functionArgs) {
Script script = ScriptUtils.getScriptFromCallStack();
Task task = new Task(script, function);
Task task = new Task(script, function, functionArgs);
addTask(task);
task.runTask(PySpigot.get());
return task.getTaskId();
Expand All @@ -59,11 +60,12 @@ public synchronized int runTask(PyFunction function) {
* <p>
* <b>Note:</b> This should be called from scripts only!
* @param function The function that should be called when the asynchronous task executes
* @param functionArgs Any arguments that should be passed to the function
* @return An ID representing the asynchronous task that was scheduled
*/
public synchronized int runTaskAsync(PyFunction function) {
public synchronized int runTaskAsync(PyFunction function, Object... functionArgs) {
Script script = ScriptUtils.getScriptFromCallStack();
Task task = new Task(script, function);
Task task = new Task(script, function, functionArgs);
addTask(task);
task.runTaskAsynchronously(PySpigot.get());
return task.getTaskId();
Expand All @@ -75,11 +77,12 @@ public synchronized int runTaskAsync(PyFunction function) {
* <b>Note:</b> This should be called from scripts only!
* @param function The function that should be called when the synchronous task executes
* @param delay The delay, in ticks, that the scheduler should wait before executing the synchronous task
* @param functionArgs Any arguments that should be passed to the function
* @return An ID representing the synchronous task that was scheduled
*/
public synchronized int runTaskLater(PyFunction function, long delay) {
public synchronized int runTaskLater(PyFunction function, long delay, Object... functionArgs) {
Script script = ScriptUtils.getScriptFromCallStack();
Task task = new Task(script, function);
Task task = new Task(script, function, functionArgs);
addTask(task);
task.runTaskLater(PySpigot.get(), delay);
return task.getTaskId();
Expand All @@ -91,11 +94,12 @@ public synchronized int runTaskLater(PyFunction function, long delay) {
* <b>Note:</b> This should be called from scripts only!
* @param function The function that should be called when the asynchronous task executes
* @param delay The delay, in ticks, that the scheduler should wait before executing the asynchronous task
* @param functionArgs Any arguments that should be passed to the function
* @return An ID representing the asynchronous task that was scheduled
*/
public synchronized int runTaskLaterAsync(PyFunction function, long delay) {
public synchronized int runTaskLaterAsync(PyFunction function, long delay, Object... functionArgs) {
Script script = ScriptUtils.getScriptFromCallStack();
Task task = new Task(script, function);
Task task = new Task(script, function, functionArgs);
addTask(task);
task.runTaskLaterAsynchronously(PySpigot.get(), delay);
return task.getTaskId();
Expand All @@ -108,11 +112,12 @@ public synchronized int runTaskLaterAsync(PyFunction function, long delay) {
* @param function The function that should be called each time the synchronous task executes
* @param delay The delay, in ticks, to wait before beginning this synchronous repeating task
* @param interval The interval, in ticks, that the synchronous repeating task should be executed
* @param functionArgs Any arguments that should be passed to the function
* @return An ID representing the synchronous task that was scheduled
*/
public synchronized int scheduleRepeatingTask(PyFunction function, long delay, long interval) {
public synchronized int scheduleRepeatingTask(PyFunction function, long delay, long interval, Object... functionArgs) {
Script script = ScriptUtils.getScriptFromCallStack();
Task task = new RepeatingTask(script, function);
Task task = new RepeatingTask(script, function, functionArgs);
addTask(task);
task.runTaskTimer(PySpigot.get(), delay, interval);
return task.getTaskId();
Expand All @@ -125,11 +130,12 @@ public synchronized int scheduleRepeatingTask(PyFunction function, long delay, l
* @param function The function that should be called each time the asynchronous task executes
* @param delay The delay, in ticks, to wait before beginning this asynchronous repeating task
* @param interval The interval, in ticks, that the asynchronous repeating task should be executed
* @param functionArgs Any arguments that should be passed to the function
* @return An ID representing the asynchronous task that was scheduled
*/
public synchronized int scheduleAsyncRepeatingTask(PyFunction function, long delay, long interval) {
public synchronized int scheduleAsyncRepeatingTask(PyFunction function, long delay, long interval, Object... functionArgs) {
Script script = ScriptUtils.getScriptFromCallStack();
Task task = new RepeatingTask(script, function);
Task task = new RepeatingTask(script, function, functionArgs);
addTask(task);
task.runTaskTimerAsynchronously(PySpigot.get(), delay, interval);
return task.getTaskId();
Expand Down

0 comments on commit 2e94374

Please sign in to comment.