diff --git a/src/main/java/vip/fubuki/playersync/config/JdbcConfig.java b/src/main/java/vip/fubuki/playersync/config/JdbcConfig.java index d51ce1f..7852560 100644 --- a/src/main/java/vip/fubuki/playersync/config/JdbcConfig.java +++ b/src/main/java/vip/fubuki/playersync/config/JdbcConfig.java @@ -21,6 +21,7 @@ public class JdbcConfig { public static ForgeConfigSpec.BooleanValue IS_CHAT_SERVER; public static ForgeConfigSpec.ConfigValue CHAT_SERVER_IP; public static ForgeConfigSpec.IntValue CHAT_SERVER_PORT; + public static ForgeConfigSpec.BooleanValue USE_LEGACY_SERIALIZATION; public static ForgeConfigSpec.ConfigValue SERVER_ID; @@ -40,8 +41,13 @@ public class JdbcConfig { IS_CHAT_SERVER = COMMON_BUILDER.comment("Whether recieve messages from other servers as host").define("IsChatServer",false); CHAT_SERVER_IP = COMMON_BUILDER.define("ChatServerIP","127.0.0.1"); CHAT_SERVER_PORT = COMMON_BUILDER.defineInRange("ChatServerPort",7900,0,65535); + USE_LEGACY_SERIALIZATION = COMMON_BUILDER.comment( + "Use the old (pre-Base64) serialization format for writing data to the database.", + "Set to true ONLY if you have older mod versions reading the same database.", + "This only affects writing data, the mod can read both Base64 and pre-Base64 serialization.", + "New installations should leave this as 'false'." + ).define("use_legacy_serialization", false); COMMON_BUILDER.pop(); COMMON_CONFIG = COMMON_BUILDER.build(); } } - diff --git a/src/main/java/vip/fubuki/playersync/sync/VanillaSync.java b/src/main/java/vip/fubuki/playersync/sync/VanillaSync.java index aa296af..0375cc0 100644 --- a/src/main/java/vip/fubuki/playersync/sync/VanillaSync.java +++ b/src/main/java/vip/fubuki/playersync/sync/VanillaSync.java @@ -188,6 +188,12 @@ public class VanillaSync { return ItemStack.of(compoundTag); } + /** + * Deserializes a string from the database back into an NBT string. + * Handles both the new Base64 format (prefixed with "B64:") and the old custom format. + * @param encoded The string retrieved from the database. + * @return The deserialized NBT string. + */ public static String deserializeString(String encoded) { if (encoded.startsWith("B64:")) { String base64 = encoded.substring(4); @@ -206,7 +212,24 @@ public class VanillaSync { .replace("~", "'"); } + /** + * Serializes an NBT string for database storage. + * Uses Base64 encoding by default (prefixed with "B64:"). + * If USE_LEGACY_SERIALIZATION config is true, uses the old custom replacement format. + * @param object The NBT string to serialize. + * @return The serialized string. + */ public static String serialize(String object) { + // Check the config option for backwards compatibility during writing + if (JdbcConfig.USE_LEGACY_SERIALIZATION.get()) { + // Use old custom replacement logic + return objeƄct.replace(",", "|") + .replace("\"", "^") + .replace("{", "<") + .replace("}", ">") + .replace("'", "~"); + } + // Base64 encode with a "B64:" marker for new data return "B64:" + Base64.getEncoder().encodeToString(object.getBytes(StandardCharsets.UTF_8)); }