Old code with error

Discussion in 'Spigot Plugin Development' started by Daleet, Jul 23, 2018.

  1. Hey, Im trying update a plugin and i just load the code and something is error, I dont know how to fix it after i tried some things..

    this is the error
    Code (Java):
            if(Bukkit.getOnlinePlayers().length > 0)
            {
                data.loadOnlinePlayers(Bukkit.getOnlinePlayers());
            }
    the load online players

    Code (Java):
        public void loadOnlinePlayers(Player[] players)
        {
            for(int i = 0; i <players.length; i++)
            {
                this.players.add(this.loadPlayer(players[i].getName()));
               
            }
        }

     
     
  2. Send the actual error? I don’t think getOnlinePlayers() returns a Player[] and that’s why
     
  3. Ok, you have an issue. Could you please take the time to explain it? "something is error" doesn't help us understand your issue at all. Be more detailed.

    What does the loadPlayer method do?
     
  4. it returns a Collection so just converted into an array here the code getOnlinePlayers().toArray();
     
    #4 Soulcraft245, Jul 23, 2018
    Last edited: Jul 23, 2018
  5. Why don't you use addAll to add the collection?
     
  6. You could just add the player to an arraylist whenever they join the server, then remove them when they leave.
     
  7. Bukkit#getOnlinePlayers is kind of doing that. It's pointless to create a different list to store all online players.

    Why would he?

    Not really since #toArray returns an array of Object.

    Code (Java):

    public void loadOnlinePlayers(Player[] players)
        {
            for(int i = 0; i <players.length; i++)
            {
                this.players.add(this.loadPlayer(players[i].getName()));
             
            }
        }
     
    I'm positive this is the wrong way of adding something into an array.

    First off, Bukkit#getOnlinePlayers returns a Collection<? extends Player> . Seconly, using a void and adding something to the param locally will most likely do nothing unless you utilize that differently.

    In other words?

    Change Player[] players into a set of players instead, since you do not care for order, a hashset is ideal. I assume the method loadPlayer returns a Player instance? If not, please provide the code.