Basic mvworld and offline world

This commit is contained in:
Ben Woo 2023-09-02 16:12:34 +08:00
parent 07b543a845
commit 5dd22b5d9b
No known key found for this signature in database
GPG Key ID: FB2A3645536E12C8
12 changed files with 154 additions and 48 deletions

View File

@ -0,0 +1,26 @@
package com.onarandombox.MultiverseCore.worldnew;
import com.onarandombox.MultiverseCore.worldnew.config.WorldConfig;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.jetbrains.annotations.NotNull;
import java.util.UUID;
public class MVWorld extends OfflineWorld {
private final UUID worldUid;
public MVWorld(
@NotNull String worldName,
@NotNull WorldConfig worldConfig,
@NotNull UUID worldUid
) {
super(worldName, worldConfig);
this.worldUid = worldUid;
}
public World getBukkitWorld() {
return Bukkit.getWorld(worldUid);
}
}

View File

@ -0,0 +1,27 @@
package com.onarandombox.MultiverseCore.worldnew;
import com.onarandombox.MultiverseCore.worldnew.config.WorldConfig;
import io.vavr.control.Try;
public class OfflineWorld {
protected final String worldName;
protected final WorldConfig worldConfig;
public OfflineWorld(String worldName, WorldConfig worldConfig) {
this.worldName = worldName;
this.worldConfig = worldConfig;
}
public String getWorldName() {
return worldName;
}
public Try<Void> setProperty(String name, Object value) {
return worldConfig.setProperty(name, value);
}
public Try<Object> getProperty(String name) {
return worldConfig.getProperty(name);
}
}

View File

