twoolie / NBT

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

Added entity support #167

Open Ery4z opened 1 year ago

Ery4z commented 1 year ago

Hello, After working on a script for copying chunk between worlds while preserving entities, I found myself writing lot of boilerplate code. But in fact as the region file are the same format for entities and regular region, it is possible to load the entities region files in the WorldFolder class. I added an option to select if the user want to load the entities files or the regular region. Then he can access the entity data with the regular get_region function.

What are you thinking of this ?

twoolie commented 1 year ago

Hi @Ery4z, thanks for the contribution.

I think it's a very good idea to add entity support, but i have a few suggestions for the implementation.

With your implementation, somebody wanting to edit both regions and entities within a world would need to create two WorldFolder objects, and editing entities would still require calling the get_regionfiles and set_regionfiles methods which could be unintuitive for new users.

I think it would be useful to create new functions specific to entity handling that mirror the region handling code.

Please let me know if you are happy and have time to amend this PR

Ery4z commented 1 year ago

Hi @twoolie, thanks for your review. I would be happy to work on this feature, but before doing that I would need your review on some point.

I implemented the entities support feature the way you described it above on a fork a year ago while working on a map editor project. And I found out multiple problematic points that need to be taken care of.

The most problematic one is that every entities (block entity such as shulker, chest,... & regular mob entity) has their absolute world position in the nbt tag. This mean that the simple action of copying a chunk is not straight forward.

To solve this problem, I implemented such a mechanism

Load the region & entity file (Single WorldFolder object) -> Select the chunk to be copied and export a normalised version of it (normalising the entities coordinate relatively to the origin of this specific chunk) -> Denormalize the chunk relatively to the paste location in the output WorldFolder object and paste them into it.

This would also allow for future feature to select if we want to do the automatic blend nearby the pasted chunk (see this ) But this feature is out of the scope of this PR but would be easily implemented.

What do you think about me implementing function for normalising entities data, exporting the chunk bundling blocks and entities & importing theses chunks ?