丛林水塘
本帖最后由 丛林水塘 于 2021-12-8 16:28 编辑
java.lang.IllegalStateException: TimeSkipEvent cannot be triggered asynchronously from another thread.

以上是报错内容。下面是Spigot官方的解释说明

@NotNull
public BukkitTask runTaskTimerAsynchronously​(@NotNull
Plugin plugin,
long delay,
long period)
                                      throws IllegalArgumentException,
IllegalStateException
Asynchronous tasks should never access any API in Bukkit. Great care should be taken to assure the thread-safety of asynchronous tasks.
Schedules this to repeatedly run asynchronously until cancelled, starting after the specified number of server ticks.

Parameters:
plugin - the reference to the plugin scheduling task
delay - the ticks to wait before running the task for the first time
period - the ticks to wait between runs
Returns:
a BukkitTask that contains the id number
Throws:
IllegalArgumentException - if plugin is null
IllegalStateException - if this was already scheduled
See Also:
BukkitScheduler.runTaskTimerAsynchronously(Plugin, Runnable, long, long)

我已经看到了说异步任务不应该访问Bukkit中的任何API。
但我的算法确实必须要调用API并修改服务器内的一些数据。

并且算法包含了循环结构,非常耗时,需要使用异步处理。
所以应该如何修改以达到我的目的?好像使用多线程也不行,绕不开此错误。
谢谢!!!


skyinr
我感觉你是问了个XY问题
建议你把你要干什么说出来

丛林水塘
skyinr 发表于 2021-12-8 19:17
我感觉你是问了个XY问题
建议你把你要干什么说出来

getServer().getWorld("XXX").setFullTime(time);

1006438726
有点想玩一下

sky浩瀚
使用scheduler转同步再进行处理

  1. Bukkit.getScheduler().runTask(plugin, () -> {
  2.     // your code
  3. });
复制代码

丛林水塘
sky浩瀚 发表于 2021-12-8 19:45
使用scheduler转同步再进行处理

把主线程卡死了

丛林水塘
sky浩瀚 发表于 2021-12-8 19:45
使用scheduler转同步再进行处理

相当于就是同步,随后无限循环的结构将卡死主线程。

狡诈师
本帖最后由 狡诈师 于 2021-12-8 23:44 编辑

有个方法叫
  1. callSyncMethod
复制代码


类似 修改方块等 这些异步调用,建议使用这种