From 2a3c8375ccf95a6a33ee74a836493842cfdc7bb0 Mon Sep 17 00:00:00 2001 From: david Date: Sat, 17 Aug 2024 03:52:45 +0200 Subject: [PATCH] Refactor world creation to use WorldPreset. Replaced deprecated WorldType with WorldPreset for better clarity in defining world types. Updated the creation logic to utilize a new level builder pattern, improving configurability and modularity. This refactor enhances maintainability and aligns with recent API changes. --- .../worlds/command/WorldCreateCommand.java | 40 +++++++++---------- .../command/argument/WorldTypeArgument.java | 19 ++++----- .../WorldTypeSuggestionProvider.java | 5 ++- 3 files changed, 34 insertions(+), 30 deletions(-) diff --git a/plugin/src/main/java/net/thenextlvl/worlds/command/WorldCreateCommand.java b/plugin/src/main/java/net/thenextlvl/worlds/command/WorldCreateCommand.java index b7488e72..065733e9 100644 --- a/plugin/src/main/java/net/thenextlvl/worlds/command/WorldCreateCommand.java +++ b/plugin/src/main/java/net/thenextlvl/worlds/command/WorldCreateCommand.java @@ -12,15 +12,15 @@ import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import net.thenextlvl.worlds.WorldsPlugin; import net.thenextlvl.worlds.api.model.Generator; +import net.thenextlvl.worlds.api.model.WorldPreset; import net.thenextlvl.worlds.api.preset.Preset; import net.thenextlvl.worlds.command.argument.*; import org.bukkit.NamespacedKey; import org.bukkit.World; -import org.bukkit.WorldCreator; -import org.bukkit.WorldType; import org.bukkit.entity.Entity; import org.jetbrains.annotations.Nullable; +import java.io.File; import java.util.concurrent.ThreadLocalRandom; import static org.bukkit.event.player.PlayerTeleportEvent.TeleportCause.COMMAND; @@ -50,7 +50,7 @@ class WorldCreateCommand { true, ThreadLocalRandom.current().nextLong())) .then(tree(this::createType)))) .executes(context -> create(context, World.Environment.NORMAL, true, - ThreadLocalRandom.current().nextLong(), WorldType.NORMAL, null, null))); + ThreadLocalRandom.current().nextLong(), WorldPreset.NORMAL, null, null))); } private RequiredArgumentBuilder tree(Creator creator) { @@ -76,25 +76,25 @@ private RequiredArgumentBuilder tree(Crea } private int create(CommandContext context, World.Environment environment, boolean structures, - long seed, WorldType worldType, @Nullable Preset preset, @Nullable Generator generator) { + long seed, WorldPreset type, @Nullable Preset preset, @Nullable Generator generator) { var key = context.getArgument("key", NamespacedKey.class); var name = key.getKey(); - var creator = new WorldCreator(name, key) - .environment(environment) - .generateStructures(structures) - .seed(seed) - .type(worldType); - if (preset != null) creator.generatorSettings(preset.serialize().toString()); + var levelFolder = new File(plugin.getServer().getWorldContainer(), name); - if (generator != null) { - creator.generator(generator.plugin().getDefaultWorldGenerator(name, generator.id())); - creator.biomeProvider(generator.plugin().getDefaultBiomeProvider(name, generator.id())); - } + var level = plugin.levelBuilder(levelFolder) + .environment(environment) + .generator(generator) + .preset(preset) + .seed(seed) + .name(name) + .structures(structures) + .type(type) + .build(); - var world = plugin.getServer().getWorld(creator.key()) == null - && plugin.getServer().getWorld(name) == null - ? creator.createWorld() : null; + var world = plugin.getServer().getWorld(level.key()) == null + && plugin.getServer().getWorld(level.name()) == null + ? level.create().orElse(null) : null; var message = world != null ? "world.create.success" : "world.create.failed"; plugin.bundle().sendMessage(context.getSource().getSender(), message, @@ -114,16 +114,16 @@ private int create(CommandContext context, World.Environment private int createGenerator(CommandContext context, World.Environment environment, boolean structures, long seed) { var generator = context.getArgument("generator", Generator.class); - return create(context, environment, structures, seed, WorldType.NORMAL, null, generator); + return create(context, environment, structures, seed, WorldPreset.NORMAL, null, generator); } private int createPreset(CommandContext context, World.Environment environment, boolean structures, long seed) { var preset = context.getArgument("preset", Preset.class); - return create(context, environment, structures, seed, WorldType.FLAT, preset, null); + return create(context, environment, structures, seed, WorldPreset.FLAT, preset, null); } private int createType(CommandContext context, World.Environment environment, boolean structures, long seed) { - var type = context.getArgument("type", WorldType.class); + var type = context.getArgument("type", WorldPreset.class); return create(context, environment, structures, seed, type, null, null); } diff --git a/plugin/src/main/java/net/thenextlvl/worlds/command/argument/WorldTypeArgument.java b/plugin/src/main/java/net/thenextlvl/worlds/command/argument/WorldTypeArgument.java index a6fceff5..25f05a9e 100644 --- a/plugin/src/main/java/net/thenextlvl/worlds/command/argument/WorldTypeArgument.java +++ b/plugin/src/main/java/net/thenextlvl/worlds/command/argument/WorldTypeArgument.java @@ -4,19 +4,20 @@ import io.papermc.paper.command.brigadier.argument.ArgumentTypes; import net.kyori.adventure.key.Key; import net.thenextlvl.worlds.WorldsPlugin; +import net.thenextlvl.worlds.api.model.WorldPreset; import net.thenextlvl.worlds.command.suggestion.WorldTypeSuggestionProvider; -import org.bukkit.WorldType; @SuppressWarnings("UnstableApiUsage") -public class WorldTypeArgument extends WrappedArgumentType { +public class WorldTypeArgument extends WrappedArgumentType { public WorldTypeArgument(WorldsPlugin plugin) { - super(ArgumentTypes.key(), (reader, type) -> switch (type.asString()) { - case "minecraft:amplified" -> WorldType.AMPLIFIED; - case "minecraft:flat" -> WorldType.FLAT; - case "minecraft:large_biomes" -> WorldType.LARGE_BIOMES; - case "minecraft:normal" -> WorldType.NORMAL; - // case "minecraft:single_biome" -> ; - // case "minecraft:debug_world" -> ; + super(ArgumentTypes.key(), (reader, type) -> switch (type.asMinimalString()) { + case "amplified" -> WorldPreset.AMPLIFIED; + case "checkerboard" -> WorldPreset.CHECKERBOARD; + case "debug_all_block_states", "debug_world", "debug" -> WorldPreset.DEBUG; + case "fixed", "single_biome" -> WorldPreset.SINGLE_BIOME; + case "flat" -> WorldPreset.FLAT; + case "large_biomes" -> WorldPreset.LARGE_BIOMES; + case "noise", "normal", "default" -> WorldPreset.NORMAL; default -> throw new IllegalArgumentException("Custom dimensions are not yet supported"); }, new WorldTypeSuggestionProvider(plugin)); } diff --git a/plugin/src/main/java/net/thenextlvl/worlds/command/suggestion/WorldTypeSuggestionProvider.java b/plugin/src/main/java/net/thenextlvl/worlds/command/suggestion/WorldTypeSuggestionProvider.java index dcbc6fc6..86231c75 100644 --- a/plugin/src/main/java/net/thenextlvl/worlds/command/suggestion/WorldTypeSuggestionProvider.java +++ b/plugin/src/main/java/net/thenextlvl/worlds/command/suggestion/WorldTypeSuggestionProvider.java @@ -18,9 +18,12 @@ public class WorldTypeSuggestionProvider implements SuggestionProvider { private final Map dimensions = Map.of( "minecraft:amplified", "world.type.amplified", + "minecraft:checkerboard", "world.type.checkerboard", + "minecraft:debug", "world.type.debug", "minecraft:flat", "world.type.flat", "minecraft:large_biomes", "world.type.large_biomes", - "minecraft:normal", "world.type.normal" + "minecraft:normal", "world.type.normal", + "minecraft:single_biome", "world.type.single_biome" ); @Override