gpt4 book ai didi

java - 创建另一个任务时取消延迟的任务

转载 作者:行者123 更新时间:2023-11-30 11:45:51 24 4
gpt4 key购买 nike

我目前有一个延迟任务,每当我点击一个按钮时它就会运行。单击按钮将 boolean 值切换为 true,7 秒后,延迟任务再次将其设置为 false

但是,如果一个人多次单击该按钮, boolean 值将在打开和关闭之间切换,打开和关闭等等,因为延迟任务堆积如山。如果多次单击按钮而不是让所有延迟的任务相互堆积,是否有一种方法可以简单地延长延迟的任务?

假设延迟任务将等待 7 秒,然后在单击按钮后将 boolean 值从 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);
}
}
}

最佳答案

为了确保该事件不会针对同一损坏事件运行后续次,您可以使用 isDamaged 变量来验证代码是否应该运行。

由于您在验证事件后将 isDamaged 设置为 true,您只需在事件中尽可能高地检查此值,如果它是 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);
}
}
}

关于java - 创建另一个任务时取消延迟的任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10150539/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com