From e9ce888d06f064a37b259b09e33ddf46ee3d5ef7 Mon Sep 17 00:00:00 2001 From: Rainnny7 Date: Mon, 8 Apr 2024 23:32:54 -0400 Subject: [PATCH] Update server models --- .../me/braydon/mc/model/MinecraftServer.java | 32 ------ .../model/server/BedrockMinecraftServer.java | 101 +++++++++++++++++- .../mc/model/server/JavaMinecraftServer.java | 55 ++++++++-- 3 files changed, 145 insertions(+), 43 deletions(-) diff --git a/src/main/java/me/braydon/mc/model/MinecraftServer.java b/src/main/java/me/braydon/mc/model/MinecraftServer.java index d862ac8..ec2ef94 100644 --- a/src/main/java/me/braydon/mc/model/MinecraftServer.java +++ b/src/main/java/me/braydon/mc/model/MinecraftServer.java @@ -678,7 +678,6 @@ package me.braydon.mc.model; import lombok.*; import me.braydon.mc.common.ColorUtils; -import me.braydon.mc.config.AppConfig; import me.braydon.mc.service.pinger.MinecraftServerPinger; import me.braydon.mc.service.pinger.impl.BedrockMinecraftServerPinger; import me.braydon.mc.service.pinger.impl.JavaMinecraftServerPinger; @@ -713,11 +712,6 @@ public class MinecraftServer { */ @NonNull private final Players players; - /** - * The favicon of this server, null if none. - */ - private final Favicon favicon; - /** * The MOTD of this server. */ @@ -760,32 +754,6 @@ public class MinecraftServer { } } - /** - * The favicon for a server. - */ - @AllArgsConstructor @Getter @ToString - public static class Favicon { - /** - * The raw Base64 encoded favicon. - */ - @NonNull private final String base64; - - /** - * The URL to the favicon. - */ - @NonNull private final String url; - - public static Favicon create(String base64, @NonNull Platform platform, @NonNull String hostname) { - if (base64 == null) { // No favicon to create - return null; - } - return new Favicon( - base64, - AppConfig.INSTANCE.getServerPublicUrl() + "/server/icon/" + platform.name().toLowerCase() + "/" + hostname - ); - } - } - /** * The MOTD for a server. */ diff --git a/src/main/java/me/braydon/mc/model/server/BedrockMinecraftServer.java b/src/main/java/me/braydon/mc/model/server/BedrockMinecraftServer.java index 44e766a..b1395bf 100644 --- a/src/main/java/me/braydon/mc/model/server/BedrockMinecraftServer.java +++ b/src/main/java/me/braydon/mc/model/server/BedrockMinecraftServer.java @@ -676,7 +676,7 @@ */ package me.braydon.mc.model.server; -import lombok.NonNull; +import lombok.*; import me.braydon.mc.model.MinecraftServer; /** @@ -684,9 +684,102 @@ import me.braydon.mc.model.MinecraftServer; * * @author Braydon */ +@Getter @ToString(callSuper = true) @EqualsAndHashCode(onlyExplicitlyIncluded = true, callSuper = true) public final class BedrockMinecraftServer extends MinecraftServer { - private BedrockMinecraftServer(@NonNull String hostname, String ip, int port, @NonNull Players players, - Favicon favicon, @NonNull MOTD motd) { - super(hostname, ip, port, players, favicon, motd); + /** + * The unique ID of this server. + */ + @EqualsAndHashCode.Include @NonNull private final String uniqueId; + + /** + * The edition of this server. + */ + @NonNull private final Edition edition; + + /** + * The version information of this server. + */ + @NonNull private final Version version; + + /** + * The gamemode of this server. + */ + @NonNull private final GameMode gamemode; + + private BedrockMinecraftServer(@NonNull String uniqueId, @NonNull String hostname, String ip, int port, + @NonNull Edition edition, @NonNull Version version, @NonNull Players players, + @NonNull MOTD motd, @NonNull GameMode gamemode) { + super(hostname, ip, port, players, motd); + this.uniqueId = uniqueId; + this.edition = edition; + this.version = version; + this.gamemode = gamemode; + } + + /** + * Create a new Bedrock Minecraft server. + * + * @param hostname the hostname of the server + * @param ip the IP address of the server + * @param port the port of the server + * @param token the status token + * @return the Bedrock Minecraft server + */ + @NonNull + public static BedrockMinecraftServer create(@NonNull String hostname, String ip, int port, @NonNull String token) { + String[] split = token.split(";"); // Split the token + Version version = new Version(Integer.parseInt(split[2]), split[3]); + Players players = new Players(Integer.parseInt(split[4]), Integer.parseInt(split[5]), null); + MOTD motd = MOTD.create(split[1] + "\n" + split[7]); + GameMode gameMode = new GameMode(split[8], Integer.parseInt(split[9])); + return new BedrockMinecraftServer(split[6], hostname, ip, port, Edition.valueOf(split[0]), version, players, motd, gameMode); + } + + /** + * The edition of a Bedrock server. + */ + @AllArgsConstructor @Getter + public enum Edition { + /** + * Minecraft: Pocket Edition. + */ + MCPE, + + /** + * Minecraft: Education Edition. + */ + MCEE + } + + /** + * Version information for a server. + */ + @AllArgsConstructor @Getter @ToString + public static class Version { + /** + * The protocol version of the server. + */ + private final int protocol; + + /** + * The version name of the server. + */ + @NonNull private final String name; + } + + /** + * The gamemode of a server. + */ + @AllArgsConstructor @Getter @ToString + public static class GameMode { + /** + * The name of this gamemode. + */ + @NonNull private final String name; + + /** + * The numeric of this gamemode. + */ + private final int numericId; } } \ No newline at end of file diff --git a/src/main/java/me/braydon/mc/model/server/JavaMinecraftServer.java b/src/main/java/me/braydon/mc/model/server/JavaMinecraftServer.java index fd06906..733eccc 100644 --- a/src/main/java/me/braydon/mc/model/server/JavaMinecraftServer.java +++ b/src/main/java/me/braydon/mc/model/server/JavaMinecraftServer.java @@ -680,6 +680,7 @@ import com.google.gson.annotations.SerializedName; import lombok.*; import me.braydon.mc.RESTfulMC; import me.braydon.mc.common.MinecraftVersion; +import me.braydon.mc.config.AppConfig; import me.braydon.mc.model.MinecraftServer; import me.braydon.mc.model.token.JavaServerStatusToken; import me.braydon.mc.service.MojangService; @@ -691,13 +692,18 @@ import net.md_5.bungee.chat.ComponentSerializer; * * @author Braydon */ -@Setter @Getter +@Setter @Getter @ToString(callSuper = true) public final class JavaMinecraftServer extends MinecraftServer { /** * The version information of this server. */ @NonNull private final Version version; + /** + * The favicon of this server, null if none. + */ + private final Favicon favicon; + /** * The Forge mod information for this server, null if none. */ @@ -726,11 +732,12 @@ public final class JavaMinecraftServer extends MinecraftServer { */ private boolean mojangBanned; - private JavaMinecraftServer(@NonNull String hostname, String ip, int port, @NonNull Players players, - Favicon favicon, @NonNull MOTD motd, @NonNull Version version, ModInfo modInfo, + private JavaMinecraftServer(@NonNull String hostname, String ip, int port, @NonNull Version version, + @NonNull Players players, @NonNull MOTD motd, Favicon favicon, ModInfo modInfo, boolean enforcesSecureChat, boolean preventsChatReports, boolean mojangBanned) { - super(hostname, ip, port, players, favicon, motd); + super(hostname, ip, port, players, motd); this.version = version; + this.favicon = favicon; this.modInfo = modInfo; this.enforcesSecureChat = enforcesSecureChat; this.preventsChatReports = preventsChatReports; @@ -752,9 +759,9 @@ public final class JavaMinecraftServer extends MinecraftServer { if (motdString == null) { // Not a string motd, convert from Json motdString = new TextComponent(ComponentSerializer.parse(RESTfulMC.GSON.toJson(token.getDescription()))).toLegacyText(); } - return new JavaMinecraftServer(hostname, ip, port, token.getPlayers(), Favicon.create(token.getFavicon(), Platform.JAVA, hostname), - MOTD.create(motdString), token.getVersion().detailedCopy(), token.getModInfo(), token.isEnforcesSecureChat(), - token.isPreventsChatReports(), false + return new JavaMinecraftServer(hostname, ip, port, token.getVersion().detailedCopy(), token.getPlayers(), + MOTD.create(motdString), Favicon.create(token.getFavicon(), Platform.JAVA, hostname), + token.getModInfo(), token.isEnforcesSecureChat(), token.isPreventsChatReports(), false ); } @@ -803,6 +810,40 @@ public final class JavaMinecraftServer extends MinecraftServer { } } + /** + * The favicon for a server. + */ + @AllArgsConstructor @Getter @ToString + public static class Favicon { + /** + * The raw Base64 encoded favicon. + */ + @NonNull private final String base64; + + /** + * The URL to the favicon. + */ + @NonNull private final String url; + + /** + * Create a new favicon for a server. + * + * @param base64 the Base64 encoded favicon + * @param platform the platform to create the favicon for + * @param hostname the server hostname + * @return the favicon, null if none + */ + public static Favicon create(String base64, @NonNull Platform platform, @NonNull String hostname) { + if (base64 == null) { // No favicon to create + return null; + } + return new Favicon( + base64, + AppConfig.INSTANCE.getServerPublicUrl() + "/server/icon/" + platform.name().toLowerCase() + "/" + hostname + ); + } + } + /** * Forge mod information for a server. */