首页 > 解决方案 > 如何读取 Minecraft .mca 文件以便在 python 中提取单个块?

问题描述

我找不到一种可以在 python 中使用的方式来读取 Minecraft 世界文件的方法

我环顾了互联网,但找不到任何教程,只有少数图书馆声称他们可以做到这一点,但实际上从来没有工作过

from nbt import *
nbtfile = nbt.NBTFile("r.0.0.mca",'rb')

我希望这可以工作,但我收到有关文件未压缩或类似问题的错误

OSError: 不是压缩文件 (b'\x00\x00')

完整错误:raceback(最近一次通话最后):文件“C:\Users\rober\Desktop\MinePy\MinecraftWorldReader.py”,第 2 行,在 nbtfile = nbt.NBTFile("r.0.0.mca",'rb' ) 文件“C:\Users\rober\AppData\Local\Programs\Python\Python36-32\lib\site-packages\nbt\nbt.py”,第 628 行,在init self.parse_file() 文件“C:\ Users\rober\AppData\Local\Programs\Python\Python36-32\lib\site-packages\nbt\nbt.py”,第 652 行,在 parse_file 类型 = TAG_Byte(buffer=self.file) 文件“C:\Users \rober\AppData\Local\Programs\Python\Python36-32\lib\site-packages\nbt\nbt.py",第 99 行,在init self._parse_buffer(buffer) 文件“C:\Users\rober\AppData\Local\Programs\Python\Python36-32\lib\site-packages\nbt\nbt.py”,第 105 行,在 _parse_buffer self.value = self .fmt.unpack(buffer.read(self.fmt.size))[0] 文件“C:\Users\rober\AppData\Local\Programs\Python\Python36-32\lib\gzip.py”,第 276 行,在读取返回 self._buffer.read(size) 文件“C:\Users\rober\AppData\Local\Programs\Python\Python36-32\lib_compression.py”,第 68 行,在 readinto data = self.read(len( byte_view)) 文件“C:\Users\rober\AppData\Local\Programs\Python\Python36-32\lib\gzip.py”,第 463 行,如果不是 self._read_gzip_header(),则在读取:文件“C:\Users \rober\AppData\Local\Programs\Python\Python36-32\lib\gzip.py",第 411 行,在 _read_gzip_header 中引发 OSError('Not a gzipped file (%r)' % magic) OSError:不是 gzip 文件 (b'\x00\x00') [在 0.2s 内完成]

任何帮助,将不胜感激,

罗伯特

标签: pythonminecraftfile-handling

解决方案


使用砧解析器。(安装pip install anvil-parser

阅读

import anvil

region = anvil.Region.from_file('r.0.0.mca')

# You can also provide the region file name instead of the object
chunk = anvil.Chunk.from_region(region, 0, 0)

# If `section` is not provided, will get it from the y coords
# and assume it's global
block = chunk.get_block(0, 0, 0)

print(block) # <Block(minecraft:air)>
print(block.id) # air
print(block.properties) # {}

https://pypi.org/project/anvil-parser/


推荐阅读