Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add region files API #2466

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/main/java/org/spongepowered/api/world/server/ChunkManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@
import org.spongepowered.api.world.ChunkRegenerateFlag;
import org.spongepowered.api.world.ChunkRegenerateFlags;
import org.spongepowered.api.world.chunk.WorldChunk;
import org.spongepowered.math.vector.Vector2i;
import org.spongepowered.math.vector.Vector3i;

import java.nio.file.Path;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -246,4 +249,65 @@ default CompletableFuture<Boolean> regenerateChunk(final Vector3i chunkPosition,
*/
CompletableFuture<Boolean> regenerateChunk(int cx, int cy, int cz, ChunkRegenerateFlag flag);

/**
* Returns the path of the directory where region files are stored.
*
* @return The path of the region directory
*/
Path regionDirPath();

/**
* Returns the path of the region file containing the given {@link WorldChunk}.
*
* @param chunk The {@link WorldChunk} for which to acquire the region file path
* @return The path of the region file for the given chunk
*/
Path regionFilePath(WorldChunk chunk);
Comment on lines +252 to +265
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd almost want to have a StorageHandler interface to deal with this so that it's not directly exposed in the API, but can be something exposed for "vanilla" kinds.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not always guaranteed that the world has region files (like mods) that could be storing chunks in a different region handler that we simply won't be able to directly interface with.


/**
* Calculates the maximum chunk coordinates for a given region coordinates (x, z) in a world.
*
* @param rx The x-coordinate of the region
* @param rz The z-coordinate of the region
* @return A {@link Vector3i} containing the maximum chunk coordinates for the given region coordinates
*/
Vector3i maxChunkFromRegion(int rx, int rz);

/**
* Calculates the maximum chunk coordinates for a given region coordinates (x, z) in a world.
*
* @param regionCoordinate The coordinate of the region
* @return A {@link Vector3i} containing the maximum chunk coordinates for the given region coordinates
*/
default Vector3i maxChunkFromRegion(Vector2i regionCoordinate) {
Objects.requireNonNull(regionCoordinate, "regionCoordinate");
return this.maxChunkFromRegion(regionCoordinate.x(), regionCoordinate.y());
}

/**
* Calculates the minimum chunk coordinates for a given region coordinates (x, z) in a world.
*
* @param rx The x-coordinate of the region
* @param rz The z-coordinate of the region
* @return A {@link Vector3i} containing the minimum chunk coordinates for the given region coordinates
*/
Vector3i minChunkFromRegion(int rx, int rz);

/**
* Calculates the maximum chunk coordinates for a given region coordinates (x, z) in a world.
*
* @param regionCoordinate The coordinate of the region
* @return A {@link Vector3i} containing the maximum chunk coordinates for the given region coordinates
*/
default Vector3i minChunkFromRegion(Vector2i regionCoordinate) {
Objects.requireNonNull(regionCoordinate, "regionCoordinate");
return this.minChunkFromRegion(regionCoordinate.x(), regionCoordinate.y());
}

/**
* Returns the region coordinates and file paths of existing regions.
*
* @return the region coordinates and their file paths.
*/
Map<Vector2i, Path> regionFiles();
}
12 changes: 12 additions & 0 deletions src/main/java/org/spongepowered/api/world/server/ServerWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Stream;

public interface ServerWorld extends World<ServerWorld, ServerLocation>, Identifiable, InteractableVolume,
ServerLocationCreator, WeatherUniverse.Mutable {
Expand Down Expand Up @@ -235,4 +236,15 @@ default boolean restoreSnapshot(final Vector3i position, final BlockSnapshot sna
*/
ChunkManager chunkManager();

/**
* Returns a stream of existing chunk positions for this world.
* <p>
* Note that consuming the stream may be slow.
* The stream must be closed or fully consumed otherwise file handles may stay open.
* </p>
*
* @return The stream of existing chunk positions.
*/
Stream<Vector3i> chunkPositions();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so how about this instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems fine


}