Like @Amongalen said your method playerListLoop()
is a blocking operation. See, you start a while
loop, but the loop in your code goes on forever as neither your on
variable gets changed, nor there is something like break;
. That means your plugin gets stuck in this method.
What you need to do is either create a new thread and have your loop run in that thread, or (and what I would recommend) you use the BukkitScheduler
prebuild into Spigot.
Sync Tasks
First we save the id so we will be able to cancel the task again:
int taskID;
Then we create our task:
- For that you need an instance of the class extending
JavaPlugin
.
- I use a lambda expression here, but you can also refer to a method.
- In this case, the 0 is the delay in in game ticks
- And the 100 is the time in ticks between every loop (1 second = 20 ticks)
taskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(YourClass.instance, () -> {
// update your API
}, 0, 100);
To cancel your task you do:
Bukkit.getScheduler().cancelTask(taskID);
Async Tasks
If you want your task to be async you need to do it a bit different.
This time we save our task using the BukkitTask
interface:
BukkitTask task;
Then it's quite similar to sync tasks:
task = Bukkit.getScheduler().runTaskTimerAsynchronously(YourClass.instance, () -> {
// update your API
}, 0, 100);
And to cancel the task you then do:
task.cancel();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…