diff --git a/src/main/java/com/nexia/core/commands/staff/MapCommand.java b/src/main/java/com/nexia/core/commands/staff/MapCommand.java index 995146a3..05f02129 100644 --- a/src/main/java/com/nexia/core/commands/staff/MapCommand.java +++ b/src/main/java/com/nexia/core/commands/staff/MapCommand.java @@ -3,9 +3,10 @@ import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import com.nexia.base.player.NexiaPlayer; import com.nexia.core.utilities.chat.ChatFormat; import com.nexia.core.utilities.commands.CommandUtil; -import com.nexia.base.player.NexiaPlayer; import com.nexia.core.utilities.time.ServerTime; import com.nexia.core.utilities.world.WorldUtil; import com.nexia.nexus.api.command.CommandSourceInfo; @@ -14,13 +15,17 @@ import com.nexia.nexus.api.world.util.Location; import net.kyori.adventure.text.Component; import net.minecraft.commands.SharedSuggestionProvider; +import net.minecraft.commands.arguments.ResourceLocationArgument; import net.minecraft.core.Registry; import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.level.ServerLevel; +import net.minecraft.world.level.biome.Biome; +import net.minecraft.world.level.biome.Biomes; import org.apache.commons.io.FileUtils; - +import org.jetbrains.annotations.NotNull; import java.io.File; +import static net.minecraft.server.commands.LocateBiomeCommand.ERROR_INVALID_BIOME; public class MapCommand { public static void register(CommandDispatcher dispatcher) { @@ -28,24 +33,32 @@ public static void register(CommandDispatcher dispatcher) { .requires(commandSourceInfo -> CommandUtil.hasPermission(commandSourceInfo, "nexia.staff.map", 2)) .then(CommandUtils.argument("type", StringArgumentType.string()) .suggests(((context, builder) -> SharedSuggestionProvider.suggest((new String[]{"delete", "create", "tp"}), builder))) - .then(CommandUtils.argument("map", StringArgumentType.greedyString()) + .then(CommandUtils.argument("map", ResourceLocationArgument.id()) .suggests(((context, builder) -> SharedSuggestionProvider.suggest((WorldUtil.getAllWorldsId()), builder))) - .executes(MapCommand::run) + .executes((context -> run(context, StringArgumentType.getString(context, "type"), context.getArgument("map", ResourceLocation.class), Biomes.THE_VOID))) + .then(CommandUtils.argument("biome", ResourceLocationArgument.id()) + .suggests(((context, builder) -> SharedSuggestionProvider.suggestResource((ServerTime.minecraftServer.registryAccess().registryOrThrow(Registry.BIOME_REGISTRY).keySet()), builder))) + .executes((context -> run(context, StringArgumentType.getString(context, "type"), context.getArgument("map", ResourceLocation.class), context.getArgument("biome", ResourceLocation.class)))) + ) ) ) )); } - private static int run(CommandContext context) { + private static int run(CommandContext context, String type, ResourceLocation map, ResourceLocation biomeLocation) throws CommandSyntaxException { + // check if biome is valid + ServerTime.minecraftServer.registryAccess().registryOrThrow(Registry.BIOME_REGISTRY).getOptional(biomeLocation).orElseThrow(() -> ERROR_INVALID_BIOME.create(biomeLocation)); + + return run(context, type, map, ResourceKey.create(Registry.BIOME_REGISTRY, biomeLocation)); + } + + private static int run(CommandContext context, String type, ResourceLocation map, @NotNull ResourceKey biome) { NexiaPlayer player = null; if(context.getSource().getExecutingEntity() instanceof Player nexusPlayer) { player = new NexiaPlayer(nexusPlayer); } - String type = StringArgumentType.getString(context, "type"); - String map = StringArgumentType.getString(context, "map"); - - if(map.trim().isEmpty() || type.trim().isEmpty()) { + if(map == null || map.toString().trim().isEmpty() || type.trim().isEmpty()) { context.getSource().sendMessage( ChatFormat.nexiaMessage .append(Component.text("Invalid name!", ChatFormat.failColor)) @@ -53,18 +66,16 @@ private static int run(CommandContext context) { return 1; } - String[] mapname = map.split(":"); - if(type.equalsIgnoreCase("create")){ - ServerLevel level = ServerTime.fantasy.getOrOpenPersistentWorld(ResourceKey.create(Registry.DIMENSION_REGISTRY, new ResourceLocation(mapname[0], mapname[1])).location(), (WorldUtil.defaultWorldConfig)).asWorld(); + ServerLevel level = ServerTime.fantasy.getOrOpenPersistentWorld(ResourceKey.create(Registry.DIMENSION_REGISTRY, map).location(), (WorldUtil.defaultWorldConfig.setGenerator(WorldUtil.getChunkGenerator(biome)))).asWorld(); if (player != null) { player.teleport(new Location(0, 80, 0, 0, 0, WorldUtil.getWorld(level))); player.sendNexiaMessage( Component.text("Created map called: ", ChatFormat.normalColor) - .append(Component.text(map, ChatFormat.brandColor2)) + .append(Component.text(map.toString(), ChatFormat.brandColor2)) ); } @@ -73,29 +84,29 @@ private static int run(CommandContext context) { } if (type.equalsIgnoreCase("delete")) { - ServerTime.fantasy.getOrOpenPersistentWorld(ResourceKey.create(Registry.DIMENSION_REGISTRY, new ResourceLocation(mapname[0], mapname[1])).location(), null).delete(); + ServerTime.fantasy.getOrOpenPersistentWorld(ResourceKey.create(Registry.DIMENSION_REGISTRY, map).location(), null).delete(); if(player != null) { player.sendNexiaMessage( Component.text("Deleted map called: ", ChatFormat.normalColor) - .append(Component.text(map, ChatFormat.brandColor2)) + .append(Component.text(map.toString(), ChatFormat.brandColor2)) ); } try { - FileUtils.forceDeleteOnExit(new File("/world/dimensions/" + mapname[0], mapname[1])); + FileUtils.forceDeleteOnExit(new File("/world/dimensions/" + map.getNamespace(), map.getPath())); } catch (Exception ignored) { } return 1; } if(type.equalsIgnoreCase("tp")) { - ServerLevel level = ServerTime.fantasy.getOrOpenPersistentWorld(ResourceKey.create(Registry.DIMENSION_REGISTRY, new ResourceLocation(mapname[0], mapname[1])).location(), null).asWorld(); + ServerLevel level = ServerTime.fantasy.getOrOpenPersistentWorld(ResourceKey.create(Registry.DIMENSION_REGISTRY, map).location(), null).asWorld(); if(player != null) { player.teleport(new Location(0, 80, 0, 0, 0, WorldUtil.getWorld(level))); player.sendNexiaMessage( Component.text("Teleported to map called: ", ChatFormat.normalColor) - .append(Component.text(map, ChatFormat.brandColor2)) + .append(Component.text(map.toString(), ChatFormat.brandColor2)) ); }