22
33import com .google .common .base .Preconditions ;
44import java .net .InetAddress ;
5+ import java .net .InetSocketAddress ;
6+ import java .net .SocketAddress ;
57import java .util .Iterator ;
68import net .kyori .adventure .text .Component ;
79import net .kyori .adventure .text .serializer .legacy .LegacyComponentSerializer ;
@@ -28,14 +30,14 @@ public class ServerListPingEvent extends ServerEvent implements Iterable<Player>
2830 private static final HandlerList HANDLER_LIST = new HandlerList ();
2931
3032 private final String hostname ;
31- private final InetAddress address ;
33+ private final SocketAddress address ;
3234 private final int numPlayers ;
3335 private Component motd ;
3436 private int maxPlayers ;
3537
3638 @ ApiStatus .Internal
3739 @ Deprecated (forRemoval = true )
38- public ServerListPingEvent (@ NotNull final String hostname , @ NotNull final InetAddress address , @ NotNull final String motd , final int numPlayers , final int maxPlayers ) {
40+ public ServerListPingEvent (@ NotNull final String hostname , @ NotNull final SocketAddress address , @ NotNull final String motd , final int numPlayers , final int maxPlayers ) {
3941 super (true );
4042 Preconditions .checkArgument (numPlayers >= 0 , "Cannot have negative number of players online" , numPlayers );
4143 this .hostname = hostname ;
@@ -47,7 +49,7 @@ public ServerListPingEvent(@NotNull final String hostname, @NotNull final InetAd
4749
4850 @ ApiStatus .Internal
4951 @ Deprecated (forRemoval = true )
50- protected ServerListPingEvent (@ NotNull final String hostname , @ NotNull final InetAddress address , @ NotNull final String motd , final int maxPlayers ) {
52+ protected ServerListPingEvent (@ NotNull final String hostname , @ NotNull final SocketAddress address , @ NotNull final String motd , final int maxPlayers ) {
5153 super (true );
5254 this .numPlayers = MAGIC_PLAYER_COUNT ;
5355 this .hostname = hostname ;
@@ -59,11 +61,11 @@ protected ServerListPingEvent(@NotNull final String hostname, @NotNull final Ine
5961 @ ApiStatus .Internal
6062 @ Deprecated (forRemoval = true )
6163 public ServerListPingEvent (@ NotNull final InetAddress address , @ NotNull final Component motd , final int numPlayers , final int maxPlayers ) {
62- this ("" , address , motd , numPlayers , maxPlayers );
64+ this ("" , new InetSocketAddress ( address , 0 ) , motd , numPlayers , maxPlayers );
6365 }
6466
6567 @ ApiStatus .Internal
66- public ServerListPingEvent (@ NotNull final String hostname , @ NotNull final InetAddress address , @ NotNull final Component motd , final int numPlayers , final int maxPlayers ) {
68+ public ServerListPingEvent (@ NotNull final String hostname , @ NotNull final SocketAddress address , @ NotNull final Component motd , final int numPlayers , final int maxPlayers ) {
6769 super (true );
6870 this .hostname = hostname ;
6971 this .address = address ;
@@ -75,7 +77,7 @@ public ServerListPingEvent(@NotNull final String hostname, @NotNull final InetAd
7577 @ ApiStatus .Internal
7678 @ Deprecated (forRemoval = true )
7779 protected ServerListPingEvent (@ NotNull final InetAddress address , @ NotNull final Component motd , final int maxPlayers ) {
78- this ("" , address , motd , maxPlayers );
80+ this ("" , new InetSocketAddress ( address , 0 ) , motd , maxPlayers );
7981 }
8082
8183 /*
@@ -84,7 +86,7 @@ protected ServerListPingEvent(@NotNull final InetAddress address, @NotNull final
8486 * count.
8587 */
8688 @ ApiStatus .Internal
87- protected ServerListPingEvent (final @ NotNull String hostname , final @ NotNull InetAddress address , final @ NotNull Component motd , final int maxPlayers ) {
89+ protected ServerListPingEvent (final @ NotNull String hostname , final @ NotNull SocketAddress address , final @ NotNull Component motd , final int maxPlayers ) {
8890 this .numPlayers = MAGIC_PLAYER_COUNT ;
8991 this .hostname = hostname ;
9092 this .address = address ;
@@ -103,14 +105,26 @@ public String getHostname() {
103105 return this .hostname ;
104106 }
105107
108+ /**
109+ * Gets the socket address the ping is coming from.
110+ *
111+ * @return the socket address
112+ */
113+ @ NotNull
114+ public SocketAddress getSocketAddress () {
115+ return this .address ;
116+ }
117+
106118 /**
107119 * Get the address the ping is coming from.
108120 *
109- * @return the address
121+ * @return the socket address, or the loopback address if the ping is
122+ * coming from a Unix socket
110123 */
111124 @ NotNull
112125 public InetAddress getAddress () {
113- return this .address ;
126+ if (this .address instanceof InetSocketAddress inet ) return inet .getAddress ();
127+ return InetAddress .getLoopbackAddress ();
114128 }
115129
116130 /**
0 commit comments