9
0
mirror of https://github.com/LeavesMC/Leaves.git synced 2025-12-19 14:59:32 +00:00

feat: fix some error again

This commit is contained in:
MC_XiaoHei
2025-09-26 07:07:37 +08:00
parent def2249b48
commit e0cf524085
4 changed files with 20 additions and 23 deletions

View File

@@ -5,12 +5,10 @@ import net.minecraft.core.UUIDUtil;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtAccounter; import net.minecraft.nbt.NbtAccounter;
import net.minecraft.nbt.NbtIo; import net.minecraft.nbt.NbtIo;
import net.minecraft.util.ProblemReporter; import net.minecraft.server.players.NameAndId;
import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.storage.LevelResource; import net.minecraft.world.level.storage.LevelResource;
import net.minecraft.world.level.storage.LevelStorageSource; 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.jetbrains.annotations.NotNull;
import org.leavesmc.leaves.util.TagUtil; import org.leavesmc.leaves.util.TagUtil;
import org.slf4j.Logger; import org.slf4j.Logger;
@@ -76,17 +74,10 @@ public class BotDataStorage implements IPlayerDataStorage {
} }
} }
@Override
public Optional<ValueInput> load(Player player, ProblemReporter reporter) {
return this.load(player.getScoreboardName(), player.getStringUUID()).map(nbt -> {
ValueInput valueInput = TagValueInput.create(reporter, player.registryAccess(), nbt);
player.load(valueInput);
return valueInput;
});
}
private Optional<CompoundTag> load(String name, String uuid) { @Override
File file = new File(this.botDir, uuid + ".dat"); public Optional<CompoundTag> load(NameAndId nameAndId) {
File file = new File(this.botDir, nameAndId.id() + ".dat");
if (file.exists() && file.isFile()) { if (file.exists() && file.isFile()) {
try { try {
@@ -94,11 +85,11 @@ public class BotDataStorage implements IPlayerDataStorage {
if (!file.delete()) { if (!file.delete()) {
throw new IOException("Failed to delete fakeplayer data"); throw new IOException("Failed to delete fakeplayer data");
} }
this.savedBotList.remove(name); this.savedBotList.remove(nameAndId.name());
this.saveBotList(); this.saveBotList();
return optional; return optional;
} catch (Exception exception) { } catch (Exception exception) {
BotDataStorage.LOGGER.warn("Failed to load fakeplayer data for {}", name); BotDataStorage.LOGGER.warn("Failed to load fakeplayer data for {}", nameAndId.name());
} }
} }

View File

@@ -20,9 +20,11 @@ import net.minecraft.world.entity.npc.AbstractVillager;
import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.projectile.ThrownEnderpearl; import net.minecraft.world.entity.projectile.ThrownEnderpearl;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import net.minecraft.world.level.storage.TagValueInput;
import net.minecraft.world.level.storage.ValueInput; import net.minecraft.world.level.storage.ValueInput;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.craftbukkit.CraftWorld; import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.event.entity.EntityRemoveEvent; import org.bukkit.event.entity.EntityRemoveEvent;
@@ -106,7 +108,12 @@ public class BotList {
bot.connection = new ServerBotPacketListenerImpl(this.server, bot); bot.connection = new ServerBotPacketListenerImpl(this.server, bot);
Optional<ValueInput> optional; Optional<ValueInput> optional;
try (ProblemReporter.ScopedCollector scopedCollector = new ProblemReporter.ScopedCollector(bot.problemPath(), LOGGER)) { try (ProblemReporter.ScopedCollector scopedCollector = new ProblemReporter.ScopedCollector(bot.problemPath(), LOGGER)) {
optional = playerIO.load(bot, scopedCollector); Optional<CompoundTag> nbt = playerIO.load(bot.nameAndId());
if (nbt.isEmpty()) {
optional = Optional.empty();
} else {
optional = Optional.of(TagValueInput.create(scopedCollector, bot.registryAccess(), nbt.orElseThrow()));
}
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@@ -118,7 +125,7 @@ public class BotList {
ResourceKey<Level> resourcekey = null; ResourceKey<Level> resourcekey = null;
if (nbt.getLong("WorldUUIDMost").isPresent() && nbt.getLong("WorldUUIDLeast").isPresent()) { 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())); World bWorld = Bukkit.getServer().getWorld(new UUID(nbt.getLong("WorldUUIDMost").orElseThrow(), nbt.getLong("WorldUUIDLeast").orElseThrow()));
if (bWorld != null) { if (bWorld != null) {
resourcekey = ((CraftWorld) bWorld).getHandle().dimension(); resourcekey = ((CraftWorld) bWorld).getHandle().dimension();
} }

View File

@@ -1,8 +1,8 @@
package org.leavesmc.leaves.bot; package org.leavesmc.leaves.bot;
import net.minecraft.util.ProblemReporter; import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.players.NameAndId;
import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.storage.ValueInput;
import java.util.Optional; import java.util.Optional;
@@ -10,5 +10,5 @@ public interface IPlayerDataStorage {
void save(Player player); void save(Player player);
Optional<ValueInput> load(Player player, ProblemReporter reporter); Optional<CompoundTag> load(NameAndId nameAndId);
} }

View File

@@ -4,7 +4,6 @@ import com.google.common.collect.Lists;
import com.mojang.serialization.Codec; import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec; import com.mojang.serialization.MapCodec;
import net.minecraft.core.component.DataComponents; import net.minecraft.core.component.DataComponents;
import net.minecraft.nbt.NbtOps;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.component.CustomData; import net.minecraft.world.item.component.CustomData;
@@ -36,9 +35,9 @@ public enum CampfireProvider implements IServerExtensionProvider<ItemStack> {
} }
stack = stack.copy(); stack = stack.copy();
int finalI = i;
CustomData customData = stack.getOrDefault(DataComponents.CUSTOM_DATA, CustomData.EMPTY) CustomData customData = stack.getOrDefault(DataComponents.CUSTOM_DATA, CustomData.EMPTY)
.update(NbtOps.INSTANCE, COOKING_TIME_CODEC, campfire.cookingTime[i] - campfire.cookingProgress[i]) .update(tag -> tag.store(COOKING_TIME_CODEC, campfire.cookingTime[finalI] - campfire.cookingProgress[finalI]));
.getOrThrow();
stack.set(DataComponents.CUSTOM_DATA, customData); stack.set(DataComponents.CUSTOM_DATA, customData);
list.add(stack); list.add(stack);