001    package org.bukkit.event.server;
002    
003    import java.net.InetAddress;
004    import java.util.Iterator;
005    
006    import org.apache.commons.lang.Validate;
007    import org.bukkit.entity.Player;
008    import org.bukkit.event.HandlerList;
009    import org.bukkit.util.CachedServerIcon;
010    
011    /**
012     * Called when a server list ping is coming in. Displayed players can be
013     * checked and removed by {@link #iterator() iterating} over this event.
014     */
015    public class ServerListPingEvent extends ServerEvent implements Iterable<Player> {
016        private static final int MAGIC_PLAYER_COUNT = Integer.MIN_VALUE;
017        private static final HandlerList handlers = new HandlerList();
018        private final InetAddress address;
019        private String motd;
020        private final int numPlayers;
021        private int maxPlayers;
022    
023        public ServerListPingEvent(final InetAddress address, final String motd, final int numPlayers, final int maxPlayers) {
024            Validate.isTrue(numPlayers >= 0, "Cannot have negative number of players online", numPlayers);
025            this.address = address;
026            this.motd = motd;
027            this.numPlayers = numPlayers;
028            this.maxPlayers = maxPlayers;
029        }
030    
031        /**
032         * This constructor is intended for implementations that provide the
033         * {@link #iterator()} method, thus provided the {@link #getNumPlayers()}
034         * count.
035         */
036        protected ServerListPingEvent(final InetAddress address, final String motd, final int maxPlayers) {
037            this.numPlayers = MAGIC_PLAYER_COUNT;
038            this.address = address;
039            this.motd = motd;
040            this.maxPlayers = maxPlayers;
041        }
042    
043        /**
044         * Get the address the ping is coming from.
045         *
046         * @return the address
047         */
048        public InetAddress getAddress() {
049            return address;
050        }
051    
052        /**
053         * Get the message of the day message.
054         *
055         * @return the message of the day
056         */
057        public String getMotd() {
058            return motd;
059        }
060    
061        /**
062         * Change the message of the day message.
063         *
064         * @param motd the message of the day
065         */
066        public void setMotd(String motd) {
067            this.motd = motd;
068        }
069    
070        /**
071         * Get the number of players sent.
072         *
073         * @return the number of players
074         */
075        public int getNumPlayers() {
076            int numPlayers = this.numPlayers;
077            if (numPlayers == MAGIC_PLAYER_COUNT) {
078                numPlayers = 0;
079                for (@SuppressWarnings("unused") final Player player : this) {
080                    numPlayers++;
081                }
082            }
083            return numPlayers;
084        }
085    
086        /**
087         * Get the maximum number of players sent.
088         *
089         * @return the maximum number of players
090         */
091        public int getMaxPlayers() {
092            return maxPlayers;
093        }
094    
095        /**
096         * Set the maximum number of players sent.
097         *
098         * @param maxPlayers the maximum number of player
099         */
100        public void setMaxPlayers(int maxPlayers) {
101            this.maxPlayers = maxPlayers;
102        }
103    
104        /**
105         * Sets the server-icon sent to the client.
106         *
107         * @param icon the icon to send to the client
108         * @throws IllegalArgumentException if the {@link CachedServerIcon} is not
109         *     created by the caller of this event; null may be accepted for some
110         *     implementations
111         * @throws UnsupportedOperationException if the caller of this event does
112         *     not support setting the server icon
113         */
114        public void setServerIcon(CachedServerIcon icon) throws IllegalArgumentException, UnsupportedOperationException {
115            throw new UnsupportedOperationException();
116        }
117    
118        @Override
119        public HandlerList getHandlers() {
120            return handlers;
121        }
122    
123        public static HandlerList getHandlerList() {
124            return handlers;
125        }
126    
127        /**
128         * {@inheritDoc}
129         * <p>
130         * Calling the {@link Iterator#remove()} method will force that particular
131         * player to not be displayed on the player list, decrease the size
132         * returned by {@link #getNumPlayers()}, and will not be returned again by
133         * any new iterator.
134         *
135         * @throws UnsupportedOperationException if the caller of this event does
136         *     not support removing players
137         */
138        @Override
139        public Iterator<Player> iterator() throws UnsupportedOperationException {
140            throw new UnsupportedOperationException();
141        }
142    }