Cancel a repeating task from another class.

Discussion in 'Plugin Development' started by DatRefillDoe, Jul 26, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    DatRefillDoe

    Okay, so I have a repeating task in the Main class, and I want to cancel it in the other class.
    Code:
        @SuppressWarnings("deprecation")
        public void feast1(){
            int feast = Bukkit.getScheduler().scheduleAsyncRepeatingTask(this, new Runnable(){
                public void run(){
                    Bukkit.broadcastMessage(ChatColor.RED + "Feast is starting at -1494 37 2415 in 5 minutes.");
                    fourmin();
                    threemin();
                    twomin();
                    onemin();
                    tensec();
                    feaststart();
                }
            }, 60, 20 * 60 * 15);
        }
    That's my task. I want to cancel it in another class.
     
  2. Offline

    Niknea

    DatRefillDoe I believe you need to create a new BukkitRunnable, as repeating tasks cant be canceled, you can then get an instance of the runnable, and cancel it.
     
  3. Offline

    DatRefillDoe

    Niknea
    How do I get the instance?
     
  4. Offline

    Niknea

    DatRefillDoe when creating the runnable, save it to a variable, you can then get an instance of the class, which will allow access to the runnable variable.
     
  5. Offline

    DatRefillDoe

    Niknea I'm sorry, I have absolutley no idea what you are talking about. Save it to a variable? Didn't I already do that?
     
  6. Offline

    xTigerRebornx

    Niknea Repeating tasks can be canceled, the method that schedules it returns a task id which you can use to cancel it.
    DatRefillDoe Why are you using an asynch task?
     
  7. Offline

    DatRefillDoe

  8. Offline

    Niknea

    xTigerRebornx Never tried that, I'll try it in one of my next plugins

    DatRefillDoe oh my bad, didn't see that, what you can now do to cancel the task is:
    PHP:
    Bukkit.getServer().getScheduler().cancelTask(feast);
    DatRefillDoe regrading async or sync, see this thread.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  9. Offline

    DatRefillDoe

    I want to cancel it in a different class, doesn't work. And, ah, I should start using sync. :)
     
  10. Offline

    Niknea

    DatRefillDoe You'd need to get an instance of the class you created the feast variable it. Use the following code in the class you'd like to retrieve the feast variable:

    PHP:
    private static final ClassNameWithTheFeastVariable INSTANCE = new ClassNameWithTheFeastVariable();//class level definition
    //static factory method
    public static ClassNameWithTheFeastVariable getInstance()
    {
      return 
    INSTANCE ;//Always returns the same INSTANCE
     
    }  
    You can then get the variable feast using the following code:
    PHP:
    ClassNameWithTheFeastVariable.getInstance().feast;
    Which can be used to cancel the runnable with the following code:

    PHP:
    Bukkit.getServer().getScheduler().cancelTask(ClassNameWithTheFeastVariable.getInstance().feast);
     
  11. Offline

    DatRefillDoe

    Niknea Now that's only getting the void's,lists,etc. It wont get the int inside the public void feast1
     
  12. Offline

    Niknea

    DatRefillDoe Create the feast variable a field, and make it public.
     
  13. Offline

    DatRefillDoe

    Niknea I need it in the void, so whenever I call it, it'll do so on.
     
  14. Offline

    Niknea

    DatRefillDoe Make it a field. Put this code on the first line in your class:
    PHP:
    public int feast;
    Then change the first line in the method feast1 to:
    PHP:
    feast Bukkit.getScheduler().scheduleAsyncRepeatingTask(this, new Runnable(){
     
  15. Offline

    DatRefillDoe

    Niknea Sorry for aruging, but wouldn't that just get the int?
     
  16. Offline

    Niknea

    DatRefillDoe not if you call feast1 before canceling it ;). Else, yes it'll cause an NPE.
     
  17. Offline

    xTigerRebornx

    Niknea Won't cause an NPE, primitives cannot be null, it will use the default value of the primitive (ints have a default value of 0)
     
  18. Offline

    fireblast709

    Niknea suggesting static where constructors can be used... Really?
    DatRefillDoe read about constructors, they are really useful. Also, use BukkitRunnables with the runTask methods inside it to schedule the task, and use the BukkitTask to cancel the task.
    Code:Java
    1. private BukkitTask task;
    2.  
    3. public void foo()
    4. {
    5. this.task = new BukkitRunnable()
    6. {
    7. @Override
    8. public void run()
    9. {
    10. // TODO: logic
    11. }
    12. // cover these by yourself. And please, no statics ;)
    13. }.runTaskTimer(plugin, delay, interval);
    14. }
    15.  
    16. public void cancel()
    17. {
    18. if(this.task != null)
    19. {
    20. this.task.cancel();
    21. this.task = null;
    22. }
    23. }
     
  19. Offline

    Niknea

    fireblast709 Theres nothing wrong with one or two statics, and I personally find it easier than constructors.
     
  20. Offline

    fireblast709

    Niknea bad practice, bad design and a high chance of NPEs with new developers.
     
    xTigerRebornx likes this.
  21. Offline

    Niknea

    fireblast709 I've actually never figured out why static's are bad, mind explaining? Or send me a link to a explanation.
     
  22. Offline

    fireblast709

    Niknea
    First of all, people forget to clean up the static. A static instance has a reference to the class, and the class holds the static instance (since static variables are class variables, not instance variables - this also presents some small bugs with people using static as instance variables, thus overriding data when using two instances (, which creates a cyclic link that prevents the GC to free the memory.

    Secondly Java is an OO language, Object Orientated. Since the data used belongs to a single class (for example a UserManager class which managers Users), encapsulation 'requires' the data itself to be hidden, and use specified methods for the manipulation of the data. After all, you don't remove an element of an ArrayList by accessing the field directly and manipulate the array backing the ArrayList but you call .remove(Object) instead.
    Moreover strongly a strongly coupled structure will be next to impossible to test with mock objects, since you cannot test it without creating a full plugin instance.

    The NPE argument should need no extra explanation ;)
     
    xTigerRebornx and Niknea like this.
Thread Status:
Not open for further replies.

Share This Page