Update server models

This commit is contained in:
Braydon 2024-04-08 23:32:54 -04:00
parent 8cca0c2b51
commit e9ce888d06
3 changed files with 145 additions and 43 deletions

@ -678,7 +678,6 @@ package me.braydon.mc.model;
import lombok.*; import lombok.*;
import me.braydon.mc.common.ColorUtils; 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.MinecraftServerPinger;
import me.braydon.mc.service.pinger.impl.BedrockMinecraftServerPinger; import me.braydon.mc.service.pinger.impl.BedrockMinecraftServerPinger;
import me.braydon.mc.service.pinger.impl.JavaMinecraftServerPinger; import me.braydon.mc.service.pinger.impl.JavaMinecraftServerPinger;
@ -713,11 +712,6 @@ public class MinecraftServer {
*/ */
@NonNull private final Players players; @NonNull private final Players players;
/**
* The favicon of this server, null if none.
*/
private final Favicon favicon;
/** /**
* The MOTD of this server. * 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. * The MOTD for a server.
*/ */

@ -676,7 +676,7 @@
*/ */
package me.braydon.mc.model.server; package me.braydon.mc.model.server;
import lombok.NonNull; import lombok.*;
import me.braydon.mc.model.MinecraftServer; import me.braydon.mc.model.MinecraftServer;
/** /**
@ -684,9 +684,102 @@ import me.braydon.mc.model.MinecraftServer;
* *
* @author Braydon * @author Braydon
*/ */
@Getter @ToString(callSuper = true) @EqualsAndHashCode(onlyExplicitlyIncluded = true, callSuper = true)
public final class BedrockMinecraftServer extends MinecraftServer { public final class BedrockMinecraftServer extends MinecraftServer {
private BedrockMinecraftServer(@NonNull String hostname, String ip, int port, @NonNull Players players, /**
Favicon favicon, @NonNull MOTD motd) { * The unique ID of this server.
super(hostname, ip, port, players, favicon, motd); */
@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;
} }
} }

@ -680,6 +680,7 @@ import com.google.gson.annotations.SerializedName;
import lombok.*; import lombok.*;
import me.braydon.mc.RESTfulMC; import me.braydon.mc.RESTfulMC;
import me.braydon.mc.common.MinecraftVersion; import me.braydon.mc.common.MinecraftVersion;
import me.braydon.mc.config.AppConfig;
import me.braydon.mc.model.MinecraftServer; import me.braydon.mc.model.MinecraftServer;
import me.braydon.mc.model.token.JavaServerStatusToken; import me.braydon.mc.model.token.JavaServerStatusToken;
import me.braydon.mc.service.MojangService; import me.braydon.mc.service.MojangService;
@ -691,13 +692,18 @@ import net.md_5.bungee.chat.ComponentSerializer;
* *
* @author Braydon * @author Braydon
*/ */
@Setter @Getter @Setter @Getter @ToString(callSuper = true)
public final class JavaMinecraftServer extends MinecraftServer { public final class JavaMinecraftServer extends MinecraftServer {
/** /**
* The version information of this server. * The version information of this server.
*/ */
@NonNull private final Version version; @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. * The Forge mod information for this server, null if none.
*/ */
@ -726,11 +732,12 @@ public final class JavaMinecraftServer extends MinecraftServer {
*/ */
private boolean mojangBanned; private boolean mojangBanned;
private JavaMinecraftServer(@NonNull String hostname, String ip, int port, @NonNull Players players, private JavaMinecraftServer(@NonNull String hostname, String ip, int port, @NonNull Version version,
Favicon favicon, @NonNull MOTD motd, @NonNull Version version, ModInfo modInfo, @NonNull Players players, @NonNull MOTD motd, Favicon favicon, ModInfo modInfo,
boolean enforcesSecureChat, boolean preventsChatReports, boolean mojangBanned) { boolean enforcesSecureChat, boolean preventsChatReports, boolean mojangBanned) {
super(hostname, ip, port, players, favicon, motd); super(hostname, ip, port, players, motd);
this.version = version; this.version = version;
this.favicon = favicon;
this.modInfo = modInfo; this.modInfo = modInfo;
this.enforcesSecureChat = enforcesSecureChat; this.enforcesSecureChat = enforcesSecureChat;
this.preventsChatReports = preventsChatReports; this.preventsChatReports = preventsChatReports;
@ -752,9 +759,9 @@ public final class JavaMinecraftServer extends MinecraftServer {
if (motdString == null) { // Not a string motd, convert from Json if (motdString == null) { // Not a string motd, convert from Json
motdString = new TextComponent(ComponentSerializer.parse(RESTfulMC.GSON.toJson(token.getDescription()))).toLegacyText(); 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), return new JavaMinecraftServer(hostname, ip, port, token.getVersion().detailedCopy(), token.getPlayers(),
MOTD.create(motdString), token.getVersion().detailedCopy(), token.getModInfo(), token.isEnforcesSecureChat(), MOTD.create(motdString), Favicon.create(token.getFavicon(), Platform.JAVA, hostname),
token.isPreventsChatReports(), false 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. * Forge mod information for a server.
*/ */