Waiting 1 Second

Discussion in 'Plugin Development' started by TehVoyager, Nov 2, 2013.

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

    TehVoyager

    Ok, I have come to a problem with my countdown. I use a while loop with a counter and at the end I need to wait 1 second. I have tried thread.sleep but then you can't do commands, eat food, ect. I have also tried using schedulers but I'm really bad with them, any help?
    Code:java
    1. int counter = 600;
    2. while(counter < 601){
    3. if(counter != 0){
    4. if(counter == 600){
    5. message.BroadCast(ChatColor.WHITE + "The Game Will Begin in 10 minutes");
    6. }
    7. else if(counter == 300){
    8. message.BroadCast(ChatColor.WHITE + "The Game Will Begin in 5 minutes");
    9. }
    10. else if(counter == 150){
    11. message.BroadCast(ChatColor.WHITE + "The Game Will Begin in 2 minutes, 30 seconds");
    12. }
    13. else if(counter == 60){
    14. message.BroadCast(ChatColor.WHITE + "The Game Will Begin in 1 minute");
    15. }
    16. else if(counter == 30){
    17. message.BroadCast(ChatColor.WHITE + "The Game Will Begin in 30 seconds");
    18. }
    19. else if(counter == 10){
    20. message.BroadCast(ChatColor.WHITE + "The Game Will Begin in 10 seconds");
    21. }
    22. else if(counter <= 5 && counter != 0){
    23. message.BroadCast(ChatColor.WHITE + "The Game Will Begin in " + counter + " seconds");
    24. }
    25. }else{
    26. Bukkit.broadcastMessage("The Game Has Begun!");
    27. counter = 602;
    28. }
    29. counter = counter - 1;
    30. //Wait 1 second.
    31. }



    Edit: Any simpler or better way to do this?

    Anybody?, Any help is appriciated

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

    NoChanceSD

    TehVoyager This is an example of a scheduler which will run 1 in 1 second, it works in ticks and 20 ticks equals 1 second and that's why i placed a 20 there. The 0 means it will start right away. You have to replace plugin for an instance of your plugin.
    Code:java
    1. plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    2. public void run() {
    3.  
    4. }
    5. }, 0, 20);

    Hope that helps :)
     
  3. Offline

    TehVoyager

    NoChanceSD Through an error and didn't work, I defined my plugin as public static Hunt(TheClassName) plugin;

    Thanks for the reply though
     
  4. Offline

    Aengo

    It should be like MainClassName plugin;
    And then you initialize it within the classes constructor.
     
  5. Offline

    TehVoyager

    Aengo
    It is the main class name, what do you mean class constructor?
     
  6. Offline

    Tss1410

    public <mainclass> plugin;
    public <classname>(<mainclass> pl){
    plugin=pl;
    }
     
  7. Offline

    Aengo

    Yes, the mainclass is the class that extends JavaPlugin.
    The constructor is something you put inside the class your using to be able to initialize plugin with ease, and it's needed in a few cases such as listeners.
    That is a constructor^
     
  8. Offline

    TehVoyager

    Aengo Still doesnt work and throws an error. Is there and easier way to do this?
     
  9. Offline

    NoChanceSD

    TehVoyager Post your whole class to see what's wrong please.
     
  10. Offline

    TehVoyager

    Class:
    Code:java
    1. package mcadventure.hunt;
    2.  
    3. import mcadventure.hunt.HuntArena.GameState;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class Hunt extends JavaPlugin{
    10.  
    11. HuntMessenger message = new HuntMessenger();
    12. public static Hunt plugin;
    13. public Hunt(Hunt pl){
    14. plugin=pl;
    15. }
    16.  
    17. public void onDisable(){
    18.  
    19. }
    20.  
    21. public void onEnable(){
    22. new ArenaManager(this);
    23. ArenaManager.getManager().getArena().setGameState(GameState.Lobby);
    24. Bukkit.getServer().getPluginManager().registerEvents(new PlayerListener(), this);
    25. int counter = 600;
    26. while(counter < 601){
    27. if(counter != 0){
    28. if(counter == 600){
    29. message.BroadCast(ChatColor.WHITE + "The Game Will Begin in 10 minutes");
    30. }
    31. else if(counter == 300){
    32. message.BroadCast(ChatColor.WHITE + "The Game Will Begin in 5 minutes");
    33. }
    34. else if(counter == 150){
    35. message.BroadCast(ChatColor.WHITE + "The Game Will Begin in 2 minutes, 30 seconds");
    36. }
    37. else if(counter == 60){
    38. message.BroadCast(ChatColor.WHITE + "The Game Will Begin in 1 minute");
    39. }
    40. else if(counter == 30){
    41. message.BroadCast(ChatColor.WHITE + "The Game Will Begin in 30 seconds");
    42. }
    43. else if(counter == 10){
    44. message.BroadCast(ChatColor.WHITE + "The Game Will Begin in 10 seconds");
    45. }
    46. else if(counter <= 5 && counter != 0){
    47. message.BroadCast(ChatColor.WHITE + "The Game Will Begin in " + counter + " seconds");
    48. }
    49. }else{
    50. Bukkit.broadcastMessage("The Game Has Begun!");
    51. ArenaManager.getManager().getArena().startGame();
    52. ArenaManager.getManager().getArena().setPvP(true);
    53. counter = 602;
    54. }
    55. //Wait 1 Second.
    56. counter = counter - 1;
    57. }
    58. }
    59. }


    NoChanceSD Aengo ^

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

    Aengo

    Oh. Since it's all in the main class anyway, you don't need any of this constructor stuff, instead of 'plugin' just use 'this'. Example:
    Code:java
    1. plugin.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    2. public void run() {
    3.  
    4. }
    5. }, 0, 20);

    And then remove all of the constructor things
     
  12. Offline

    TehVoyager

    Aengo
    Error:
    Code:
    C:\Users\Jay\Desktop\Bukkit Server 1>java -Xmx1024M -jar craftbukkit.jar -o true
     
    20:32:58 [INFO] Starting minecraft server version 1.6.4
    20:32:58 [INFO] Loading properties
    20:32:58 [INFO] Default game type: SURVIVAL
    20:32:58 [INFO] Generating keypair
    20:32:59 [INFO] Starting Minecraft server on *:25565
    20:32:59 [INFO] This server is running CraftBukkit version git-Bukkit-1.6.2-R1.0
    -3-g9532cb6-b2889jnks (MC: 1.6.4) (Implementing API version 1.6.4-R0.1-SNAPSHOT)
     
    20:33:00 [INFO] [Hunt] Loading Hunt v1
    20:33:00 [INFO] [Essentials] Loading Essentials v2.12.1
    20:33:00 [INFO] [EssentialsChat] Loading EssentialsChat v2.12.1
    20:33:00 [INFO] Preparing level "world"
    20:33:00 [WARNING] ----- Bukkit Auto Updater -----
    20:33:00 [WARNING] Your version of CraftBukkit is out of date. Version 1.6.4-R2.
    0 (build #2918) was released on Wed Oct 30 23:39:40 EDT 2013.
    20:33:00 [WARNING] Details: /downloads/craftbukkit/view/02389_1.6.4-R2.0/
    20:33:00 [WARNING] Download: /downloads/craftbukkit/get/02389_1.6.4-R2.0/craftbu
    kkit.jar
    20:33:00 [WARNING] ----- ------------------- -----
    20:33:00 [INFO] Preparing start region for level 0 (Seed: 5733173794271605996)
    20:33:01 [INFO] Preparing spawn area: 58%
    20:33:02 [INFO] Preparing start region for level 1 (Seed: 5733173794271605996)
    20:33:02 [INFO] Preparing start region for level 2 (Seed: 5733173794271605996)
    20:33:03 [INFO] [Hunt] Enabling Hunt v1
    20:33:03 [SEVERE] Error occurred while enabling Hunt v1 (Is it up to date?)
    java.lang.NullPointerException
            at mcadventure.hunt.Hunt.onEnable(Hunt.java:52)
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
    .java:457)
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
    r.java:381)
            at org.bukkit.craftbukkit.v1_6_R3.CraftServer.loadPlugin(CraftServer.jav
    a:282)
            at org.bukkit.craftbukkit.v1_6_R3.CraftServer.enablePlugins(CraftServer.
    java:264)
            at net.minecraft.server.v1_6_R3.MinecraftServer.l(MinecraftServer.java:3
    15)
            at net.minecraft.server.v1_6_R3.MinecraftServer.f(MinecraftServer.java:2
    92)
            at net.minecraft.server.v1_6_R3.MinecraftServer.a(MinecraftServer.java:2
    52)
            at net.minecraft.server.v1_6_R3.DedicatedServer.init(DedicatedServer.jav
    a:152)
            at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java
    :393)
            at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:5
    83)
    20:33:03 [INFO] [Essentials] Enabling Essentials v2.12.1
    20:33:03 [INFO] Essentials: Using config file enhanced permissions.
    20:33:03 [INFO] Permissions listed in as player-commands will be given to all us
    ers.
    20:33:03 [INFO] [EssentialsChat] Enabling EssentialsChat v2.12.1
    20:33:03 [INFO] Server permissions file permissions.yml is empty, ignoring it
    20:33:03 [INFO] Done (3.316s)! For help, type "help" or "?"
    20:33:14 [INFO] nickg2001[/127.0.0.1:54597] logged in with entity id 187 at ([wo
    rld] -197.8435910750006, 63.0, 250.66259611699775)
    20:33:14 [INFO] Lobby
    20:33:14 [INFO] [Hunt] nickg2001 Has joined the Hunt.
    20:33:15 [INFO] nickg2001 issued server command: /ip
    20:33:17 [INFO] nickg2001 issued server command: /reload
    20:33:17 [INFO] [EssentialsChat] Disabling EssentialsChat v2.12.1
    20:33:17 [INFO] [Essentials] Disabling Essentials v2.12.1
    20:33:17 [INFO] [Hunt] Disabling Hunt v1
    20:33:17 [INFO] [Hunt] Loading Hunt v1
    20:33:17 [INFO] [Essentials] Loading Essentials v2.12.1
    20:33:17 [INFO] [EssentialsChat] Loading EssentialsChat v2.12.1
    20:33:17 [INFO] [Hunt] Enabling Hunt v1
    20:33:17 [INFO] [Hunt] The Game Will Begin in 10 minutes
    20:33:17 [SEVERE] Error occurred while enabling Hunt v1 (Is it up to date?)
    java.lang.NullPointerException
            at mcadventure.hunt.Hunt.onEnable(Hunt.java:52)
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
    .java:457)
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
    r.java:381)
            at org.bukkit.craftbukkit.v1_6_R3.CraftServer.loadPlugin(CraftServer.jav
    a:282)
            at org.bukkit.craftbukkit.v1_6_R3.CraftServer.enablePlugins(CraftServer.
    java:264)
            at org.bukkit.craftbukkit.v1_6_R3.CraftServer.reload(CraftServer.java:60
    9)
            at org.bukkit.Bukkit.reload(Bukkit.java:277)
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:
    23)
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:19
    2)
            at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchCommand(CraftServe
    r.java:523)
            at net.minecraft.server.v1_6_R3.PlayerConnection.handleCommand(PlayerCon
    nection.java:959)
            at net.minecraft.server.v1_6_R3.PlayerConnection.chat(PlayerConnection.j
    ava:877)
            at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java
    :834)
            at net.minecraft.server.v1_6_R3.Packet3Chat.handle(SourceFile:49)
            at net.minecraft.server.v1_6_R3.NetworkManager.b(NetworkManager.java:296
    )
            at net.minecraft.server.v1_6_R3.PlayerConnection.e(PlayerConnection.java
    :116)
            at net.minecraft.server.v1_6_R3.ServerConnection.b(SourceFile:37)
            at net.minecraft.server.v1_6_R3.DedicatedServerConnection.b(SourceFile:3
    0)
            at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:5
    92)
            at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:2
    27)
            at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:4
    88)
            at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java
    :421)
            at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:5
    83)
    20:33:17 [INFO] [Essentials] Enabling Essentials v2.12.1
    20:33:18 [INFO] Essentials: Using config file enhanced permissions.
    20:33:18 [INFO] Permissions listed in as player-commands will be given to all us
    ers.
    20:33:18 [INFO] [EssentialsChat] Enabling EssentialsChat v2.12.1
    20:33:18 [INFO] Server permissions file permissions.yml is empty, ignoring it
    20:33:18 [INFO] nickg2001: Reload complete.
    20:33:37 [INFO] nickg2001 issued server command: /reload
    20:33:38 [INFO] [EssentialsChat] Disabling EssentialsChat v2.12.1
    20:33:38 [INFO] [Essentials] Disabling Essentials v2.12.1
    20:33:38 [INFO] [Hunt] Disabling Hunt v1
    20:33:38 [INFO] [Hunt] Loading Hunt v1
    20:33:38 [INFO] [Essentials] Loading Essentials v2.12.1
    20:33:38 [INFO] [EssentialsChat] Loading EssentialsChat v2.12.1
    20:33:38 [INFO] [Hunt] Enabling Hunt v1
    20:33:38 [INFO] [Hunt] The Game Will Begin in 10 minutes
    20:33:38 [SEVERE] Error occurred while enabling Hunt v1 (Is it up to date?)
    java.lang.NullPointerException
            at mcadventure.hunt.Hunt.onEnable(Hunt.java:52)
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
    .java:457)
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
    r.java:381)
            at org.bukkit.craftbukkit.v1_6_R3.CraftServer.loadPlugin(CraftServer.jav
    a:282)
            at org.bukkit.craftbukkit.v1_6_R3.CraftServer.enablePlugins(CraftServer.
    java:264)
            at org.bukkit.craftbukkit.v1_6_R3.CraftServer.reload(CraftServer.java:60
    9)
            at org.bukkit.Bukkit.reload(Bukkit.java:277)
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:
    23)
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:19
    2)
            at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchCommand(CraftServe
    r.java:523)
            at net.minecraft.server.v1_6_R3.PlayerConnection.handleCommand(PlayerCon
    nection.java:959)
            at net.minecraft.server.v1_6_R3.PlayerConnection.chat(PlayerConnection.j
    ava:877)
            at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java
    :834)
            at net.minecraft.server.v1_6_R3.Packet3Chat.handle(SourceFile:49)
            at net.minecraft.server.v1_6_R3.NetworkManager.b(NetworkManager.java:296
    )
            at net.minecraft.server.v1_6_R3.PlayerConnection.e(PlayerConnection.java
    :116)
            at net.minecraft.server.v1_6_R3.ServerConnection.b(SourceFile:37)
            at net.minecraft.server.v1_6_R3.DedicatedServerConnection.b(SourceFile:3
    0)
            at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:5
    92)
            at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:2
    27)
            at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:4
    88)
            at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java
    :421)
            at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:5
    83)
    20:33:38 [INFO] [Essentials] Enabling Essentials v2.12.1
    20:33:38 [INFO] Essentials: Using config file enhanced permissions.
    20:33:38 [INFO] Permissions listed in as player-commands will be given to all us
    ers.
    20:33:38 [INFO] [EssentialsChat] Enabling EssentialsChat v2.12.1
    20:33:38 [INFO] Server permissions file permissions.yml is empty, ignoring it
    20:33:38 [INFO] nickg2001: Reload complete.
    20:33:43 [INFO] <nickg2001> c
    20:33:47 [INFO] <nickg2001> c
    >
    Main Class:
    Code:java
    1. package mcadventure.hunt;
    2.  
    3. import mcadventure.hunt.HuntArena.GameState;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class Hunt extends JavaPlugin{
    10.  
    11. HuntMessenger message = new HuntMessenger();
    12. public static Hunt plugin;
    13.  
    14. public void onDisable(){
    15.  
    16. }
    17.  
    18. public void onEnable(){
    19. new ArenaManager(this);
    20. ArenaManager.getManager().getArena().setGameState(GameState.Lobby);
    21. Bukkit.getServer().getPluginManager().registerEvents(new PlayerListener(), this);
    22. int counter = 600;
    23. while(counter < 601){
    24. if(counter != 0){
    25. if(counter == 600){
    26. message.BroadCast(ChatColor.WHITE + "The Game Will Begin in 10 minutes");
    27. }
    28. else if(counter == 300){
    29. message.BroadCast(ChatColor.WHITE + "The Game Will Begin in 5 minutes");
    30. }
    31. else if(counter == 150){
    32. message.BroadCast(ChatColor.WHITE + "The Game Will Begin in 2 minutes, 30 seconds");
    33. }
    34. else if(counter == 60){
    35. message.BroadCast(ChatColor.WHITE + "The Game Will Begin in 1 minute");
    36. }
    37. else if(counter == 30){
    38. message.BroadCast(ChatColor.WHITE + "The Game Will Begin in 30 seconds");
    39. }
    40. else if(counter == 10){
    41. message.BroadCast(ChatColor.WHITE + "The Game Will Begin in 10 seconds");
    42. }
    43. else if(counter <= 5 && counter != 0){
    44. message.BroadCast(ChatColor.WHITE + "The Game Will Begin in " + counter + " seconds");
    45. }
    46. }else{
    47. Bukkit.broadcastMessage("The Game Has Begun!");
    48. ArenaManager.getManager().getArena().startGame();
    49. ArenaManager.getManager().getArena().setPvP(true);
    50. counter = 602;
    51. }
    52. plugin.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    53. public void run() {
    54.  
    55. }
    56. }, 0, 20);
     
  13. Offline

    Aengo

    TehVoyager this code is so massively wrong. Don't the red lines within eclipse indicate to you that something isn't working?! I'm not on my computer with my IDE at the moment so I am unable to fix this. Sorry :(
     
  14. Offline

    TehVoyager

    Apperently The Way I do it will never work I guess, How do other minigame plugins do it?

    Aengo
    There are no red lines

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

    Phinary

    Completely remove the variable "plugin". Since you are trying to do it in your main class, you do not need a pointer the main class.
    Change:
    Code:
    plugin.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    to:
    Code:
    getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    Since your class extends JavaPlugin, you are inheriting the method called "getServer()"
     
  16. Offline

    TehVoyager

    Phinary No Errors but it doesn't delay for a second(20 ticks) code:
    Code:java
    1. int counter = 600;
    2. while(counter < 601){
    3. if(counter != 0){
    4. if(counter == 600){
    5. message.BroadCast(ChatColor.WHITE + "The Game Will Begin in 10 minutes");
    6. }
    7. else if(counter == 300){
    8. message.BroadCast(ChatColor.WHITE + "The Game Will Begin in 5 minutes");
    9. }
    10. else if(counter == 150){
    11. message.BroadCast(ChatColor.WHITE + "The Game Will Begin in 2 minutes, 30 seconds");
    12. }
    13. else if(counter == 60){
    14. message.BroadCast(ChatColor.WHITE + "The Game Will Begin in 1 minute");
    15. }
    16. else if(counter == 30){
    17. message.BroadCast(ChatColor.WHITE + "The Game Will Begin in 30 seconds");
    18. }
    19. else if(counter == 10){
    20. message.BroadCast(ChatColor.WHITE + "The Game Will Begin in 10 seconds");
    21. }
    22. else if(counter <= 5 && counter != 0){
    23. message.BroadCast(ChatColor.WHITE + "The Game Will Begin in " + counter + " seconds");
    24. }
    25. }else{
    26. Bukkit.broadcastMessage("The Game Has Begun!");
    27. ArenaManager.getManager().getArena().startGame();
    28. ArenaManager.getManager().getArena().setPvP(true);
    29. counter = 602;
    30. }
    31. getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    32. public void run() {
    33.  
    34. }
    35. }, 0, 20L);
    36. counter = counter - 1;
    37. }
     
  17. Offline

    Phinary

    That is because you are doing almost everything wrong. The way you are doing it now, if it did work the entire server would lock up until the countdown finished. There is much better ways to do this, but for simplicity sake I would recommend you replace your while loop with the repeating task you are creating, then inside that task you can do your counter and then stop the task when the counter is finished. Anyways though, this seems to be way beyond your experience with java at this point so I recommend you read this and learn a bit about the what a Runnable is and how to use it. If you want, you can also try to check out the source code of some other plugins that use timers in their plugin to try to work it out, although please don't just copy and paste code. If you don't understand the code you are putting in your plugin, you probably shouldn't be using it.
     
  18. Offline

    TheKomputerKing

    I would reccomend learning Java first.
     
    swampshark19 likes this.
Thread Status:
Not open for further replies.

Share This Page