脚本调度器使用示例
使用Utils.getScheduler()获得调取器实例 Scheduler内方法
| 方法介绍 | 方法参数 | 返回值 |
|---|---|---|
| 取消任务ID | cancelTask(int id) | void |
| 取消所有任务 | cancelAllTasks() | void |
| 任务ID是否还在运行 | isCurrentlyRunning(int id) | boolean |
| 任务是否在队列中 | isQueued(int id) | 返回boolean |
| 获得待处理的任务 | getPendingTasks() | 返回task List |
| 获得正在执行中的任务 | getActiveWorkers() | 返回 worker list |
| 在下一tick运行任务 | runTask(JexlContext,jexlMethod,args...) | task |
| 在下一tick运行一个异步任务 | runTaskAsynchronously(JexlContext,jexlMethod,args...) | task |
| 在指定tick后运行任务 | runTaskLater(JexlContext,jexlMethod,tick,args...) | task |
| 在指定tick后运行异步任务 | runTaskLaterAsynchronously(JexlContext,jexlMethod,tick,args...) | task |
| 在指定tick后间隔tick运行任务 | runTaskTimer(JexlContext,jexlMethod,tick,tick,args...) | task |
| 在指定tick后间隔tick运行异步任务 | runTaskTimerAsynchronously(JexlContext,jexlMethod,tick,tick,args...) | task |
Task内方法
| 方法介绍 | 方法参数 | 返回值 |
|---|---|---|
| 获取任务id | getTaskId() | 返回int |
| 是否主线程 | isSync() | 返回boolean |
| 是否被取消了 | isCancelled() | 返回boolean |
| 取消任务 | cancel() | void方法 |
Worker内方法
| 方法介绍 | 方法参数 | 返回值 |
|---|---|---|
| 获取任务id | getTaskId() | int |
| 获得线程实例 | getThread() | java.lang.Thread |
下面是一个示例GUI,可以复制到萌芽GermPlugin/gui文件夹内打开测试
timeout:
options:
script:
openScript: |
var scheduler = Utils.getScheduler();
Log.chat("打开界面");
scheduler.cancelAllTasks();
closeScript: |
var timerTask = GuiScreen.getCacheMap().get("timerTask");
if(timerTask!=null && !timerTask.isCancelled()){
timerTask.cancel();
Log.chat("取消定时任务 "+timerTask.getTaskId());
}
Log.chat("关闭");
button:
type: button
defaultPath: textures/gui/slot.png
hoverPath: textures/gui/slot.png
w: 20
h: 20
clickScript: |
var haha = "哈哈哈哈";
var test = (text)=>{
Log.chat(text+" 当前线程 "+Utils.forClass("java.lang.Thread").currentThread().getName());
};
Utils.runTimeout(JexlContext, test, 1000, "延迟1000");
Utils.runTimeoutAsync(JexlContext, test, 1000, "异步延迟1000 ");
Utils.runTimeoutTick(JexlContext,test, 20,"tick延迟20 ");
/*也可以在方法内直接引用上面声明的变量 haha */
Utils.runTimeoutTick(JexlContext,()=>{
Log.chat("tick延迟10 "+haha);
}, 10);
/*这里将haha修改为 苦哈哈 但上面 runTimeoutTick 的输出还是 哈哈哈哈*/
haha = "苦哈哈";
var scheduler = Utils.getScheduler();
scheduler.runTaskLater(JexlContext, ()=>{
Log.chat("延迟任务20tick的任务执行结束 ");
}, 20);
var task = scheduler.runTaskLaterAsynchronously(JexlContext, ()=>{
Log.chat("异步延迟20tick任务 "+Utils.forClass("java.lang.Thread").currentThread().getName());
}, 20);
var text = "执行";
var part = GuiPart;
/*一个主线程的循环任务*/
var timerTask = scheduler.runTaskTimer(JexlContext,()=>{
Log.chat("主线程定时任务 "+text+" "+part.getRealName());
}, 20, 20);
GuiScreen.getCacheMap().put("timerTask", timerTask);