I currently have a delayed task that runs whenever I click a button.
Clicking the button toggles a boolean to true
and after 7 seconds, the delayed task sets it to false
again.
However, what happens is that if a person clicks the button multiple times, the boolean is toggled on and off, on and off, and so on since the delayed tasks are piling up. Is there a way to simply prolong the delayed task if the button is clicked multiple times instead of having all the delayed task pile up on each other?
So let s say that the delayed task would wait 7 seconds before switching the boolean from true
to false
once the button is clicked.
How would I change the code so that if the button is pressed again while the delayed task has not executed, it cancels the previous and simply executes the next delayed task (I am using this for a server so simply cancelling the previous task may not mean that it was the actual delayed task but some other task)?
@EventHandler
public void damage (EntityDamageEvent event) {
Entity victim = event.getEntity();
if (event instanceof EntityDamageByEntityEvent) {
EntityDamageByEntityEvent edbeEvent = (EntityDamageByEntityEvent) event;
if(edbeEvent.getDamager() instanceof Player && victim instanceof Player) {
EntityDamageByEntityEvent edbeEvent1 = (EntityDamageByEntityEvent) event;
Entity attacker = edbeEvent1.getDamager();
//Checks if both players are humans and not NPCS.
final Player player = (Player) victim;
final Player player2 = (Player) attacker;
//Saving the players as the variable Player.
Damagelist.add(player);
Damagelist.add(player2);
//Adds both players to the hashmap to be used by other classes.
isDamaged = true;
//Toggles the boolean to true.
int delay = plugin.getConfig().getInt("StunDuration") * 20;
//The above line gets the delay time from the config.yml file.
plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
public void run() {
Damagelist.remove(player);
Damagelist.remove(player2);
//After an x amount of time, removes players from hashmap.
isDamaged = false;
playeradd = true;
//Toggles the booleans around.
}
}, delay);
}
}
}