Last active
August 29, 2015 14:04
-
-
Save Jofkos/03999c047339f1763815 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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