Merge pull request #53 from EoD/advancement-sync-optional

make advancement sync optional
This commit is contained in:
mlus 2025-05-03 02:02:40 +08:00 committed by GitHub
commit f43c47f78d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 22 deletions

View File

@ -16,6 +16,7 @@ public class JdbcConfig {
public static ForgeConfigSpec.ConfigValue<String> PASSWORD; public static ForgeConfigSpec.ConfigValue<String> PASSWORD;
public static ForgeConfigSpec.ConfigValue<String> DATABASE_NAME; public static ForgeConfigSpec.ConfigValue<String> DATABASE_NAME;
public static ForgeConfigSpec.ConfigValue<List<String>> SYNC_WORLD; public static ForgeConfigSpec.ConfigValue<List<String>> SYNC_WORLD;
public static ForgeConfigSpec.BooleanValue SYNC_ADVANCEMENTS;
public static ForgeConfigSpec.BooleanValue USE_SSL; public static ForgeConfigSpec.BooleanValue USE_SSL;
public static ForgeConfigSpec.BooleanValue SYNC_CHAT; public static ForgeConfigSpec.BooleanValue SYNC_CHAT;
public static ForgeConfigSpec.BooleanValue IS_CHAT_SERVER; public static ForgeConfigSpec.BooleanValue IS_CHAT_SERVER;
@ -37,6 +38,8 @@ public class JdbcConfig {
DATABASE_NAME = COMMON_BUILDER.comment("database name").define("db_name","playersync"); DATABASE_NAME = COMMON_BUILDER.comment("database name").define("db_name","playersync");
SERVER_ID = COMMON_BUILDER.comment("the server id should be unique").define("Server_id", new Random().nextInt(1,Integer.MAX_VALUE-1)); SERVER_ID = COMMON_BUILDER.comment("the server id should be unique").define("Server_id", new Random().nextInt(1,Integer.MAX_VALUE-1));
SYNC_WORLD = COMMON_BUILDER.comment("The worlds that will be synchronized. If running on a server, leave array empty.").define("sync_world", new ArrayList<>()); SYNC_WORLD = COMMON_BUILDER.comment("The worlds that will be synchronized. If running on a server, leave array empty.").define("sync_world", new ArrayList<>());
SYNC_ADVANCEMENTS = COMMON_BUILDER.comment("Whether to sync advancements between servers")
.define("sync_advancements", true);
SYNC_CHAT= COMMON_BUILDER.comment("Whether synchronize chat").define("sync_chat", true); SYNC_CHAT= COMMON_BUILDER.comment("Whether synchronize chat").define("sync_chat", true);
IS_CHAT_SERVER = COMMON_BUILDER.comment("Whether recieve messages from other servers as host").define("IsChatServer",false); 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_IP = COMMON_BUILDER.define("ChatServerIP","127.0.0.1");

View File

@ -48,6 +48,9 @@ public class VanillaSync {
@SubscribeEvent @SubscribeEvent
public static void onDataPackSyncEvent(OnDatapackSyncEvent event) throws SQLException, IOException { public static void onDataPackSyncEvent(OnDatapackSyncEvent event) throws SQLException, IOException {
if (!JdbcConfig.SYNC_ADVANCEMENTS.get())
return; // advancement sync disabled
final ServerPlayer serverPlayer = event.getPlayer(); final ServerPlayer serverPlayer = event.getPlayer();
if (serverPlayer == null) { if (serverPlayer == null) {
PlayerSync.LOGGER.debug("No player joining"); PlayerSync.LOGGER.debug("No player joining");
@ -364,8 +367,11 @@ public class VanillaSync {
CompoundTag effectTag = entry.getValue().save(new CompoundTag()); CompoundTag effectTag = entry.getValue().save(new CompoundTag());
effectMap.put(MobEffect.getId(entry.getKey()), serialize(effectTag.toString())); effectMap.put(MobEffect.getId(entry.getKey()), serialize(effectTag.toString()));
} }
// Advancements // Advancements
File advancements = null; File advancements = null;
byte[] advancementBytes = new byte[0];
if (JdbcConfig.SYNC_ADVANCEMENTS.get()) {
File gameDir = Objects.requireNonNull(player.getServer()).getServerDirectory(); File gameDir = Objects.requireNonNull(player.getServer()).getServerDirectory();
final MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); final MinecraftServer server = ServerLifecycleHooks.getCurrentServer();
if (server != null && server.isDedicatedServer() ) { if (server != null && server.isDedicatedServer() ) {
@ -383,14 +389,14 @@ public class VanillaSync {
} }
} }
} }
byte[] bytes = new byte[0];
if (advancements != null) { if (advancements != null) {
PlayerSync.LOGGER.debug("Storing advancements for " + player_uuid + " from " + advancements.toPath()); PlayerSync.LOGGER.debug("Storing advancements for " + player_uuid + " from " + advancements.toPath());
bytes = Files.readAllBytes(advancements.toPath()); advancementBytes = Files.readAllBytes(advancements.toPath());
} else { } else {
PlayerSync.LOGGER.error("Unable to save advancements for player " + player_uuid); PlayerSync.LOGGER.error("Unable to save advancements for player " + player_uuid);
} }
String json = new String(bytes, StandardCharsets.UTF_8); }
String json = new String(advancementBytes, StandardCharsets.UTF_8);
PlayerSync.LOGGER.trace("Storing advancements for player " + player_uuid + ": " + json); PlayerSync.LOGGER.trace("Storing advancements for player " + player_uuid + ": " + json);
// SQL Operation for player data // SQL Operation for player data