From cf4a8756eff22aab1b3a10979c11544142105c33 Mon Sep 17 00:00:00 2001
From: thiccaxe <51303986+thiccaxe@users.noreply.github.com>
Date: Sun, 4 Apr 2021 15:43:19 -0700
Subject: [PATCH 01/19] StatusRequestEvent
Added basic event
notes:
- minestom will only accept the 1.16.5 protocol version, as of now there is no way to get the protocol version from the event. Assume 1.16.5
- there is also no way to currently get the address the client used to ping the server, as that is sent in another packet and minestom does not currently pass that data through.
- favicon appears to not be working
---
.../event/server/StatusRequestEvent.java | 61 +++++++++++++++++++
.../client/status/StatusRequestPacket.java | 14 ++++-
src/test/java/demo/Main.java | 10 +++
src/test/java/demo/PlayerInit.java | 10 ++-
4 files changed, 89 insertions(+), 6 deletions(-)
create mode 100644 src/main/java/net/minestom/server/event/server/StatusRequestEvent.java
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")));
+
};
}
From 4f8e94dcf284ee6e98bd1eb997b2238408191a9c Mon Sep 17 00:00:00 2001
From: thiccaxe <51303986+thiccaxe@users.noreply.github.com>
Date: Sun, 4 Apr 2021 15:57:42 -0700
Subject: [PATCH 02/19] Note about favicon format
---
src/main/java/net/minestom/server/ping/ResponseData.java | 2 ++
src/test/java/demo/Main.java | 5 ++++-
2 files changed, 6 insertions(+), 1 deletion(-)
diff --git a/src/main/java/net/minestom/server/ping/ResponseData.java b/src/main/java/net/minestom/server/ping/ResponseData.java
index 8c5f43ed0..52fc674a5 100644
--- a/src/main/java/net/minestom/server/ping/ResponseData.java
+++ b/src/main/java/net/minestom/server/ping/ResponseData.java
@@ -146,6 +146,8 @@ public class ResponseData {
/**
* Sets the response favicon.
*
+ * MUST start with "data:image/png;base64,"
+ *
* @param favicon The favicon for the response data.
*/
public void setFavicon(String favicon) {
diff --git a/src/test/java/demo/Main.java b/src/test/java/demo/Main.java
index 75b636dc5..600bd8b4e 100644
--- a/src/test/java/demo/Main.java
+++ b/src/test/java/demo/Main.java
@@ -67,7 +67,10 @@ public class Main {
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());
+ responseData.addPlayer("Use " + (char)0x00a7 + "7section characters", UUID.randomUUID());
+ responseData.addPlayer((char)0x00a7 + "7" + (char)0x00a7 + "ofor formatting" + (char)0x00a7 + "r: (" + (char)0x00a7 + "6char" + (char)0x00a7 + "r)" + (char)0x00a7 + "90x00a7", UUID.randomUUID());
+
+
});
PlayerInit.init();
From 7c0fcadd4fd2ac3fd633df1c489f7e99f87c0b23 Mon Sep 17 00:00:00 2001
From: thiccaxe <51303986+thiccaxe@users.noreply.github.com>
Date: Sun, 4 Apr 2021 16:26:58 -0700
Subject: [PATCH 03/19] Add HandshakeEvent
---
.../server/event/server/HandshakeEvent.java | 51 +++++++++++++++++++
.../client/handshake/HandshakePacket.java | 6 ++-
src/test/java/demo/Main.java | 3 +-
3 files changed, 57 insertions(+), 3 deletions(-)
create mode 100644 src/main/java/net/minestom/server/event/server/HandshakeEvent.java
diff --git a/src/main/java/net/minestom/server/event/server/HandshakeEvent.java b/src/main/java/net/minestom/server/event/server/HandshakeEvent.java
new file mode 100644
index 000000000..4bbed4391
--- /dev/null
+++ b/src/main/java/net/minestom/server/event/server/HandshakeEvent.java
@@ -0,0 +1,51 @@
+package net.minestom.server.event.server;
+
+import net.minestom.server.event.Event;
+import net.minestom.server.network.player.PlayerConnection;
+import net.minestom.server.ping.ResponseData;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+
+/**
+ * Called by a HandshakePacket.
+ */
+public class HandshakeEvent extends Event {
+
+ private final String serverAddress;
+ private final int serverPort;
+ private final int protocolVersion;
+ private final PlayerConnection connection;
+
+ public HandshakeEvent(@Nullable String serverAddress, int serverPort, int protocolVersion, @NotNull PlayerConnection connection) {
+ this.serverAddress = serverAddress;
+ this.serverPort = serverPort;
+ this.protocolVersion = protocolVersion;
+ this.connection = connection;
+ }
+
+ /**
+ * @return the server address a client used to connect to the server
+ */
+ public @Nullable String getServerAddress() {
+ return serverAddress;
+ }
+ /**
+ * @return the server port a client used to connect to the server
+ */
+ public int getServerPort() {
+ return serverPort;
+ }
+ /**
+ * @return the protocol version a client used when connecting to the server
+ */
+ public int getProtocolVersion() {
+ return protocolVersion;
+ }
+ /**
+ * @return the player connection
+ */
+ public @NotNull PlayerConnection getConnection() {
+ return connection;
+ }
+}
diff --git a/src/main/java/net/minestom/server/network/packet/client/handshake/HandshakePacket.java b/src/main/java/net/minestom/server/network/packet/client/handshake/HandshakePacket.java
index efe60b9b8..d0c14820d 100644
--- a/src/main/java/net/minestom/server/network/packet/client/handshake/HandshakePacket.java
+++ b/src/main/java/net/minestom/server/network/packet/client/handshake/HandshakePacket.java
@@ -4,6 +4,7 @@ import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.minestom.server.MinecraftServer;
import net.minestom.server.entity.PlayerSkin;
+import net.minestom.server.event.server.HandshakeEvent;
import net.minestom.server.extras.bungee.BungeeCordProxy;
import net.minestom.server.network.ConnectionState;
import net.minestom.server.network.packet.client.ClientPreplayPacket;
@@ -81,20 +82,21 @@ public class HandshakePacket implements ClientPreplayPacket {
nettyPlayerConnection.UNSAFE_setBungeeUuid(playerUuid);
nettyPlayerConnection.UNSAFE_setBungeeSkin(playerSkin);
-
} else {
nettyPlayerConnection.sendPacket(new LoginDisconnectPacket(INVALID_BUNGEE_FORWARDING));
nettyPlayerConnection.disconnect();
return;
}
} else {
- // Happen when a client ping the server, ignore
+ // Happen when a client ping the server, trigger HandshakeEvent
+ MinecraftServer.getGlobalEventHandler().callEvent(HandshakeEvent.class, new HandshakeEvent(null, serverPort, protocolVersion, connection));
return;
}
}
switch (nextState) {
case 1:
+ MinecraftServer.getGlobalEventHandler().callEvent(HandshakeEvent.class, new HandshakeEvent(serverAddress, serverPort, protocolVersion, connection));
connection.setConnectionState(ConnectionState.STATUS);
break;
case 2:
diff --git a/src/test/java/demo/Main.java b/src/test/java/demo/Main.java
index 600bd8b4e..b3ed5b852 100644
--- a/src/test/java/demo/Main.java
+++ b/src/test/java/demo/Main.java
@@ -8,6 +8,7 @@ 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.HandshakeEvent;
import net.minestom.server.event.server.StatusRequestEvent;
import net.minestom.server.extras.optifine.OptifineSupport;
import net.minestom.server.instance.block.BlockManager;
@@ -70,7 +71,7 @@ public class Main {
responseData.addPlayer("Use " + (char)0x00a7 + "7section characters", UUID.randomUUID());
responseData.addPlayer((char)0x00a7 + "7" + (char)0x00a7 + "ofor formatting" + (char)0x00a7 + "r: (" + (char)0x00a7 + "6char" + (char)0x00a7 + "r)" + (char)0x00a7 + "90x00a7", UUID.randomUUID());
-
+
});
PlayerInit.init();
From 2dec3618361b52a3a3bc0b806c5c65f8c1c1d64c Mon Sep 17 00:00:00 2001
From: thiccaxe <51303986+thiccaxe@users.noreply.github.com>
Date: Sun, 4 Apr 2021 18:50:41 -0700
Subject: [PATCH 04/19] Deprecate ResponseDataConsumer
Not sure if this is how to correctly deprecate, please lmk!
---
.../net/minestom/server/MinecraftServer.java | 45 +++++++++++++++++--
.../server/ping/ResponseDataConsumer.java | 3 ++
2 files changed, 45 insertions(+), 3 deletions(-)
diff --git a/src/main/java/net/minestom/server/MinecraftServer.java b/src/main/java/net/minestom/server/MinecraftServer.java
index 8c3ce2903..c09bed0bc 100644
--- a/src/main/java/net/minestom/server/MinecraftServer.java
+++ b/src/main/java/net/minestom/server/MinecraftServer.java
@@ -135,7 +135,7 @@ public final class MinecraftServer {
private static int compressionThreshold = 256;
private static boolean packetCaching = true;
private static boolean groupedPacket = true;
- private static ResponseDataConsumer responseDataConsumer;
+ @Deprecated private static ResponseDataConsumer responseDataConsumer;
private static String brandName = "Minestom";
private static Difficulty difficulty = Difficulty.NORMAL;
private static LootTableManager lootTableManager;
@@ -616,7 +616,10 @@ public final class MinecraftServer {
* Gets the consumer executed to show server-list data.
*
* @return the response data consumer
+ *
+ * @deprecated listen to the {@link net.minestom.server.event.server.StatusRequestEvent} instead
*/
+ @Deprecated
public static ResponseDataConsumer getResponseDataConsumer() {
checkInitStatus(responseDataConsumer);
return responseDataConsumer;
@@ -742,7 +745,10 @@ public final class MinecraftServer {
* @param port the server port
* @param responseDataConsumer the response data consumer, can be null
* @throws IllegalStateException if called before {@link #init()} or if the server is already running
+ *
+ * @deprecated use {@link #start(String, int)} and listen to the {@link net.minestom.server.event.server.StatusRequestEvent} event instead of ResponseDataConsumer
*/
+ @Deprecated
public void start(@NotNull String address, int port, @Nullable ResponseDataConsumer responseDataConsumer) {
Check.stateCondition(!initialized, "#start can only be called after #init");
Check.stateCondition(started, "The server is already started");
@@ -780,13 +786,46 @@ public final class MinecraftServer {
/**
* Starts the server.
+ *
+ * It should be called after {@link #init()} and probably your own initialization code.
*
* @param address the server address
* @param port the server port
- * @see #start(String, int, ResponseDataConsumer)
+ * @throws IllegalStateException if called before {@link #init()} or if the server is already running
*/
public void start(@NotNull String address, int port) {
- start(address, port, null);
+ Check.stateCondition(!initialized, "#start can only be called after #init");
+ Check.stateCondition(started, "The server is already started");
+
+ MinecraftServer.started = true;
+
+ LOGGER.info("Starting Minestom server.");
+ MinecraftServer.responseDataConsumer = null;
+
+ updateManager.start();
+
+ // Init & start the TCP server
+ nettyServer.init();
+ nettyServer.start(address, port);
+
+ if (extensionManager.shouldLoadOnStartup()) {
+ final long loadStartTime = System.nanoTime();
+ // Load extensions
+ extensionManager.loadExtensions();
+ // Init extensions
+ extensionManager.getExtensions().forEach(Extension::preInitialize);
+ extensionManager.getExtensions().forEach(Extension::initialize);
+ extensionManager.getExtensions().forEach(Extension::postInitialize);
+
+ final double loadTime = MathUtils.round((System.nanoTime() - loadStartTime) / 1_000_000D, 2);
+ LOGGER.info("Extensions loaded in {}ms", loadTime);
+ } else {
+ LOGGER.warn("Extension loadOnStartup option is set to false, extensions are therefore neither loaded or initialized.");
+ }
+
+ LOGGER.info("Minestom server started successfully.");
+
+ commandManager.startConsoleThread();
}
/**
diff --git a/src/main/java/net/minestom/server/ping/ResponseDataConsumer.java b/src/main/java/net/minestom/server/ping/ResponseDataConsumer.java
index 251ef7179..2bc2a9b70 100644
--- a/src/main/java/net/minestom/server/ping/ResponseDataConsumer.java
+++ b/src/main/java/net/minestom/server/ping/ResponseDataConsumer.java
@@ -7,8 +7,11 @@ import net.minestom.server.network.player.PlayerConnection;
*
*
Can be specified in {@link net.minestom.server.MinecraftServer#start(String, int,
* ResponseDataConsumer)}.
+ *
+ * @deprecated listen to the {@link net.minestom.server.event.server.StatusRequestEvent} instead
*/
@FunctionalInterface
+@Deprecated
public interface ResponseDataConsumer {
/**
From 02982c0ee2ff95770f19eb4bcd58979eb2e2bc01 Mon Sep 17 00:00:00 2001
From: thiccaxe <51303986+thiccaxe@users.noreply.github.com>
Date: Sun, 4 Apr 2021 18:52:55 -0700
Subject: [PATCH 05/19] Shortcut methods in StatusRequestEvent
---
.../event/server/StatusRequestEvent.java | 121 ++++++++++++++++++
1 file changed, 121 insertions(+)
diff --git a/src/main/java/net/minestom/server/event/server/StatusRequestEvent.java b/src/main/java/net/minestom/server/event/server/StatusRequestEvent.java
index f6fa0e1cb..5d4a73246 100644
--- a/src/main/java/net/minestom/server/event/server/StatusRequestEvent.java
+++ b/src/main/java/net/minestom/server/event/server/StatusRequestEvent.java
@@ -1,11 +1,16 @@
package net.minestom.server.event.server;
+import net.kyori.adventure.text.Component;
+import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
+import net.minestom.server.entity.Player;
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;
+import java.util.UUID;
+
/**
* Called by a StatusRequestPacket.
@@ -58,4 +63,120 @@ public class StatusRequestEvent extends Event implements CancellableEvent {
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
+
+ // Shortcut Methods
+ /**
+ * Sets the name for the response.
+ *
+ * @param name The name for the response data.
+ * @deprecated Use {@link #setVersion(String)}
+ */
+ @Deprecated
+ public void setName(String name) {
+ responseData.setVersion(name);
+ }
+
+ /**
+ * Sets the version name for the response.
+ *
+ * @param version The version name for the response data.
+ */
+ public void setVersion(String version) {
+ responseData.setVersion(version);
+ }
+
+ /**
+ * Sets the response protocol version.
+ *
+ * @param protocol The protocol version for the response data.
+ */
+ public void setProtocol(int protocol) {
+ responseData.setProtocol(protocol);
+ }
+
+ /**
+ * Sets the response maximum player count.
+ *
+ * @param maxPlayer The maximum player count for the response data.
+ */
+ public void setMaxPlayer(int maxPlayer) {
+ responseData.setMaxPlayer(maxPlayer);
+ }
+
+ /**
+ * Sets the response online count.
+ *
+ * @param online The online count for the response data.
+ */
+ public void setOnline(int online) {
+ responseData.setOnline(online);
+ }
+
+ /**
+ * Adds some players to the response.
+ *
+ * @param players the players
+ */
+ public void addPlayer(Iterable players) {
+ for (Player player : players) {
+ addPlayer(player);
+ }
+ }
+
+ /**
+ * Adds a player to the response.
+ *
+ * @param player the player
+ */
+ public void addPlayer(Player player) {
+ addPlayer(player.getUsername(), player.getUuid());
+ }
+
+ /**
+ * Adds a player to the response.
+ *
+ * @param name The name of the player.
+ * @param uuid The unique identifier of the player.
+ */
+ public void addPlayer(String name, UUID uuid) {
+ }
+
+ /**
+ * Removes all of the ping players from this {@link #responseData#pingPlayers}. The {@link #responseData#pingPlayers} list
+ * will be empty this call returns.
+ */
+ public void clearPlayers() {
+ responseData.clearPlayers();
+ }
+
+ /**
+ * Sets the response description.
+ *
+ * @param description The description for the response data.
+ * @deprecated Use {@link #setDescription(Component)}
+ */
+ @Deprecated
+ public void setDescription(String description) {
+ responseData.setDescription(LegacyComponentSerializer.legacySection().deserialize(description));
+ }
+
+ /**
+ * Sets the response description.
+ *
+ * @param description The description for the response data.
+ */
+ public void setDescription(Component description) {
+ responseData.setDescription(description);
+ }
+
+ /**
+ * Sets the response favicon.
+ *
+ * MUST start with "data:image/png;base64,"
+ *
+ * @param favicon The favicon for the response data.
+ */
+ public void setFavicon(String favicon) {
+ responseData.setFavicon(favicon);
+ }
}
From 02538f0d311716abb3f99bff4d57aeb24cd6f6f9 Mon Sep 17 00:00:00 2001
From: thiccaxe <51303986+thiccaxe@users.noreply.github.com>
Date: Sun, 4 Apr 2021 18:58:05 -0700
Subject: [PATCH 06/19] Update Demo
reflect changes new deprecations in demo.
---
src/test/java/demo/Main.java | 19 ++++++++++++++-----
src/test/java/demo/PlayerInit.java | 12 ------------
2 files changed, 14 insertions(+), 17 deletions(-)
diff --git a/src/test/java/demo/Main.java b/src/test/java/demo/Main.java
index b3ed5b852..9f422b721 100644
--- a/src/test/java/demo/Main.java
+++ b/src/test/java/demo/Main.java
@@ -6,6 +6,7 @@ import demo.blocks.UpdatableBlockDemo;
import demo.commands.*;
import net.kyori.adventure.text.Component;
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;
@@ -66,10 +67,18 @@ 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 + "7section characters", UUID.randomUUID());
- responseData.addPlayer((char)0x00a7 + "7" + (char)0x00a7 + "ofor formatting" + (char)0x00a7 + "r: (" + (char)0x00a7 + "6char" + (char)0x00a7 + "r)" + (char)0x00a7 + "90x00a7", UUID.randomUUID());
+ 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());
});
@@ -83,7 +92,7 @@ public class Main {
//MojangAuth.init();
- minecraftServer.start("0.0.0.0", 25565, PlayerInit.getResponseDataConsumer());
+ minecraftServer.start("0.0.0.0", 25565);
//Runtime.getRuntime().addShutdownHook(new Thread(MinecraftServer::stopCleanly));
}
diff --git a/src/test/java/demo/PlayerInit.java b/src/test/java/demo/PlayerInit.java
index 461e0d82e..c13710d83 100644
--- a/src/test/java/demo/PlayerInit.java
+++ b/src/test/java/demo/PlayerInit.java
@@ -249,17 +249,5 @@ public class PlayerInit {
});
}
- public static ResponseDataConsumer getResponseDataConsumer() {
- return (playerConnection, responseData) -> {
- responseData.setMaxPlayer(0);
- responseData.setOnline(MinecraftServer.getConnectionManager().getOnlinePlayers().size());
- 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")));
-
- };
- }
}
From 56f87288904fe5433318d24672d81a2015ead45a Mon Sep 17 00:00:00 2001
From: thiccaxe <51303986+thiccaxe@users.noreply.github.com>
Date: Sun, 4 Apr 2021 19:53:01 -0700
Subject: [PATCH 07/19] Fix Deprecation of ResponseDataConsumer
---
src/main/java/net/minestom/server/MinecraftServer.java | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/main/java/net/minestom/server/MinecraftServer.java b/src/main/java/net/minestom/server/MinecraftServer.java
index c09bed0bc..557475e86 100644
--- a/src/main/java/net/minestom/server/MinecraftServer.java
+++ b/src/main/java/net/minestom/server/MinecraftServer.java
@@ -135,7 +135,7 @@ public final class MinecraftServer {
private static int compressionThreshold = 256;
private static boolean packetCaching = true;
private static boolean groupedPacket = true;
- @Deprecated private static ResponseDataConsumer responseDataConsumer;
+ private static ResponseDataConsumer responseDataConsumer;
private static String brandName = "Minestom";
private static Difficulty difficulty = Difficulty.NORMAL;
private static LootTableManager lootTableManager;
From 159ff3f646287d86d9f679b8ebc4652052b735a5 Mon Sep 17 00:00:00 2001
From: thiccaxe <51303986+thiccaxe@users.noreply.github.com>
Date: Mon, 5 Apr 2021 09:57:18 -0700
Subject: [PATCH 08/19] Remove reprecated code
Removed deprecated code,
Added missing code,
Optimized imports.
---
.../server/event/server/HandshakeEvent.java | 1 -
.../event/server/StatusRequestEvent.java | 27 ++-----------------
2 files changed, 2 insertions(+), 26 deletions(-)
diff --git a/src/main/java/net/minestom/server/event/server/HandshakeEvent.java b/src/main/java/net/minestom/server/event/server/HandshakeEvent.java
index 4bbed4391..c0b23356f 100644
--- a/src/main/java/net/minestom/server/event/server/HandshakeEvent.java
+++ b/src/main/java/net/minestom/server/event/server/HandshakeEvent.java
@@ -2,7 +2,6 @@ package net.minestom.server.event.server;
import net.minestom.server.event.Event;
import net.minestom.server.network.player.PlayerConnection;
-import net.minestom.server.ping.ResponseData;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
diff --git a/src/main/java/net/minestom/server/event/server/StatusRequestEvent.java b/src/main/java/net/minestom/server/event/server/StatusRequestEvent.java
index 5d4a73246..d3ca7c652 100644
--- a/src/main/java/net/minestom/server/event/server/StatusRequestEvent.java
+++ b/src/main/java/net/minestom/server/event/server/StatusRequestEvent.java
@@ -65,17 +65,6 @@ public class StatusRequestEvent extends Event implements CancellableEvent {
}
// Shortcut Methods
- /**
- * Sets the name for the response.
- *
- * @param name The name for the response data.
- * @deprecated Use {@link #setVersion(String)}
- */
- @Deprecated
- public void setName(String name) {
- responseData.setVersion(name);
- }
-
/**
* Sets the version name for the response.
*
@@ -118,9 +107,7 @@ public class StatusRequestEvent extends Event implements CancellableEvent {
* @param players the players
*/
public void addPlayer(Iterable players) {
- for (Player player : players) {
- addPlayer(player);
- }
+ responseData.addPlayer(players);
}
/**
@@ -139,6 +126,7 @@ public class StatusRequestEvent extends Event implements CancellableEvent {
* @param uuid The unique identifier of the player.
*/
public void addPlayer(String name, UUID uuid) {
+ responseData.addPlayer(name, uuid);
}
/**
@@ -149,17 +137,6 @@ public class StatusRequestEvent extends Event implements CancellableEvent {
responseData.clearPlayers();
}
- /**
- * Sets the response description.
- *
- * @param description The description for the response data.
- * @deprecated Use {@link #setDescription(Component)}
- */
- @Deprecated
- public void setDescription(String description) {
- responseData.setDescription(LegacyComponentSerializer.legacySection().deserialize(description));
- }
-
/**
* Sets the response description.
*
From cd27d1d6c7129f65b80bf0cbf00342d4ed254ac9 Mon Sep 17 00:00:00 2001
From: thiccaxe <51303986+thiccaxe@users.noreply.github.com>
Date: Thu, 8 Apr 2021 14:19:32 -0700
Subject: [PATCH 09/19] ServerListPingEvent
An implementation of what could be done to make this event - not perfect in my opinion.
---
.../server/event/server/HandshakeEvent.java | 50 -------------------
...estEvent.java => ServerListPingEvent.java} | 26 ++++++----
.../client/handshake/HandshakePacket.java | 10 ++--
.../client/status/StatusRequestPacket.java | 6 +--
.../network/player/PlayerConnection.java | 19 +++++++
.../minestom/server/ping/HandshakeData.java | 45 +++++++++++++++++
6 files changed, 88 insertions(+), 68 deletions(-)
delete mode 100644 src/main/java/net/minestom/server/event/server/HandshakeEvent.java
rename src/main/java/net/minestom/server/event/server/{StatusRequestEvent.java => ServerListPingEvent.java} (88%)
create mode 100644 src/main/java/net/minestom/server/ping/HandshakeData.java
diff --git a/src/main/java/net/minestom/server/event/server/HandshakeEvent.java b/src/main/java/net/minestom/server/event/server/HandshakeEvent.java
deleted file mode 100644
index c0b23356f..000000000
--- a/src/main/java/net/minestom/server/event/server/HandshakeEvent.java
+++ /dev/null
@@ -1,50 +0,0 @@
-package net.minestom.server.event.server;
-
-import net.minestom.server.event.Event;
-import net.minestom.server.network.player.PlayerConnection;
-import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
-
-
-/**
- * Called by a HandshakePacket.
- */
-public class HandshakeEvent extends Event {
-
- private final String serverAddress;
- private final int serverPort;
- private final int protocolVersion;
- private final PlayerConnection connection;
-
- public HandshakeEvent(@Nullable String serverAddress, int serverPort, int protocolVersion, @NotNull PlayerConnection connection) {
- this.serverAddress = serverAddress;
- this.serverPort = serverPort;
- this.protocolVersion = protocolVersion;
- this.connection = connection;
- }
-
- /**
- * @return the server address a client used to connect to the server
- */
- public @Nullable String getServerAddress() {
- return serverAddress;
- }
- /**
- * @return the server port a client used to connect to the server
- */
- public int getServerPort() {
- return serverPort;
- }
- /**
- * @return the protocol version a client used when connecting to the server
- */
- public int getProtocolVersion() {
- return protocolVersion;
- }
- /**
- * @return the player connection
- */
- public @NotNull PlayerConnection getConnection() {
- return connection;
- }
-}
diff --git a/src/main/java/net/minestom/server/event/server/StatusRequestEvent.java b/src/main/java/net/minestom/server/event/server/ServerListPingEvent.java
similarity index 88%
rename from src/main/java/net/minestom/server/event/server/StatusRequestEvent.java
rename to src/main/java/net/minestom/server/event/server/ServerListPingEvent.java
index d3ca7c652..fe3f60bc9 100644
--- a/src/main/java/net/minestom/server/event/server/StatusRequestEvent.java
+++ b/src/main/java/net/minestom/server/event/server/ServerListPingEvent.java
@@ -1,29 +1,23 @@
package net.minestom.server.event.server;
import net.kyori.adventure.text.Component;
-import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.minestom.server.entity.Player;
import net.minestom.server.event.CancellableEvent;
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.NotNull;
import java.util.UUID;
-
-/**
- * Called by a StatusRequestPacket.
- * Can be used to modify the {@link ResponseData} response data.
- */
-public class StatusRequestEvent extends Event implements CancellableEvent {
+public class ServerListPingEvent extends Event implements CancellableEvent {
+ private boolean cancelled = false;
private final ResponseData responseData;
private final PlayerConnection connection;
- private boolean cancelled;
- public StatusRequestEvent(@NotNull ResponseData responseData, @NotNull PlayerConnection connection) {
+ public ServerListPingEvent(ResponseData responseData, PlayerConnection connection) {
this.responseData = responseData;
this.connection = connection;
}
@@ -37,6 +31,16 @@ public class StatusRequestEvent extends Event implements CancellableEvent {
return responseData;
}
+ /**
+ * HandshakeData of previous handshake packet
+ *
+ * equivalent to {@link #getConnection()#getHandshakeData()}
+ * @return
+ */
+ public HandshakeData getHandshakeData() {
+ return connection.getHandshakeData();
+ }
+
/**
* PlayerConnection of received packet.
*
@@ -156,4 +160,6 @@ public class StatusRequestEvent extends Event implements CancellableEvent {
public void setFavicon(String favicon) {
responseData.setFavicon(favicon);
}
+
+
}
diff --git a/src/main/java/net/minestom/server/network/packet/client/handshake/HandshakePacket.java b/src/main/java/net/minestom/server/network/packet/client/handshake/HandshakePacket.java
index d0c14820d..e74f31601 100644
--- a/src/main/java/net/minestom/server/network/packet/client/handshake/HandshakePacket.java
+++ b/src/main/java/net/minestom/server/network/packet/client/handshake/HandshakePacket.java
@@ -4,13 +4,13 @@ import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.minestom.server.MinecraftServer;
import net.minestom.server.entity.PlayerSkin;
-import net.minestom.server.event.server.HandshakeEvent;
import net.minestom.server.extras.bungee.BungeeCordProxy;
import net.minestom.server.network.ConnectionState;
import net.minestom.server.network.packet.client.ClientPreplayPacket;
import net.minestom.server.network.packet.server.login.LoginDisconnectPacket;
import net.minestom.server.network.player.NettyPlayerConnection;
import net.minestom.server.network.player.PlayerConnection;
+import net.minestom.server.ping.HandshakeData;
import net.minestom.server.utils.binary.BinaryReader;
import net.minestom.server.utils.binary.BinaryWriter;
import org.jetbrains.annotations.NotNull;
@@ -88,15 +88,15 @@ public class HandshakePacket implements ClientPreplayPacket {
return;
}
} else {
- // Happen when a client ping the server, trigger HandshakeEvent
- MinecraftServer.getGlobalEventHandler().callEvent(HandshakeEvent.class, new HandshakeEvent(null, serverPort, protocolVersion, connection));
+ // Happen when a client ping the server, ignore
return;
}
}
-
+ connection.setHandshakeData(new HandshakeData(
+ serverAddress, serverPort, protocolVersion
+ ));
switch (nextState) {
case 1:
- MinecraftServer.getGlobalEventHandler().callEvent(HandshakeEvent.class, new HandshakeEvent(serverAddress, serverPort, protocolVersion, connection));
connection.setConnectionState(ConnectionState.STATUS);
break;
case 2:
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 0e130db92..b13b313a9 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,7 +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.event.server.ServerListPingEvent;
import net.minestom.server.network.packet.client.ClientPreplayPacket;
import net.minestom.server.network.packet.server.handshake.ResponsePacket;
import net.minestom.server.network.player.PlayerConnection;
@@ -31,8 +31,8 @@ public class StatusRequestPacket implements ClientPreplayPacket {
consumer.accept(connection, responseData);
// Call event
- StatusRequestEvent statusRequestEvent = new StatusRequestEvent(responseData, connection);
- MinecraftServer.getGlobalEventHandler().callEvent(StatusRequestEvent.class, statusRequestEvent);
+ ServerListPingEvent statusRequestEvent = new ServerListPingEvent(responseData, connection);
+ MinecraftServer.getGlobalEventHandler().callEvent(ServerListPingEvent.class, statusRequestEvent);
// Send packet only if event has not been cancelled
if (!statusRequestEvent.isCancelled()) {
diff --git a/src/main/java/net/minestom/server/network/player/PlayerConnection.java b/src/main/java/net/minestom/server/network/player/PlayerConnection.java
index e183c772a..392297f1c 100644
--- a/src/main/java/net/minestom/server/network/player/PlayerConnection.java
+++ b/src/main/java/net/minestom/server/network/player/PlayerConnection.java
@@ -9,6 +9,7 @@ import net.minestom.server.listener.manager.ServerPacketConsumer;
import net.minestom.server.network.ConnectionManager;
import net.minestom.server.network.ConnectionState;
import net.minestom.server.network.packet.server.ServerPacket;
+import net.minestom.server.ping.HandshakeData;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -26,6 +27,7 @@ public abstract class PlayerConnection {
private Player player;
private volatile ConnectionState connectionState;
+ private HandshakeData handshakeData;
private boolean online;
// Text used to kick client sending too many packets
@@ -170,6 +172,23 @@ public abstract class PlayerConnection {
return connectionState;
}
+ /**
+ * Gets the HandshakeData from the most recent HandshakePacket.
+ *
+ * @return the most recent HandshakeData
+ */
+ @Nullable
+ public HandshakeData getHandshakeData() {
+ return handshakeData;
+ }
+
+ /**
+ * Sets the HandshakeData - usually from a HandshakePacket
+ */
+ public void setHandshakeData(HandshakeData handshakeData) {
+ this.handshakeData = handshakeData;
+ }
+
/**
* Gets the number of packet the client sent over the last second.
*
diff --git a/src/main/java/net/minestom/server/ping/HandshakeData.java b/src/main/java/net/minestom/server/ping/HandshakeData.java
new file mode 100644
index 000000000..89a9a31d8
--- /dev/null
+++ b/src/main/java/net/minestom/server/ping/HandshakeData.java
@@ -0,0 +1,45 @@
+package net.minestom.server.ping;
+
+import org.jetbrains.annotations.Nullable;
+
+public final class HandshakeData {
+ private final @Nullable String serverAddress;
+ private final int serverPort;
+ private final int protocolVersion;
+
+ public HandshakeData(@Nullable String serverAddress, int serverPort, int protocolVersion) {
+ this.serverAddress = serverAddress;
+ this.serverPort = serverPort;
+ this.protocolVersion = protocolVersion;
+ }
+
+
+ /**
+ * Get the server address a client used to connect.
+ * may be null
+ * @return the server address
+ */
+ public @Nullable String getServerAddress() {
+ return serverAddress;
+ }
+
+ /**
+ * Get the server port a client used to connect.
+ *
+ * @return the server port
+ */
+ public int getServerPort() {
+ return serverPort;
+ }
+
+ /**
+ * Get the protocol version a client used to connect.
+ *
+ * @return the protocol version
+ */
+ public int getProtocolVersion() {
+ return protocolVersion;
+ }
+
+
+}
From cad128a39385462ca85a099f5b09efc176c3140d Mon Sep 17 00:00:00 2001
From: thiccaxe <51303986+thiccaxe@users.noreply.github.com>
Date: Thu, 8 Apr 2021 14:41:22 -0700
Subject: [PATCH 10/19] update demo to show example
may be cut down later as it is very excessive.
---
.../event/server/ServerListPingEvent.java | 36 +++++++++++++++++++
src/test/java/demo/Main.java | 35 +++++++++++++-----
2 files changed, 62 insertions(+), 9 deletions(-)
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"))
+ );
+ }
+
+
+
+
});
From 94c4ec79e1ff7a02cfb275ee21e4ed8df4d33fbf Mon Sep 17 00:00:00 2001
From: thiccaxe <51303986+thiccaxe@users.noreply.github.com>
Date: Thu, 8 Apr 2021 16:51:50 -0700
Subject: [PATCH 11/19] simplify data storage, fix annotations
Removed HandshakeData - too excessive
Added abstract methods in PlayerConnection as replacement
in FakePlayerConnection setters do nothing and Getters return MinecraftServer defaults
---
.../net/minestom/server/MinecraftServer.java | 33 +--------
.../event/server/ServerListPingEvent.java | 43 ++++++------
.../client/handshake/HandshakePacket.java | 12 +---
.../network/player/FakePlayerConnection.java | 25 +++++++
.../network/player/NettyPlayerConnection.java | 52 ++++++++++----
.../network/player/PlayerConnection.java | 67 ++++++++++++++-----
.../minestom/server/ping/HandshakeData.java | 45 -------------
src/test/java/demo/Main.java | 4 +-
8 files changed, 141 insertions(+), 140 deletions(-)
delete mode 100644 src/main/java/net/minestom/server/ping/HandshakeData.java
diff --git a/src/main/java/net/minestom/server/MinecraftServer.java b/src/main/java/net/minestom/server/MinecraftServer.java
index 557475e86..42e4d261c 100644
--- a/src/main/java/net/minestom/server/MinecraftServer.java
+++ b/src/main/java/net/minestom/server/MinecraftServer.java
@@ -750,38 +750,8 @@ public final class MinecraftServer {
*/
@Deprecated
public void start(@NotNull String address, int port, @Nullable ResponseDataConsumer responseDataConsumer) {
- Check.stateCondition(!initialized, "#start can only be called after #init");
- Check.stateCondition(started, "The server is already started");
-
- MinecraftServer.started = true;
-
- LOGGER.info("Starting Minestom server.");
MinecraftServer.responseDataConsumer = responseDataConsumer;
-
- updateManager.start();
-
- // Init & start the TCP server
- nettyServer.init();
- nettyServer.start(address, port);
-
- if (extensionManager.shouldLoadOnStartup()) {
- final long loadStartTime = System.nanoTime();
- // Load extensions
- extensionManager.loadExtensions();
- // Init extensions
- extensionManager.getExtensions().forEach(Extension::preInitialize);
- extensionManager.getExtensions().forEach(Extension::initialize);
- extensionManager.getExtensions().forEach(Extension::postInitialize);
-
- final double loadTime = MathUtils.round((System.nanoTime() - loadStartTime) / 1_000_000D, 2);
- LOGGER.info("Extensions loaded in {}ms", loadTime);
- } else {
- LOGGER.warn("Extension loadOnStartup option is set to false, extensions are therefore neither loaded or initialized.");
- }
-
- LOGGER.info("Minestom server started successfully.");
-
- commandManager.startConsoleThread();
+ start(address, port);
}
/**
@@ -800,7 +770,6 @@ public final class MinecraftServer {
MinecraftServer.started = true;
LOGGER.info("Starting Minestom server.");
- MinecraftServer.responseDataConsumer = null;
updateManager.start();
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 b12b238af..e40f0d8a4 100644
--- a/src/main/java/net/minestom/server/event/server/ServerListPingEvent.java
+++ b/src/main/java/net/minestom/server/event/server/ServerListPingEvent.java
@@ -1,14 +1,17 @@
package net.minestom.server.event.server;
import net.kyori.adventure.text.Component;
+import net.minestom.server.MinecraftServer;
import net.minestom.server.entity.Player;
import net.minestom.server.event.CancellableEvent;
import net.minestom.server.event.Event;
+import net.minestom.server.network.player.NettyPlayerConnection;
import net.minestom.server.network.player.PlayerConnection;
-import net.minestom.server.ping.HandshakeData;
import net.minestom.server.ping.ResponseData;
+import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
+import java.net.InetSocketAddress;
import java.util.Objects;
import java.util.UUID;
@@ -29,19 +32,10 @@ public class ServerListPingEvent extends Event implements CancellableEvent {
*
* @return the response data being returned
*/
- public ResponseData getResponseData() {
+ public @NotNull ResponseData getResponseData() {
return responseData;
}
- /**
- * HandshakeData of previous handshake packet
- *
- * equivalent to {@link #getConnection()#getHandshakeData()}
- * @return
- */
- public HandshakeData getHandshakeData() {
- return connection.getHandshakeData();
- }
/**
* PlayerConnection of received packet.
@@ -51,7 +45,7 @@ public class ServerListPingEvent extends Event implements CancellableEvent {
* @return the playerConnection.
*/
- public PlayerConnection getConnection() {
+ public @NotNull PlayerConnection getConnection() {
return connection;
}
@@ -76,7 +70,7 @@ public class ServerListPingEvent extends Event implements CancellableEvent {
*
* @param version The version name for the response data.
*/
- public void setVersion(String version) {
+ public void setVersion(@NotNull String version) {
responseData.setVersion(version);
}
@@ -112,7 +106,7 @@ public class ServerListPingEvent extends Event implements CancellableEvent {
*
* @param players the players
*/
- public void addPlayer(Iterable players) {
+ public void addPlayer(@NotNull Iterable players) {
responseData.addPlayer(players);
}
@@ -121,7 +115,7 @@ public class ServerListPingEvent extends Event implements CancellableEvent {
*
* @param player the player
*/
- public void addPlayer(Player player) {
+ public void addPlayer(@NotNull Player player) {
addPlayer(player.getUsername(), player.getUuid());
}
@@ -131,13 +125,15 @@ public class ServerListPingEvent extends Event implements CancellableEvent {
* @param name The name of the player.
* @param uuid The unique identifier of the player.
*/
- public void addPlayer(String name, UUID uuid) {
+ public void addPlayer(@NotNull String name, @NotNull UUID uuid) {
responseData.addPlayer(name, uuid);
}
/**
* Adds a player to the response.
* {@link UUID#randomUUID()} is used as a default parameter for uuid
+ *
+ * @param name The name of the player.
*/
public void addPlayer(String name) {
responseData.addPlayer(name, UUID.randomUUID());
@@ -173,11 +169,11 @@ public class ServerListPingEvent extends Event implements CancellableEvent {
/**
* 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();
+ public @Nullable String getRemoteServerAddress() {
+ return connection.getServerAddress();
}
/**
@@ -185,8 +181,8 @@ public class ServerListPingEvent extends Event implements CancellableEvent {
*
* @return the server port
*/
- public int getClientServerPort() {
- return Objects.requireNonNull(connection.getHandshakeData()).getServerPort();
+ public int getServerPort() {
+ return connection.getServerPort();
}
/**
@@ -195,7 +191,10 @@ public class ServerListPingEvent extends Event implements CancellableEvent {
* @return the protocol version
*/
public int getClientProtocolVersion() {
- return Objects.requireNonNull(connection.getHandshakeData()).getProtocolVersion();
+ if (connection instanceof NettyPlayerConnection) {
+ return connection.getProtocolVersion();
+ }
+ return MinecraftServer.PROTOCOL_VERSION;
}
}
diff --git a/src/main/java/net/minestom/server/network/packet/client/handshake/HandshakePacket.java b/src/main/java/net/minestom/server/network/packet/client/handshake/HandshakePacket.java
index e74f31601..88d201b0b 100644
--- a/src/main/java/net/minestom/server/network/packet/client/handshake/HandshakePacket.java
+++ b/src/main/java/net/minestom/server/network/packet/client/handshake/HandshakePacket.java
@@ -10,7 +10,6 @@ import net.minestom.server.network.packet.client.ClientPreplayPacket;
import net.minestom.server.network.packet.server.login.LoginDisconnectPacket;
import net.minestom.server.network.player.NettyPlayerConnection;
import net.minestom.server.network.player.PlayerConnection;
-import net.minestom.server.ping.HandshakeData;
import net.minestom.server.utils.binary.BinaryReader;
import net.minestom.server.utils.binary.BinaryWriter;
import org.jetbrains.annotations.NotNull;
@@ -67,7 +66,6 @@ public class HandshakePacket implements ClientPreplayPacket {
final SocketAddress socketAddress = new java.net.InetSocketAddress(split[1],
((java.net.InetSocketAddress) connection.getRemoteAddress()).getPort());
nettyPlayerConnection.setRemoteAddress(socketAddress);
-
UUID playerUuid = UUID.fromString(
split[2]
.replaceFirst(
@@ -92,9 +90,9 @@ public class HandshakePacket implements ClientPreplayPacket {
return;
}
}
- connection.setHandshakeData(new HandshakeData(
- serverAddress, serverPort, protocolVersion
- ));
+ connection.setProtocolVersion(protocolVersion);
+ connection.setServerAddress(serverAddress);
+ connection.setServerPort(serverPort);
switch (nextState) {
case 1:
connection.setConnectionState(ConnectionState.STATUS);
@@ -103,10 +101,6 @@ public class HandshakePacket implements ClientPreplayPacket {
if (protocolVersion == MinecraftServer.PROTOCOL_VERSION) {
connection.setConnectionState(ConnectionState.LOGIN);
- if (connection instanceof NettyPlayerConnection) {
- // Give to the connection the server info that the client used
- ((NettyPlayerConnection) connection).refreshServerInformation(serverAddress, serverPort);
- }
} else {
// Incorrect client version
connection.sendPacket(new LoginDisconnectPacket(INVALID_VERSION_TEXT));
diff --git a/src/main/java/net/minestom/server/network/player/FakePlayerConnection.java b/src/main/java/net/minestom/server/network/player/FakePlayerConnection.java
index 9cb1b4637..3b92c09a7 100644
--- a/src/main/java/net/minestom/server/network/player/FakePlayerConnection.java
+++ b/src/main/java/net/minestom/server/network/player/FakePlayerConnection.java
@@ -6,6 +6,7 @@ import net.minestom.server.entity.fakeplayer.FakePlayer;
import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.utils.validate.Check;
import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
@@ -41,4 +42,28 @@ public class FakePlayerConnection extends PlayerConnection {
Check.argCondition(!(player instanceof FakePlayer), "FakePlayerController needs a FakePlayer object");
super.setPlayer(player);
}
+
+ @Override
+ public void setProtocolVersion(int protocolVersion) {}
+
+ @Override
+ public int getProtocolVersion() {
+ return MinecraftServer.PROTOCOL_VERSION;
+ }
+
+ @Override
+ public @Nullable String getServerAddress() {
+ return MinecraftServer.getNettyServer().getAddress();
+ }
+
+ @Override
+ public void setServerAddress(@Nullable String serverAddress) {}
+
+ @Override
+ public int getServerPort() {
+ return MinecraftServer.getNettyServer().getPort();
+ }
+
+ @Override
+ public void setServerPort(int serverPort) {}
}
diff --git a/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java b/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java
index 0efb81a11..04dd87edb 100644
--- a/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java
+++ b/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java
@@ -41,6 +41,7 @@ public class NettyPlayerConnection extends PlayerConnection {
private SocketAddress remoteAddress;
+
private boolean encrypted = false;
private boolean compressed = false;
@@ -51,6 +52,7 @@ public class NettyPlayerConnection extends PlayerConnection {
private String loginUsername;
private String serverAddress;
private int serverPort;
+ private int protocolVersion;
// Used for the login plugin request packet, to retrieve the channel from a message id,
// cleared once the player enters the play state
@@ -238,6 +240,24 @@ public class NettyPlayerConnection extends PlayerConnection {
this.remoteAddress = remoteAddress;
}
+ /**
+ * Gets the protocol version of a client.
+ *
+ * @return protocol version of client.
+ */
+ public int getProtocolVersion() {
+ return protocolVersion;
+ }
+
+ /**
+ * Changes the protocol version of a connecting client.
+ * @param protocolVersion the protocol version of the client
+ */
+ public void setProtocolVersion(int protocolVersion) {
+ this.protocolVersion = protocolVersion;
+ }
+
+
@Override
public void disconnect() {
this.channel.close();
@@ -276,11 +296,20 @@ public class NettyPlayerConnection extends PlayerConnection {
*
* @return the server address used
*/
- @Nullable
- public String getServerAddress() {
+ public @Nullable String getServerAddress() {
return serverAddress;
}
+ /**
+ * Set the server address the client used to connect.
+ *
+ * @param serverAddress the server address
+ */
+ public void setServerAddress(@Nullable String serverAddress) {
+ this.serverAddress = serverAddress;
+ }
+
+
/**
* Gets the server port that the client used to connect.
*
@@ -292,6 +321,15 @@ public class NettyPlayerConnection extends PlayerConnection {
return serverPort;
}
+ /**
+ * Set the server port the client used to connect.
+ *
+ * @param serverPort the server port
+ */
+ public void setServerPort(int serverPort) {
+ this.serverPort = serverPort;
+ }
+
@Nullable
public UUID getBungeeUuid() {
return bungeeUuid;
@@ -349,16 +387,6 @@ public class NettyPlayerConnection extends PlayerConnection {
}
}
- /**
- * Used in {@link net.minestom.server.network.packet.client.handshake.HandshakePacket} to change the internal fields.
- *
- * @param serverAddress the server address which the client used
- * @param serverPort the server port which the client used
- */
- public void refreshServerInformation(@Nullable String serverAddress, int serverPort) {
- this.serverAddress = serverAddress;
- this.serverPort = serverPort;
- }
@NotNull
public ByteBuf getTickBuffer() {
diff --git a/src/main/java/net/minestom/server/network/player/PlayerConnection.java b/src/main/java/net/minestom/server/network/player/PlayerConnection.java
index 392297f1c..324cf22ff 100644
--- a/src/main/java/net/minestom/server/network/player/PlayerConnection.java
+++ b/src/main/java/net/minestom/server/network/player/PlayerConnection.java
@@ -9,7 +9,6 @@ import net.minestom.server.listener.manager.ServerPacketConsumer;
import net.minestom.server.network.ConnectionManager;
import net.minestom.server.network.ConnectionState;
import net.minestom.server.network.packet.server.ServerPacket;
-import net.minestom.server.ping.HandshakeData;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -27,7 +26,6 @@ public abstract class PlayerConnection {
private Player player;
private volatile ConnectionState connectionState;
- private HandshakeData handshakeData;
private boolean online;
// Text used to kick client sending too many packets
@@ -119,6 +117,55 @@ public abstract class PlayerConnection {
@NotNull
public abstract SocketAddress getRemoteAddress();
+
+ /**
+ * Changes the protocol version of the client.
+ *
+ * @param protocolVersion the protocol version
+ */
+ public abstract void setProtocolVersion(int protocolVersion);
+
+ /**
+ * Gets protocol version of client.
+ *
+ * @return the protocol version
+ */
+ public abstract int getProtocolVersion();
+
+ /**
+ * Gets the server address that the client used to connect.
+ *
+ * WARNING: it is given by the client, it is possible for it to be wrong.
+ *
+ * @return the server address used
+ */
+ public abstract @Nullable String getServerAddress();
+
+ /**
+ * Set the server address the client used to connect.
+ *
+ * @param serverAddress the server address
+ */
+ public abstract void setServerAddress(@Nullable String serverAddress);
+
+
+ /**
+ * Gets the server port that the client used to connect.
+ *
+ * WARNING: it is given by the client, it is possible for it to be wrong.
+ *
+ * @return the server port used
+ */
+ public abstract int getServerPort();
+
+ /**
+ * Set the server port the client used to connect.
+ *
+ * @param serverPort the server port
+ */
+ public abstract void setServerPort(int serverPort);
+
+
/**
* Forcing the player to disconnect.
*/
@@ -172,22 +219,6 @@ public abstract class PlayerConnection {
return connectionState;
}
- /**
- * Gets the HandshakeData from the most recent HandshakePacket.
- *
- * @return the most recent HandshakeData
- */
- @Nullable
- public HandshakeData getHandshakeData() {
- return handshakeData;
- }
-
- /**
- * Sets the HandshakeData - usually from a HandshakePacket
- */
- public void setHandshakeData(HandshakeData handshakeData) {
- this.handshakeData = handshakeData;
- }
/**
* Gets the number of packet the client sent over the last second.
diff --git a/src/main/java/net/minestom/server/ping/HandshakeData.java b/src/main/java/net/minestom/server/ping/HandshakeData.java
deleted file mode 100644
index 89a9a31d8..000000000
--- a/src/main/java/net/minestom/server/ping/HandshakeData.java
+++ /dev/null
@@ -1,45 +0,0 @@
-package net.minestom.server.ping;
-
-import org.jetbrains.annotations.Nullable;
-
-public final class HandshakeData {
- private final @Nullable String serverAddress;
- private final int serverPort;
- private final int protocolVersion;
-
- public HandshakeData(@Nullable String serverAddress, int serverPort, int protocolVersion) {
- this.serverAddress = serverAddress;
- this.serverPort = serverPort;
- this.protocolVersion = protocolVersion;
- }
-
-
- /**
- * Get the server address a client used to connect.
- * may be null
- * @return the server address
- */
- public @Nullable String getServerAddress() {
- return serverAddress;
- }
-
- /**
- * Get the server port a client used to connect.
- *
- * @return the server port
- */
- public int getServerPort() {
- return serverPort;
- }
-
- /**
- * Get the protocol version a client used to connect.
- *
- * @return the protocol version
- */
- public int getProtocolVersion() {
- return protocolVersion;
- }
-
-
-}
diff --git a/src/test/java/demo/Main.java b/src/test/java/demo/Main.java
index 82fc895ec..71afa452b 100644
--- a/src/test/java/demo/Main.java
+++ b/src/test/java/demo/Main.java
@@ -75,9 +75,9 @@ public class Main {
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();
+ String ip = event.getRemoteServerAddress();
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 +"7PORT: " + (char)0x00a7 + "e" + event.getServerPort());
event.addPlayer((char)0x00a7 + "8- " + (char)0x00a7 +"7VERSION: " + (char)0x00a7 + "e" + event.getClientProtocolVersion());
// Check if client supports RGB color
From b086dc82ca1f72c438e7032b506b27f3bb7cc0f5 Mon Sep 17 00:00:00 2001
From: thiccaxe <51303986+thiccaxe@users.noreply.github.com>
Date: Thu, 8 Apr 2021 18:20:23 -0700
Subject: [PATCH 12/19] Remove extra code in FakePlayerConnection, add
refershServerInformation back
---
.../event/server/ServerListPingEvent.java | 2 --
.../client/handshake/HandshakePacket.java | 7 ++--
.../network/player/NettyPlayerConnection.java | 31 ++++++++---------
.../network/player/PlayerConnection.java | 33 +++++--------------
src/test/java/demo/Main.java | 1 -
5 files changed, 26 insertions(+), 48 deletions(-)
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 e40f0d8a4..3811f5c72 100644
--- a/src/main/java/net/minestom/server/event/server/ServerListPingEvent.java
+++ b/src/main/java/net/minestom/server/event/server/ServerListPingEvent.java
@@ -11,8 +11,6 @@ import net.minestom.server.ping.ResponseData;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
-import java.net.InetSocketAddress;
-import java.util.Objects;
import java.util.UUID;
public class ServerListPingEvent extends Event implements CancellableEvent {
diff --git a/src/main/java/net/minestom/server/network/packet/client/handshake/HandshakePacket.java b/src/main/java/net/minestom/server/network/packet/client/handshake/HandshakePacket.java
index 88d201b0b..bc8d69f76 100644
--- a/src/main/java/net/minestom/server/network/packet/client/handshake/HandshakePacket.java
+++ b/src/main/java/net/minestom/server/network/packet/client/handshake/HandshakePacket.java
@@ -90,14 +90,15 @@ public class HandshakePacket implements ClientPreplayPacket {
return;
}
}
- connection.setProtocolVersion(protocolVersion);
- connection.setServerAddress(serverAddress);
- connection.setServerPort(serverPort);
switch (nextState) {
case 1:
connection.setConnectionState(ConnectionState.STATUS);
break;
case 2:
+ if (connection instanceof NettyPlayerConnection) {
+ ((NettyPlayerConnection) connection).setProtocolVersion(protocolVersion);
+ ((NettyPlayerConnection) connection).refreshServerInformation(serverAddress, serverPort);
+ }
if (protocolVersion == MinecraftServer.PROTOCOL_VERSION) {
connection.setConnectionState(ConnectionState.LOGIN);
diff --git a/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java b/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java
index 04dd87edb..246854f41 100644
--- a/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java
+++ b/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java
@@ -296,20 +296,11 @@ public class NettyPlayerConnection extends PlayerConnection {
*
* @return the server address used
*/
+ @Override
public @Nullable String getServerAddress() {
return serverAddress;
}
- /**
- * Set the server address the client used to connect.
- *
- * @param serverAddress the server address
- */
- public void setServerAddress(@Nullable String serverAddress) {
- this.serverAddress = serverAddress;
- }
-
-
/**
* Gets the server port that the client used to connect.
*
@@ -317,18 +308,11 @@ public class NettyPlayerConnection extends PlayerConnection {
*
* @return the server port used
*/
+ @Override
public int getServerPort() {
return serverPort;
}
- /**
- * Set the server port the client used to connect.
- *
- * @param serverPort the server port
- */
- public void setServerPort(int serverPort) {
- this.serverPort = serverPort;
- }
@Nullable
public UUID getBungeeUuid() {
@@ -348,6 +332,17 @@ public class NettyPlayerConnection extends PlayerConnection {
this.bungeeSkin = bungeeSkin;
}
+
+ /**
+ * Used in {@link net.minestom.server.network.packet.client.handshake.HandshakePacket} to change the internal fields.
+ *
+ * @param serverAddress the server address which the client used
+ * @param serverPort the server port which the client used
+ */
+ public void refreshServerInformation(@Nullable String serverAddress, int serverPort) {
+ this.serverAddress = serverAddress;
+ this.serverPort = serverPort;
+ }
/**
* Adds an entry to the plugin request map.
*
diff --git a/src/main/java/net/minestom/server/network/player/PlayerConnection.java b/src/main/java/net/minestom/server/network/player/PlayerConnection.java
index 324cf22ff..2b69b3983 100644
--- a/src/main/java/net/minestom/server/network/player/PlayerConnection.java
+++ b/src/main/java/net/minestom/server/network/player/PlayerConnection.java
@@ -118,19 +118,14 @@ public abstract class PlayerConnection {
public abstract SocketAddress getRemoteAddress();
- /**
- * Changes the protocol version of the client.
- *
- * @param protocolVersion the protocol version
- */
- public abstract void setProtocolVersion(int protocolVersion);
-
/**
* Gets protocol version of client.
*
* @return the protocol version
*/
- public abstract int getProtocolVersion();
+ public int getProtocolVersion() {
+ return MinecraftServer.PROTOCOL_VERSION;
+ }
/**
* Gets the server address that the client used to connect.
@@ -139,14 +134,9 @@ public abstract class PlayerConnection {
*
* @return the server address used
*/
- public abstract @Nullable String getServerAddress();
-
- /**
- * Set the server address the client used to connect.
- *
- * @param serverAddress the server address
- */
- public abstract void setServerAddress(@Nullable String serverAddress);
+ public @Nullable String getServerAddress() {
+ return MinecraftServer.getNettyServer().getAddress();
+ }
/**
@@ -156,14 +146,9 @@ public abstract class PlayerConnection {
*
* @return the server port used
*/
- public abstract int getServerPort();
-
- /**
- * Set the server port the client used to connect.
- *
- * @param serverPort the server port
- */
- public abstract void setServerPort(int serverPort);
+ public int getServerPort() {
+ return MinecraftServer.getNettyServer().getPort();
+ }
/**
diff --git a/src/test/java/demo/Main.java b/src/test/java/demo/Main.java
index 71afa452b..924469596 100644
--- a/src/test/java/demo/Main.java
+++ b/src/test/java/demo/Main.java
@@ -17,7 +17,6 @@ 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;
From fce63a91d502008fafb42f08993e5cb331d9695f Mon Sep 17 00:00:00 2001
From: thiccaxe <51303986+thiccaxe@users.noreply.github.com>
Date: Thu, 8 Apr 2021 18:21:00 -0700
Subject: [PATCH 13/19] actually remove the code this time
---
.../network/player/FakePlayerConnection.java | 25 +------------------
1 file changed, 1 insertion(+), 24 deletions(-)
diff --git a/src/main/java/net/minestom/server/network/player/FakePlayerConnection.java b/src/main/java/net/minestom/server/network/player/FakePlayerConnection.java
index 3b92c09a7..dc6e90668 100644
--- a/src/main/java/net/minestom/server/network/player/FakePlayerConnection.java
+++ b/src/main/java/net/minestom/server/network/player/FakePlayerConnection.java
@@ -42,28 +42,5 @@ public class FakePlayerConnection extends PlayerConnection {
Check.argCondition(!(player instanceof FakePlayer), "FakePlayerController needs a FakePlayer object");
super.setPlayer(player);
}
-
- @Override
- public void setProtocolVersion(int protocolVersion) {}
-
- @Override
- public int getProtocolVersion() {
- return MinecraftServer.PROTOCOL_VERSION;
- }
-
- @Override
- public @Nullable String getServerAddress() {
- return MinecraftServer.getNettyServer().getAddress();
- }
-
- @Override
- public void setServerAddress(@Nullable String serverAddress) {}
-
- @Override
- public int getServerPort() {
- return MinecraftServer.getNettyServer().getPort();
- }
-
- @Override
- public void setServerPort(int serverPort) {}
+
}
From 61242dc185de5b6fc289253eea465f079ecc4be5 Mon Sep 17 00:00:00 2001
From: thiccaxe <51303986+thiccaxe@users.noreply.github.com>
Date: Thu, 8 Apr 2021 22:06:16 -0700
Subject: [PATCH 14/19] Remove extra setters and congregate into
refreshServerInformation
---
.../net/minestom/server/MinecraftServer.java | 2 +-
.../client/handshake/HandshakePacket.java | 12 +++--
.../network/player/FakePlayerConnection.java | 2 +-
.../network/player/NettyPlayerConnection.java | 52 +++++++++----------
4 files changed, 33 insertions(+), 35 deletions(-)
diff --git a/src/main/java/net/minestom/server/MinecraftServer.java b/src/main/java/net/minestom/server/MinecraftServer.java
index 42e4d261c..5d805f5c8 100644
--- a/src/main/java/net/minestom/server/MinecraftServer.java
+++ b/src/main/java/net/minestom/server/MinecraftServer.java
@@ -746,7 +746,7 @@ public final class MinecraftServer {
* @param responseDataConsumer the response data consumer, can be null
* @throws IllegalStateException if called before {@link #init()} or if the server is already running
*
- * @deprecated use {@link #start(String, int)} and listen to the {@link net.minestom.server.event.server.StatusRequestEvent} event instead of ResponseDataConsumer
+ * @deprecated use {@link #start(String, int)} and listen to the {@link net.minestom.server.event.server.ServerListPingEvent} event instead of ResponseDataConsumer
*/
@Deprecated
public void start(@NotNull String address, int port, @Nullable ResponseDataConsumer responseDataConsumer) {
diff --git a/src/main/java/net/minestom/server/network/packet/client/handshake/HandshakePacket.java b/src/main/java/net/minestom/server/network/packet/client/handshake/HandshakePacket.java
index bc8d69f76..e6f8cbe34 100644
--- a/src/main/java/net/minestom/server/network/packet/client/handshake/HandshakePacket.java
+++ b/src/main/java/net/minestom/server/network/packet/client/handshake/HandshakePacket.java
@@ -66,6 +66,7 @@ public class HandshakePacket implements ClientPreplayPacket {
final SocketAddress socketAddress = new java.net.InetSocketAddress(split[1],
((java.net.InetSocketAddress) connection.getRemoteAddress()).getPort());
nettyPlayerConnection.setRemoteAddress(socketAddress);
+
UUID playerUuid = UUID.fromString(
split[2]
.replaceFirst(
@@ -90,18 +91,19 @@ public class HandshakePacket implements ClientPreplayPacket {
return;
}
}
+
+ if (connection instanceof NettyPlayerConnection) {
+ // Give to the connection the server info that the client used
+ ((NettyPlayerConnection) connection).refreshServerInformation(serverAddress, serverPort, protocolVersion);
+ }
+
switch (nextState) {
case 1:
connection.setConnectionState(ConnectionState.STATUS);
break;
case 2:
- if (connection instanceof NettyPlayerConnection) {
- ((NettyPlayerConnection) connection).setProtocolVersion(protocolVersion);
- ((NettyPlayerConnection) connection).refreshServerInformation(serverAddress, serverPort);
- }
if (protocolVersion == MinecraftServer.PROTOCOL_VERSION) {
connection.setConnectionState(ConnectionState.LOGIN);
-
} else {
// Incorrect client version
connection.sendPacket(new LoginDisconnectPacket(INVALID_VERSION_TEXT));
diff --git a/src/main/java/net/minestom/server/network/player/FakePlayerConnection.java b/src/main/java/net/minestom/server/network/player/FakePlayerConnection.java
index dc6e90668..f9d709450 100644
--- a/src/main/java/net/minestom/server/network/player/FakePlayerConnection.java
+++ b/src/main/java/net/minestom/server/network/player/FakePlayerConnection.java
@@ -42,5 +42,5 @@ public class FakePlayerConnection extends PlayerConnection {
Check.argCondition(!(player instanceof FakePlayer), "FakePlayerController needs a FakePlayer object");
super.setPlayer(player);
}
-
+
}
diff --git a/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java b/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java
index 246854f41..c6e1bb97f 100644
--- a/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java
+++ b/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java
@@ -240,23 +240,6 @@ public class NettyPlayerConnection extends PlayerConnection {
this.remoteAddress = remoteAddress;
}
- /**
- * Gets the protocol version of a client.
- *
- * @return protocol version of client.
- */
- public int getProtocolVersion() {
- return protocolVersion;
- }
-
- /**
- * Changes the protocol version of a connecting client.
- * @param protocolVersion the protocol version of the client
- */
- public void setProtocolVersion(int protocolVersion) {
- this.protocolVersion = protocolVersion;
- }
-
@Override
public void disconnect() {
@@ -313,6 +296,30 @@ public class NettyPlayerConnection extends PlayerConnection {
return serverPort;
}
+ /**
+ * Gets the protocol version of a client.
+ *
+ * @return protocol version of client.
+ */
+ @Override
+ public int getProtocolVersion() {
+ return protocolVersion;
+ }
+
+
+ /**
+ * Used in {@link net.minestom.server.network.packet.client.handshake.HandshakePacket} to change the internal fields.
+ *
+ * @param serverAddress the server address which the client used
+ * @param serverPort the server port which the client used
+ * @param protocolVersion the protocol version which the client used
+ */
+ public void refreshServerInformation(@Nullable String serverAddress, int serverPort, int protocolVersion) {
+ this.serverAddress = serverAddress;
+ this.serverPort = serverPort;
+ this.protocolVersion = protocolVersion;
+ }
+
@Nullable
public UUID getBungeeUuid() {
@@ -332,17 +339,6 @@ public class NettyPlayerConnection extends PlayerConnection {
this.bungeeSkin = bungeeSkin;
}
-
- /**
- * Used in {@link net.minestom.server.network.packet.client.handshake.HandshakePacket} to change the internal fields.
- *
- * @param serverAddress the server address which the client used
- * @param serverPort the server port which the client used
- */
- public void refreshServerInformation(@Nullable String serverAddress, int serverPort) {
- this.serverAddress = serverAddress;
- this.serverPort = serverPort;
- }
/**
* Adds an entry to the plugin request map.
*
From 8a9abff98bad94c761e253a3f6a099e43393d18b Mon Sep 17 00:00:00 2001
From: thiccaxe <51303986+thiccaxe@users.noreply.github.com>
Date: Sat, 10 Apr 2021 21:14:36 -0700
Subject: [PATCH 15/19] Remove Duplicate Code; unlinking ResponseData &
ServerListPingEvent
Removed code that would cause maintaining more difficult after an update in ResponseData, and additionally PlayerConnection
Updated demo to reflect changes.
---
.../event/server/ServerListPingEvent.java | 143 +-----------------
.../client/status/StatusRequestPacket.java | 13 +-
.../minestom/server/ping/ResponseData.java | 17 ++-
src/test/java/demo/Main.java | 32 ++--
4 files changed, 39 insertions(+), 166 deletions(-)
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 3811f5c72..da79dae2c 100644
--- a/src/main/java/net/minestom/server/event/server/ServerListPingEvent.java
+++ b/src/main/java/net/minestom/server/event/server/ServerListPingEvent.java
@@ -1,17 +1,10 @@
package net.minestom.server.event.server;
-import net.kyori.adventure.text.Component;
-import net.minestom.server.MinecraftServer;
-import net.minestom.server.entity.Player;
import net.minestom.server.event.CancellableEvent;
import net.minestom.server.event.Event;
-import net.minestom.server.network.player.NettyPlayerConnection;
import net.minestom.server.network.player.PlayerConnection;
import net.minestom.server.ping.ResponseData;
import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
-
-import java.util.UUID;
public class ServerListPingEvent extends Event implements CancellableEvent {
private boolean cancelled = false;
@@ -34,7 +27,6 @@ public class ServerListPingEvent extends Event implements CancellableEvent {
return responseData;
}
-
/**
* PlayerConnection of received packet.
*
@@ -42,11 +34,11 @@ public class ServerListPingEvent extends Event implements CancellableEvent {
*
* @return the playerConnection.
*/
-
public @NotNull PlayerConnection getConnection() {
return connection;
}
+
@Override
public boolean isCancelled() {
return cancelled;
@@ -62,137 +54,4 @@ public class ServerListPingEvent extends Event implements CancellableEvent {
this.cancelled = cancel;
}
- // Shortcut Methods
- /**
- * Sets the version name for the response.
- *
- * @param version The version name for the response data.
- */
- public void setVersion(@NotNull String version) {
- responseData.setVersion(version);
- }
-
- /**
- * Sets the response protocol version.
- *
- * @param protocol The protocol version for the response data.
- */
- public void setProtocol(int protocol) {
- responseData.setProtocol(protocol);
- }
-
- /**
- * Sets the response maximum player count.
- *
- * @param maxPlayer The maximum player count for the response data.
- */
- public void setMaxPlayer(int maxPlayer) {
- responseData.setMaxPlayer(maxPlayer);
- }
-
- /**
- * Sets the response online count.
- *
- * @param online The online count for the response data.
- */
- public void setOnline(int online) {
- responseData.setOnline(online);
- }
-
- /**
- * Adds some players to the response.
- *
- * @param players the players
- */
- public void addPlayer(@NotNull Iterable players) {
- responseData.addPlayer(players);
- }
-
- /**
- * Adds a player to the response.
- *
- * @param player the player
- */
- public void addPlayer(@NotNull Player player) {
- addPlayer(player.getUsername(), player.getUuid());
- }
-
- /**
- * Adds a player to the response.
- *
- * @param name The name of the player.
- * @param uuid The unique identifier of the player.
- */
- public void addPlayer(@NotNull String name, @NotNull UUID uuid) {
- responseData.addPlayer(name, uuid);
- }
-
- /**
- * Adds a player to the response.
- * {@link UUID#randomUUID()} is used as a default parameter for uuid
- *
- * @param name The name of the player.
- */
- 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.
- */
- public void clearPlayers() {
- responseData.clearPlayers();
- }
-
- /**
- * Sets the response description.
- *
- * @param description The description for the response data.
- */
- public void setDescription(Component description) {
- responseData.setDescription(description);
- }
-
- /**
- * Sets the response favicon.
- *
- * MUST start with "data:image/png;base64,"
- *
- * @param favicon The favicon for the response data.
- */
- public void setFavicon(String favicon) {
- responseData.setFavicon(favicon);
- }
-
- /**
- * Get the server address a client used to connect.
- *
- * @return the server address
- */
- public @Nullable String getRemoteServerAddress() {
- return connection.getServerAddress();
- }
-
- /**
- * Get the server port a client used to connect.
- *
- * @return the server port
- */
- public int getServerPort() {
- return connection.getServerPort();
- }
-
- /**
- * Get the protocol version a client used to connect.
- *
- * @return the protocol version
- */
- public int getClientProtocolVersion() {
- if (connection instanceof NettyPlayerConnection) {
- return connection.getProtocolVersion();
- }
- return MinecraftServer.PROTOCOL_VERSION;
- }
-
}
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 b13b313a9..f7f0c9622 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
@@ -32,15 +32,14 @@ public class StatusRequestPacket implements ClientPreplayPacket {
// Call event
ServerListPingEvent statusRequestEvent = new ServerListPingEvent(responseData, connection);
- MinecraftServer.getGlobalEventHandler().callEvent(ServerListPingEvent.class, statusRequestEvent);
+ MinecraftServer.getGlobalEventHandler().callCancellableEvent(ServerListPingEvent.class, statusRequestEvent,
+ () -> {
+ ResponsePacket responsePacket = new ResponsePacket();
+ responsePacket.jsonResponse = responseData.build().toString();
- // Send packet only if event has not been cancelled
- if (!statusRequestEvent.isCancelled()) {
- ResponsePacket responsePacket = new ResponsePacket();
- responsePacket.jsonResponse = responseData.build().toString();
+ connection.sendPacket(responsePacket);
+ });
- connection.sendPacket(responsePacket);
- }
}
@Override
diff --git a/src/main/java/net/minestom/server/ping/ResponseData.java b/src/main/java/net/minestom/server/ping/ResponseData.java
index 52fc674a5..a9bb04e55 100644
--- a/src/main/java/net/minestom/server/ping/ResponseData.java
+++ b/src/main/java/net/minestom/server/ping/ResponseData.java
@@ -15,8 +15,7 @@ import java.util.UUID;
/**
* Represents the data sent to the player when refreshing the server list.
*
- * Filled by {@link ResponseDataConsumer} and specified in {@link
- * net.minestom.server.MinecraftServer#start(String, int, ResponseDataConsumer)}.
+ *
Edited by listening to the {@link net.minestom.server.event.server.ServerListPingEvent}.
*/
public class ResponseData {
private final List pingPlayers;
@@ -115,6 +114,20 @@ public class ResponseData {
this.pingPlayers.add(pingPlayer);
}
+ /**
+ * Adds a player to the response.
+ *
+ * {@link UUID#randomUUID()} is used as the player's UUID.
+ *
+ * @param name The name of the player.
+ */
+ public void addPlayer(String name) {
+ PingPlayer pingPlayer = new PingPlayer();
+ pingPlayer.name = name;
+ pingPlayer.uuid = UUID.randomUUID();
+ this.pingPlayers.add(pingPlayer);
+ }
+
/**
* Removes all of the ping players from this {@link #pingPlayers}. The {@link #pingPlayers} list
* will be empty this call returns.
diff --git a/src/test/java/demo/Main.java b/src/test/java/demo/Main.java
index 924469596..1e45266c1 100644
--- a/src/test/java/demo/Main.java
+++ b/src/test/java/demo/Main.java
@@ -13,6 +13,7 @@ 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;
@@ -65,27 +66,28 @@ public class Main {
MinecraftServer.getSchedulerManager().buildShutdownTask(() -> System.out.println("Good night")).schedule();
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.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());
+ ResponseData responseData = event.getResponseData();
+ responseData.setMaxPlayer(0);
+ responseData.setOnline(MinecraftServer.getConnectionManager().getOnlinePlayers().size());
+ responseData.addPlayer("The first line is separated from the others", UUID.randomUUID());
+ responseData.addPlayer("Could be a name, or a message", UUID.randomUUID());
+ responseData.addPlayer("IP test: " + event.getConnection().getRemoteAddress().toString(), UUID.randomUUID());
+ responseData.addPlayer("Use " + (char)0x00a7 + "7section characters", UUID.randomUUID());
+ responseData.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.getRemoteServerAddress();
- 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.getServerPort());
- event.addPlayer((char)0x00a7 + "8- " + (char)0x00a7 +"7VERSION: " + (char)0x00a7 + "e" + event.getClientProtocolVersion());
+ responseData.addPlayer("Connection Info:");
+ String ip = event.getConnection().getServerAddress();
+ responseData.addPlayer((char)0x00a7 + "8- " + (char)0x00a7 +"7IP: " + (char)0x00a7 + "e" + (ip != null ? ip : "???"));
+ responseData.addPlayer((char)0x00a7 + "8- " + (char)0x00a7 +"7PORT: " + (char)0x00a7 + "e" + event.getConnection().getServerPort());
+ responseData.addPlayer((char)0x00a7 + "8- " + (char)0x00a7 +"7VERSION: " + (char)0x00a7 + "e" + event.getConnection().getProtocolVersion());
// Check if client supports RGB color
- if (event.getClientProtocolVersion() >= 713) { // Snapshot 20w17a
- event.setDescription(Component.text("You can do ")
+ if (event.getConnection().getProtocolVersion() >= 713) { // Snapshot 20w17a
+ responseData.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 ")
+ responseData.setDescription(Component.text("You can do ")
.append(Component.text("RGB", NamedTextColor.nearestTo(TextColor.color(0x66b3ff))))
.append(Component.text(" color here,"))
.append(Component.newline())
From c451d46ce037f9449764cd9dffbdcc79c42f57f6 Mon Sep 17 00:00:00 2001
From: thiccaxe <51303986+thiccaxe@users.noreply.github.com>
Date: Sun, 11 Apr 2021 10:30:47 -0700
Subject: [PATCH 16/19] document ServerListPingEvent
---
.../net/minestom/server/event/server/ServerListPingEvent.java | 4 ++++
1 file changed, 4 insertions(+)
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 da79dae2c..490a10d91 100644
--- a/src/main/java/net/minestom/server/event/server/ServerListPingEvent.java
+++ b/src/main/java/net/minestom/server/event/server/ServerListPingEvent.java
@@ -6,6 +6,10 @@ import net.minestom.server.network.player.PlayerConnection;
import net.minestom.server.ping.ResponseData;
import org.jetbrains.annotations.NotNull;
+/**
+ * Called when a {@link PlayerConnection} sends a status packet,
+ * usually to display information on the server list.
+ */
public class ServerListPingEvent extends Event implements CancellableEvent {
private boolean cancelled = false;
From fadbe394940def5880595a9d64278b575fd7dfbf Mon Sep 17 00:00:00 2001
From: thiccaxe <51303986+thiccaxe@users.noreply.github.com>
Date: Sun, 11 Apr 2021 10:46:09 -0700
Subject: [PATCH 17/19] Getters for fields in HandshakeData
---
.../minestom/server/ping/ResponseData.java | 64 ++++++++++++++++++-
src/test/java/demo/Main.java | 1 -
2 files changed, 61 insertions(+), 4 deletions(-)
diff --git a/src/main/java/net/minestom/server/ping/ResponseData.java b/src/main/java/net/minestom/server/ping/ResponseData.java
index a9bb04e55..f27feeb6f 100644
--- a/src/main/java/net/minestom/server/ping/ResponseData.java
+++ b/src/main/java/net/minestom/server/ping/ResponseData.java
@@ -9,6 +9,7 @@ import net.minestom.server.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
import java.util.UUID;
@@ -54,6 +55,14 @@ public class ResponseData {
this.version = version;
}
+ /**
+ * Get the version name for the response.
+ * @return the version name for the response.
+ */
+ public String getVersion() {
+ return version;
+ }
+
/**
* Sets the response protocol version.
*
@@ -63,6 +72,13 @@ public class ResponseData {
this.protocol = protocol;
}
+ /**
+ * Get the response protocol version.
+ * @return the response protocol version.
+ */
+ public int getProtocol() {
+ return protocol;
+ }
/**
* Sets the response maximum player count.
*
@@ -72,6 +88,13 @@ public class ResponseData {
this.maxPlayer = maxPlayer;
}
+ /**
+ * Get the response maximum player count.
+ * @return the response maximum player count.
+ */
+ public int getMaxPlayer() {
+ return maxPlayer;
+ }
/**
* Sets the response online count.
*
@@ -81,6 +104,15 @@ public class ResponseData {
this.online = online;
}
+ /**
+ * Get the response online count.
+ *
+ * @return the response online count.
+ */
+ public int getOnline() {
+ return online;
+ }
+
/**
* Adds some players to the response.
*
@@ -136,6 +168,14 @@ public class ResponseData {
this.pingPlayers.clear();
}
+ /**
+ * Get the list of the response players.
+ * @return the list of the response players.
+ */
+ public List getPlayers() {
+ return pingPlayers;
+ }
+
/**
* Sets the response description.
*
@@ -156,6 +196,15 @@ public class ResponseData {
this.description = description;
}
+ /**
+ * Get the response description
+ *
+ * @return the response description
+ */
+ public Component getDescription() {
+ return description;
+ }
+
/**
* Sets the response favicon.
*
@@ -167,6 +216,15 @@ public class ResponseData {
this.favicon = favicon;
}
+ /**
+ * Get the response favicon.
+ * @return the response favicon.
+ */
+ public String getFavicon() {
+ return favicon;
+ }
+
+
/**
* Converts the response data into a {@link JsonObject}.
*
@@ -208,8 +266,8 @@ public class ResponseData {
/**
* Represents a player line in the server list hover.
*/
- private static class PingPlayer {
- private String name;
- private UUID uuid;
+ public static class PingPlayer {
+ public String name;
+ public UUID uuid;
}
}
diff --git a/src/test/java/demo/Main.java b/src/test/java/demo/Main.java
index 1e45266c1..3f5eb89ee 100644
--- a/src/test/java/demo/Main.java
+++ b/src/test/java/demo/Main.java
@@ -98,7 +98,6 @@ public class Main {
-
});
PlayerInit.init();
From 9f3b7d4ca8551c4aecb9f7389105f2e5189de61a Mon Sep 17 00:00:00 2001
From: TheMode
Date: Sun, 11 Apr 2021 20:32:09 +0200
Subject: [PATCH 18/19] Update FakePlayerConnection.java
---
.../net/minestom/server/network/player/FakePlayerConnection.java | 1 -
1 file changed, 1 deletion(-)
diff --git a/src/main/java/net/minestom/server/network/player/FakePlayerConnection.java b/src/main/java/net/minestom/server/network/player/FakePlayerConnection.java
index f9d709450..a605ed22b 100644
--- a/src/main/java/net/minestom/server/network/player/FakePlayerConnection.java
+++ b/src/main/java/net/minestom/server/network/player/FakePlayerConnection.java
@@ -42,5 +42,4 @@ public class FakePlayerConnection extends PlayerConnection {
Check.argCondition(!(player instanceof FakePlayer), "FakePlayerController needs a FakePlayer object");
super.setPlayer(player);
}
-
}
From db8f071633e673d629310b51d2637595ba35882f Mon Sep 17 00:00:00 2001
From: TheMode
Date: Sun, 11 Apr 2021 20:46:48 +0200
Subject: [PATCH 19/19] Cleanup, make PingPlayer immutable
---
.../network/player/FakePlayerConnection.java | 1 -
.../network/player/NettyPlayerConnection.java | 1 -
.../network/player/PlayerConnection.java | 1 -
.../minestom/server/ping/ResponseData.java | 42 ++++++++++++++-----
4 files changed, 31 insertions(+), 14 deletions(-)
diff --git a/src/main/java/net/minestom/server/network/player/FakePlayerConnection.java b/src/main/java/net/minestom/server/network/player/FakePlayerConnection.java
index a605ed22b..9cb1b4637 100644
--- a/src/main/java/net/minestom/server/network/player/FakePlayerConnection.java
+++ b/src/main/java/net/minestom/server/network/player/FakePlayerConnection.java
@@ -6,7 +6,6 @@ import net.minestom.server.entity.fakeplayer.FakePlayer;
import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.utils.validate.Check;
import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
diff --git a/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java b/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java
index c6e1bb97f..d79b461a0 100644
--- a/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java
+++ b/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java
@@ -41,7 +41,6 @@ public class NettyPlayerConnection extends PlayerConnection {
private SocketAddress remoteAddress;
-
private boolean encrypted = false;
private boolean compressed = false;
diff --git a/src/main/java/net/minestom/server/network/player/PlayerConnection.java b/src/main/java/net/minestom/server/network/player/PlayerConnection.java
index 2b69b3983..730836dbd 100644
--- a/src/main/java/net/minestom/server/network/player/PlayerConnection.java
+++ b/src/main/java/net/minestom/server/network/player/PlayerConnection.java
@@ -204,7 +204,6 @@ public abstract class PlayerConnection {
return connectionState;
}
-
/**
* Gets the number of packet the client sent over the last second.
*
diff --git a/src/main/java/net/minestom/server/ping/ResponseData.java b/src/main/java/net/minestom/server/ping/ResponseData.java
index f27feeb6f..6c2a03b6d 100644
--- a/src/main/java/net/minestom/server/ping/ResponseData.java
+++ b/src/main/java/net/minestom/server/ping/ResponseData.java
@@ -9,7 +9,6 @@ import net.minestom.server.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
-import java.util.Collections;
import java.util.List;
import java.util.UUID;
@@ -57,6 +56,7 @@ public class ResponseData {
/**
* Get the version name for the response.
+ *
* @return the version name for the response.
*/
public String getVersion() {
@@ -74,11 +74,13 @@ public class ResponseData {
/**
* Get the response protocol version.
+ *
* @return the response protocol version.
*/
public int getProtocol() {
return protocol;
}
+
/**
* Sets the response maximum player count.
*
@@ -90,11 +92,13 @@ public class ResponseData {
/**
* Get the response maximum player count.
+ *
* @return the response maximum player count.
*/
public int getMaxPlayer() {
return maxPlayer;
}
+
/**
* Sets the response online count.
*
@@ -140,23 +144,19 @@ public class ResponseData {
* @param uuid The unique identifier of the player.
*/
public void addPlayer(String name, UUID uuid) {
- PingPlayer pingPlayer = new PingPlayer();
- pingPlayer.name = name;
- pingPlayer.uuid = uuid;
+ PingPlayer pingPlayer = PingPlayer.of(name, uuid);
this.pingPlayers.add(pingPlayer);
}
/**
* Adds a player to the response.
- *
+ *
* {@link UUID#randomUUID()} is used as the player's UUID.
*
* @param name The name of the player.
*/
public void addPlayer(String name) {
- PingPlayer pingPlayer = new PingPlayer();
- pingPlayer.name = name;
- pingPlayer.uuid = UUID.randomUUID();
+ PingPlayer pingPlayer = PingPlayer.of(name, UUID.randomUUID());
this.pingPlayers.add(pingPlayer);
}
@@ -170,6 +170,7 @@ public class ResponseData {
/**
* Get the list of the response players.
+ *
* @return the list of the response players.
*/
public List getPlayers() {
@@ -207,7 +208,7 @@ public class ResponseData {
/**
* Sets the response favicon.
- *
+ *
* MUST start with "data:image/png;base64,"
*
* @param favicon The favicon for the response data.
@@ -218,6 +219,7 @@ public class ResponseData {
/**
* Get the response favicon.
+ *
* @return the response favicon.
*/
public String getFavicon() {
@@ -267,7 +269,25 @@ public class ResponseData {
* Represents a player line in the server list hover.
*/
public static class PingPlayer {
- public String name;
- public UUID uuid;
+
+ private static @NotNull PingPlayer of(@NotNull String name, @NotNull UUID uuid) {
+ return new PingPlayer(name, uuid);
+ }
+
+ private final String name;
+ private final UUID uuid;
+
+ private PingPlayer(@NotNull String name, @NotNull UUID uuid) {
+ this.name = name;
+ this.uuid = uuid;
+ }
+
+ public @NotNull String getName() {
+ return name;
+ }
+
+ public @NotNull UUID getUuid() {
+ return uuid;
+ }
}
}