Thread.sleep() Problems [Solved]

Discussion in 'Spigot Plugin Development' started by MrMaster_, Jun 29, 2015.

  1. Hi! I need to be able to use Thread.sleep in order to make a 10 second delay for my game, but the problem is, it stops everything for 10 seconds. It basically freezes everything in game and resumes after the 10 seconds. So what i need is to be able to make MY program sleep, but not the server.. How would i go about doing this? :)

    Also, im working inside of a public static void being accessed by the main java script.

    Code (Text):
    public static void startGame(Player player) { // Where the magic happends //
           
            for(Player msg : Bukkit.getServer().getOnlinePlayers()) {
                if(Snowba_Main.plrsInQueue.contains(msg.getName())) {
                    msg.sendMessage(ChatColor.BLUE + "[Snowba] You have 10 seconds until battle!");
                }
            }
           
            // Game Started //
           
            try { // Timer //
                Thread.sleep(10000);
               
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
           
            for(Player givesnob : Bukkit.getServer().getOnlinePlayers()) {
                if(Snowba_Main.plrsInQueue.contains(givesnob.getName())) {
                    ItemStack snobs = new ItemStack(Material.SNOW_BALL, 64);
                    givesnob.getPlayer().getInventory().addItem(snobs);
                    givesnob.sendMessage(ChatColor.YELLOW + "" + ChatColor.BOLD + "[Snowba] Battle Has Begun!");

                }
            }
           
            Snowba_Main.gameInProg = true;

        }
     
  2. Most of the server including your plugin runs on the same thread so when you freeze that thread then nothing works and the server will crash after some time. If you want delayed actions then just use the scheduler, BukkitRunnable is a great way of using it:
    This will delay something:
    Code (Text):
    new BukkitRunnable() {

                @Override
                public void run() {
                    // do something
                }
            }.runTaskLater(yourPlugin, delayInTicks);
     
    #2 MyzelYam, Jun 29, 2015
    Last edited: Jun 29, 2015
  3. Thread.sleep makes your whole Plugin sleep or ?
     
  4. Awesome thanks! Although i cannot use 'this' in a static void. D: How should i declare 'yourPlugin'?
     
  5. No it makes the whole server sleep, using Thread.sleep() is never a good idea in a plugin, since you shouldn't create own threads or freeze the main thread, you should use the scheduler instead.
     
    • Agree Agree x 1
  6. Are you sure your method needs to be static? Static is almost never required, but if you really need it, this is a little trick to bypass the problem:
    Code (Text):
    Bukkit.getPluginManager().getPlugin("yourPluginNameDefinedInYourPlugin.YmlFile")
     
    • Winner Winner x 1
  7. Why is your method static...?

    Edit: ninja'd
     
    • Funny Funny x 1
  8. Do i need static if im calling a function from another .java file? That's why i'm using it, but is this even needed?
     
    #8 MrMaster_, Jun 29, 2015
    Last edited: Jun 29, 2015
  9. Looks like you're new to java, please learn java basics before programming bukkit plugins, it will improve your plugin quality, speed and save our time. You don't need static to use methods from another class, you can just put your plugin instance as a requirement into your constructor and save it afterwards so you're able to access it anytime, or just use
    Code (Text):
    Bukkit.getPluginManager().getPlugin("yourPluginNameDefinedInYourPlugin.YmlFile")
    you can cast it to your main class safely and access your main classes methods then.
     
  10. What you're supposed to do is make it non-static and public, and then pass the instance of your class around. Then from the other class you can just do instance.________
     
    • Agree Agree x 1
    • Useful Useful x 1
  11. Is this e
    How do i "Pass the instance of the class around"?
     
  12. I'm trying to access a variable in another class from the main class.
     
  13. I'm fairly new to java, i've developed some working plugins, but i just dont know some things, such as
    - Constructors
    - ScheduledTasks

    And a lot more, but i mainly need to get those two down.

    The java site never seems to help me out when it comes to constructors. Where they go, what they do, ect. Thats what im caught up on. :(
     
  14. When instantiating a class, you use a constructor. A constructor is formatted as follows
    Code (Text):
    public class TestClass {
         public TestClass(Type param1, Type param2) {

         }
    }
    It is just public and then the class name. So you can pass your JavaPlugin instance in as a parameter in your constructor, which you can then use wherever you want in your new class.
     
  15. If you use Thread.sleep(10_000); in main-thread the server will do nothing for 10 seconds. Try create your own thread or use the Bukkit scheduler.
     
  16. I finally understand now! But there is one problem. I get a console error every time i do this. Do i need to have something inside of the Run method?
    Code (Text):
            new BukkitRunnable() {

                @Override
                public void run() {
                 
                }
             
            }.runTaskTimer(plugin, 0L, 10L);
    Console error is an unhanded exception when executing this command/
     
  17. Stack trace please
     
  18. Code (Text):
    org.bukkit.command.CommandException: Unhandled exception executing command 'snow
    ba' in plugin SnowballArena v1.1
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spi
    got.jar:git-Spigot-44c59bf-8f0f4ed]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:14
    1) ~[spigot.jar:git-Spigot-44c59bf-8f0f4ed]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServe
    r.java:642) ~[spigot.jar:git-Spigot-44c59bf-8f0f4ed]
            at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerCon
    nection.java:1135) [spigot.jar:git-Spigot-44c59bf-8f0f4ed]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java
    :970) [spigot.jar:git-Spigot-44c59bf-8f0f4ed]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java
    :45) [spigot.jar:git-Spigot-44c59bf-8f0f4ed]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java
    :1) [spigot.jar:git-Spigot-44c59bf-8f0f4ed]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:1
    3) [spigot.jar:git-Spigot-44c59bf-8f0f4ed]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
    ?:1.8.0_45]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_45]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SystemUtils.java:19) [spig
    ot.jar:git-Spigot-44c59bf-8f0f4ed]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:7
    18) [spigot.jar:git-Spigot-44c59bf-8f0f4ed]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:3
    67) [spigot.jar:git-Spigot-44c59bf-8f0f4ed]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:6
    57) [spigot.jar:git-Spigot-44c59bf-8f0f4ed]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java
    :560) [spigot.jar:git-Spigot-44c59bf-8f0f4ed]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_45]
    Caused by: java.lang.IllegalArgumentException: Plugin cannot be null
            at org.apache.commons.lang.Validate.notNull(Validate.java:192) ~[spigot.
    jar:git-Spigot-44c59bf-8f0f4ed]
            at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftScheduler.validate(Craf
    tScheduler.java:397) ~[spigot.jar:git-Spigot-44c59bf-8f0f4ed]
            at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftScheduler.runTaskTimer(
    CraftScheduler.java:123) ~[spigot.jar:git-Spigot-44c59bf-8f0f4ed]
            at org.bukkit.scheduler.BukkitRunnable.runTaskTimer(BukkitRunnable.java:
    100) ~[spigot.jar:git-Spigot-44c59bf-8f0f4ed]
            at me.bukkit.MrMaster_.Snowba_Game.startGame(Snowba_Game.java:34) ~[?:?]

            at me.bukkit.MrMaster_.Snowba_Main.onCommand(Snowba_Main.java:142) ~[?:?
    ]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spi
    got.jar:git-Spigot-44c59bf-8f0f4ed]
            ... 15 more
     
  19. plugin is null
     
  20. Using Thread#sleep doesn't make the server sleep, it makes the current thread sleep.
    It just so happens that the server is largely single-threaded, but there is a technical difference.

    And it's absolutely fine to create your own threads, for example, running operations outside of the main thread (such as cpu-intensive math calculations) but you have to be very careful to observe thread safety, which means that you can't use the majority of the methods in the API - Bukkit simply wasn't written with thread safety in mind.