Why isn't this working?

Discussion in 'Plugin Development' started by A5H73Y, Jan 2, 2013.

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

    A5H73Y

    So I have declared
    Code:
    List < String > players = new ArrayList < String > ();
    And added myself to the array, when I try to run the below code it simply doesn't work? Any suggestions?

    Code:
    public void Finished(String player, String map, int lives){
            for (Player playingp : Bukkit.getServer().getOnlinePlayers()){
                if (plugin.players.contains(playingp)){
                      playingp.sendMessage("finished!");
                }
            }
        }
     
  2. Offline

    JeroenV

    A5H73Y

    Well your list is <String> instead of <Player> since ..
    Bukkit.getServer().getOnlinePlayers()
    ..returns Player objects. To fix this you've got 2 options:

    1. Change the list to Player
    List < Player > players = new ArrayList < Player > ();
    Don't use this option, carrying around larger objects in lists is not recommended. (As fireblast709 pointed out.)

    2.Keep the list the same but change the method. (Carrying around a list with all the player information instead of just the name while doing a simple check like this is unnecessary.)
    Code:
     for (Player playingp : Bukkit.getServer().getOnlinePlayers()){
    String playername  playingp.getPlayerListName();
     
                if (plugin.players.contains(playername)){
     
                      playingp.sendMessage("finished!");
     
                }
     
            }
     
  3. Offline

    A5H73Y

    Thank you! The second method worked great :)
     
  4. Offline

    fireblast709

    JeroenV Don't use Player objects in Lists/Sets/Maps, it causes memory leaks
     
  5. Offline

    Assult

    You should not save a players entity in a list, you should save his name (as a string), if you dont you will end up having errors and memory leaks like fireblast709 said
     
  6. Offline

    JeroenV

    Yeah, as the second option explains I was aware that carrying around playerdata isn't a good thing to do. I was just letting him know that there are multiple ways of solving the issue. However I should have indeed noted that the PlayerList method might not be recommended.

    A5H73Y If you ever get to a point where you do need to transfer lists that carry around locations/players it's best to parse it into a string according to the information that you need. For example to save a location, you could use:
    Code:
    String locstr = loc.getWorld().getName()+","+loc.getX()+","+loc.getY()+","+loc.getZ();
    
    (you could also parse in Pitch and so on if you want to be more specific)

    then you can easily get the location back using
    Code:
    String[] locsplt = locstr.split(",");
    Location loc = new Location(Bukkit.getWorld(locsplt[0]),Float.parseFloat(locsplt[1]),Float.parseFloat(locsplt[2]),Float.parseFloat(locsplt[3]))
    
    Just a little info for if you ever need to do that.
     
Thread Status:
Not open for further replies.

Share This Page