Skip to content
This repository has been archived by the owner on Feb 29, 2024. It is now read-only.

Optionally load chunks when pasting a schematic #33

Open
mworzala opened this issue Dec 19, 2022 · 2 comments · May be fixed by #36
Open

Optionally load chunks when pasting a schematic #33

mworzala opened this issue Dec 19, 2022 · 2 comments · May be fixed by #36
Labels
enhancement New feature or request

Comments

@mworzala
Copy link
Collaborator

It would be useful to have an option to automatically load chunks when pasting a schematic.

You can determine the covered area of the schematic using something like

var schem = ...; // Schematic
var pos = ...; // Paste location
var min = pos.add(schem.offset());
var max = min.add(schem.size());

Note: Any chunks loaded to paste should be unloaded afterwards if they still do not have any viewers).

@mworzala mworzala added the enhancement New feature or request label Dec 19, 2022
@Tofaa2
Copy link

Tofaa2 commented Dec 27, 2022

@mworzala I've made a little implementation of getting and loading chunks you can add it yourself
It does require reflection but it shouldnt affect performance much, this is due to limitations of Minestom API in terms of accessibility

    /**
     * @param rotation The rotation to apply before placement.
     * @return long array of affected chunks
     */
    public long[] getAffectedChunks(Rotation rotation, Point pastePos) {
        try {
            final RelativeBlockBatch batch = build(rotation, null);
            final Field field = batch.toAbsoluteBatch(pastePos.blockX(), pastePos.blockY(), pastePos.blockZ()).getClass().getDeclaredField("chunkBatchesMap");
            boolean isAccessible = field.canAccess(batch);
            field.setAccessible(true);
            @SuppressWarnings("unchecked")
            final Long2ObjectMap<ChunkBatch> map = (Long2ObjectMap<ChunkBatch>) field.get(batch.toAbsoluteBatch(offset.blockX(), offset.blockY(), offset.blockZ()));
            return map.keySet().toLongArray();
        }
        catch (NoSuchFieldException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
    ```
    
    Then you can simply 
    ```java
    ChunkUtils.optionalLoadAll(Instance, getAffectedChunks(Rotation, Position)).thenRun(() -> {
       // Batch application code here
    });
    ```

@mworzala
Copy link
Collaborator Author

I do not believe this is the right way to handle this. It's true that Minestom does not expose any way to get affected chunks from a BlockBatch, however the Schematic contains enough information to compute the affected area. I would prefer using that (math outlined in original comment).

Also, optionalLoadAll is not great to use in these cases, because Minestom will never unload them automatically. It would be better to keep track of the loaded chunks and unload them again afterwards if there are still no viewers.

@GreatWyrm GreatWyrm linked a pull request Jan 1, 2023 that will close this issue
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants