I am programming a plugin that records the coordinates of players in minecraft throughout the game, in Java with Bukkit API.
I have used a 'repeating task' structure in my bukkit/java code below.
However, I need to 'cancel' this repeating task when a particular player logs off, as otherwise, if player 'A' logs in and logs off 50 times in a row, I will have 50 running tasks.
The idea is to stop the runnable of player 'A' if and only if player 'A' logs off (and not B).
Am I right in saying that the following code will cancel the repeating task for a particular player only when that particular player logs off?
I would be so grateful for a helping hand!
@EventHandler
public void onLogin(final PlayerJoinEvent event) {
final Player thePlayer = event.getPlayer();
this.stopRepeater = true;
final Location playerSpawnLocation = thePlayer.getLocation();
getLogger().info(String.valueOf(thePlayer.getName()) + " is logging in!");
getLogger().info("Welcome " + thePlayer.getName() + ". Your current position is: " + playerSpawnLocation);
int taskID = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, () -> {
if(this.stopRepeater) {
this.logToFile(thePlayer, thePlayer.getLocation());
}
}, 0L, 20L);}
@EventHandler
public void onQuit(final PlayerQuitEvent event) {
Player thePlayer = event.getPlayer();
if(!thePlayer.isOnline()) {
this.stopRepeater = false;
Bukkit.getScheduler().cancelTask(taskID);
getLogger().info(String.valueOf(event.getPlayer().getName()) + " has left the game");
}
}