Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8158519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:37:36+00:00 2026-06-06T17:37:36+00:00

I currently have a delayed task that runs whenever I click a button. Clicking

  • 0

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);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T17:37:38+00:00Added an answer on June 6, 2026 at 5:37 pm

    In order to ensure that the event does not run subsequent times for the same damage event you can use the isDamaged variable to validate if the code should run.

    Since you set isDamaged to true after validating the event you simply need to make a check against this value as high up in the event as possible and if it is true then just return out of the entire method which would stop another scheduled task being created.

    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;
    }
    

    Here is your code modified to show an example use.

    @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);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently have a drop down menu that is activated on a hover (from display:none
I currently have a XSLT 2.0 Stylesheet that I am trying to remove empty
I currently have one project that currently contains multiple packages. These packages make up
I currently have a deployed app (fortworth.herokuapp.com) that I am attempting to sort movies
Currently I have the following code for a project that represents some probability trees
Currently I have a Rails 3 app that subscribes new users up to MailChimp.
Currently when I have a delayed method in my code like the following: CommentMailer.delay.deliver_comments(@comment,
I have a script for jQuery/Javascript tabs, that change every 2 seconds, and if
I currently have a small socket server that I'm trying to convert to a
I have a class currently called Promise that works as follows: It holds a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.