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
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); .
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'
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.
@timtower this in my main class : https://pastebin.com/raw/qUiiZtiU Yes sorry I have forgotten to precise my MainClass
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.
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.
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.
MainClass : https://pastebin.com/raw/9cxa86zT CommandHomeClass : https://pastebin.com/raw/6n7Sx8VT TpTimerClass : https://pastebin.com/raw/PVCtaMkX Ok, so now, I don't know why, when I try to do '/home' command, the animation of portal particle (just before) the timer is running, and nothing happened. And just after, the server is crashing... (I don't have any error in the console because the console is crashing too)
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.
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)
@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.
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.
@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.
@KarimAKL And do you know an other way than 'sleep' ? Because this method is much easier than to do a timer like I did.
@Aragone Use a BukkitRunnable. Code:Java new BukkitRunnable() { public void run() { // Code to run 48 ticks later here }}.runTaskLater(plugin, 48);