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 //
            } catch (InterruptedException e) {
            for(Player givesnob : Bukkit.getServer().getOnlinePlayers()) {
                if(Snowba_Main.plrsInQueue.contains(givesnob.getName())) {
                    ItemStack snobs = new ItemStack(Material.SNOW_BALL, 64);
                    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() {

                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):
    • 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):
    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() {

                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( ~[spi
            at org.bukkit.command.SimpleCommandMap.dispatch(
    1) ~[spigot.jar:git-Spigot-44c59bf-8f0f4ed]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServe ~[spigot.jar:git-Spigot-44c59bf-8f0f4ed]
            at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerCon [spigot.jar:git-Spigot-44c59bf-8f0f4ed]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(
    :970) [spigot.jar:git-Spigot-44c59bf-8f0f4ed]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(
    :45) [spigot.jar:git-Spigot-44c59bf-8f0f4ed]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(
    :1) [spigot.jar:git-Spigot-44c59bf-8f0f4ed]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$
    3) [spigot.jar:git-Spigot-44c59bf-8f0f4ed]
            at java.util.concurrent.Executors$ Source) [
            at Source) [?:1.8.0_45]
            at net.minecraft.server.v1_8_R3.SystemUtils.a( [spig
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(
    18) [spigot.jar:git-Spigot-44c59bf-8f0f4ed]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(
    67) [spigot.jar:git-Spigot-44c59bf-8f0f4ed]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(
    57) [spigot.jar:git-Spigot-44c59bf-8f0f4ed]
    :560) [spigot.jar:git-Spigot-44c59bf-8f0f4ed]
            at Source) [?:1.8.0_45]
    Caused by: java.lang.IllegalArgumentException: Plugin cannot be null
            at org.apache.commons.lang.Validate.notNull( ~[spigot.
            at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftScheduler.validate(Craf ~[spigot.jar:git-Spigot-44c59bf-8f0f4ed]
            at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftScheduler.runTaskTimer( ~[spigot.jar:git-Spigot-44c59bf-8f0f4ed]
            at org.bukkit.scheduler.BukkitRunnable.runTaskTimer(
    100) ~[spigot.jar:git-Spigot-44c59bf-8f0f4ed]
            at me.bukkit.MrMaster_.Snowba_Game.startGame( ~[?:?]

            at me.bukkit.MrMaster_.Snowba_Main.onCommand( ~[?:?
            at org.bukkit.command.PluginCommand.execute( ~[spi
            ... 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.