Alternatives to Thread.sleep()?

Discussion in 'Plugin Development' started by MrJoe223, Aug 15, 2013.

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

    MrJoe223

    My bud and I have been using Thread.sleep() to delay execution of code. During a 30-second for loop(of Thread.sleep()s), nobody can chat, or enter commands. What are good alternatives to Thread.sleep()? We have them encased in a runTaskAsynchronously(), but that seems to do nothing.

    Code below, if there's an easier fix:
    Code:java
    1. Bukkit.getServer().getScheduler().runTaskAsynchronously(this.plugin, new Runnable() {
    2.  
    3. public void run() {
    4. String proofix = ChatColor.GOLD + "[" + ChatColor.ITALIC + ChatColor.RED + "Sabotage" + ChatColor.GOLD + "]" + ChatColor.RESET;
    5.  
    6. for(int i = 0; i <31; i++) {
    7. if(ButtonPressListener.redDefused) {
    8. Bukkit.getServer().broadcastMessage(proofix + ChatColor.GRAY + "Blue team has defused the bomb! New bomber: " + ChatColor.GOLD + Sabotage.bomber);
    9. redPlanted = false;
    10. ButtonPressListener.redDefused = false;
    11. break;
    12. }
    13. if(i == 0) {
    14.  
    15. Bukkit.getServer().broadcastMessage(proofix +
    16. ChatColor.RED + "Red Team has planted the bomb! 30 seconds until"
    17. + ChatColor.GOLD + " detonation.");
    18. System.out.println("red plant");
    19.  
    20. }
    21.  
    22. if(i == 10) {
    23. Bukkit.getServer().broadcastMessage(proofix + ChatColor.GOLD + " 20 Seconds.");
    24. }
    25.  
    26. if(i == 20) {
    27. Bukkit.getServer().broadcastMessage(proofix + ChatColor.GOLD + " 10 Seconds.");
    28. }
    29.  
    30. if(i == 30) {
    31. try {
    32. playah.getWorld().createExplosion(x, y, z, 5, false, false);
    33. playah.getWorld().createExplosion(194, 106, 356, 100, false, false);
    34. Bukkit.getServer().broadcastMessage(proofix + ChatColor.GOLD + "Red Wins!");
    35. Thread.sleep(1000);
    36. Bukkit.getServer().broadcastMessage("Restart in 3..");
    37. Thread.sleep(1000);
    38. Bukkit.getServer().broadcastMessage("Restart in 2..");
    39. Thread.sleep(1000);
    40. Bukkit.getServer().broadcastMessage("Restart in 1..");
    41. } catch(InterruptedException e1) {
    42. e1.printStackTrace();
    43. }
    44.  
    45. for(Player p : Bukkit.getServer().getOnlinePlayers())
    46. {
    47. p.kickPlayer(proofix + ChatColor.GOLD + "Join for another round! By Kedalion and MrJoe223");
    48. }
    49.  
    50. Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "plugman reload Sabotage");
    51.  
    52. }
    53.  
    54. try {
    55. Thread.sleep(1000);
    56. } catch (InterruptedException e) {
    57.  
    58. e.printStackTrace();
    59. }
    60. }
    61. }
    62.  
    63. });
     
  2. Offline

    Axe2760

    Run it Synchronously, as you are making calls to the bukkit API.
     
    Tirelessly likes this.
  3. Offline

    Tirelessly

    ^That, which means you can't really use sleep. You need to schedule another task or use a repeating task.
     
  4. Offline

    KedalionPlugins

    I'm his bud. Can you give some examples? I haven't worked with that in the API.
     
  5. Offline

    ZeusAllMighty11

    His post already shows he has knowledge of a scheduler, just change it from runTaskAsync to scheduleAsyncRepeatingTask()
     
  6. Offline

    tommycake50

    Instead of calling bukkit methods from inside the Asynchronous task(dangerous) call synchronized methods inside the class that the scheduler is inside of maybe.
     
  7. MrJoe223 In fact there aren't any alternatives to Thread.sleep() because you are not supposed to use it :d But there are alternatives to create some kind of delay on a safe way (see what those nice guys wrote up here)
     
Thread Status:
Not open for further replies.

Share This Page