Solved Array changed to Collection

Discussion in 'Spigot Plugin Development' started by matthijspc, Jun 25, 2016.

  1. Hello,
    I have an old plugin on Bukkit Dev I would like to update and would like to put on Spigot. In Spigot 1.8 Array was changed into a Collection. How do I get this code working?
    Code (Text):
    String str = arrayOfString[i];
            cast = cast + str + " ";
            String s = ChatColor.translateAlternateColorCodes('&', cast);
            Bukkit.broadcastMessage(ChatColor.RED + "[ALERT] " + s);
            Player[] arrayOfPlayer;
            int m = (arrayOfPlayer = Bukkit.getOnlinePlayers()).length;
            for (int k = 0; k < m; k++)
            {
              Player player = arrayOfPlayer[k];
              if (player.hasPermission("WorldAlert.receive")) {
                player.sendMessage(ChatColor.RED + "[ALERT] " + s);
    Thanks in advance
     
  2. Change type of arrayOfPlayer to List<Player>, and call size() instead of getting the field length on it.
    On the for loop you could rather just do a forEach.
     
  3. Do the above or the easy dirty fix would be to change the Collection to an array via:
    Code (Java):
    arrayOfPlayer = Bukkit.getOnlinePlayers().toArray( new Player[ Bukkit.getOnlinePlayers().size() ] );
     
  4. You can simply replace this with
    Code (Text):
    for (Player player : Bukkit.getOnlinePlayers()) {
    if you don't need the 'k' variable.
     
  5. Thnx! That worked!
     
    • Friendly Friendly x 1