Getting all online players except ONE

Discussion in 'Plugin Development' started by AaronL98, Jul 26, 2013.

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

    AaronL98

    So im creating a plugin which uses the scoreboard feature and i want to add every player to one team of a score board except ONE player which i will add to the opposite team. I have the player i want to add to the opposite team stored in a player variable.
     
  2. Offline

    rippin

    Add all players to a list and remove the player that will be on the other team?
     
  3. Offline

    AaronL98

    rippin How would i remove the other player?
     
  4. Offline

    soulofw0lf

    for (Player p : Bukkit.getOnlinePlayers()){
    if (p.getName().equals(//variable you stored)){
    continue;
    }
    //code to do stuff to the rest of players
    }

    that will do something to every player but one

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  5. Offline

    AaronL98

    soulofw0lf But if i were to put 'oppositeTeam.addPlayer(variablestored);' then for the other team would i just use

    'Team.addPlayer(Bukkit.getOnlinePlayers);' ?

    I cant really see that working if the opposite team member will also be added to the normal team.
     
  6. Offline

    soulofw0lf

    for (Player p : Bukkit.getOnlinePlayers()){
    if (p.getName().equals(//variable you stored)){
    continue;
    }
    Team.addPlayer(p);
    }

    continue; will stop processing that current iteration and move onto the next by putting it above your team add that player will effectively be "skipped" when it comes to adding players to a team

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  7. Offline

    JPG2000

    soulofw0lf Just use a arraylist, and the player tghat you want just remove them!
     
  8. Offline

    soulofw0lf

    @JPG2000
    that's pretty much what the Bukkit.getOnlinePlayers() for list there does.. just without having to create a seperate array and store all the players in it seperately... for what he wants it really is the best method.
     
  9. Offline

    AaronL98

    Also how could I send all the players in the array list a message or apply potion effects etc to them? After referencing arraylistName . i don't get the usual options i get for typing player . (options).

    Here's my array list variable.

    Code:java
    1. public static ArrayList<Player> players = new ArrayList<Player>(Arrays.asList(Bukkit.getServer().getOnlinePlayers()));
    2.  
    3. //I'm also removing 1 player from this array list just before i apply potion effects with
    4.  
    5. players.remove(variableOfOtherPlayerOnOtherTeam);
    6. [/sytax=java]
    7.  
    8. I Would really like some help on this.
    9.  
    10. EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  10. Offline

    soulofw0lf

    never store a player in a list, store the player name instead if you want to use a list.
     
  11. Offline

    microgeek

    You should learn more about the Java language and/or the Bukkit API before attempting to write a plugin.
     
    Cirno and Rprrr like this.
  12. Offline

    AaronL98

    I
    I've wrote multiple plugins already, basic ones and haven't used these features yet.
     
  13. Offline

    metalhedd


    Never say never, there are plenty of valid reasons to create a List of players. you just don't want to keep it around longer than necessary.
     
  14. Offline

    AaronL98

    How could i do that? This is the first time I've tried to store player names.



    Are lists reset after server reload or restart just like hashmaps? If so that seems good, because this plugin is for a custom gamemode, and the server restarts after each game.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  15. Offline

    metalhedd

    Yes they are, but that's not what I was getting at. you shouldn't store a List<Player> for *ANY* length of time, you can create it for immediate use, but then you should discard it as soon as you're done with it. do not store the list as a variable and reference it later.
     
  16. Offline

    Rprrr

    Which doesn't even mean you know Java. You seem not to be able to use a simple List.. which indicates you aren't very familiar with the basics of Java.
     
    metalhedd likes this.
  17. Offline

    soulofw0lf

    no there really aren't any valid reasons to create lists of players, that just leads to bad habbits and potential memory leaks. please never post on here anywhere that there are valid reasons to do it as that is teaching people it's ok when it is not. ALWAYS store "players" by their name, regardless of what you are using it for. in any situation in which you would want to use lists of players you can very very easily iterate through the list of strings and use Bukkit.getPlayer(listVariable); so there is no reason at all to EVER use players saved in any fashion to any type of list or map.
     
  18. Offline

    Rprrr

    soulofw0lf
    You really don't read @metalhedd's post, do you?

    It sure is valid to store players in a list if I want to perform certain actions on all players in that list. If I'm being given the player objects, and not the names, and I want to directly (so not later) perform actions with those objects, a list would be perfectly fine. Lists of players only lead to memory leaks when you store them over time (read: for later use), not when you use them directly and then dispose them.
     
  19. Offline

    metalhedd

    I stand by my statement. if you wanted to print a sorted list of players by their current XP, you would first generate a list of Player objects, then sort it, then loop through it printing the names. There's NOTHING wrong with that. Problems arise when you keep that List and try to re-use it later.

    as a rule of thumb: "Never store a Player object" is a good idea. but you're misinterpreting the intent.
     
  20. Offline

    microgeek

    You obviously do not have a very good grasp on computing logic if you cannot manage to get one element out of a collection.
     
Thread Status:
Not open for further replies.

Share This Page