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.
This commit is contained in:
thiccaxe 2021-04-10 21:14:36 -07:00
parent 61242dc185
commit 8a9abff98b
4 changed files with 39 additions and 166 deletions

View File

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

View File

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

View File

@ -15,8 +15,7 @@ import java.util.UUID;
/**
* Represents the data sent to the player when refreshing the server list.
*
* <p>Filled by {@link ResponseDataConsumer} and specified in {@link
* net.minestom.server.MinecraftServer#start(String, int, ResponseDataConsumer)}.
* <p>Edited by listening to the {@link net.minestom.server.event.server.ServerListPingEvent}.
*/
public class ResponseData {
private final List<PingPlayer> 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.

View File

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