[Tutorial] UHC Team Maker

Discussion in 'Spigot Plugin Development' started by Bladian, May 4, 2015.

  1. I've seen a couple of threads that said they were having trouble making teams, so I decided to make one and see how it came out.

    This is my first tutorial ever so feel free to criticise all you can.

    This teams tutorial will be taken from my UHC plugin so feel free to change around to use strings or coloured names. Anything works

    THE TEAMS CLASS

    PHP:
    public class Team {

        private int teamID;

        private List<String> players = new ArrayList<String>();

        public Team(int teamID) {
            this.teamID = teamID;
       
        }


    }
     
    Here we have the team class itself, the constructor will contain the teamID and the color

    The List will contain a list of all the players in the team, you can of course use, a hashmap, bit I think this gives a bit more flexibility.

    Now for the getters:

    PHP:
    public class Team {

        private int teamID;

        private List<String> players = new ArrayList<String>();

        private ChatColor color;

        public Team(int teamID) {
            this.teamID = teamID;
        }

        public int getTeamID() {return team;}

        public List<String> getPlayers() {return players;}

        public ChatColor getChatColor(){return color;}
    }
     
    Now that we have a getters we can go on and make the team manager class:

    Since this is a UHC team class, I'm going to have it make a certain amount of teams depending by the command, this will all be done using a method.

    I made three separate methods so hopefully it's easier to understand.

    TEAM MANAGER CLASS

    PHP:

    public List<Team> getTeams() {return teams;}

    private List<String> players = new ArrayList<String>();
     
    Here we have the two main lists, one containing the teams, and the second containing all the players you want to add to the teams.

    Here I would recommend using a PlayerJoinEvent or you can loop through all the players and add them to a list.

    PHP:

        private int allPlayers
        public void createTeamsRandom(int amount) {
            for (int times = 1; times <= amount; times++) {
                Team team = new Team(times + teams.size());
                teams.add(team);
            }
            Bukkit.broadcastMessage("A total of " + teams.size() + " have been created");
        }
     
    Now this method has been made to work with a command, which I will post at the bottom of the tutorial.

    Basically here we create a certain amount of teams depending by the amount written by the sender of the message.

    PHP:

        public void addPlayersRandom(int amount) {

            int timesDone = 0;

            for (Team team : teams) {

                if (players.size() != 0) {

                    for (int times = 0; times <= amount; times++) {

                        if (players.size() != 0) {

                            timesDone++;

                            if(timesDone != amount) {

                                int random = new Random().nextInt(players.size());
                                String name = players.get(random);
                                team.getPlayers().add(name);
                                players.remove(name);
                                Player p = Bukkit.getServer().getPlayer(name);
                                Bukkit.broadcastMessage("§b" + timesDone + "§8/§b" + allPlayers.size() + " §ahave been added to a team!");
                                timesDone++;
                            }
                            else {
                                timesDone = 0;

                            }

                        } else {
                            break;

                        }


                    }
                }
                else {
                    break;
                }
            }
        }
     
    In this part of the code we loop through all the teams. The int amount represent the amount of people which are allowed per team, again which will be decided via the command.

    This will loop through all the teams, check if the amount per team is = to the amount, if it's not it will get a random player from the list, and add him to the team.

    PHP:

        public void addColorsRandom() {

            for (Team team : teams) {

                int count = 0;
                int offset = 16;
                ChatColor format = null;

                for (String name : team.getPlayers()) {

                    Player members = Bukkit.getServer().getPlayer(name);

                    ChatColor[] colors = ChatColor.values();

                    ChatColor color = colors[count];

                    if (format != null) {
                        members.setPlayerListName(format + members.getName());
                    }

                    members.setPlayerListName(color + members.getPlayerListName());

                    count++;
                    if (count == 16) {
                        count = 0;
                        format = colors[offset];
                        offset++;
                        if (offset == 21) {
                            format = null;
                            offset = 16;
                        }
                    }
                }
            }
        }
    Now if you want to add colors to the name's of the teams you can use this method, which I thank @vk2gpz greatly for helping me out.

    This will loop through the teams, and then the colors, once the team has finished it will move onto the next team and use another color.

    THE COMMAND CLASS

    Now because we want to check if the arguments are integers we'll use this method.

    PHP:
     public static boolean isNumber(String s) {
            try {
                Integer.parseInt(s);
            } catch (NumberFormatException nfe) {
                return false;
            }
            return true;
        }

    }
    This will basically check if the number is an int, and if it is it will return true.

    Now the commands.

    Code (Text):
    public class Commands implements CommandExecutor {

        private String prefixT = "§3Teams§8§l▐";

        @Override
        public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) {

            if (!(commandSender instanceof Player)) {
                commandSender.sendMessage("§4The console does not have permission to use this command!");
            } else {
                Player p = (Player) commandSender;

                if (command.getName().equalsIgnoreCase("uhc")) {

                    if (strings.length > 2) {
                        p.sendMessage("§4You can use the command like this: /uhc team <amountOfTeams> <amountPerTeam>");
                    } else {

                        if (strings[1].equalsIgnoreCase("team")) {

                            if (isNumber(strings[2])) {

                                if(isNumber(strings[3])) {

                                    TeamManager.getInstance().createTeamsRandom(strings[2]);
                                    TeamManager.getInstance().addPlayersRandom(strings[3]);
                                    TeamManager.getInstance().addColorsRandom();
                                }
                                else {
                                    p.sendMessage("§4You can use the command like this: /uhc team <amountOfTeams> <amountPerTeam>");
                                }
                        }
                            else {
                                p.sendMessage("§4You can use the command like this: /uhc team <amountOfTeams> <amountPerTeam>");
                            }
                    }
                        else {
                            p.sendMessage("§4You can use the command like this: /uhc team <amountOfTeams> <amountPerTeam>");
                        }

                        }
                }
            }
        }

        public static boolean isNumber(String s) {
            try {
                Integer.parseInt(s);
            } catch (NumberFormatException nfe) {
                return false;
            }
            return true;
        }

    }
     
    This will basically check if the numbers are ints and if everything is correct, it will run those methods and everything should work perfectly:

    Final think is setting an executor for the command:

    Code (Text):
    @Override
        public void onEnable() {

            getCommand("uhc").setExecutor(new Commands());

            getServer().getPluginManager().registerEvents(new Events(), this);

        }
    And this is it.

    Thank you for taking the time, and I hope you enjoyed the tutorial.

    If there is anything I can fix, don't mind commenting it, and I'll change it immediately.

    Again this is my first tutorial every, so thanks for reading it.
     
    • Like Like x 1
  2. Use ChatColor.(COLOR) instead of §
     
    • Funny Funny x 1
  3. Is there any difference?

    They have the same result, but is there any difference to speed?
     
    • Like Like x 1
  4. Well, it is more easy to do ChatColor.(COLOR) instead of §

    @danenco How is that funny?
     
  5. @danenco How is that funny?[/QUOTE]

    He did that probably accidentally.
     
  6. It's easier to do alt + 21 when you get the hang of it. However, IMO it doesn't look very professional. I also switched drives so I saved my entire workspace to a USB, when transfered to my new hard drive I had to get rid of all the alt + 21's because they got messed up.
     
  7. If the symbol changes your plugin will not work while ours will.
     
  8. True.