twoolie / NBT

Python Parser/Writer for the NBT file format, and it's container the RegionFile.
MIT License
361 stars 74 forks source link

(Question) Trying to modify region binary to replace stuff in minecraft world using blockdata #158

Open julianprincipe opened 2 years ago

julianprincipe commented 2 years ago

I'm trying to write a simple script that replace NBT tags in a world, i'm testing it with a brand new world with a simple "give" command in a command block:

give @a golden_axe

My idea is to use get_blockdata() to unpack the binary, binary = binary.replace(b'minecraft:golden_axe', b'minecraft:golden_sword') to replace the item and write_blockdata() to write it to the file

This is the code i have:

for region in world.iter_regions():
            process_region_file(region, start, stop)
            for i in region.get_metadata():
                x = i.x
                z = i.z
                binary = region.get_blockdata(x,z)
                binary = binary.replace(b'minecraft:golden_axe', b'minecraft:golden_sword')
                nbt = region.write_blockdata(x, z, binary, compression=2)
                print(region.get_blockdata(x,z))

The code works with really specific stuff, i've managed to change random text in a command block but whenever i try to change items in a "give" command or something like that the command block just disappears.

I've manually checked the replacement by printing the binary and it works: Command\x00\x1cgive @p minecraft:golden_axe gets replaced to: Command\x00\x1cgive @p minecraft:golden_sword in the binary, but when i open the world the command block isn't there and if i check the binary after opening the world it just disappears from the binary aswell

Any ideas why it happens? Would you approach this idea differently? I'm really confused