2

我目前有一个延迟任务,只要我点击一个按钮就会运行。单击按钮将布尔值切换为true7 秒后,延迟任务将其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);
        }
    }
}
4

1 回答 1

1

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

由于您在验证事件后设置isDamagedtrue,您只需要在事件中尽可能高地检查此值,如果为真,则只需退出整个方法,这将停止创建另一个计划任务。

如果您也希望停止对玩家造成伤害,那么您也可以在返回之前取消整个事件以传递给其他在冷却期间不应受到伤害的插件。

// 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);
        }
    }
}
于 2012-07-01T17:44:06.790 回答