[ help ] - Pause a script so a task can finish running.

Discussion in 'Plugin Development' started by WinneonSword, Aug 11, 2013.

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

    WinneonSword

    Hello,

    I need to pause a script so a new Task().run(); can completely finish running. How would I do this?

    Here is my full script for the sub-command. The part that says new Task(this.plugin, welcomeMessages, 40L).run(); is what I'm talking about. I want to stop everything after that so it can run, and then after it's done running, the rest continues.
    Code:java
    1. if (player.hasPermission("mm.start")){
    2. if (started == false){
    3. for (World world : Bukkit.getWorlds()){
    4. boolean arenaSet = plugin.getConfig().getBoolean("arenaSet");
    5. if (arenaSet != true){
    6. sender.sendMessage("§e[ §7mm §e] §c- You have not set an arena! Go to the center of your arena and type '/mm set'!");
    7. return true;
    8. }
    9.  
    10. List<String> mmPlayers = plugin.getConfig().getStringList("MM.players");
    11. World arenaWorld = Bukkit.getWorld(plugin.getConfig().getString("arenaWorld" + ".world"));
    12. int x = plugin.getConfig().getInt("arenaWorld" + ".x");
    13. int y = plugin.getConfig().getInt("arenaWorld" + ".y");
    14. int z = plugin.getConfig().getInt("arenaWorld" + ".z");
    15.  
    16. List<String> welcomeMessages = plugin.getConfig().getStringList("welcomeMessages");
    17.  
    18. String rule = "doDaylightCycle";
    19. String value = "false";
    20.  
    21. // This is set to 1 currently. At the release it will be set at 5.
    22. if (mmPlayers.size() < 1){
    23. sender.sendMessage("§e[ §7mm §e] §c- There is not 5 or more people currently in the player queue!");
    24. return true;
    25. }
    26.  
    27. started = true;
    28. roundcounter = 0;
    29.  
    30. new Task(this.plugin, welcomeMessages, 40L).run();
    31.  
    32. world.setFullTime(18000);
    33. world.setGameRuleValue(rule, value);
    34.  
    35. for (int i = 0; i < 20;){
    36. world.spawnEntity(new Location(arenaWorld, x, y, z), org.bukkit.entity.EntityType.ZOMBIE);
    37. i++;
    38. }
    39. player.teleport(new Location(arenaWorld, x, y, z));
    40. return true;
    41. }
    42. }
    43. if (started == true){
    44. sender.sendMessage("§e[ §7mm §e] §c- There is already a game started!");
    45. // Everything after this is for testing purposes only, and will be removed for the final release.
    46. started = false;
    47. for (World world : Bukkit.getWorlds()){
    48. String rule = "doDaylightCycle";
    49. String value = "true";
    50.  
    51. world.setFullTime(6000);
    52. world.setGameRuleValue(rule, value);
    53. }
    54. return true;
    55. }


    Also here is the new Task script.
    Code:java
    1. class Task implements Runnable{
    2. private mm plugin;
    3. private List<String> list;
    4. private long time;
    5. private int count = 0;
    6.  
    7. public Task(mm plugin, List<String> list, long time){
    8. this.plugin = plugin;
    9. this.list = list;
    10. this.time = time;
    11. }
    12.  
    13. public void run(){
    14. plugin.getServer().broadcastMessage(list.get(count++));
    15.  
    16. if (count < list.size()){
    17. plugin.getServer().getScheduler().runTaskLater(plugin, this, 20);
    18. }
    19. }
    20. }
     
  2. Offline

    Dyprex

    You can't pause the task because it would force the whole server to freeze unless you use async tasks, which are very dangerous to use with the Bukkit API because they are not thread-safe.
     
  3. Offline

    WinneonSword

    I'm pretty sure there is a way to do this with the Bukkit Scheduler. Otherwise other plugins such as VariableTriggers wouldn't be able to proper pause things.
     
Thread Status:
Not open for further replies.

Share This Page