Skip to content

Instantly share code, notes, and snippets.

@Jofkos Jofkos/PlayerList.java
Last active Aug 29, 2015

Embed
What would you like to do?
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
public class PlayerList implements Iterable<Player> {
private List<String> players = new ArrayList<String>();
public PlayerList() {}
public PlayerList(final List<String> players) {
players.addAll(players);
}
public void add(Player... players) {
Validate.notNull(players, "Players cannot be null");
for (Player p : players) {
this.players.add(p.getName());
}
}
public void add(Collection<Player> players) {
Validate.notNull(players, "Players cannot be null");
for (Player p : players) {
this.players.add(p.getName());
}
}
public void addByName(Collection<String> players) {
Validate.notNull(players, "Players cannot be null");
players.addAll(players);
}
public void remove(Player... players) {
Validate.notNull(players, "Players cannot be null");
for (Player p : players) {
this.players.remove(p.getName());
}
}
public boolean contains(Player player) {
Validate.notNull(players, "Player cannot be null");
return players.contains(player.getName());
}
public List<String> getNameList() {
return new ArrayList<String>(players);
}
public void shuffle() {
Collections.shuffle(players);
}
public void clear() {
players.clear();
}
public int size() {
return players.size();
}
public Iterator<Player> iterator() {
return new PlayerListIterator(players);
}
private class PlayerListIterator implements Iterator<Player> {
private final List<String> players, playersFinal;
private int cursor = 0;
public PlayerListIterator(List<String> players) {
this.players = players;
this.playersFinal = new ArrayList<String>(players);
}
public boolean hasNext() {
return cursor < playersFinal.size();
}
public Player next() {
return Bukkit.getPlayer(playersFinal.get(cursor++));
}
public void remove() {
players.remove(playersFinal.get(cursor));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.