9
0
mirror of https://github.com/LeavesMC/Leaves.git synced 2025-12-29 20:09:23 +00:00
This commit is contained in:
MC_XiaoHei
2025-07-03 19:42:52 +08:00
parent fd658a0c19
commit 1aea18e943
4 changed files with 30 additions and 25 deletions

View File

@@ -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<CompoundTag> load(Player player) {
public Optional<ValueInput> 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;
});
}

View File

@@ -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<CompoundTag> optional = playerIO.load(bot);
Optional<ValueInput> 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<Level> 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<CompoundTag> optional = Optional.ofNullable(save);
public ServerBot placeNewBot(@NotNull ServerBot bot, ServerLevel world, Location location, ValueInput save) {
Optional<ValueInput> 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)));

View File

@@ -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;

View File

@@ -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<CompoundTag> 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<CompoundTag> 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<CompoundTag> 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<CompoundTag> 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);