我目前有一个延迟任务,只要我点击一个按钮就会运行。单击按钮将布尔值切换为true
7 秒后,延迟任务将其false
再次设置为。
然而,如果一个人多次点击按钮,布尔值会被打开和关闭,打开和关闭等等,因为延迟的任务正在堆积。如果多次单击按钮而不是让所有延迟的任务相互堆积,有没有办法简单地延长延迟的任务?
因此,假设单击按钮后,延迟任务将等待 7 秒,然后将布尔值从 切换true
为。false
我将如何更改代码,以便如果在延迟任务尚未执行时再次按下按钮,它将取消前一个并简单地执行下一个延迟任务(我将它用于服务器,所以简单地取消前一个任务可能并不意味着这是实际延迟的任务,而是其他一些任务)?
@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);
}
}
}