我目前有一个延迟的任务,每当我点击一个按钮就会运行。
单击该按钮可将布尔值切换为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);
}
}
}
答案 0 :(得分:1)
为了确保事件不会在相同的损坏事件后续运行,您可以使用isDamaged
变量来验证代码是否应该运行。
由于您在验证事件后将isDamaged
设置为true
,因此您只需要在事件中尽可能高地检查此值,如果它是真的,那么只需返回整个方法将停止创建另一个计划任务。
如果您希望停止玩家的伤害,那么您也可以取消整个事件,然后返回传递给其他插件,以便在冷却期间不会有任何损坏。
// End event, add setCancelled(true), and exit method if the isDamaged modifier is set to true
if (isDamaged) {
event.setCancelled(true);
return;
}
以下是您修改的代码以显示示例用法。
@EventHandler
public void damage (EntityDamageEvent event) {
// Get the victim of the damage event.
Entity victim = event.getEntity();
// End event, add setCancelled(true), and exit method if the isDamaged modifier is set to true
if (isDamaged) {
event.setCancelled(true);
return;
}
if (event instanceof EntityDamageByEntityEvent) {
// Get the attacker of the event.
EntityDamageByEntityEvent edbeEvent = (EntityDamageByEntityEvent) event;
Entity attacker = edbeEvent.getDamager();
// Continue only if both players are humans and not NPCS.
if(edbeEvent.getDamager() instanceof Player && victim instanceof Player) {
// Saving the players as the variable Player.
final Player player = (Player) victim;
final Player player2 = (Player) attacker;
// Add both players to the hashmap to be used by other classes.
Damagelist.add(player);
Damagelist.add(player2);
// Toggle to true.
isDamaged = true;
// Get delay time from the config.yml file.
int delay = plugin.getConfig().getInt("StunDuration") * 20;
// Setup delayed task to end cooldown set under StunDuration.
plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
// Define task to be run by scheduler.
public void run() {
// After amount of time set by StunDuration, remove players from hashmap.
Damagelist.remove(player);
Damagelist.remove(player2);
// Toggle back to false;
isDamaged = false;
playeradd = true;
}
}, delay);
}
}
}