Repeating task within an if statement.

Discussion in 'Plugin Development' started by TheCrazyCrafter5, Dec 10, 2013.

Thread Status:
Not open for further replies.
  1. Ok, so I've got the basic understanding of how to use a repeating task. However, I'm having an issue when I try to place it inside of an if statement. Okay, so here is my code:
    Code:java
    1. int players = 2; boolean gameStart = false; int timer = 30;
    2.  
    3. if(players > 1){
    4. int countDown = this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
    5. public void run(){
    6. if(timer != -1){
    7. if(timer != 0){
    8. Bukkit.broadcastMessage(ChatColor.DARK_BLUE + "[" + ChatColor.GOLD + "CF" + ChatColor.DARK_BLUE + "]" + ChatColor.GOLD + "" + ChatColor.BOLD + " " + timer);
    9. timer--;
    10. }else{
    11. Bukkit.broadcastMessage(ChatColor.DARK_BLUE + "[" + ChatColor.GOLD + "CF" + ChatColor.DARK_BLUE + "]" + ChatColor.GREEN + "" + ChatColor.BOLD + " " + "GO!");
    12. timer--;
    13. gameStart = true;
    14. }
    15. }
    16. }
    17. }, 0L, 20L);
    18. }
    19. if(gameStart){
    20. Bukkit.getServer().getScheduler().cancelTask(countDown);

    Okay, so the goal is to only run the countdown if there is more than 1 player. The problem is that the variable "countDown" is not accessible when the task is inside of an if statement. It works when I remove the if statement, but then it'll start regardless of how many players there are, which I do not want. Keep in mind this is not my full code. Also, some of the variables, such as timer and players are actually in a different location, but I am showing you them so you know how they are used, I suppose. I can show more if need be, but this should suffice for now. I appreciate all help, thank you.

    P.S. Bonus points(a like) for whomever can tell me how I can make it so that when a player leaves, the timer cancels and the game doesn't start. Screw it... Here's some more code, lol:
    Code:java
    1. int players = 0; boolean gameStart;int num = 0;int timer = 30;
    2.  
    3. @EventHandler
    4. public void onClick(PlayerInteractEvent e){
    5. ArrayList<Player> pList = new ArrayList<Player>();
    6. Player p = e.getPlayer();
    7. World world = e.getPlayer().getWorld();
    8. Location queueSpawn = new Location(world, 100, 100, 100);
    9. Location gameSpawn = new Location(world, 100, 99, 65);
    10. ScoreboardManager manager = Bukkit.getScoreboardManager();
    11. Scoreboard board = manager.getNewScoreboard();
    12. if (e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
    13. if (e.getClickedBlock().getType() == Material.QUARTZ_ORE){
    14. if(players > 8){
    15. e.getPlayer().teleport(world.getSpawnLocation());
    16. e.getPlayer().sendMessage(ChatColor.RED + "This game is full, sorry!");
    17. }else{
    18. p.teleport(queueSpawn);
    19. players += 1;
    20. pList.add(p);
    21. num++;
    22. getServer().broadcastMessage(ChatColor.DARK_BLUE + "[" + ChatColor.GOLD + "CF" + ChatColor.DARK_BLUE + "]" + ChatColor.LIGHT_PURPLE + e.getPlayer().getName() + ChatColor.GREEN + " has joined the game!");
    23. getServer().broadcastMessage(ChatColor.DARK_BLUE + "[" + ChatColor.GOLD + "CF" + ChatColor.DARK_BLUE + "]" + ChatColor.GREEN + "There are now " + ChatColor.BOLD + ChatColor.GOLD + players + ChatColor.RESET + ChatColor.GREEN + " players");
    24. }
    25. }
    26. }
    27. if (e.getAction().equals(Action.RIGHT_CLICK_BLOCK)){
    28. if (e.getClickedBlock().getType() == Material.IRON_BLOCK){
    29. players -= 1;
    30. pList.remove(p);
    31. p.teleport(world.getSpawnLocation());
    32. getServer().broadcastMessage(ChatColor.DARK_BLUE + "[" + ChatColor.GOLD + "CF" + ChatColor.DARK_BLUE + "]" + ChatColor.LIGHT_PURPLE + e.getPlayer().getName() + ChatColor.GREEN + " has left the game.");
    33. getServer().broadcastMessage(ChatColor.DARK_BLUE + "[" + ChatColor.GOLD + "CF" + ChatColor.DARK_BLUE + "]" + ChatColor.GREEN + "There are now " + ChatColor.BOLD + ChatColor.GOLD + players + ChatColor.RESET + ChatColor.GREEN + " players");
    34. }
    35. }
    36. int countDown = this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
    37. public void run(){
    38. if(timer != -1){
    39. if(timer != 0){
    40. Bukkit.broadcastMessage(ChatColor.DARK_BLUE + "[" + ChatColor.GOLD + "CF" + ChatColor.DARK_BLUE + "]" + ChatColor.GOLD + "" + ChatColor.BOLD + " " + timer);
    41. timer--;
    42. }else{
    43. Bukkit.broadcastMessage(ChatColor.DARK_BLUE + "[" + ChatColor.GOLD + "CF" + ChatColor.DARK_BLUE + "]" + ChatColor.GREEN + "" + ChatColor.BOLD + " " + "GO!");
    44. timer--;
    45. gameStart = true;
    46. }
    47. }
    48. }
    49. }, 0L, 20L);
    50. if(gameStart){
    51. Bukkit.getServer().getScheduler().cancelTask(countDown);
    52. int arraySize = pList.size();
    53. int teamNum = 0;
    54. Team red = board.registerNewTeam("Red");
    55. Team blue = board.registerNewTeam("Blue");
    56. while(arraySize != 0){
    57. if(teamNum == 0){
    58. red.addPlayer(pList.get(arraySize));
    59. teamNum++;
    60. arraySize--;
    61. }else if(teamNum == 1){
    62. blue.addPlayer(pList.get(arraySize));
    63. teamNum--;
    64. arraySize--;
    65. }
    66. }
    67. }

    Really, I just want to know if I'm going about this the right way, cause I'm getting a sort of 'stuck' feeling. :3

    EDIT: When I run my plugin, the timer counts infinitely down and it doesn't execute code from the gameStart area. Why is this? I tried to stop it, but it's not working. Any ideas why? Thanks again.
    DOUBLE EDIT: The statements in above edit with a strike through it no longer apply. I have fixed that issue and changed the above code accordingly; I had a squiggly bracket in the wrong place.

    Anyone? I could really use some help. I'm confused as to why I cannot use the task within an if statement.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
Thread Status:
Not open for further replies.

Share This Page