Attempting to Make a Countdown

Discussion in 'Plugin Development' started by TheHandfish, Apr 10, 2014.

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

    TheHandfish

    Hello again,

    I'm trying to make a Horse Race plugin. Once the players are all mounted on their horses and once enough players (defined by a config) are added, a countdown commences.

    Well, at least that's what I'm trying to get it to do. I did a ton of searching for different ways to count down (inevitably running into the apparently dreaded "Thread.sleep" method which freezes your server), and I was brought to this "runnable" thing.

    Unfortunately, I can't figure out how to get it to work. Here is a snippet from my code:

    Code:java
    1.  
    2. private int counter = 60;
    3.  
    4. public void startCounter(Player p, int count, boolean init)
    5. {
    6. p.sendMessage("Starting count down!");
    7. BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
    8. scheduler.scheduleSyncDelayedTask((Plugin) this, new Runnable() {
    9. @Override
    10. public void run()
    11. {
    12. counter =- 5;
    13. if(counter == 0)
    14. {
    15. scheduler.cancelAllTasks();
    16. p.sendMessage("Tiiiime's up!");
    17. }
    18. }
    19. }, 100L);
    20. }
    21.  


    I simply haven't a clue what's wrong. x_x I checked the Bukkit tutorials, but it won't work the way it's supposed to. It's not even giving me errors. Any tips on how to fix this or come up with a new method of counting? Thanks. Please keep in mind I'm a little new to Bukkit plugins. Thanks!
     
  2. Offline

    Wolfey

    What's exactly wrong?

    Also, maybe you should take a look at this tutorial. (Only one I could find, sorry):
     
    unforgiven5232 likes this.
  3. Offline

    CakePvP

    I'm guessing the error is here
    Code:java
    1. scheduler.scheduleSyncDelayedTask((Plugin) this, new Runnable() {

    Instead of using casting that class to a plugin, you need to get the instance of the class that extends JavaPlugin (your main class).
     
  4. This won't fix your error, I think CakePvP's will, but this is just 2 tips, one that is basically necessary.

    Firstly, you're calling cancelAllTasks(). You do realise this cancels all other plugin's tasks, and most plugins on BukkitDev that have multiple features have Schedulers. Either get the ID of the task and store it to a private variable, then cancel it using cancelTask(ID) or use cancelTasks(plugin). The plugin object is your plugin instance that extends JavaPlugin.

    Secondly, use runTaskLater and not scheduleSyncDelayedTask. A lot of outdated tutorials are saying to use scheduleSyncDelayedTask, but use runTaskLater (even the Bukkit Wiki has updated to it) as it is more performance efficient. (Tests done by DreTaX).
     
  5. Offline

    NonameSL

    I don't understand what the error is, but I do see some holes. This is the fixed code & explenation:
    Code:java
    1. public void startCounter(Player p, int countTo)
    2. {
    3. int taskId;
    4. p.sendMessage("Starting count down!");
    5. BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
    6. taskId = scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
    7. @Override
    8. public void run()
    9. {
    10. counter--;
    11. if(counter == 0)
    12. {
    13. scheduler.cancelTask(taskId);
    14. p.sendMessage("Tiiiime's up!");
    15. }
    16. }
    17. }, 20L);
    18. }

    Now, the stuff I corrected:
    1. int count and boolean init were unused as you had int counter, so I removed the three and replace int count with int countTo - which is the number of seconds the counter will work.
    2. You did (Plugin)this, which means the class you are working in does'nt extend JavaPlugin. Is it the main class? If it is, then you are doing it ALL wrong. extend the class with JavaPlugin.
    3. When you wrote counter=-5 all it did is turn the count into minus 5. counter-- removes one from the int.
    4. You dont want to cancel all tasks, just this one. What you do is you create an id for the task and then cancel it when needed.
    5. 100L is 5 seconds, 20L is 1 second.
    EDIT: 6. I used a repeating task so every second if, counter isn't 0, will remove 1 from the counter.
    7. Is the most important one of all - If you don't know how to program, then dont copy paste. Obviously you are new to java and not just bukkit plugins, and copy and pasting from tutorials isn't going to help you learn. Learn more java. If you don't follow this correction, the other 6 won't help...
     
  6. Offline

    TheHandfish

    Thanks, guys, I'll check these solutions out and let you know my findings.

    ^ EDIT:

    1. Okay, thanks.

    2. Yeah I actually have fixed that bit.

    3. I was using a specific C++-like code language before. I'm trying to get it to subtract 5 from "count" each time.

    4. How might I do that?

    5. I know. I don't want it counting every 1 second. It'd be really annoying in the chat.

    6. I do know how to program. Yes, I'm a little new to Java. Personally I don't really think that my knowledge of Java is the problem here. I mean, I accomplished so many other things in my plugin, stuff I didn't just pull out of some tutorial, LOL. :p Thanks for the concern though.
     
  7. Offline

    mazentheamazin

    TheHandfish
    This is an example of an efficient timer:
    Code:java
    1. int count = 60; //Time in seconds (Can reference config)
    2. Bukkit.broadcastMessage("Game starting in " + count + " seconds..");
    3. new BukkitRunnable() {
    4. @Override
    5. public void run() {
    6. if(count == 0) {
    7. //Start game method
    8. Bukkit.broadcastMessage("Teleporting players to arena");
    9. cancel(); //Cancels the timer
    10. }else{
    11. count--;
    12. Bukkit.broadcastMessage("Game starting in " + count + " seconds..");
    13. }
    14. }
    15. }.runTaskTimer(instanceOfPlugin, 20L /* The amount of time until the timer starts */, 20L /* The delay of each call */);
     
  8. Offline

    TheHandfish

    Okay, I used your code and messed around a bit with it.

    Now I've got a new problem. I can't reference non-static methods from a static context. Now before anyone jumps on me and tells me "learn Java before learning Bukkit" I just want to ask if there's a way I can work around this problem.

    The thing is:

    Code:
        public static void startRace(Player p)
    (Static)

    and

    Code:
    public void startCounter(Player p, int countTo)
    Non static. That's because startRace is a "public static void" whereas startCounter is just a "public void" right? I can't turn startCounter into a static void without getting a ton of errors.

    I switched to @mazentheamazin's (no clue how to tag, sorry ;-; ) idea and it's no longer giving me that error. However, I now see this:

    [​IMG]

    I'm a little confused as to what to put in the place of "instanceOfPlugin". That's a placeholder, right? Well I popped "plugin" in there, so I hope that fixed it.

    Then there's a new problem. It's really really simple.

    [​IMG]

    Yes, I know I probably got the syntax wrong there (it's supposed to subtract 5 from "count", this is how you do it in C++, so I just did that to see if it would work here [it was worth a shot]). But that doesn't seem to be the main problem. Should I put a "final" statement somewhere?
     
  9. Offline

    mazentheamazin

    TheHandfish
    I just realized where I was mistaken, you will have to make the variable count outside of the method, and make it static. So, it would look something like this:
    Code:java
    1. static int count = 60;
    2.  
    3. //Insert countdown method here

    Regarding your second problem, you would do count = (count - 5); Instead of count--
     
  10. Offline

    TheHandfish

    Actually I ended up doing just that. See? I'm learning. :D

    Thanks! I'll put it in. Another bug.

    I attempt to start my Bukkit server and I get:

    Show Spoiler

    Code:
    [14:48:28 INFO]: [HorseRace] Enabling HorseRace v1.0
    [14:48:28 ERROR]: Error occurred while enabling HorseRace v1.0 (Is it up to date?)
    java.lang.IllegalArgumentException: Plugin already initialized!
        at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:98) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:59) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at com.hand.Race.Join.<init>(Join.java:40) ~[?:?]
        at com.hand.Race.Main.onEnable(Main.java:30) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:250) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:350) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:389) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.craftbukkit.v1_7_R1.CraftServer.loadPlugin(CraftServer.java:439) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.craftbukkit.v1_7_R1.CraftServer.enablePlugins(CraftServer.java:375) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.m(MinecraftServer.java:342) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.g(MinecraftServer.java:319) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.a(MinecraftServer.java:275) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.DedicatedServer.init(DedicatedServer.java:175) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:424) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Caused by: java.lang.IllegalStateException: Initial initialization
        at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:101) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:59) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at com.hand.Race.Main.<init>(Main.java:23) ~[?:?]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[?:1.8.0]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[?:1.8.0]
        at java.lang.reflect.Constructor.newInstance(Constructor.java:408) ~[?:1.8.0]
        at java.lang.Class.newInstance(Class.java:433) ~[?:1.8.0]
        at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:52) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:133) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:313) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:236) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.craftbukkit.v1_7_R1.CraftServer.loadPlugins(CraftServer.java:350) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.craftbukkit.v1_7_R1.CraftServer.<init>(CraftServer.java:312) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.PlayerList.<init>(PlayerList.java:63) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.DedicatedPlayerList.<init>(SourceFile:14) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.DedicatedServer.init(DedicatedServer.java:126) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        ... 2 more
    
    Is there a way I can fix this, too?
     
  11. Offline

    mazentheamazin

    TheHandfish
    Are you by any chance creating a new instance of your main class anywhere in your plugin? i.e
    Code:java
    1. Main main = new Main();
     
    TheHandfish likes this.
  12. Offline

    TheHandfish

    I was extending "JavaPlugin" when I didn't need to. Removed that and it all worked great! Thanks, man! :D
     
  13. Offline

    mazentheamazin

    TheHandfish
    Alrighty then, no problem. Here to help ;)
     
    TheHandfish likes this.
Thread Status:
Not open for further replies.

Share This Page