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 all 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
51 changes: 51 additions & 0 deletions src/main/java/org/spongepowered/api/world/chunk/OfflineChunk.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* This file is part of SpongeAPI, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.api.world.chunk;

import org.spongepowered.api.data.persistence.DataContainer;
import org.spongepowered.api.data.persistence.DataSerializable;
import org.spongepowered.math.vector.Vector3i;

/**
* Represents an offline chunk and its data.
* The chunk may also be currently loaded!
*/
public interface OfflineChunk extends DataSerializable {

/**
* Returns the chunk position.
*
* @return the chunk position
*/
Vector3i chunkPosition();

/**
* Returns the offline chunk data.
*
* @return the offline chunk data
*/
@Override
DataContainer toContainer();
}
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();
}
27 changes: 27 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 @@ -36,6 +36,7 @@
import org.spongepowered.api.world.World;
import org.spongepowered.api.world.WorldType;
import org.spongepowered.api.world.WorldTypes;
import org.spongepowered.api.world.chunk.OfflineChunk;
import org.spongepowered.api.world.chunk.WorldChunk;
import org.spongepowered.api.world.explosion.Explosion;
import org.spongepowered.api.world.generation.ChunkGenerator;
Expand All @@ -50,6 +51,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 +237,29 @@ 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 is slow, consider consuming it asynchronously.
* <p>
* The stream must be closed or fully consumed otherwise file handles may stay open.
*
* @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



/**
* Returns a stream of existing chunks for this world.
* <p>
* Note that consuming the stream is very slow, consider consuming it asynchronously.
* <p>
* The stream must be closed or fully consumed otherwise file handles may stay open.
* <p>
* You should not modify data of currently loaded chunks.
*
* @return The stream of existing chunks.
*/
Stream<OfflineChunk> offlineChunks();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

and this if you want the chunk data too


}