Skip to content

Commit

Permalink
Ability to specify an ID for a Temporary World (#41)
Browse files Browse the repository at this point in the history
* Add ability to specify an ID to a temporary world

* Add ability to manually tick to delete or unload a world.
  • Loading branch information
kyrptonaught authored Dec 13, 2023
1 parent f0ff603 commit dbab6af
Showing 1 changed file with 21 additions and 5 deletions.
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

0 comments on commit dbab6af

Please sign in to comment.