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

Ability to specify an ID for a Temporary World #41

Merged
merged 2 commits into from
Dec 13, 2023
Merged
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
26 changes: 21 additions & 5 deletions src/main/java/xyz/nucleoid/fantasy/Fantasy.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,23 @@ private void tick() {
* @return a future providing the created world
*/
public RuntimeWorldHandle openTemporaryWorld(RuntimeWorldConfig config) {
RuntimeWorld world = this.addTemporaryWorld(config);
return this.openTemporaryWorld(generateTemporaryWorldKey(), config);
}

/**
* Creates a new temporary world with the given identifier and {@link RuntimeWorldConfig} that will not be saved and will be
* deleted when the server exits.
* <p>
* The created world is returned asynchronously through a {@link RuntimeWorldHandle}.
* This handle can be used to acquire the {@link ServerWorld} object through {@link RuntimeWorldHandle#asWorld()},
* as well as to delete the world through {@link RuntimeWorldHandle#delete()}.
*
* @param key the unique identifier for this dimension
* @param config the config with which to construct this temporary world
* @return a future providing the created world
*/
public RuntimeWorldHandle openTemporaryWorld(Identifier key, RuntimeWorldConfig config) {
RuntimeWorld world = this.addTemporaryWorld(key, config);
return new RuntimeWorldHandle(this, world);
}

Expand Down Expand Up @@ -147,8 +163,8 @@ private RuntimeWorld addPersistentWorld(Identifier key, RuntimeWorldConfig confi
return this.worldManager.add(worldKey, config, RuntimeWorld.Style.PERSISTENT);
}

private RuntimeWorld addTemporaryWorld(RuntimeWorldConfig config) {
RegistryKey<World> worldKey = RegistryKey.of(RegistryKeys.WORLD, generateTemporaryWorldKey());
private RuntimeWorld addTemporaryWorld(Identifier key, RuntimeWorldConfig config) {
RegistryKey<World> worldKey = RegistryKey.of(RegistryKeys.WORLD, key);

try {
LevelStorage.Session session = this.serverAccess.getSession();
Expand All @@ -171,7 +187,7 @@ void enqueueWorldUnloading(ServerWorld world) {
});
}

private boolean tickDeleteWorld(ServerWorld world) {
public boolean tickDeleteWorld(ServerWorld world) {
if (this.isWorldUnloaded(world)) {
this.worldManager.delete(world);
return true;
Expand All @@ -181,7 +197,7 @@ private boolean tickDeleteWorld(ServerWorld world) {
}
}

private boolean tickUnloadWorld(ServerWorld world) {
public boolean tickUnloadWorld(ServerWorld world) {
if (this.isWorldUnloaded(world)) {
this.worldManager.unload(world);
return true;
Expand Down