First off I'll create some Player and Team objects (you probably have something like these already).
public class Player {
private String name;
public Player(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
A team is just a list of players. We will consider it "complete" when there are at least 2 players, and "full" when there are 4. Our Team class will implement compareTo so we can sort a list of teams by their size.
public class Team implements Comparable<Team> {
private String name;
private List<Player> players;
public Team(String name) {
this.name = name;
this.players = new ArrayList<Player>();
}
public String getName() {
return name;
}
public List<Player> getPlayers() {
return players;
}
public int getSize() {
return players.size();
}
public boolean isFull() {
return (players.size() >= 4);
}
public boolean isComplete() {
return (players.size() >= 2);
}
public void add( Player player )
{
players.add(player);
}
@Override
public int compareTo(Team otherTeam) {
int thisSize = getSize();
int otherSize = otherTeam.getSize();
return (thisSize == otherSize) ? 0 :
(thisSize > otherSize) ? 1 : -1;
}
}
Now we can create some teams ...
List<Team> teams = new ArrayList<Team>();
teams.add( new Team("Red Team") );
teams.add( new Team("Blue Team") );
teams.add( new Team("Green Team") );
... and some players. All players should start off on the "noteam" list.
List<Player> noteam = new ArrayList<Player>();
for (int i = 0; i < 10; i++) {
noteam.add( new Player("Player " + i) ); // create some players
}
To place a player onto a team ... we need to determine the smallest team that isn't full. We'll list all teams that aren't full, and then sort them by number of players.
for (Player player : noteam) { // add players to teams
List<Team> potentialTeams = new ArrayList<Team>();
for (Team team : teams) {
if (!team.isFull()) {
potentialTeams.add(team);
}
}
if (potentialTeams.isEmpty()) {
// cannot add player because all teams are full - we could do something about it here
break;
} else {
Collections.sort(potentialTeams);
Team smallestTeam = potentialTeams.get(0);
smallestTeam.add(player);
}
}
This is just one way of going about this, and its only an example. Also, nothing about this answer is specific to the "Minecraft Bukkit Plugin".