for(Player online : Bukkit.getOnlinePlayers()){

Discussion in 'Plugin Development' started by TheQurekPL, May 5, 2019.

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

    TheQurekPL

    I dont understand why not work
    Code:
    Bukkit.getScheduler().runTaskAsynchronously(Main.getInstance(), () -> {
                for(Player online : Bukkit.getOnlinePlayers()){
                    List <String> users172 = new ArrayList<>();
                    List <String> users176 = new ArrayList<>();
                    List <String> users18 = new ArrayList<>();
                    List <String> users19 = new ArrayList<>();
                    List <String> usersVIP = new ArrayList<>();
                    for (int i = 0; i < online; i++){
                        Player p = online[i];
                        User u = UserManager.getUser(p.getUniqueId());
                        if (p.hasPermission("core.vip")){
                            usersVIP.add(p.getName());
                        }
                        if (u.getLauncherVersion() == 4){
                            users172.add(p.getName());
                        }
                        if (u.getLauncherVersion() == 5){
                            users176.add(p.getName());
                        }
                        if (u.getLauncherVersion() == 47){
                            users18.add(p.getName());
                        }
                        if (u.getLauncherVersion() > 47){
                            users19.add(p.getName());
                        }
                    }
                    sender.sendMessage(ChatUtil.fixColors("&7Gracze z wersja 1.7.2/1.7.5 - &3"+ users172.size()));
                    sender.sendMessage(ChatUtil.fixColors("&f"+users172.toString().replace("[", "").replace("]", "")));
                    sender.sendMessage(ChatUtil.fixColors("&7Gracze z wersja 1.7.6/1.7.10 - &9"+ users176.size()));
                    sender.sendMessage(ChatUtil.fixColors("&9"+users176.toString().replace("[", "").replace("]", "")));
                    sender.sendMessage(ChatUtil.fixColors("&7Gracze z wersja 1.8 - &5"+ users18.size()));
                    sender.sendMessage(ChatUtil.fixColors("&5"+users18.toString().replace("[", "").replace("]", "")));
                    sender.sendMessage(ChatUtil.fixColors("&7Gracze z wersja 1.9 - &b"+ users19.size()));
                    sender.sendMessage(ChatUtil.fixColors("&b"+users19.toString().replace("[", "").replace("]", "")));
                    sender.sendMessage(ChatUtil.fixColors("&7Gracze &6VIP&7 - &3"+ usersVIP.size()));
                    sender.sendMessage(ChatUtil.fixColors("&6"+usersVIP.toString().replace("[", "").replace("]", "")));
                    sender.sendMessage(ChatUtil.fixColors("&7Graczy ogolem - &8"+ online.length));
       
                }
     
  2. Online

    timtower Ninja on the waves Moderator

    @TheQurekPL What doenssn't work about it? If it is the second for loop, can't iterate over a player with an int loop.
     
  3. Offline

    TheQurekPL

    so what then I have to correct, change or delete
     
  4. Online

    timtower Ninja on the waves Moderator

    You don't need the first loop, only the second one. But that one needs to loop over the online players, not over the online variable.
     
  5. Offline

    TheQurekPL

    I removed the first loops, but they are still wrong
    im use translator because my English is bad
     
  6. Online

    timtower Ninja on the waves Moderator

    What is your current code?
     
  7. Offline

    TheQurekPL

    Code:
    @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Bukkit.getScheduler().runTaskAsynchronously(Main.getInstance(), () -> {
                Player online = Bukkit.getOnlinePlayers()
                    List <String> users172 = new ArrayList<>();
                    List <String> users176 = new ArrayList<>();
                    List <String> users18 = new ArrayList<>();
                    List <String> users19 = new ArrayList<>();
                    List <String> usersVIP = new ArrayList<>();
                    for (int i = 0; i < online; i++){
                        Player p = online[i];
                        User u = UserManager.getUser(p.getUniqueId());
                        if (p.hasPermission("core.vip")){
                            usersVIP.add(p.getName());
                        }
                        if (u.getLauncherVersion() == 4){
                            users172.add(p.getName());
                        }
                        if (u.getLauncherVersion() == 5){
                            users176.add(p.getName());
                        }
                        if (u.getLauncherVersion() == 47){
                            users18.add(p.getName());
                        }
                        if (u.getLauncherVersion() > 47){
                            users19.add(p.getName());
                        }
                    }
                    sender.sendMessage(ChatUtil.fixColors("&7Gracze z wersja 1.7.2/1.7.5 - &3"+ users172.size()));
                    sender.sendMessage(ChatUtil.fixColors("&f"+users172.toString().replace("[", "").replace("]", "")));
                    sender.sendMessage(ChatUtil.fixColors("&7Gracze z wersja 1.7.6/1.7.10 - &9"+ users176.size()));
                    sender.sendMessage(ChatUtil.fixColors("&9"+users176.toString().replace("[", "").replace("]", "")));
                    sender.sendMessage(ChatUtil.fixColors("&7Gracze z wersja 1.8 - &5"+ users18.size()));
                    sender.sendMessage(ChatUtil.fixColors("&5"+users18.toString().replace("[", "").replace("]", "")));
                    sender.sendMessage(ChatUtil.fixColors("&7Gracze z wersja 1.9 - &b"+ users19.size()));
                    sender.sendMessage(ChatUtil.fixColors("&b"+users19.toString().replace("[", "").replace("]", "")));
                    sender.sendMessage(ChatUtil.fixColors("&7Gracze &6VIP&7 - &3"+ usersVIP.size()));
                    sender.sendMessage(ChatUtil.fixColors("&6"+usersVIP.toString().replace("[", "").replace("]", "")));
       
              });
            return true;
        }
     
  8. Offline

    KarimAKL

    @TheQurekPL A few mistakes i could see:
    1. 'Bukkit.getOnlinePlayers()' does not return a player.
    2. You can't check if a player is higher than an integer.
     
  9. Offline

    TheQurekPL

    I got it !
    im use:
    Code:
    @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Bukkit.getScheduler().runTaskAsynchronously(Main.getInstance(), () -> {
                final Collection<? extends Player> players = Bukkit.getOnlinePlayers();
                    List <String> users172 = new ArrayList<>();
                    List <String> users176 = new ArrayList<>();
                    List <String> users18 = new ArrayList<>();
                    List <String> users19 = new ArrayList<>();
                    List <String> usersVIP = new ArrayList<>();
                    for (Player player : players) {
                        Player p = player;
                        User u = UserManager.getUser(p.getUniqueId());
                        if (p.hasPermission("core.vip")){
                            usersVIP.add(p.getName());
                        }
                        if (u.getLauncherVersion() == 4){
                            users172.add(p.getName());
                        }
                        if (u.getLauncherVersion() == 5){
                            users176.add(p.getName());
                        }
                        if (u.getLauncherVersion() == 47){
                            users18.add(p.getName());
                        }
                        if (u.getLauncherVersion() > 47){
                            users19.add(p.getName());
                        }
                    }
                    sender.sendMessage(ChatUtil.fixColors("&7Gracze z wersja 1.7.2/1.7.5 - &3"+ users172.size()));
                    sender.sendMessage(ChatUtil.fixColors("&f"+users172.toString().replace("[", "").replace("]", "")));
                    sender.sendMessage(ChatUtil.fixColors("&7Gracze z wersja 1.7.6/1.7.10 - &9"+ users176.size()));
                    sender.sendMessage(ChatUtil.fixColors("&9"+users176.toString().replace("[", "").replace("]", "")));
                    sender.sendMessage(ChatUtil.fixColors("&7Gracze z wersja 1.8 - &5"+ users18.size()));
                    sender.sendMessage(ChatUtil.fixColors("&5"+users18.toString().replace("[", "").replace("]", "")));
                    sender.sendMessage(ChatUtil.fixColors("&7Gracze z wersja 1.9 - &b"+ users19.size()));
                    sender.sendMessage(ChatUtil.fixColors("&b"+users19.toString().replace("[", "").replace("]", "")));
                    sender.sendMessage(ChatUtil.fixColors("&7Gracze &6VIP&7 - &3"+ usersVIP.size()));
                    sender.sendMessage(ChatUtil.fixColors("&6"+usersVIP.toString().replace("[", "").replace("]", "")));
                    sender.sendMessage(ChatUtil.fixColors("&7Graczy ogolem - &8"+ players));
              });
            return true;
        }
     
    Last edited: May 5, 2019
  10. Offline

    KarimAKL

    @TheQurekPL A few things that you could change, it doesn't really change much but in my opinion it looks better:
    1. Instead of making the 'players' variable you could just make the loop like this:
    Code:
    for (Player player : Bukkit.getOnlinePlayers()) {
    2. Instead of making the variable 'p' you could just change the name of 'player' to 'p'.
     
Thread Status:
Not open for further replies.

Share This Page