summaryrefslogtreecommitdiffstats
path: root/src/main
diff options
context:
space:
mode:
authorWesley Wolfe <weswolf@aol.com>2014-01-18 20:33:44 -0600
committerWesley Wolfe <weswolf@aol.com>2014-01-19 15:56:09 -0600
commit2a2e90233e88d825cd29e352b6c903096c4c6828 (patch)
treea7c8ef400d7d89364e3d09baa4a3a17d4e572809 /src/main
parent520168827ce49953ca73ac83faa6af95d9d40ee6 (diff)
downloadbukkit-2a2e90233e88d825cd29e352b6c903096c4c6828.tar
bukkit-2a2e90233e88d825cd29e352b6c903096c4c6828.tar.gz
bukkit-2a2e90233e88d825cd29e352b6c903096c4c6828.tar.lz
bukkit-2a2e90233e88d825cd29e352b6c903096c4c6828.tar.xz
bukkit-2a2e90233e88d825cd29e352b6c903096c4c6828.zip
Add ServerListPingEvent player list API. Adds BUKKIT-5121, BUKKIT-2465
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/org/bukkit/event/server/ServerListPingEvent.java45
1 files changed, 43 insertions, 2 deletions
diff --git a/src/main/java/org/bukkit/event/server/ServerListPingEvent.java b/src/main/java/org/bukkit/event/server/ServerListPingEvent.java
index 5c368b6c..c61afdf7 100644
--- a/src/main/java/org/bukkit/event/server/ServerListPingEvent.java
+++ b/src/main/java/org/bukkit/event/server/ServerListPingEvent.java
@@ -1,14 +1,19 @@
package org.bukkit.event.server;
import java.net.InetAddress;
+import java.util.Iterator;
+import org.apache.commons.lang.Validate;
+import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.bukkit.util.CachedServerIcon;
/**
- * Called when a server list ping is coming in.
+ * Called when a server list ping is coming in. Displayed players can be
+ * checked and removed by {@link #iterator() iterating} over this event.
*/
-public class ServerListPingEvent extends ServerEvent {
+public class ServerListPingEvent extends ServerEvent implements Iterable<Player> {
+ private static final int MAGIC_PLAYER_COUNT = Integer.MIN_VALUE;
private static final HandlerList handlers = new HandlerList();
private final InetAddress address;
private String motd;
@@ -16,6 +21,7 @@ public class ServerListPingEvent extends ServerEvent {
private int maxPlayers;
public ServerListPingEvent(final InetAddress address, final String motd, final int numPlayers, final int maxPlayers) {
+ Validate.isTrue(numPlayers >= 0, "Cannot have negative number of players online", numPlayers);
this.address = address;
this.motd = motd;
this.numPlayers = numPlayers;
@@ -23,6 +29,18 @@ public class ServerListPingEvent extends ServerEvent {
}
/**
+ * This constructor is intended for implementations that provide the
+ * {@link #iterator()} method, thus provided the {@link #getNumPlayers()}
+ * count.
+ */
+ protected ServerListPingEvent(final InetAddress address, final String motd, final int maxPlayers) {
+ this.numPlayers = MAGIC_PLAYER_COUNT;
+ this.address = address;
+ this.motd = motd;
+ this.maxPlayers = maxPlayers;
+ }
+
+ /**
* Get the address the ping is coming from.
*
* @return the address
@@ -55,6 +73,13 @@ public class ServerListPingEvent extends ServerEvent {
* @return the number of players
*/
public int getNumPlayers() {
+ int numPlayers = this.numPlayers;
+ if (numPlayers == MAGIC_PLAYER_COUNT) {
+ numPlayers = 0;
+ for (@SuppressWarnings("unused") final Player player : this) {
+ numPlayers++;
+ }
+ }
return numPlayers;
}
@@ -98,4 +123,20 @@ public class ServerListPingEvent extends ServerEvent {
public static HandlerList getHandlerList() {
return handlers;
}
+
+ /**
+ * {@inheritDoc}
+ * <p>
+ * Calling the {@link Iterator#remove()} method will force that particular
+ * player to not be displayed on the player list, decrease the size
+ * returned by {@link #getNumPlayers()}, and will not be returned again by
+ * any new iterator.
+ *
+ * @throws UnsupportedOperationException if the caller of this event does
+ * not support removing players
+ */
+ @Override
+ public Iterator<Player> iterator() throws UnsupportedOperationException {
+ throw new UnsupportedOperationException();
+ }
}