From 1aea18e9434fda9a3d465567aa69b2c91067b4ce Mon Sep 17 00:00:00 2001 From: MC_XiaoHei Date: Thu, 3 Jul 2025 19:42:52 +0800 Subject: [PATCH] fix bot --- .../leavesmc/leaves/bot/BotDataStorage.java | 10 ++++++--- .../java/org/leavesmc/leaves/bot/BotList.java | 22 ++++++++++++------- .../leaves/bot/IPlayerDataStorage.java | 1 - .../org/leavesmc/leaves/bot/ServerBot.java | 22 ++++++++----------- 4 files changed, 30 insertions(+), 25 deletions(-) diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/BotDataStorage.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/BotDataStorage.java index 73da10d7..f05cb095 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/BotDataStorage.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/BotDataStorage.java @@ -5,9 +5,12 @@ import net.minecraft.core.UUIDUtil; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.NbtAccounter; import net.minecraft.nbt.NbtIo; +import net.minecraft.util.ProblemReporter; import net.minecraft.world.entity.player.Player; import net.minecraft.world.level.storage.LevelResource; import net.minecraft.world.level.storage.LevelStorageSource; +import net.minecraft.world.level.storage.TagValueInput; +import net.minecraft.world.level.storage.ValueInput; import org.jetbrains.annotations.NotNull; import org.leavesmc.leaves.util.TagUtil; import org.slf4j.Logger; @@ -74,10 +77,11 @@ public class BotDataStorage implements IPlayerDataStorage { } @Override - public Optional load(Player player) { + public Optional load(Player player, ProblemReporter reporter) { return this.load(player.getScoreboardName(), player.getStringUUID()).map(nbt -> { - TagUtil.loadEntity(player, nbt); - return nbt; + ValueInput valueInput = TagValueInput.create(reporter, player.registryAccess(), nbt); + player.load(valueInput); + return valueInput; }); } 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 3c99268f..93a065bd 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 @@ -14,9 +14,11 @@ import net.minecraft.resources.ResourceKey; import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; +import net.minecraft.util.ProblemReporter; import net.minecraft.world.entity.Entity; import net.minecraft.world.level.Level; import net.minecraft.world.level.storage.TagValueInput; +import net.minecraft.world.level.storage.ValueInput; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.command.CommandSender; @@ -96,15 +98,20 @@ public class BotList { ServerBot bot = new ServerBot(this.server, this.server.getLevel(Level.OVERWORLD), new GameProfile(uuid, realName)); bot.connection = new ServerBotPacketListenerImpl(this.server, bot); - Optional optional = playerIO.load(bot); + Optional optional; + try (ProblemReporter.ScopedCollector scopedCollector = new ProblemReporter.ScopedCollector(bot.problemPath(), LOGGER)) { + optional = playerIO.load(bot, scopedCollector); + } catch (Exception e) { + throw new RuntimeException(e); + } if (optional.isEmpty()) { return null; } - CompoundTag nbt = optional.get(); + ValueInput nbt = optional.get(); ResourceKey resourcekey = null; - if (nbt.contains("WorldUUIDMost") && nbt.contains("WorldUUIDLeast")) { + if (nbt.getLong("WorldUUIDMost").isPresent() && nbt.getLong("WorldUUIDLeast").isPresent()) { org.bukkit.World bWorld = Bukkit.getServer().getWorld(new UUID(nbt.getLong("WorldUUIDMost").orElseThrow(), nbt.getLong("WorldUUIDLeast").orElseThrow())); if (bWorld != null) { resourcekey = ((CraftWorld) bWorld).getHandle().dimension(); @@ -118,8 +125,8 @@ public class BotList { return this.placeNewBot(bot, world, bot.getLocation(), nbt); } - public ServerBot placeNewBot(@NotNull ServerBot bot, ServerLevel world, Location location, @Nullable CompoundTag save) { - Optional optional = Optional.ofNullable(save); + public ServerBot placeNewBot(@NotNull ServerBot bot, ServerLevel world, Location location, ValueInput save) { + Optional optional = Optional.ofNullable(save); bot.isRealPlayer = true; bot.loginTime = System.currentTimeMillis(); @@ -145,9 +152,8 @@ public class BotList { bot.supressTrackerForLogin = true; world.addNewPlayer(bot); optional.ifPresent(nbt -> { - TagValueInput input = TagFactory.input(nbt); - bot.loadAndSpawnEnderPearls(input); - bot.loadAndSpawnParentVehicle(input); + bot.loadAndSpawnEnderPearls(nbt); + bot.loadAndSpawnParentVehicle(nbt); }); BotJoinEvent event1 = new BotJoinEvent(bot.getBukkitEntity(), PaperAdventure.asAdventure(Component.translatable("multiplayer.player.joined", bot.getDisplayName())).style(Style.style(NamedTextColor.YELLOW))); diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/IPlayerDataStorage.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/IPlayerDataStorage.java index 36f10f17..614fb645 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/IPlayerDataStorage.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/IPlayerDataStorage.java @@ -1,6 +1,5 @@ package org.leavesmc.leaves.bot; -import net.minecraft.nbt.CompoundTag; import net.minecraft.util.ProblemReporter; import net.minecraft.world.entity.player.Player; import net.minecraft.world.level.storage.ValueInput; diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/ServerBot.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/ServerBot.java index c72b8409..6ad93ec5 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/ServerBot.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/ServerBot.java @@ -378,19 +378,17 @@ public class ServerBot extends ServerPlayer { nbt.store("createStatus", CompoundTag.CODEC, createNbt); if (!this.actions.isEmpty()) { - ListTag actionNbt = new ListTag(); + ValueOutput.TypedOutputList actionNbt = nbt.list("actions", CompoundTag.CODEC); for (AbstractBotAction action : this.actions) { actionNbt.add(action.save(new CompoundTag())); } - nbt.put("actions", actionNbt); } if (!this.configs.isEmpty()) { - ListTag configNbt = new ListTag(); + ValueOutput.TypedOutputList configNbt = nbt.list("configs", CompoundTag.CODEC); for (AbstractBotConfig config : this.configs.values()) { configNbt.add(config.save(new CompoundTag())); } - nbt.put("configs", configNbt); } } @@ -418,23 +416,21 @@ public class ServerBot extends ServerPlayer { this.gameProfile = new BotList.CustomGameProfile(this.getUUID(), this.createState.name(), this.createState.skin()); - if (nbt.contains("actions")) { - ListTag actionNbt = nbt.getList("actions").orElseThrow(); - for (int i = 0; i < actionNbt.size(); i++) { - CompoundTag actionTag = actionNbt.getCompound(i).orElseThrow(); + if (nbt.list("actions", CompoundTag.CODEC).isPresent()) { + ValueInput.TypedInputList actionNbt = nbt.list("actions", CompoundTag.CODEC).orElseThrow(); + actionNbt.forEach(actionTag -> { AbstractBotAction action = Actions.getForName(actionTag.getString("actionName").orElseThrow()); if (action != null) { AbstractBotAction newAction = action.create(); newAction.load(actionTag); this.actions.add(newAction); } - } + }); } - if (nbt.contains("configs")) { - ListTag configNbt = nbt.getList("configs").orElseThrow(); - for (int i = 0; i < configNbt.size(); i++) { - CompoundTag configTag = configNbt.getCompound(i).orElseThrow(); + if (nbt.list("configs", CompoundTag.CODEC).isPresent()) { + ValueInput.TypedInputList configNbt = nbt.list("configs", CompoundTag.CODEC).orElseThrow(); + for (CompoundTag configTag : configNbt) { Configs configKey = Configs.getConfig(configTag.getString("configName").orElseThrow()); if (configKey != null) { this.configs.get(configKey).load(configTag);