Solved TimerTask

Discussion in 'Plugin Development' started by Aragone, Jun 11, 2019.

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

    Aragone

    Hello,

    I have a problem with a timer in my Home plugin. I want to set a timer of 2.4 s when the player execute the command /home but I don't what to put in the task.runTaskTimer(??, 0, 48); I put 'this' but it doesn't run.

    Thanks !

    CommandHomeClass : https://pastebin.com/raw/F63cAyYu
    TimerClass : https://pastebin.com/raw/aw52K2h2
     
  2. Offline

    timtower Administrator Administrator Moderator

    @Aragone Put sql there.
    Needs to be your main class
     
  3. Offline

    drives_a_ford

    He's even injected the dependency but is just not using it.
    Instead of
    Code:
    task.runTaskTimer (this, 0, 48);
    just do
    Code:
    task.runTaskTimer (this.sql, 0, 48);
    .
     
  4. Offline

    timtower Administrator Administrator Moderator

    @drives_a_ford Third line of the onCommand is where he is using it.
     
  5. Offline

    Aragone

    It doesn't work. I have the error "The method runTaskTimer in the type BukkitRunnable is not applicable for the argument (SqlHomePlugin, int, int)" even if I put 'this.sql'
     
  6. Offline

    timtower Administrator Administrator Moderator

    Then what is your main class?
     
  7. Offline

    drives_a_ford

    I meant he's not using it where it's needed. But as we saw we'd made an incorrect assumption.

    I, as well as timtower, apparantly, made the assumption that the SqlHomePlugin class is your main class (the one that extends JavaPlugin).However that doesn't seem to be the case.
    As the method signature clearly says, you need an instance of Plugin there and your main class extends JavaPlugin, which in turn implements Plugin so that's what you need there.

    The reason we both thought that this was your main class is because it has "Plugin" in there. If it's not the actual plugin, I wouldn't suggest naming it a *Plugin.
     
  8. Offline

    Aragone

  9. Offline

    timtower Administrator Administrator Moderator

  10. Offline

    Aragone

    Yes I understood that but I'm searching the syntax. How put it in this argument.
     
  11. Offline

    timtower Administrator Administrator Moderator

    You need to pass it along using the constructors.
     
  12. Offline

    Aragone

    Sorry I don't understand. Can you write what are you thinking ?
     
  13. Offline

    timtower Administrator Administrator Moderator

    sql = new SqlHomePlugin(this, "jdbc:mysql://","localhost","homeplugin","root","");
     
  14. Offline

    Aragone

    But why dou you want to match the database with my plugin. I just want to start a timer when the player execute the /home command. That's just to wait after the ending of the animation of particles, so I start a timer of 2400ms.
     
  15. Offline

    timtower Administrator Administrator Moderator

    The timer needs an instance of the main class.
    You need to get that main class to the CommandHomeClass somehow. Constructors is the way to go here.
     
  16. Offline

    drives_a_ford

    Yes, but the example you gave was for the SQL handler and not the CommandHome :)

    In your Main you have (a few of these similar lines):
    Code:
    getCommand("sethome").setExecutor(new CommandHome(sql));
    Instead you need
    Code:
    getCommand("sethome").setExecutor(new CommandHome(this, sql));
    And thus in your CommandHome class you need to add the argument in the constructor and store a reference, i,e instead of
    Code:
    private SqlHomePlugin sql;
    // other stuff
    public CommandHome (SqlHomePlugin sql) {
    this.sql = sql;
    }
    
    you should do
    Code:
    private SqlHomePlugin sql;
    private final Main main; // the naming scheme is horrible, but I'll stick with this for now
    // While it doesn't necessarily need to be final, there's no downside and a bunch of benefits
    // other stuff
    
    public CommandHome (SqlHomePlugin sql, Main main) {
    this.sql = sql;
    this.main = main;
    }
    
    Now you can use the main class instance in the #runTask method.

    EDIT:
    On another note, you're creating three different instances of CommandHome for the three different commands:
    Code:
    getCommand("sethome").setExecutor(new CommandHome(sql));
    getCommand("home").setExecutor(new CommandHome(sql));
    getCommand("delhome").setExecutor(new CommandHome(sql));
    
    If they're different commands (which they are, they're even handled completely separately in CommandHome#onCommand), you'd generally want them in a different command handler. Failing that, at least create on instance and have them all refer to the same executor.

    Furthermore, your CommandHome uses static variables in a way that really isn't the intended use of the static keyword. I usually like to refer people to the Beginner Programming Mistakes and Why You're Making Them thread over at spigotmc.
     
    Last edited: Jun 11, 2019
  17. Offline

    Aragone

  18. Offline

    timtower Administrator Administrator Moderator

    Then check your <server directory>/logs/latest.log
    And limit the multiple instances of new CommandHome and new CommandSpawn to a single one by making a variable for them.
     
  19. Offline

    Aragone

    There is nothing in the logs too... What type of variable can I make?
     
  20. Offline

    timtower Administrator Administrator Moderator

    A CommandHome and a CommandSpawn one.
    And the server is crashing with a reason.
    Start it through the cmd window instead of double clicking the run.bat (or how yours may be called)
     
  21. Offline

    Aragone

  22. Offline

    timtower Administrator Administrator Moderator

    @Aragone Why are you using runTaskTimer but only running it once? And why are you using it anyways? It doesn't do anything as far as I can tell.
     
  23. Offline

    Aragone

    I'm using runTaskTimer because I want to teleporter the player after the animation of particles, so 2400ms after he executed '/home' command. Before I did the method Thread.sleep(2400) but, I don't know why, the server freeze during it for everyone. So I'm trying to do it by other way : a timer.
     
  24. Offline

    timtower Administrator Administrator Moderator

    @Aragone Yeah, but then you put the teleport code inside the timer, you don't wait for it because then the server will still freeze.
     
  25. Offline

    KarimAKL

    That's because you are making the main thread "sleep" for 2400 milliseconds.
     
  26. Offline

    Aragone

    @KarimAKL And do you know an other way than 'sleep' ? Because this method is much easier than to do a timer like I did.
     
  27. Offline

    KarimAKL

    @Aragone Use a BukkitRunnable.
    Code:Java
    1. new BukkitRunnable() {
    2. public void run() {
    3. // Code to run 48 ticks later here
    4. }
    5. }.runTaskLater(plugin, 48);
     
  28. Offline

    Aragone

    @KarimAKL I have to put this in my CommandHome class ?
     
  29. Offline

    KarimAKL

    @Aragone Wherever you want to make the delay.
     
  30. Offline

    Aragone

Thread Status:
Not open for further replies.

Share This Page