Skip to content

Commit

Permalink
Refactor world creation to use WorldPreset.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
NonSwag committed Aug 17, 2024
1 parent 440e03e commit 2a3c837
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<CommandSourceStack, World.Environment> tree(Creator<CommandSourceStack> creator) {
Expand All @@ -76,25 +76,25 @@ private RequiredArgumentBuilder<CommandSourceStack, World.Environment> tree(Crea
}

private int create(CommandContext<CommandSourceStack> 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,
Expand All @@ -114,16 +114,16 @@ private int create(CommandContext<CommandSourceStack> context, World.Environment

private int createGenerator(CommandContext<CommandSourceStack> 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<CommandSourceStack> 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<CommandSourceStack> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Key, WorldType> {
public class WorldTypeArgument extends WrappedArgumentType<Key, WorldPreset> {
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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ public class WorldTypeSuggestionProvider implements SuggestionProvider {

private final Map<String, String> 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
Expand Down

0 comments on commit 2a3c837

Please sign in to comment.