Implement AddWorldOptions to condense method parameters

This commit is contained in:
Ben Woo 2023-09-02 15:19:30 +08:00
parent f7295e2c2e
commit 07b543a845
No known key found for this signature in database
GPG Key ID: FB2A3645536E12C8
3 changed files with 194 additions and 3 deletions

View File

@ -3,6 +3,7 @@ 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 jakarta.inject.Inject;
import org.bukkit.configuration.ConfigurationSection;
import org.jetbrains.annotations.NotNull;
@ -28,8 +29,13 @@ public class WorldManager {
saveWorldsConfig();
}
public void addWorld(String worldName) {
WorldConfig worldConfig = worldsConfigFile.getWorldConfig(worldName);
/**
* Adds a world to the worlds config file.
*
* @param options The options for customizing the creation of a new world.
*/
public void addWorld(AddWorldOptions options) {
WorldConfig worldConfig = worldsConfigFile.getWorldConfig(options.worldName());
// TODO: Implement logic
saveWorldsConfig();
}

View File

@ -0,0 +1,184 @@
package com.onarandombox.MultiverseCore.worldnew.options;
import org.bukkit.World;
import org.bukkit.WorldType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Options for customizing the creation of a new world.
*/
public class AddWorldOptions {
/**
* Creates a new {@link AddWorldOptions} instance with the given world name.
*
* @param worldName The name of the world to create.
* @return A new {@link AddWorldOptions} instance.
*/
public static @NotNull AddWorldOptions worldName(@NotNull String worldName) {
return new AddWorldOptions(worldName);
}
private final String worldName;
private World.Environment environment = World.Environment.NORMAL;
private boolean generateStructures = true;
private String generator = null;
private long seed = Long.MIN_VALUE;
private boolean useSpawnAdjust = false;
private WorldType worldType = WorldType.NORMAL;
/**
* Creates a new {@link AddWorldOptions} instance with the given world name.
*
* @param worldName The name of the world to create.
*/
AddWorldOptions(@NotNull String worldName) {
this.worldName = worldName;
}
/**
* Gets the name of the world to create.
*
* @return The name of the world to create.
*/
public @NotNull String worldName() {
return worldName;
}
/**
* Sets the environment of the world to create.
*
* @param environment The environment of the world to create.
* @return This {@link AddWorldOptions} instance.
*/
public @NotNull AddWorldOptions environment(@NotNull World.Environment environment) {
this.environment = environment;
return this;
}
/**
* Gets the environment of the world to create.
*
* @return The environment of the world to create.
*/
public @NotNull World.Environment environment() {
return environment;
}
/**
* 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.
*/
public @NotNull AddWorldOptions generateStructures(boolean generateStructures) {
this.generateStructures = generateStructures;
return this;
}
/**
* Gets whether structures such as NPC villages should be generated.
*
* @return Whether structures such as NPC villages should be generated.
*/
public boolean generateStructures() {
return generateStructures;
}
/**
* Sets the custom generator plugin and its parameters.
*
* @param generator The custom generator plugin and its parameters.
* @return This {@link AddWorldOptions} instance.
*/
public @NotNull AddWorldOptions generator(@Nullable String generator) {
this.generator = generator;
return this;
}
/**
* Gets the custom generator plugin and its parameters.
*
* @return The custom generator plugin and its parameters.
*/
public @Nullable String generator() {
return generator;
}
/**
* Sets the seed of the world to create. If the seed is a number, it will be parsed as a long. Otherwise, it will be
* hashed.
*
* @param seed The seed of the world to create.
* @return This {@link AddWorldOptions} instance.
*/
public @NotNull AddWorldOptions seed(@NotNull String seed) {
try {
this.seed = Long.parseLong(seed);
} catch (NumberFormatException numberformatexception) {
this.seed = seed.hashCode();
}
return this;
}
/**
* Sets the seed of the world to create.
*
* @param seed The seed of the world to create.
* @return This {@link AddWorldOptions} instance.
*/
public @NotNull AddWorldOptions seed(long seed) {
this.seed = seed;
return this;
}
/**
* Gets the seed of the world to create.
*
* @return The seed of the world to create.
*/
public long seed() {
return seed;
}
/**
* 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.
*/
public @NotNull AddWorldOptions useSpawnAdjust(boolean useSpawnAdjust) {
this.useSpawnAdjust = useSpawnAdjust;
return this;
}
/**
* Gets whether multiverse will search for a safe spawn location.
*
* @return Whether multiverse will search for a safe spawn location.
*/
public boolean useSpawnAdjust() {
return useSpawnAdjust;
}
/**
* Sets the world type.
*
* @param worldType The world type.
* @return This {@link AddWorldOptions} instance.
*/
public @NotNull AddWorldOptions worldType(@NotNull WorldType worldType) {
this.worldType = worldType;
return this;
}
/**
* Gets the world type.
*
* @return The world type.
*/
public @NotNull WorldType worldType() {
return worldType;
}
}

View File

@ -1,6 +1,7 @@
package org.mvplugins.multiverse.core.world
import com.onarandombox.MultiverseCore.worldnew.WorldManager
import com.onarandombox.MultiverseCore.worldnew.options.AddWorldOptions
import org.mvplugins.multiverse.core.TestWithMockBukkit
import kotlin.test.BeforeTest
import kotlin.test.Test
@ -17,7 +18,7 @@ class WorldManagerTest : TestWithMockBukkit() {
@Test
fun `Add world`() {
worldManager.addWorld("world")
worldManager.addWorld(AddWorldOptions.worldName("world"))
// TODO: When logic is implemented, check that the world is added
}