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
This commit is contained in:
thiccaxe 2021-04-08 16:51:50 -07:00
parent cad128a393
commit 94c4ec79e1
8 changed files with 141 additions and 140 deletions

View File

@ -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();

View File

@ -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<Player> players) {
public void addPlayer(@NotNull Iterable<Player> 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;
}
}

View File

@ -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));

View File

@ -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) {}
}

View File

@ -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.
* <p>
@ -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() {

View File

@ -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.
* <p>
* 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.
* <p>
* 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.

View File

@ -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;
}
}

View File

@ -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