@ -3,22 +3,30 @@ package com.onarandombox.MultiverseCore.worldnew;
import com.dumptruckman.minecraft.util.Logging;
import com.onarandombox.MultiverseCore.worldnew.config.WorldConfig;
import com.onarandombox.MultiverseCore.worldnew.config.WorldsConfigFile;
import com.onarandombox.MultiverseCore.worldnew.options.AddWorldOptions;
import com.onarandombox.MultiverseCore.worldnew.options.CreateWorldOptions;
import io.vavr.control.Option;
import jakarta.inject.Inject;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.World;
import org.bukkit.WorldCreator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jvnet.hk2.annotations.Service;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
@Service
public class WorldManager {
private final Map<String, OfflineWorld> offlineWorldsMap;
private final Map<String, MVWorld> worldsMap;
private final WorldsConfigFile worldsConfigFile;
@Inject
WorldManager(@NotNull WorldsConfigFile worldsConfigFile) {
this.offlineWorldsMap = new HashMap<>();
this.worldsMap = new HashMap<>();
this.worldsConfigFile = worldsConfigFile;
this.worldsConfigFile.load();
}
public void loadAllWorlds() {
@ -30,13 +38,41 @@ public class WorldManager {
}
/**
* Adds a world to the worlds config file.
* Creates a new world.
*
* @param options The options for customizing the creation of a new world.
*/
public void addWorld(AddWorldOptions options) {
public void createWorld(CreateWorldOptions options) {
// Check valid worldname
// Check if world already exists
// Create bukkit world
World world = WorldCreator.name(options.worldName())
.environment(options.environment())
.generateStructures(options.generateStructures())
.generator(options.generator())
.seed(options.seed())
.type(options.worldType())
.createWorld();
if (world == null) {
// TODO: Better result handling
Logging.severe("Failed to create world: " + options.worldName());
return;
}
// Our multiverse world
WorldConfig worldConfig = worldsConfigFile.getWorldConfig(options.worldName());
// TODO: Implement logic
worldConfig.setEnvironment(options.environment());
worldConfig.setGenerator(options.generator());
worldConfig.setSeed(world.getSeed());
OfflineWorld offlineWorld = new OfflineWorld(world.getName(), worldConfig);
offlineWorldsMap.put(options.worldName(), offlineWorld);
MVWorld mvWorld = new MVWorld(world.getName(), worldConfig, world.getUID());
worldsMap.put(options.worldName(), mvWorld);
saveWorldsConfig();
}
@ -61,12 +97,12 @@ public class WorldManager {
saveWorldsConfig();
}
public void getMVWorld(String worldName) {
// TODO: Implement logic
public Option<OfflineWorld> getOfflineWorld(@NotNull String worldName) {
return Option.of(offlineWorldsMap.get(worldName));
}
public void getUnloadedWorld(String worldName) {
// TODO: Implement logic
public Option<MVWorld> getMVWorld(@NotNull String worldName) {
return Option.of(worldsMap.get(worldName));
}
public void saveWorldsConfig() {

View File

@ -179,11 +179,11 @@ public class WorldConfig {
return configHandle.set(configNodes.SCALE, scale);
}
public @Nullable String getSeed() {
public long getSeed() {
return configHandle.get(configNodes.SEED);
}
public Try<Void> setSeed(String seed) {
public Try<Void> setSeed(long seed) {
return configHandle.set(configNodes.SEED, seed);
}

View File

@ -116,8 +116,8 @@ public class WorldConfigNodes {
.name("scale")
.build());
public final ConfigNode<String> SEED = node(ConfigNode.builder("seed", String.class)
.defaultValue("")
public final ConfigNode<Long> SEED = node(ConfigNode.builder("seed", Long.class)
.defaultValue(Long.MIN_VALUE)
.name("seed")
.build());

View File

@ -21,6 +21,7 @@ public class WorldsConfigFile {
@Inject
public WorldsConfigFile(@NotNull MultiverseCore core) {
worldConfigFile = core.getDataFolder().toPath().resolve(CONFIG_FILENAME).toFile();
load();
}
public void load() {

View File

@ -8,16 +8,16 @@ import org.jetbrains.annotations.Nullable;
/**
* Options for customizing the creation of a new world.
*/
public class AddWorldOptions {
public class CreateWorldOptions {
/**
* Creates a new {@link AddWorldOptions} instance with the given world name.
* Creates a new {@link CreateWorldOptions} instance with the given world name.
*
* @param worldName The name of the world to create.
* @return A new {@link AddWorldOptions} instance.
* @return A new {@link CreateWorldOptions} instance.
*/
public static @NotNull AddWorldOptions worldName(@NotNull String worldName) {
return new AddWorldOptions(worldName);
public static @NotNull CreateWorldOptions worldName(@NotNull String worldName) {
return new CreateWorldOptions(worldName);
}
private final String worldName;
@ -29,11 +29,11 @@ public class AddWorldOptions {
private WorldType worldType = WorldType.NORMAL;
/**
* Creates a new {@link AddWorldOptions} instance with the given world name.
* Creates a new {@link CreateWorldOptions} instance with the given world name.
*
* @param worldName The name of the world to create.
*/
AddWorldOptions(@NotNull String worldName) {
CreateWorldOptions(@NotNull String worldName) {
this.worldName = worldName;
}
@ -50,9 +50,9 @@ public class AddWorldOptions {
* Sets the environment of the world to create.
*
* @param environment The environment of the world to create.
* @return This {@link AddWorldOptions} instance.
* @return This {@link CreateWorldOptions} instance.
*/
public @NotNull AddWorldOptions environment(@NotNull World.Environment environment) {
public @NotNull CreateWorldOptions environment(@NotNull World.Environment environment) {
this.environment = environment;
return this;
}
@ -70,9 +70,9 @@ public class AddWorldOptions {
* Sets whether structures such as NPC villages should be generated.
*
* @param generateStructures Whether structures such as NPC villages should be generated.
* @return This {@link AddWorldOptions} instance.
* @return This {@link CreateWorldOptions} instance.
*/
public @NotNull AddWorldOptions generateStructures(boolean generateStructures) {
public @NotNull CreateWorldOptions generateStructures(boolean generateStructures) {
this.generateStructures = generateStructures;
return this;
}
@ -90,9 +90,9 @@ public class AddWorldOptions {
* Sets the custom generator plugin and its parameters.
*
* @param generator The custom generator plugin and its parameters.
* @return This {@link AddWorldOptions} instance.
* @return This {@link CreateWorldOptions} instance.
*/
public @NotNull AddWorldOptions generator(@Nullable String generator) {
public @NotNull CreateWorldOptions generator(@Nullable String generator) {
this.generator = generator;
return this;
}
@ -111,9 +111,9 @@ public class AddWorldOptions {
* hashed.
*
* @param seed The seed of the world to create.
* @return This {@link AddWorldOptions} instance.
* @return This {@link CreateWorldOptions} instance.
*/
public @NotNull AddWorldOptions seed(@NotNull String seed) {
public @NotNull CreateWorldOptions seed(@NotNull String seed) {
try {
this.seed = Long.parseLong(seed);
} catch (NumberFormatException numberformatexception) {
@ -126,9 +126,9 @@ public class AddWorldOptions {
* Sets the seed of the world to create.
*
* @param seed The seed of the world to create.
* @return This {@link AddWorldOptions} instance.
* @return This {@link CreateWorldOptions} instance.
*/
public @NotNull AddWorldOptions seed(long seed) {
public @NotNull CreateWorldOptions seed(long seed) {
this.seed = seed;
return this;
}
@ -146,9 +146,9 @@ public class AddWorldOptions {
* Sets whether multiverse will search for a safe spawn location.
*
* @param useSpawnAdjust Whether multiverse will search for a safe spawn location.
* @return This {@link AddWorldOptions} instance.
* @return This {@link CreateWorldOptions} instance.
*/
public @NotNull AddWorldOptions useSpawnAdjust(boolean useSpawnAdjust) {
public @NotNull CreateWorldOptions useSpawnAdjust(boolean useSpawnAdjust) {
this.useSpawnAdjust = useSpawnAdjust;
return this;
}
@ -166,9 +166,9 @@ public class AddWorldOptions {
* Sets the world type.
*
* @param worldType The world type.
* @return This {@link AddWorldOptions} instance.
* @return This {@link CreateWorldOptions} instance.
*/
public @NotNull AddWorldOptions worldType(@NotNull WorldType worldType) {
public @NotNull CreateWorldOptions worldType(@NotNull WorldType worldType) {
this.worldType = worldType;
return this;
}

View File

@ -1,10 +1,14 @@
package org.mvplugins.multiverse.core.world
import com.onarandombox.MultiverseCore.worldnew.WorldManager
import com.onarandombox.MultiverseCore.worldnew.options.AddWorldOptions
import com.onarandombox.MultiverseCore.worldnew.options.CreateWorldOptions
import org.bukkit.World
import org.bukkit.WorldType
import org.mvplugins.multiverse.core.TestWithMockBukkit
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
class WorldManagerTest : TestWithMockBukkit() {
@ -17,9 +21,21 @@ class WorldManagerTest : TestWithMockBukkit() {
}
@Test
fun `Add world`() {
worldManager.addWorld(AddWorldOptions.worldName("world"))
// TODO: When logic is implemented, check that the world is added
fun `Creates a new world`() {
worldManager.createWorld(CreateWorldOptions.worldName("world_nether")
.environment(World.Environment.NETHER)
.generateStructures(false)
.seed(1234L)
.useSpawnAdjust(true)
.worldType(WorldType.FLAT)
)
val world = worldManager.getMVWorld("world_nether").get()
assertNotNull(world)
assertEquals("world_nether", world.worldName)
assertEquals(World.Environment.NETHER, world.getProperty("environment").get())
assertEquals("", world.getProperty("generator").get())
assertEquals(1234L, world.getProperty("seed").get())
}
@Test

View File

@ -17,7 +17,7 @@ world:
pvp: true
respawn-world: ''
scale: 1.0
seed: ''
seed: -9223372036854775808
world-blacklist: []
world_nether:
adjust-spawn: false
@ -38,5 +38,5 @@ world_nether:
pvp: true
respawn-world: ''
scale: 1.0
seed: ''
seed: -9223372036854775808
world-blacklist: []

View File

@ -17,5 +17,5 @@ world_nether:
pvp: true
respawn-world: ''
scale: 1.0
seed: ''
seed: -9223372036854775808
world-blacklist: []

View File

@ -17,7 +17,7 @@ world:
pvp: true
respawn-world: ''
scale: 1.0
seed: ''
seed: -9223372036854775808
world-blacklist: []
world_nether:
adjust-spawn: false
@ -38,7 +38,7 @@ world_nether:
pvp: true
respawn-world: ''
scale: 1.0
seed: ''
seed: -9223372036854775808
world-blacklist: []
newworld:
adjust-spawn: false
@ -59,5 +59,5 @@ newworld:
pvp: true
respawn-world: ''
scale: 1.0
seed: ''
seed: -9223372036854775808
world-blacklist: []

View File

@ -17,7 +17,7 @@ world:
pvp: true
respawn-world: ''
scale: 1.0
seed: ''
seed: -9223372036854775808
world-blacklist: []
world_nether:
adjust-spawn: false
@ -38,5 +38,5 @@ world_nether:
pvp: true
respawn-world: ''
scale: 1.0
seed: ''
seed: -9223372036854775808
world-blacklist: []