Split players into two teams

Discussion in 'Plugin Development' started by nrs23, Jan 20, 2014.

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

    nrs23

    Ive hit a brick wall, I've registered the teams in onEnable but i can't add players to the teams using a command, can anyone help.

    Code:java
    1. public void onEnable()
    2. {
    3. PluginDescriptionFile pdfFile = getDescription();
    4. logger.log(Level.INFO, "{0} Version {1} Has Been Enabled!", new Object[] { pdfFile.getName(), pdfFile.getVersion() });
    5. startup();
    6. this.plugin = this;
    7. this.bukkit = this.plugin.getServer();
    8. this.logger = this.plugin.getLogger();
    9. ScoreboardManager m = Bukkit.getScoreboardManager();
    10. Scoreboard s = m.getNewScoreboard();
    11.  
    12. final Team red = s.registerNewTeam("red");
    13. final Team blue = s.registerNewTeam("blue");
    14.  
    15. red.setPrefix(ChatColor.RED + "");
    16. blue.setPrefix(ChatColor.BLUE + "");
    17. red.setAllowFriendlyFire(false);
    18. blue.setAllowFriendlyFire(false);
    19. }



    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
    2. {
    3. Player p = (Player)sender;
    4. if (checkIfCommandSenderIsPlayer(sender))
    5. if (cmd.getName().equalsIgnoreCase("split")) {
    6. int i = 0;
    7. for (Player player : Bukkit.getOnlinePlayers()) {
    8. if (i < Bukkit.getOnlinePlayers().length / 2){
    9. //add to red team
    10. } else {
    11. // add to blue team
    12. }
    13. i++;
    14. }
    15. }
    16. return true;
    17. }
    18.  
    19. private boolean checkIfCommandSenderIsPlayer(CommandSender sender) {
    20. boolean success = false;
    21. if ((sender instanceof Player))
    22. {
    23. success = true;
    24. }
    25. return success;
    26. }
     
  2. nrs23
    I would create a random integer between 0-100 and check if its less than 50, put on red. More than 50, put on blue.

    Code:java
    1. for (Player player : Bukkit.getOnlinePlayers()) {
    2. Random rand = new Random();
    3. int random = rand.nextInt(100) + 1;
    4. if (random >= 0 && random <= 50) {
    5. // add to red
    6. } else if (random >= 51 && random <= 100) {
    7. // add to blue
    8. }
    9. }
     
  3. Offline

    CraftBang

    Did you remove the code at the part //add to red team.
    I don't really see what your problem is?
    What doesn't work?
    Did you try debugging messages (like just broadcast messages with "Point 1", "point 2" and look where it goes wrong?
    Won't that give problems when players is 15 (just asking I'm not sure my self, you would get 7.5)

    EDITED
    I agree with above, random integer looks better. (I might edit this again)
     
  4. Offline

    calebbfmv

    Or you could do ArrayList<String>, add to them (I like them better than Bukkits).
    You could create your own Team.java class (which I have done in my Framework for SUPER easy team management.)
    Or you could do random...
     
  5. Offline

    nrs23

    @CraftBang, I haven't removed the code to add them to the teams that the bit of code I want, how to take the player and add them to the team.
     
Thread Status:
Not open for further replies.

Share This Page