Solved Calling cancel() method in BukkitRunnable

Discussion in 'Plugin Development' started by Smeary_Subset, Mar 30, 2022.

  1. Offline

    Smeary_Subset

    I stumbled upon an unexpected issue while coding a BukkitRunnable in my class called Blink. Inside the run() method within the runnable, I want to call a method within the Blink class called cancel(). However, when I try to do so, it instead calls the cancel() method for the BukkitRunnable. Other than renaming my Blink class's cancel() method, how can I make it so that method is called instead of the one within the BukkitRunnable class? Below is the a simplified version of the code.

    Code:
    public class Blink extends AbstractAbility {
        @Override
        protected void cancel() {
            // do something
        }
       
        private void foo() {
            new BukkitRunnable() {
                public void run() {
                    cancel(); // I want to call the overriden cancel method here, not the BukkitRunnable cancel
                }
            }.runTaskLater(CLI.plugin, 20);
        }
    }
    Additionally, I have another question. Why is it that the cancel() method within the BukkitRunnable can be called without an instance variable?
     
  2. Online

    timtower Administrator Administrator Moderator

    @Smeary_Subset
    Code:
    public class Blink extends AbstractAbility {
        @Override
        protected void cancel() {
            // do something
        }
     
        private void foo() {
    final Blick temp = this;
            new BukkitRunnable() {
                public void run() {
                    temp.cancel(); // I want to call the overriden cancel method here, not the BukkitRunnable cancel
                }
            }.runTaskLater(CLI.plugin, 20);
        }
    }
    Something like this should work, takes the top class, calls cancel on it.

    The BukkitRunnable is the instance
     
  3. Offline

    Smeary_Subset

    Awesome, thanks. I also realized another possible solution is to do Blink.this.cancel().

    Solved.
     

Share This Page