Set players to teams randomly

Discussion in 'Plugin Development' started by x17Andrew71x, Jul 3, 2013.

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

    x17Andrew71x

    Aight, Hello y'all!

    First off, I am new to Java and teaching myself, and I am trying to create a plugin for my server.
    I need some help however as I do not know what to do...
    And this is where all of you super smart ppl come in to save the day! :D

    Now what I am trying to do is as the title says, upon the use of the command /join, I wish to set that player to a random team. Either Blue or Red, I will have multiple worlds and some may have more than those two teams; ie - Yellow, Green, Blue, Red.

    So first, how do I get it to set the player, upon the use of the command, to one of the teams?
    And how can I set different teams per world?

    Edit: Or how to setup the teams upon the game starting?

    Here is my code:
    Code:java
    1. package me.x17Andrew71x.EFPM;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7.  
    8. public class PlayerJoinTeam {
    9.  
    10. public boolean onCommand(CommandSender sender, Command cmd, String CommandLabel, String[] args){
    11. Player player = (Player)sender;
    12. if(!player.hasPermission("efpm.join")){
    13. player.sendMessage(ChatColor.RED + "You do not have the required permissions to do this.");
    14. return true;
    15. } else if(cmd.getLabel().equalsIgnoreCase("join")){
    16.  
    17. }
    18. return false;
    19. }
    20. }

    Thanks in advance!
     
  2. Offline

    EcMiner

    To really do it random you could do this:
    Code:java
    1. Random random = new Random();
    2. boolean value = random.nextBoolean();
    3. if(value == true) {
    4. // put him team red or whatever
    5. } else {
    6. // put him in team blue or whatever
    7. }
     
  3. Offline

    Alex5657

    EcMiner already answered your question, but there is a problem with your code. Your class must extend JavaPlugin.
    Code:
    public class PlayerJoinTeam extends JavaPlugin {
    And don't forget to specify it in your plugin.yml!
     
  4. Offline

    coobro123

    First create a list
    Code:
    List<Player> blue = new ArrayList<Player>();
    List<Player> red = new ArrayList<Player>();
    Then use this method instead of randomizing. So that the teams are even.

    Code:
    if(blue.length >= red.length) {
        blue.add(player);
    } else if(red.length <= blue.team) {
        red.add(player);
    }
    Should be something like that. Hope this helps
     
  5. Offline

    Garris0n

    My OCD wishes to point out that if the teams are equal that gives one priority. I'd randomize the team in the case of both being equal so I could sleep at night :p
     
  6. Offline

    microgeek

    Don't store Player instances!
     
    EcMiner likes this.
  7. Offline

    xXSilentYoshiXx

    microgeek
    Because the people who say "Don't store Player instances!" are annoying and want attention. XD

    Cons about not storing Player instances: YOU CAN'T DO ANYTHING ABOUT THE PLAYER EASILY
    Pros: IT'S FREAKING EASY TO DO ANYTHING WITH THE PLAYER :)
     
  8. Offline

    microgeek

    T3h Cr33p3r
    xXSilentYoshiXx
    It causes memory leaks, because the VM's Garbage Collector cannot destroy Object instances that reference the Player's instance.
    http://javarevisited.blogspot.com/2011/04/garbage-collection-in-java.html

    Just make a simple wrapper class;

    Code:
    class SafePlayer {
      private String playerName;
      public SafePlayer(String playerName) {
        this.playerName = playerName;
      }
     
      public Player getPlayer() {
        //might be wrong, I just typed this up
        return Bukkit.getPlayer(playerName);
      }
     
    }
     
    TheGreenGamerHD likes this.
  9. Offline

    TomFromCollege

    microgeek
    I like how you linked them to some random garbage collection article that; without any insight into how the Player object is instanced, gives you absolutely no insight into the problem you're addressing.
     
  10. Offline

    microgeek

    There have been countless posts on the problem, I linked to the article, hoping people would educate themselves on how the VM's GC works.

    T3h Cr33p3r
    Bukkit.getServer().getPlayer() should work fine.

    What errors?

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

    x17Andrew71x

    Okay so, this is what I got now, but it first off, doesn't look right to even me and I dont know really what I'm doing. Also, it gives me this error that I cannot resolve:


    Code:
    package me.x17Andrew71x.EFPM;
     
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
     
    public class PlayerJoinTeam {
       
        List<Player> blue = new ArrayList<Player>();
        List<Player> red = new ArrayList<Player>();
        List<Player> waiting = new ArrayList<Player>();
        Random rand = new Random();
     
        public boolean onCommand(CommandSender sender, Command cmd, String CommandLabel, String[] args){
            Player player = (Player)sender;
            if(!player.hasPermission("efpm.join")){
                player.sendMessage(ChatColor.RED + "You do not have the required permissions to do this.");
                return true;
            } else if(cmd.getLabel().equalsIgnoreCase("join")){
                if (waiting.size() == 0) {
                    waiting.add(player);
                } else {
                    Player player2 = waiting.get(0);
                    if (rand.nextBoolean()) {
                        red.add(player.getName());
                        blue.add(player2.getName());
                    } else {
                        blue.add(player.getName());
                        red.add(player2.getName());
                    }
                }
           
            }
            return false;
        }
    }
     
  12. Offline

    ZeusAllMighty11

    Honestly, you could store player instances, but it's highly recommended that you don't. If you don't even know what an instance is, then you should really just store the player's name.


    The reason you should store the name was already explained above. It's really not difficult to retrieve, and can be done in 3 lines or less from the built-in method in Bukkit 'getPlayer()'

    Everyone always complains about it, but it's really your choice - would you rather have a buggy, memory-leaking plugin or a normal one? -.-
     
Thread Status:
Not open for further replies.

Share This Page