Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
ServerListPingEvent.java
1 package org.bukkit.event.server;
2 
3 import java.net.InetAddress;
4 import java.util.Iterator;
5 
6 import org.apache.commons.lang.Validate;
7 import org.bukkit.entity.Player;
10 
11 /**
12  * Called when a server list ping is coming in. Displayed players can be
13  * checked and removed by {@link #iterator() iterating} over this event.
14  */
15 public class ServerListPingEvent extends ServerEvent implements Iterable<Player> {
16  private static final int MAGIC_PLAYER_COUNT = Integer.MIN_VALUE;
17  private static final HandlerList handlers = new HandlerList();
18  private final InetAddress address;
19  private String motd;
20  private final int numPlayers;
21  private int maxPlayers;
22 
23  public ServerListPingEvent(final InetAddress address, final String motd, final int numPlayers, final int maxPlayers) {
24  Validate.isTrue(numPlayers >= 0, "Cannot have negative number of players online", numPlayers);
25  this.address = address;
26  this.motd = motd;
27  this.numPlayers = numPlayers;
28  this.maxPlayers = maxPlayers;
29  }
30 
31  /**
32  * This constructor is intended for implementations that provide the
33  * {@link #iterator()} method, thus provided the {@link #getNumPlayers()}
34  * count.
35  */
36  protected ServerListPingEvent(final InetAddress address, final String motd, final int maxPlayers) {
37  this.numPlayers = MAGIC_PLAYER_COUNT;
38  this.address = address;
39  this.motd = motd;
40  this.maxPlayers = maxPlayers;
41  }
42 
43  /**
44  * Get the address the ping is coming from.
45  *
46  * @return the address
47  */
48  public InetAddress getAddress() {
49  return address;
50  }
51 
52  /**
53  * Get the message of the day message.
54  *
55  * @return the message of the day
56  */
57  public String getMotd() {
58  return motd;
59  }
60 
61  /**
62  * Change the message of the day message.
63  *
64  * @param motd the message of the day
65  */
66  public void setMotd(String motd) {
67  this.motd = motd;
68  }
69 
70  /**
71  * Get the number of players sent.
72  *
73  * @return the number of players
74  */
75  public int getNumPlayers() {
76  int numPlayers = this.numPlayers;
77  if (numPlayers == MAGIC_PLAYER_COUNT) {
78  numPlayers = 0;
79  for (@SuppressWarnings("unused") final Player player : this) {
80  numPlayers++;
81  }
82  }
83  return numPlayers;
84  }
85 
86  /**
87  * Get the maximum number of players sent.
88  *
89  * @return the maximum number of players
90  */
91  public int getMaxPlayers() {
92  return maxPlayers;
93  }
94 
95  /**
96  * Set the maximum number of players sent.
97  *
98  * @param maxPlayers the maximum number of player
99  */
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 }
ServerListPingEvent(final InetAddress address, final String motd, final int maxPlayers)