English 中文(简体)
当另一项任务产生时,完成一项推迟的任务
原标题:Cancelling a delayed task when another task is created
  • 时间:2012-04-14 02:31:47
  •  标签:
  • java
  • bukkit

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);
        }
    }
}
最佳回答

为了确保同一损害事件不会发生后继时间,你可以使用<条码>isDamaged变量,验证该代码是否应当运行。

既然你在确认活动之后建立了<条码>isDamaged至true,那么你只需在可能的情况下对这种价值进行核对,如果情况确实如此,就只能从停止另一项预定任务的整个方法中收回。

If you wish to stop the player damage as well then you can also cancel the entire event before returning to pass along to other plugins that no damage should be access during the cooldown period.

// 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);
        }
    }
}
问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签