From d2d73973e348981e32eb637108ce26b206f622a2 Mon Sep 17 00:00:00 2001 From: MC_XiaoHei Date: Sun, 12 Oct 2025 17:50:43 +0800 Subject: [PATCH] feat: bot resume autosave migrate --- .../java/org/leavesmc/leaves/bot/BotList.java | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/BotList.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/BotList.java index b0ea29ba..82e5b9a7 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/BotList.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/BotList.java @@ -62,6 +62,7 @@ public class BotList { private final Map botsByUUID = Maps.newHashMap(); private final Map botsByName = Maps.newHashMap(); private final Map> botsNameByWorldUuid = Maps.newHashMap(); + private final Map> legacyBotsNameByWorldUuid = Maps.newHashMap(); public BotList(@NotNull MinecraftServer server) { this.server = server; @@ -303,10 +304,6 @@ public class BotList { } CompoundTag savedBotList = this.getResumeBotList().copy(); for (String realName : savedBotList.keySet()) { - CompoundTag nbt = savedBotList.getCompound(realName).orElseThrow(); - if (!nbt.getBoolean("resume").orElse(false)) { - continue; - } UUID levelUuid = BotUtil.getBotLevel(realName, this.resumeDataStorage); if (levelUuid == null) { LOGGER.warn("Bot {} has no world UUID, skipping loading.", realName); @@ -316,18 +313,33 @@ public class BotList { .computeIfAbsent(levelUuid.toString(), (k) -> new HashSet<>()) .add(realName); } + loadLegacyResumeBotInfo(); + } + + private void loadLegacyResumeBotInfo() { + CompoundTag savedBotList = this.getManualSavedBotList().copy(); + for (String realName : savedBotList.keySet()) { + CompoundTag nbt = savedBotList.getCompound(realName).orElseThrow(); + if (!nbt.getBoolean("resume").orElse(false)) { + continue; + } + UUID levelUuid = BotUtil.getBotLevel(realName, this.manualSaveDataStorage); + if (levelUuid == null) { + LOGGER.warn("Bot {} has no world UUID, skipping loading.", realName); + continue; + } + this.legacyBotsNameByWorldUuid + .computeIfAbsent(levelUuid.toString(), (k) -> new HashSet<>()) + .add(realName); + } } public void loadResume(String worldUuid) { if (!LeavesConfig.modify.fakeplayer.enable || !LeavesConfig.modify.fakeplayer.canResident) { return; } - Set bots = this.botsNameByWorldUuid.get(worldUuid); - if (bots == null) { - return; - } - Set botsCopy = new HashSet<>(bots); - botsCopy.forEach(this::loadNewResumeBot); + new HashSet<>(this.botsNameByWorldUuid.getOrDefault(worldUuid, new HashSet<>())).forEach(this::loadNewResumeBot); + new HashSet<>(this.legacyBotsNameByWorldUuid.getOrDefault(worldUuid, new HashSet<>())).forEach(this::loadNewManualSavedBot); } public void updateBotLevel(@NotNull ServerBot bot, @NotNull ServerLevel level) {