diff --git a/src/main/java/net/minestom/server/event/server/ServerListPingEvent.java b/src/main/java/net/minestom/server/event/server/ServerListPingEvent.java index fe3f60bc9..b12b238af 100644 --- a/src/main/java/net/minestom/server/event/server/ServerListPingEvent.java +++ b/src/main/java/net/minestom/server/event/server/ServerListPingEvent.java @@ -7,7 +7,9 @@ import net.minestom.server.event.Event; import net.minestom.server.network.player.PlayerConnection; import net.minestom.server.ping.HandshakeData; import net.minestom.server.ping.ResponseData; +import org.jetbrains.annotations.Nullable; +import java.util.Objects; import java.util.UUID; public class ServerListPingEvent extends Event implements CancellableEvent { @@ -133,6 +135,14 @@ public class ServerListPingEvent extends Event implements CancellableEvent { responseData.addPlayer(name, uuid); } + /** + * Adds a player to the response. + * {@link UUID#randomUUID()} is used as a default parameter for uuid + */ + public void addPlayer(String name) { + responseData.addPlayer(name, UUID.randomUUID()); + } + /** * Removes all of the ping players from this {@link #responseData#pingPlayers}. The {@link #responseData#pingPlayers} list * will be empty this call returns. @@ -161,5 +171,31 @@ public class ServerListPingEvent extends Event implements CancellableEvent { responseData.setFavicon(favicon); } + /** + * Get the server address a client used to connect. + * may be null + * @return the server address + */ + public @Nullable String getClientServerAddress() { + return Objects.requireNonNull(connection.getHandshakeData()).getServerAddress(); + } + + /** + * Get the server port a client used to connect. + * + * @return the server port + */ + public int getClientServerPort() { + return Objects.requireNonNull(connection.getHandshakeData()).getServerPort(); + } + + /** + * Get the protocol version a client used to connect. + * + * @return the protocol version + */ + public int getClientProtocolVersion() { + return Objects.requireNonNull(connection.getHandshakeData()).getProtocolVersion(); + } } diff --git a/src/test/java/demo/Main.java b/src/test/java/demo/Main.java index 9f422b721..82fc895ec 100644 --- a/src/test/java/demo/Main.java +++ b/src/test/java/demo/Main.java @@ -9,16 +9,15 @@ import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.TextColor; import net.minestom.server.MinecraftServer; import net.minestom.server.command.CommandManager; -import net.minestom.server.event.server.HandshakeEvent; -import net.minestom.server.event.server.StatusRequestEvent; +import net.minestom.server.event.server.ServerListPingEvent; import net.minestom.server.extras.optifine.OptifineSupport; import net.minestom.server.instance.block.BlockManager; import net.minestom.server.instance.block.rule.vanilla.RedstonePlacementRule; -import net.minestom.server.ping.ResponseData; import net.minestom.server.storage.StorageManager; import net.minestom.server.storage.systems.FileStorageSystem; import net.minestom.server.utils.time.TimeUnit; import net.minestom.server.utils.time.UpdateOption; +import org.w3c.dom.Text; import java.util.UUID; @@ -66,20 +65,38 @@ public class Main { MinecraftServer.getSchedulerManager().buildShutdownTask(() -> System.out.println("Good night")).schedule(); - MinecraftServer.getGlobalEventHandler().addEventCallback(StatusRequestEvent.class, event -> { + MinecraftServer.getGlobalEventHandler().addEventCallback(ServerListPingEvent.class, event -> { event.setMaxPlayer(0); event.setOnline(MinecraftServer.getConnectionManager().getOnlinePlayers().size()); event.addPlayer("The first line is separated from the others", UUID.randomUUID()); event.addPlayer("Could be a name, or a message", UUID.randomUUID()); - event.setDescription(Component.text("You can do ") - .append(Component.text("RGB", TextColor.color(0x66b3ff))) - .append(Component.text(" color here"))); - - event.addPlayer("IP test: " + event.getConnection().getRemoteAddress().toString(), UUID.randomUUID()); event.addPlayer("Use " + (char)0x00a7 + "7section characters", UUID.randomUUID()); event.addPlayer((char)0x00a7 + "7" + (char)0x00a7 + "ofor formatting" + (char)0x00a7 + "r: (" + (char)0x00a7 + "6char" + (char)0x00a7 + "r)" + (char)0x00a7 + "90x00a7", UUID.randomUUID()); + event.addPlayer("Connection Info:"); + String ip = event.getClientServerAddress(); + event.addPlayer((char)0x00a7 + "8- " + (char)0x00a7 +"7IP: " + (char)0x00a7 + "e" + (ip != null ? ip : "???")); + event.addPlayer((char)0x00a7 + "8- " + (char)0x00a7 +"7PORT: " + (char)0x00a7 + "e" + event.getClientServerPort()); + event.addPlayer((char)0x00a7 + "8- " + (char)0x00a7 +"7VERSION: " + (char)0x00a7 + "e" + event.getClientProtocolVersion()); + + // Check if client supports RGB color + if (event.getClientProtocolVersion() >= 713) { // Snapshot 20w17a + event.setDescription(Component.text("You can do ") + .append(Component.text("RGB", TextColor.color(0x66b3ff))) + .append(Component.text(" color here"))); + } else { + event.setDescription(Component.text("You can do ") + .append(Component.text("RGB", NamedTextColor.nearestTo(TextColor.color(0x66b3ff)))) + .append(Component.text(" color here,")) + .append(Component.newline()) + .append(Component.text("if you are on 1.16 or up")) + ); + } + + + + });