Adding a player to a random team when they join

Discussion in 'Plugin Development' started by BDKing88, Aug 4, 2013.

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


    Hey, I'm making a teams plugin (Red and blue) and I would like it so when a player joins the server they are randomly selected for a team. This is what I have so far (I do have an error on line 19 because PlayerJoinEvent won't let me do waiting.length) :
    1. public class Main extends JavaPlugin implements Listener {
    3. List<Player> blue = new ArrayList<Player>();
    4. List<Player> red = new ArrayList<Player>();
    5. List<Player> waiting = new ArrayList<Player>();
    6. Random random = new Random();
    8. public void onDisable() {
    10. }
    12. public void onEnable() {
    14. }
    16. @EventHandler
    17. public void onPlayerJoin(PlayerJoinEvent e) {
    18. Player player = e.getPlayer();
    19. if (waiting == 0) {
    20. waiting.add(player);
    21. } else {
    22. Player player2 = waiting.get(0);
    23. if (random.nextBoolean()) {
    24. red.add(player);
    25. blue.add(player2);
    26. } else {
    27. blue.add(player);
    28. red.add(player2);
    29. }
    30. }
    32. }
    34. }

    If someone can help, I'll be grateful! :)
  2. Offline


    First, register the event, that's pretty important. :p also, change if(waiting == 0) to if(waiting.size() == 0)
    Blir likes this.
  3. Offline


    ArrayLists have a size() method you can call rather than a length field that an array has.

    Call waiting.size() rather than waiting.length.

    Edit: fanaticmw2 barely beat me to it, oh well. :p
    fanaticmw2 likes this.
  4. Offline


    also don't store player entites in lists.. store their names instead use List<String>
  5. Offline


    You can store the player entity if you just do proper cleanup on player quit.
  6. Offline


  7. Offline


    without going into the 9 million reasons you shouldn't store players lets just simplify it since people on here seem to think it's ok to give bad advice.... a string of the players name is tiny, storing a whole player object is not, so even if you ignore every single other reason not to store a player object at least quit telling people that it's a good idea to use a lot more memory space.... don't give advice on here or correct people if you don't know what you're talking about. It is ALWAYS a bad idea to save a player object rather than the name.
  8. Offline


    soulofw0lf Well, you are wrong, my dear. I am really sick of the ongoing discussion about that but I cannot leave it like this. A String just like a Player is an Object. When you save a String in a List the JVM takes the memory address and puts it into the list. Guess what! It does the same with the Player object. That does not use more memory.

    The problem ist the garbage collection. If a player quits, the Player object gets obsolete and the garbage collector looks for such objects every now and then to remove them. But if you still have the address of the object saved in the List, then the object is not removed by the garbage collector. Thus for server with hundreds of player this can be a problem because all Players objkects that are in the list stay in memory.

    It really depends on the plugin if it's possible to cleanup ion quit or limit the time until the whole list is cleared. You could for example remove the Player on quit or clear the teams after every game round. This should be as good as saving the name.

    But without explaining what actually happens and how to fix, I would not recommend using Player objects. Using only Player names however, should not cause this problem. Although you would need to mention something else then.

    Using Bukkit.getPlayer(name) as usually recommended is not the proper way the get the Player object back from the name. Use Bukkit.getPlayerExact(name) instead. Use getPlayer(...) if you would like to select a Player object based on keyboard input like in a command.
  9. Offline


    Ok, so I'm a bit confused, which way is the "best" way or easiest?
  10. Offline


    The one which you can't do anything wrong with is using player names as String and Bukki.getPlayerExact(name) to get back the PLayer object.
Thread Status:
Not open for further replies.

Share This Page