Schedulers Confuse Me

Discussion in 'Plugin Development' started by AwesomelyToad, Dec 21, 2018.

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

    AwesomelyToad

    I have been trying to change my TimerTask to the bukkit synced scheduler (so that I can drop an item from within the scheduled task) but for some reason I cannot wrap my head around it.
    I am very much a noob so please know I am aware of how basic some of these questions are but I would greatly appreciate help with them :)

    The timertask I want to recreate is :
    Show Spoiler

    Sorry for any formatting errors. Promise it is nice and neat in my actual file :p
    Code:
    Timer timer = new Timer();
    TimerTask tt = new TimerTask() {
               int count = 0;
                 @Override
                 public void run() {
                   count ++;
                   [code stuff]
                         if ([check]){
                             [more code stuff]
                             
                             also this is where I would like to add "DropItemNaturally"
                             if ([check2]) {
                                  count = 5000;
                             }
                         }
                   
                        if(count >= 5000) {
                           [more code]
                           this.cancel();
                        }
                 }
                   
               };
             timer.schedule(tt, 1,1);
    


    Basically I just need some help understanding how to create the Bukkit scheduled task. Honestly I have written and rewritten it so many times at this point I dont even care if I understand it as long as it works....

    Things that are tripping me up:
    1. Do I create a class ("public class ExampleSelfCancelingTask extends BukkitRunnable {...}"), or just "new BukkitRunnable(){...}"
    Also, how? (Cant understand creating vs scheduling the task)
    2. I have seen lots of stuff about "public static JavaPlugin plugin" and "this.plugin=plugin", which is then used later in the scheduling of the task. My IDE keeps telling me "this.plugin=plugin" does nothing, and that "plugin" is never used.

    Mostly the Bukkit wiki page and other pages I have seen on this have such different formats than my plugin, and my knowledge of bukkit and java is limited enough that I cant figure out how to adapt it to my code.

    If it helps, my class is defined as "public class Main extends JavaPlugin implements Listener{...}"
    My onEnable looks like
    Code:
      @Override
       public void onEnable(){
        getServer().getPluginManager().registerEvents(this, this);
         getLogger().info("[plugin] plugin has been enabled.");
       }
    
    (I include that because both of those seem to be used in defining the plugin variable)

    I am sorry for not having a concise question. Really I just cant get this to work and would love somebody patient to help talk me through it.

    And a question for any staff who see this: I got a message for doubleposting. Sorry about that. I read the rules and it seems we are not allowed to post twice in a thread within 24 hours, and should instead edit our original post. However, we are also not allowed to exchange contact information. Are we allowed to ask somebody to PM us on here, so that we can have conversations without clogging threads? And in PMs do that same rules apply about sharing outside contact info?
     
  2. Offline

    Zombie_Striker

    Both have their advantages and disadvantages. If you just need to do something once in one location, not needing to copy and paste the code, then you can get away with doing new BukkitRunnable{}. Having a class for that is better for situations where you need the same type of task to be called in multiple scenarios or when you need to have access to the task if you need to modify it while the task is running/ waiting to run (e.g. having a task that repeats every 5 seconds to show a progress bar, and you need a method to tell the task how much is completed or whether the task is complete)
    I'm not entirely sure what you mean by this. For both situations, you need to create and "schedule" the task. You create the task so the server knows what to do when it needs to be called, and you schedule it so bukkit knows when the task should happen.
    You really don't need that. First, you should almost never need the static modifier when making plugins, as it can create memory leaks and normally shows that you did not structure your plugin. Also, unless the scheduler needs to be in a separate class from the one that extends Java plugin, you should be able to access the main class using "this".
    BTW: You don't need this. Bukkit logs your plugins for you. You can remove this line.
    Not staff, but:
    1. No. All thread related posts should stay on the main thread. All non-thread related posts should get their own threads.
    2. You really should not be in any situation where anyone needs to solicit any external information. If you have a conversation on the bukkit forums, it should stay on the bukkit forums.
    3. All rules that apply to the public messages should apply to PMs as well.
     
  3. Offline

    AwesomelyToad

    @Zombie_Striker Thank you for your reply. I found a method that seems promising but I am running into a problem.

    Inside new Runnable() {...} I have
    Code:
    public int taskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(this.instance, this, 1L, 1L);
    The format for scheduleSyncRepeatingTask is "scheduleSyncRepeatingTask(Plugin plugin, Runnable task, long delay, long period)"
    The code I found used "scheduleSyncRepeatingTask(instance, this, 0, 0)"
    Trying to define an instance as per this post broke the plugin. What is required here? I currently have "this" in the Runnable arg1 slot with no visible error. "this.plugin" doesn't work (

    Hopefully after figuring out that plugin slot it will work. You say "...you should be able to access the main class using "this"." I'm still not certain what the code you say is unnecessary was even used for. In this case the "this" keyword seems to be providing the enclosing runnable. "(Plugin) Classname" doesn't work.

    Thank you again for your answers :)
    Also you are right about not needing to solicit outside information. I think I just need to get used to posting conversations publicly when in general I prefer to speak privately. It's just a different setting than I'm used to so it threw me.
     
  4. Offline

    AwesomelyToad

    Would still love some help with this :) <3
     
  5. Offline

    Zombie_Striker

    From that post, what you want to do is copy the #2 codeblock to add the constructor so that you get the plugin's instance. The reason you need the main plugin's instance there is in case the server shuts down or if that plugin is disabled. Once it is disabled, the runnable checks to make sure the plugin is still running before calling the runnable.
     
  6. Offline

    AwesomelyToad

    What I don't understand is that I currently have 1 class (Main). So in order to do the 2nd method, should I move my whole eventhandler setup into a new inner class, or should i declare the instance in an inner class and then access that from the main class? Unfortunately I can't figure out how to do either. Sorry if I'm being dense! I really appreciate you taking a look.

    (When I put "public class Class1 {etc}", and then try to use "Class1.instance" it suggests changing instance to static but otherwise won't work. As for creating an event handler inside the inner class, I don't know what to attempt past just pasting all the current code into the class which evidently doesn't work)

    Merry Christmas if you celebrate btw!
     
  7. Offline

    Zombie_Striker

    @AwesomelyToad
    You shouldn't need an inner class for this. Can you post the code you are using?
     
  8. Offline

    AwesomelyToad

    @Zombie_Striker
    I interpreted the code you pointed me to (second code block in this post) as a class "Class1" being created within the main class "Main." I simply used the code you pointed me to but I could be misunderstanding it. If Main is referring to something else I am not sure how to use it... My main class is called Main so that name is taken to refer to that. (Tired as of writing this sorry if I worded it poorly or misunderstood)
     
Thread Status:
Not open for further replies.

Share This Page