C4K3 / nbted

Command-line NBT editor
Creative Commons Zero v1.0 Universal
100 stars 8 forks source link

Feature Request: Option to fix coordinates in renamed region files #10

Open Elliander opened 1 year ago

Elliander commented 1 year ago

I have been working on a project that hit a wall: When a region file is moved from one location to another chest contents as well as mob and village spawns break. This happens because the game stores the coordinates as absolute coordinates rather than relative coordinates and I am not skilled enough to make a fix for that myself even though I understand what calculations would need to be made. Fixing this problem would need to be done to restore natural spawns.

I opened a bug report about it more than 2 years ago with a suggestion on how to correct the issue and it was eventually marked as closed since moving region files by hand is not supported:

https://bugs.mojang.com/browse/MC-217192

Without the ability to correct the issue my own project couldn't be completed so I put it on hold, but then I found this project and it gave me a bit of hope. If it can convert to plain text and it can do the reverse it could, presumably, make corrections to the file, right? If so I don't suppose there would be a simple way to use your program to do that?

Here's the fix I am thinking of:

The program could convert the file to plain text, look for all instances of coordinates and compare to the current file name. If the coordinates don't match the current file name it changes them to match it according to the following formula:

Relative Coordinate = absolute coordinate % 512 // that's a Modulo operation ((region coordinate) * 512) + relative coordinate = coordinate at region.

So, to give an example, suppose I renamed a region from from r.-5.-5.mca to r.1.-3.mca. An object exists in the region file which shows:

id: minecraft:shulker_box x: -2288 y: 72 z: -2248

However, since those coordinates are not possible for the new region file location the box is empty. Correcting the file would require the following calculations which just plug in the current file name and current X/Z coordinates to generate new coordinates:

X: ((1) * 512) + (-2288 % 512) = 784

Z: ((-3) * 512) + (-2248 % 512) = -752

Therefore, in this case, what reads as (-2288 72 -2248) should be changed to (784 72 -752) and after making that change the region files will be fixed and work properly as if naturally generated.

Could I use this program to do something like this? If so could someone at least point me in the right direction? In my case I am still working on region files created in Minecraft 1.16.

Thanks!