diff --git a/src/main/java/vip/fubuki/playersync/config/JdbcConfig.java b/src/main/java/vip/fubuki/playersync/config/JdbcConfig.java index da7f978..dbe3dad 100644 --- a/src/main/java/vip/fubuki/playersync/config/JdbcConfig.java +++ b/src/main/java/vip/fubuki/playersync/config/JdbcConfig.java @@ -36,7 +36,7 @@ public class JdbcConfig { PASSWORD = COMMON_BUILDER.comment("password").define("password", "pleaseChangeThisPassword"); 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)); - SYNC_WORLD = COMMON_BUILDER.comment("The worlds that will be synchronized.If running in server it is supposed to have only one").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_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); CHAT_SERVER_IP = COMMON_BUILDER.define("ChatServerIP","127.0.0.1"); diff --git a/src/main/java/vip/fubuki/playersync/sync/VanillaSync.java b/src/main/java/vip/fubuki/playersync/sync/VanillaSync.java index 1fb44a7..897db93 100644 --- a/src/main/java/vip/fubuki/playersync/sync/VanillaSync.java +++ b/src/main/java/vip/fubuki/playersync/sync/VanillaSync.java @@ -12,6 +12,7 @@ import net.minecraft.world.effect.MobEffectInstance; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.storage.WorldData; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.event.TickEvent; import net.minecraftforge.event.entity.player.PlayerEvent; @@ -142,7 +143,7 @@ public class VanillaSync { // Restore Advancements File gameDir = Objects.requireNonNull(serverPlayer.getServer()).getServerDirectory(); if (Dist.CLIENT.isDedicatedServer()){ - File advancements = new File(gameDir, JdbcConfig.SYNC_WORLD.get().get(0) + "/advancements" + "/" + player_uuid + ".json"); + File advancements = new File(gameDir, getSyncWorldForServer() + "/advancements" + "/" + player_uuid + ".json"); if (!advancements.exists()) { advancements.createNewFile(); } @@ -319,7 +320,7 @@ public class VanillaSync { File advancements = null; File gameDir = Objects.requireNonNull(player.getServer()).getServerDirectory(); if (isServer) { - advancements = new File(gameDir, JdbcConfig.SYNC_WORLD.get().get(0) + "/advancements" + "/" + player_uuid + ".json"); + advancements = new File(gameDir, getSyncWorldForServer() + "/advancements" + "/" + player_uuid + ".json"); } else { File[] files = scanAdvancementsFile(player_uuid, gameDir); long latestModifiedDate = 0; @@ -345,6 +346,25 @@ public class VanillaSync { } } + private static String getSyncWorldForServer() { + if (!JdbcConfig.SYNC_WORLD.get().isEmpty()) { + PlayerSync.LOGGER.warn("Using configuration 'sync_world' on servers is deprecated. Please leave the array empty. Falling back to first entry."); + return JdbcConfig.SYNC_WORLD.get().get(0); + } + + final MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); + if (server == null) { + PlayerSync.LOGGER.error("Unable to get current server. Assuming default level-name 'world'."); + return "world"; + } + + final WorldData worldData = server.getWorldData(); + final String levelName = worldData.getLevelName(); + PlayerSync.LOGGER.debug("Using server level-name: " + levelName); + + return levelName; + } + private static File[] scanAdvancementsFile(String player_uuid, File gameDir) { File[] files = new File[JdbcConfig.SYNC_WORLD.get().size()]; for (int i = 0; i < JdbcConfig.SYNC_WORLD.get().size(); i++) {