Int issues. Any solution to fixing it?

Discussion in 'Spigot Plugin Development' started by TimeVisualSales, May 3, 2015.

Thread Status:
Not open for further replies.
  1. Hi there, I've created a survivalgames plugin but, when the game starts it constanting repeats the timer over and over again. Here's an example of what it does.
    [​IMG]

    My code:
    Code (Text):
     
      public static int LobbyCounter = 120;
      public static void startLobby()
      {
          Bukkit.getScheduler().scheduleSyncRepeatingTask(Main.getInstance(), new Runnable()
          {
              public void run()
              {
                  if (Main.status == GameState.LOBBY) {
                      if (Bukkit.getOnlinePlayers().size() >= 2) {
                          if ((Lobby.LobbyCounter == 120) || (Lobby.LobbyCounter == 90) || (Lobby.LobbyCounter == 60)
                          || (Lobby.LobbyCounter == 30) || (Lobby.LobbyCounter == 15) || (Lobby.LobbyCounter == 10)
                          || (Lobby.LobbyCounter == 5) || (Lobby.LobbyCounter == 4) || (Lobby.LobbyCounter == 3)
                          || (Lobby.LobbyCounter == 2) || (Lobby.LobbyCounter == 1)) {
                              Bukkit.broadcastMessage(Main.pr + "§eGame starts in §e§l" + Lobby.LobbyCounter + " §r§eseconds.");
                              for (Player all : Bukkit.getOnlinePlayers())
                              {
                                  LatestFeatures.sendTitle(all, 4, 12, 4, "§f§lStarting in: §6§l" + Lobby.LobbyCounter);
                                  all.playSound(all.getLocation(), Sound.NOTE_PLING, 1.0F, 1.0F);
                              }
                          }
                          for (Player all : Bukkit.getOnlinePlayers())
                          {
                              all.setLevel(Lobby.LobbyCounter);
                             all.setExp(0.0F);
                             BossBar.setName(all, "§6§lSurvivalGames §f§l:: §6§lLobby §e§l" + Lobby.LobbyCounter, Lobby.LobbyCounter);
                          }
                          if (Lobby.LobbyCounter == 12)
                          {
                              Global.voting = false;
                              Voting_Settings.endVote();
                              Bukkit.broadcastMessage(Main.pr + "§eThe map §e§l" + Global.winMap.getName() + " §r§ehas been chosen!\n"
                               + Main.pr + "§eMap won with §e§l" + Global.winMap.vote + " §r§evotes.");
                          }
                          if (Lobby.LobbyCounter == 0)
                          {
                              Main.status = GameState.WARMUP;
                              Bukkit.broadcastMessage(Main.pr + "§a§lTHE GAME HAS STARTED! §cGrace period begins in 20 seconds.");
                              WarmUp.startWarmUp();
                              Global.winMap.spawnPlayers();
                              for (final Player all : Bukkit.getOnlinePlayers())
                              {
                                 all.getInventory().clear();
                                 all.getInventory().setHelmet(null);
                                 all.getInventory().setChestplate(null);
                                 all.getInventory().setLeggings(null);
                                 all.getInventory().setBoots(null);
                                 all.setFoodLevel(20);
                                 all.setHealth(20.0D);
                                 all.setLevel(0);
                                 all.setExp(0.0F);
                                 Global.ingame.add(all.getName());
                                 Global.alive.add(all.getName());
                                 new BukkitRunnable()
                                 {
                                     public void run()
                                     {
                                         ItemStack ItemStack = new ItemStack(Material.DOUBLE_PLANT);
                                         ItemMeta ItemMeta = ItemStack.getItemMeta();
                                         ItemMeta.setDisplayName(ChatColor.GREEN + "" + ChatColor.BOLD + "Perks Shop");
                                         ItemStack.setItemMeta(ItemMeta);
                                         all.getInventory().setItem(8, ItemStack);
                                     }
                                  }.runTaskLater(Main.getInstance(), 40L);
                                 BossBar.removeBar(all);
                                 int alives = Global.alive.size();
                                 int dead = Global.spectator.size();
                                 BossBar.setName(all, "§6§lSurvivalGames §f§l:: §e§l" + alives + " §6§lAlive §e§l" + dead + " §6§lDead", Lobby.LobbyCounter);
                              }
                          }
                          Lobby.LobbyCounter -= 1;
                      }
                      else
                      {
                          for (Player all : Bukkit.getOnlinePlayers())
                          {
                             all.setLevel(Lobby.LobbyCounter);
                             all.setExp(0.0F);
                          }
                          Lobby.LobbyCounter = 120;
                      }
                  }
              }
          }, 0L, 20L);
      }
     
     
    #1 TimeVisualSales, May 3, 2015
    Last edited: May 3, 2015
  2. Add debugging to make sure that else isn't being called every time.
     
  3. If there are less than 2 players online it sets the counter to 120. You are broadcasting a message if the counter is 120 which means it does this constantly if there are less than 2 players online.
     
  4. How could I resolve this issue?
     
  5. Only display the message if the minimum player cap is reached.
     
  6. It works but It does this...
    [​IMG]
     
  7. Check if the timer is 0, if it is start your game and stop the timer (or if you still need it stop the messaging process).
     
  8. How do I stop the timer and I have made it so it checks if the timer is 0
     
  9. You're checking if it's 0 indeed, but never stopping it. To stop it store the scheduler id and stop it with.

    Code (Text):
    Bukkit.getScheduler().cancelTask(theId);
    This may not be what you want though, are you going to need this timer after the lobby period?
     
  10. Code (Text):
    if ((Lobby.LobbyCounter == 120) || (Lobby.LobbyCounter == 90) || (Lobby.LobbyCounter == 60)
                          || (Lobby.LobbyCounter == 30) || (Lobby.LobbyCounter == 15) || (Lobby.LobbyCounter == 10)
                          || (Lobby.LobbyCounter == 5) || (Lobby.LobbyCounter == 4) || (Lobby.LobbyCounter == 3)
                          || (Lobby.LobbyCounter == 2) || (Lobby.LobbyCounter == 1))
    equals

    Code (Text):
    if((Lobby.LobbyCounter % 30 == 0 || Lobby.LobbyCounter == 15 || Lobby.LobbyCounter <= 10) && Lobby.LobbyCounter != 0)
    Just for clean the code :p
     
    • Agree Agree x 1
  11. Check for > than 0 though, not !=.
     
  12. I need this timer during the lobby period.
     
  13. Ok ill go ahead and try it.
     
  14. Are you going to use any other timers? It's better to have one "GameTimer" during the full game instead of multiple timers. If you do this, though, you may want to clean up your class a bit.
     
  15. Might try that, could you give me an example or is it just the same as what Im doing?
     
  16. @TimeVisualSales

    Code (Text):
        if(GameState.getGameState() == GameState.LOBBY)
        {
            //Tick lobby methods
        }
        else if(GameState.getGameState() == GameState.IN_GAME)
        {
            //Tick in game methods
        }
        else if(GameState.getGameState() == GameState.DEATHMATCH)
        {
            //Tick deathmatch methods
        }
    This is a way to do it.
     
    • Agree Agree x 1
Thread Status:
Not open for further replies.