diff --git a/src/main/java/net/minestom/server/event/server/StatusRequestEvent.java b/src/main/java/net/minestom/server/event/server/StatusRequestEvent.java new file mode 100644 index 000000000..f6fa0e1cb --- /dev/null +++ b/src/main/java/net/minestom/server/event/server/StatusRequestEvent.java @@ -0,0 +1,61 @@ +package net.minestom.server.event.server; + +import net.minestom.server.event.CancellableEvent; +import net.minestom.server.event.Event; +import net.minestom.server.network.player.PlayerConnection; +import net.minestom.server.ping.ResponseData; +import org.jetbrains.annotations.NotNull; + + +/** + * Called by a StatusRequestPacket. + * Can be used to modify the {@link ResponseData} response data. + */ +public class StatusRequestEvent extends Event implements CancellableEvent { + + private final ResponseData responseData; + private final PlayerConnection connection; + + private boolean cancelled; + + public StatusRequestEvent(@NotNull ResponseData responseData, @NotNull PlayerConnection connection) { + this.responseData = responseData; + this.connection = connection; + } + + /** + * ResponseData being returned. + * + * @return the response data being returned + */ + public ResponseData getResponseData() { + return responseData; + } + + /** + * PlayerConnection of received packet. + * + * Note that the player has not joined the server at this time. + * + * @return the playerConnection. + */ + + public PlayerConnection getConnection() { + return connection; + } + + @Override + public boolean isCancelled() { + return cancelled; + } + + /** + * Cancelling this event will cause you server to appear offline in the vanilla server list. + * + * @param cancel true if the event should be cancelled, false otherwise + */ + @Override + public void setCancelled(boolean cancel) { + this.cancelled = cancel; + } +} diff --git a/src/main/java/net/minestom/server/network/packet/client/status/StatusRequestPacket.java b/src/main/java/net/minestom/server/network/packet/client/status/StatusRequestPacket.java index 24da7a1f0..0e130db92 100644 --- a/src/main/java/net/minestom/server/network/packet/client/status/StatusRequestPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/status/StatusRequestPacket.java @@ -2,6 +2,7 @@ package net.minestom.server.network.packet.client.status; import net.kyori.adventure.text.Component; import net.minestom.server.MinecraftServer; +import net.minestom.server.event.server.StatusRequestEvent; import net.minestom.server.network.packet.client.ClientPreplayPacket; import net.minestom.server.network.packet.server.handshake.ResponsePacket; import net.minestom.server.network.player.PlayerConnection; @@ -29,10 +30,17 @@ public class StatusRequestPacket implements ClientPreplayPacket { if (consumer != null) consumer.accept(connection, responseData); - ResponsePacket responsePacket = new ResponsePacket(); - responsePacket.jsonResponse = responseData.build().toString(); + // Call event + StatusRequestEvent statusRequestEvent = new StatusRequestEvent(responseData, connection); + MinecraftServer.getGlobalEventHandler().callEvent(StatusRequestEvent.class, statusRequestEvent); - connection.sendPacket(responsePacket); + // Send packet only if event has not been cancelled + if (!statusRequestEvent.isCancelled()) { + ResponsePacket responsePacket = new ResponsePacket(); + responsePacket.jsonResponse = responseData.build().toString(); + + connection.sendPacket(responsePacket); + } } @Override diff --git a/src/test/java/demo/Main.java b/src/test/java/demo/Main.java index c357c124d..75b636dc5 100644 --- a/src/test/java/demo/Main.java +++ b/src/test/java/demo/Main.java @@ -8,14 +8,18 @@ import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; import net.minestom.server.MinecraftServer; import net.minestom.server.command.CommandManager; +import net.minestom.server.event.server.StatusRequestEvent; 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 java.util.UUID; + public class Main { @@ -60,6 +64,12 @@ public class Main { MinecraftServer.getSchedulerManager().buildShutdownTask(() -> System.out.println("Good night")).schedule(); + MinecraftServer.getGlobalEventHandler().addEventCallback(StatusRequestEvent.class, event -> { + ResponseData responseData = event.getResponseData(); + responseData.addPlayer("IP test: " + event.getConnection().getRemoteAddress().toString(), UUID.randomUUID()); + responseData.addPlayer("Use " + (char)0x00a7 + "6section characters for formatting: (char)0x00a7", UUID.randomUUID()); + }); + PlayerInit.init(); OptifineSupport.enable(); diff --git a/src/test/java/demo/PlayerInit.java b/src/test/java/demo/PlayerInit.java index 64d50e014..461e0d82e 100644 --- a/src/test/java/demo/PlayerInit.java +++ b/src/test/java/demo/PlayerInit.java @@ -3,6 +3,7 @@ package demo; import demo.generator.ChunkGeneratorDemo; import demo.generator.NoiseTestGenerator; import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.TextColor; import net.minestom.server.MinecraftServer; import net.minestom.server.adventure.audience.Audiences; import net.minestom.server.benchmark.BenchmarkManager; @@ -252,9 +253,12 @@ public class PlayerInit { return (playerConnection, responseData) -> { responseData.setMaxPlayer(0); responseData.setOnline(MinecraftServer.getConnectionManager().getOnlinePlayers().size()); - responseData.addPlayer("A name", UUID.randomUUID()); - responseData.addPlayer("Could be some message", UUID.randomUUID()); - responseData.setDescription("IP test: " + playerConnection.getRemoteAddress()); + responseData.addPlayer("The first line is separated from the others", UUID.randomUUID()); + responseData.addPlayer("Could be a name, or a message", UUID.randomUUID()); + responseData.setDescription(Component.text("You can do ") + .append(Component.text("RGB", TextColor.color(0x66b3ff))) + .append(Component.text(" color here"))); + }; }