如何读取Minecraft .mca文件,以便在Python中我可以提取单个块?

xvw2m8pv  于 2023-01-19  发布在  Python
关注(0)|答案(2)|浏览(314)

我找不到一种可以在Python中使用的方式来阅读Minecraft世界文件
我在互联网上找过,但是没有找到教程,只有少数几个库声称他们可以做到这一点,但实际上从来没有工作过

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

我期望这个工作,但相反,我得到了关于文件没有被压缩或类似的错误
完整错误:

Traceback (most recent call last):
  File "C:\Users\rober\Desktop\MinePy\MinecraftWorldReader.py", line 2, in <module>
    nbtfile = nbt.NBTFile("r.0.0.mca",'rb')
  File "C:\Users\rober\AppData\Local\Programs\Python\Python36-32\lib\site-packages\nbt\nbt.py", line 628, in __init__
    self.parse_file()
  File "C:\Users\rober\AppData\Local\Programs\Python\Python36-32\lib\site-packages\nbt\nbt.py", line 652, in parse_file
    type = TAG_Byte(buffer=self.file)
  File "C:\Users\rober\AppData\Local\Programs\Python\Python36-32\lib\site-packages\nbt\nbt.py", line 99, in __init__
    self._parse_buffer(buffer)
  File "C:\Users\rober\AppData\Local\Programs\Python\Python36-32\lib\site-packages\nbt\nbt.py", line 105, in _parse_buffer
    self.value = self.fmt.unpack(buffer.read(self.fmt.size))[0]
  File "C:\Users\rober\AppData\Local\Programs\Python\Python36-32\lib\gzip.py", line 276, in read
    return self._buffer.read(size)
  File "C:\Users\rober\AppData\Local\Programs\Python\Python36-32\lib\_compression.py", line 68, in readinto
    data = self.read(len(byte_view))
  File "C:\Users\rober\AppData\Local\Programs\Python\Python36-32\lib\gzip.py", line 463, in read
    if not self._read_gzip_header():
  File "C:\Users\rober\AppData\Local\Programs\Python\Python36-32\lib\gzip.py", line 411, in _read_gzip_header
    raise OSError('Not a gzipped file (%r)' % magic)
OSError: Not a gzipped file (b'\x00\x00')
kb5ga3dv

kb5ga3dv1#

使用砧座解析器。(使用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/

x6492ojm

x6492ojm2#

根据this page,.mca文件并不完全是NBT文件。它以一个8KiB的头文件开始,其中包括区域文件本身中块的偏移量和这些块的最后更新时间戳。我建议您查看the offical announcementthis page以了解更多信息。

相关问题