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

使用anvil-parser访问Minecraft 1.20.4 .mca容器实体遇属性错误

解决Anvil-Parser读取Minecraft 1.20.4容器实体时的AttributeError问题

错误原因分析

你遇到的AttributeError: 'Chunk' object has no attribute 'sections',本质是anvil-parser版本与Minecraft 1.20.4的区块结构不兼容。旧版本的anvil-parser会假设Chunk对象包含sections属性,但1.20.4的区块格式或库的最新实现已经调整了属性结构,导致调用时触发错误。

修复步骤

1. 升级anvil-parser到最新兼容版本

首先确保你使用的是支持1.20.4的库版本,执行升级命令:

pip install --upgrade anvil-parser

2. 调整区块实体的访问方式

新版本的anvil-parser中,区块的实体数据不再直接通过chunk.entities访问,而是需要从chunk.data字典中获取。修改你的scan_chunk_for_containers函数:

def scan_chunk_for_containers(chunk):
    containers = []
    # 从区块数据中提取实体列表,默认空列表避免KeyError
    entities = chunk.data.get('entities', [])
    for entity in entities:
        entity_id = entity.get('id', '')
        target_ids = ['minecraft:chest', 'minecraft:shulker_box', 'minecraft:furnace', 'minecraft:barrel']
        if entity_id in target_ids:
            containers.append(entity)
    return containers

3. 优化区块加载逻辑

部分版本中,region.get_chunk可能无法正确加载完整区块数据,建议替换为region.load_chunk:

# 替换原有的chunk获取代码
chunk = region.load_chunk(chunk_x, chunk_z)

完整修改后的代码

import os
import anvil
import json
import pandas as pd

def scan_chunk_for_containers(chunk):
    containers = []
    entities = chunk.data.get('entities', [])
    for entity in entities:
        entity_id = entity.get('id', '')
        if entity_id in ['minecraft:chest', 'minecraft:shulker_box', 'minecraft:furnace', 'minecraft:barrel']:
            containers.append(entity)
    return containers

def process_region_file(region_file_path, output_folder):
    region = anvil.Region.from_file(region_file_path)
    all_containers = []
    for chunk_x in range(32):
        for chunk_z in range(32):
            if region.chunk_location(chunk_x, chunk_z) is not None:
                try:
                    # 使用load_chunk替代get_chunk,确保加载完整区块数据
                    chunk = region.load_chunk(chunk_x, chunk_z)
                    containers = scan_chunk_for_containers(chunk)
                    all_containers.extend(containers)
                except (anvil.errors.ChunkNotFound, Exception) as e:
                    print(f"加载区块({chunk_x}, {chunk_z})失败: {str(e)}")
                    continue

    if all_containers:
        output_file_path = os.path.join(output_folder, os.path.basename(region_file_path) + '_containers.json')
        with open(output_file_path, 'w') as output_file:
            json.dump(all_containers, output_file, indent=4)
        
        df = pd.json_normalize(all_containers)
        spreadsheet_path = os.path.join(output_folder, os.path.basename(region_file_path) + '_containers.xlsx')
        df.to_excel(spreadsheet_path, index=False)
        print(f"容器数据已保存到 {spreadsheet_path}")

def main():
    region_folder = "你的区域文件路径" 
    output_folder = "你的输出路径"
    
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)
    
    for file_name in os.listdir(region_folder):
        if file_name.endswith('.mca'):
            region_file_path = os.path.join(region_folder, file_name)
            process_region_file(region_file_path, output_folder)

if __name__ == "__main__":
    main()

额外注意事项

  • 如果仍有问题,检查你的Minecraft世界文件是否为1.20.4版本,避免跨版本读取导致的结构不匹配。
  • 部分容器(如双箱子)会以两个实体的形式存在,代码会自动收集,后续可以根据坐标合并数据。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 05:33:11