Solved Countdown messaging problem.

Discussion in 'Plugin Development' started by MinecraftMart, Feb 28, 2015.

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

    MinecraftMart

    So im calling a countdown method on every player in a list. But the problem is it puts the method on the player multiple times. If i had 4 players in that list it calls the method on that player 4 times.

    Can somebody help me here?

    Start method.
    Code:
        public void startTheGame(Player p, String ar){
            ArrayList<UUID> listofplayer = new ArrayList<UUID>();
            main.game.running.add(ar);
            for (Entry<UUID, String> entry : main.game.players.entrySet())
            {
                if(entry.getValue().equalsIgnoreCase(ar)){
                    listofplayer.add(entry.getKey());
                }
    
            }
            for(int i=0; i<listofplayer.size(); i++){
               UUID uuid = listofplayer.get(i);
               for(Player player : Bukkit.getServer().getOnlinePlayers()){
                   if(player.getUniqueId().equals(uuid)){
                       String spawn = Integer.toString(i + 1);
                      World world = Bukkit.getWorld(main.arenas.getString("arenas." + ar + "." + spawn + ".world"));
                    
                       Location loc = new Location(world, main.arenas.getDouble("arenas." + ar + "." + spawn + ".X")
                               ,main.arenas.getDouble("arenas." + ar + "." + spawn + ".Y"),
                               main.arenas.getDouble("arenas." + ar + "." + spawn + ".Z"));
                       player.teleport(loc);
                       player.sendMessage(ChatColor.GREEN + "You’re a wizard " + player.getName() + "! Good luck!");
                       FreezeCooldown(player);
                       player.getInventory().clear();
                       giveWands(player);
                       main.game.playing.put(player.getUniqueId(), ar);
                   }
               }
            }                  
        }
    Countdown method
    Code:
    public void FreezeCooldown(final Player p){
            main.game.feezed.add(p);
            new BukkitRunnable() {
                int seconds = 10;
                  @Override
                  public void run() {
                      switch(seconds){
                      case 10:
                          p.sendMessage(ChatColor.RED + Integer.toString(seconds));
                          break;
                      case 9:
                          p.sendMessage(ChatColor.RED + Integer.toString(seconds));
                          break;
                      case 8:
                          p.sendMessage(ChatColor.RED + Integer.toString(seconds));
                          break;
                      case 7:
                          p.sendMessage(ChatColor.RED + Integer.toString(seconds));
                          break;
                      case 6:
                          p.sendMessage(ChatColor.RED + Integer.toString(seconds));
                          break;
                      case 5:
                          p.sendMessage(ChatColor.RED + Integer.toString(seconds));
                          break;
                      case 4:
                          p.sendMessage(ChatColor.RED + Integer.toString(seconds));
                          break;
                      case 3:
                          p.sendMessage(ChatColor.RED + Integer.toString(seconds));
                          break;
                      case 2:
                          p.sendMessage(ChatColor.RED + Integer.toString(seconds));
                          break;
                      case 1:
                          p.sendMessage(ChatColor.RED + Integer.toString(seconds));
                          break;
                      case 0:
                          p.sendMessage(ChatColor.GREEN + "GO");
                            main.game.feezed.remove(p);
                          break;
                     default:
                         break;
                      }
                      seconds--;
                  }
            }.runTaskTimer(main, 20L, 20L);
          
        }
    I know this switch thing could be deeted and jut do a mesage with seconds-- but its this now :p
     
  2. @MinecraftMart
    Firstly, you could change that massive switch by using if/else:
    Code:
    if(seconds == 0) {
        p.sendMessage(ChatColor.GREEN + "GO");
        main.game.feezed.remove(p);
    } else
        p.sendMessage(ChatColor.RED+Integer.toString(seconds));

    This is why it's running the method multiple times.
    View attachment 23515
     
  3. Offline

    MinecraftMart

    1. Yeah i know :p just showing someone how to use switch
    2. Eehm I looked at it again and this is what it does right?
    It takes the 1st out of the list, then checks for every player if its the right uuid and match them. If so it runs a code and puts the method on them.

    What am I seeing wrong?
     
Thread Status:
Not open for further replies.

Share This Page