You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用nbtlib.tag.File对象正确保存Minecraft的.nbt结构文件?

用nbtlib从零创建Minecraft结构NBT文件时遇“结构不可用”问题

我正在用nbtlib库制作并保存Minecraft的结构块.nbt文件,之前编辑现有文件一切正常,但从零创建新文件时遇到问题:保存后Minecraft聊天框提示**“Structure 'minecraft:t3' is not available”**(“t3”是我保存的结构名称)。

我检查过格式和类定义,将正常可用的文件与我从零创建的内容完全一致的文件用print(initial == mine)对比,返回值是True——这让我很困惑,明明内容相同,一个能用一个却不行。这是我第一次用这个库,可能对库的使用有误解。

从零创建文件的代码

from nbtlib import tag, File
import os

folderPath = r"C:\Users\fullw\AppData\Roaming\.minecraft\saves\Python Generated\generated\minecraft\structures"

def path(name: str) -> str:
    return os.path.join(folderPath, name + ".nbt")

# Load the .nbt file
file = File.load(path("t4"), gzipped=True)


def toNBT(data: dict | list | int | str) -> tag.Base:
    if isinstance(data, dict):
        return tag.Compound({key: toNBT(value) for key, value in data.items()})
    elif isinstance(data, list):
        return tag.List([toNBT(element) for element in data])
    elif isinstance(data, int):
        return tag.Int(data)
    elif isinstance(data, str):
        return tag.String(data)
    else:
        raise ValueError(f"Unsupported data type: {type(data)}")

class Structure():
    def __init__(self, size: list[int], replaceWithAir: str = False):
        self.replaceWithAir = replaceWithAir
        self.structure = {"size": size, "blocks": [], "palette": [], "entities": [], "DataVersion": 3700}
    
    def finaliseStructure(self) -> File:
        result = File()
        for key, value in self.structure.items():
            result[key] = toNBT(value)
        return result

    def save(self, name: str) -> None:
        file = self.finaliseStructure()
        file.save(path(name))


struct = Structure([1,1,1])
print(struct.finaliseStructure() == file) # 输出True
file = struct.finaliseStructure() # 取消注释就失效,注释掉就正常
file.save(path("t6"))

此前编辑现有文件的代码

from nbtlib import File, tag
import os

folderPath = r"C:\Users\fullw\AppData\Roaming\.minecraft\saves\Python Generated\generated\minecraft\structures"

def path(name: str) -> str:
    return os.path.join(folderPath, name + ".nbt")

# Load the .nbt file
file = File.load(path("t1"), gzipped=True)

# 访问并修改数据
file["blocks"][0]["state"] = tag.Int(1)
del file["blocks"][0]

# 将修改保存回文件
file.save(path("t6"))

内容的提问来源于stack exchange,提问作者fullwoodenshovel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 20:43:27