From ab13472aee225d327bca5dfcc4dfe16f1c95e71b Mon Sep 17 00:00:00 2001 From: david Date: Thu, 6 Jun 2024 15:23:04 +0200 Subject: [PATCH] updated cloud --- .../worlds/command/WorldCommand.java | 59 +++++++------- .../worlds/command/WorldCreateCommand.java | 76 ++++++++++--------- .../worlds/command/WorldDeleteCommand.java | 14 ++-- .../worlds/command/WorldExportCommand.java | 18 +++-- .../worlds/command/WorldImportCommand.java | 20 +++-- .../worlds/command/WorldInfoCommand.java | 26 ++++--- .../worlds/command/WorldLinkCommand.java | 47 +++++++----- .../worlds/command/WorldListCommand.java | 11 +-- .../worlds/command/WorldSetSpawnCommand.java | 28 ++++--- .../worlds/command/WorldTeleportCommand.java | 11 +-- 10 files changed, 170 insertions(+), 140 deletions(-) diff --git a/plugin/src/main/java/net/thenextlvl/worlds/command/WorldCommand.java b/plugin/src/main/java/net/thenextlvl/worlds/command/WorldCommand.java index 4854d5f..ebcda41 100644 --- a/plugin/src/main/java/net/thenextlvl/worlds/command/WorldCommand.java +++ b/plugin/src/main/java/net/thenextlvl/worlds/command/WorldCommand.java @@ -1,10 +1,9 @@ package net.thenextlvl.worlds.command; import com.google.gson.JsonParseException; +import io.papermc.paper.command.brigadier.CommandSourceStack; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import net.thenextlvl.worlds.Worlds; -import org.bukkit.command.CommandSender; -import org.incendo.cloud.SenderMapper; import org.incendo.cloud.bukkit.parser.PlayerParser; import org.incendo.cloud.bukkit.parser.WorldParser; import org.incendo.cloud.exception.ArgumentParseException; @@ -15,12 +14,16 @@ import org.incendo.cloud.minecraft.extras.MinecraftExceptionHandler; import org.incendo.cloud.paper.PaperCommandManager; -public class WorldCommand extends PaperCommandManager { +@SuppressWarnings("UnstableApiUsage") +public class WorldCommand { + private final PaperCommandManager commandManager; private final Worlds plugin; public WorldCommand(Worlds plugin) { - super(plugin, ExecutionCoordinator.simpleCoordinator(), SenderMapper.identity()); - MinecraftExceptionHandler.createNative() + this.commandManager = PaperCommandManager.builder() + .executionCoordinator(ExecutionCoordinator.simpleCoordinator()) + .buildOnEnable(plugin); + MinecraftExceptionHandler.create(CommandSourceStack::getSender) .handler(InvalidSyntaxException.class, (formatter, context) -> { var syntax = context.exception().correctSyntax() .replace("[", "[").replace("]", "]") @@ -29,42 +32,38 @@ public WorldCommand(Worlds plugin) { return plugin.bundle().deserialize(" /" + syntax); }) .handler(InvalidCommandSenderException.class, (formatter, context) -> - plugin.bundle().component(context.context().sender(), "command.sender")) + plugin.bundle().component(context.context().sender().getSender(), "command.sender")) .handler(NoPermissionException.class, (formatter, context) -> - plugin.bundle().component(context.context().sender(), "command.permission", + plugin.bundle().component(context.context().sender().getSender(), "command.permission", Placeholder.parsed("permission", context.exception().missingPermission().permissionString()))) - .handler(ArgumentParseException.class, (formatter, context) -> { - context.exception().printStackTrace(); - return plugin.bundle().component(context.context().sender(), "command.argument"); - }) + .handler(ArgumentParseException.class, (formatter, context) -> + plugin.bundle().component(context.context().sender().getSender(), "command.argument")) .handler(PlayerParser.PlayerParseException.class, (formatter, context) -> - plugin.bundle().component(context.context().sender(), "player.unknown", + plugin.bundle().component(context.context().sender().getSender(), "player.unknown", Placeholder.parsed("player", context.exception().input()))) .handler(WorldParser.WorldParseException.class, (formatter, context) -> - plugin.bundle().component(context.context().sender(), "world.unknown", + plugin.bundle().component(context.context().sender().getSender(), "world.unknown", Placeholder.parsed("world", context.exception().input()))) .handler(JsonParseException.class, (formatter, context) -> - plugin.bundle().component(context.context().sender(), "world.preset.invalid")) + plugin.bundle().component(context.context().sender().getSender(), "world.preset.invalid")) .defaultCommandExecutionHandler() - .registerTo(this); - commandSyntaxFormatter(new CustomSyntaxFormatter<>(this)); - registerAsynchronousCompletions(); - registerBrigadier(); + .registerTo(commandManager); + commandManager.commandSyntaxFormatter(new CustomSyntaxFormatter<>(commandManager)); this.plugin = plugin; } public void register() { - var world = commandBuilder("world"); - command(new WorldCreateCommand(plugin, world).create()); - command(new WorldDeleteCommand(plugin, world).create()); - command(new WorldExportCommand(plugin, world).create()); - command(new WorldImportCommand(plugin, world).create()); - command(new WorldInfoCommand(plugin, world).create()); - command(new WorldLinkCommand.Create(plugin, world).create()); - command(new WorldLinkCommand.Delete(plugin, world).create()); - command(new WorldLinkCommand.List(plugin, world).create()); - command(new WorldListCommand(plugin, world).create()); - command(new WorldSetSpawnCommand(plugin, world).create()); - command(new WorldTeleportCommand(plugin, world).create()); + var world = commandManager.commandBuilder("world"); + commandManager.command(new WorldCreateCommand(plugin, world).create()); + commandManager.command(new WorldDeleteCommand(plugin, world).create()); + commandManager.command(new WorldExportCommand(plugin, world).create()); + commandManager.command(new WorldImportCommand(plugin, world).create()); + commandManager.command(new WorldInfoCommand(plugin, world).create()); + commandManager.command(new WorldLinkCommand.Create(plugin, world).create()); + commandManager.command(new WorldLinkCommand.Delete(plugin, world).create()); + commandManager.command(new WorldLinkCommand.List(plugin, world).create()); + commandManager.command(new WorldListCommand(plugin, world).create()); + commandManager.command(new WorldSetSpawnCommand(plugin, world).create()); + commandManager.command(new WorldTeleportCommand(plugin, world).create()); } } 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 b4bb360..224d775 100644 --- a/plugin/src/main/java/net/thenextlvl/worlds/command/WorldCreateCommand.java +++ b/plugin/src/main/java/net/thenextlvl/worlds/command/WorldCreateCommand.java @@ -2,6 +2,7 @@ import com.google.gson.JsonObject; import core.io.IO; +import io.papermc.paper.command.brigadier.CommandSourceStack; import lombok.RequiredArgsConstructor; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; @@ -17,7 +18,6 @@ import org.bukkit.World; import org.bukkit.World.Environment; import org.bukkit.WorldType; -import org.bukkit.command.CommandSender; import org.bukkit.entity.Entity; import org.bukkit.event.player.PlayerTeleportEvent; import org.bukkit.generator.WorldInfo; @@ -45,11 +45,12 @@ import java.util.concurrent.ThreadLocalRandom; @RequiredArgsConstructor +@SuppressWarnings("UnstableApiUsage") class WorldCreateCommand { private final Worlds plugin; - private final Command.Builder builder; + private final Command.Builder builder; - Command.Builder create() { + Command.Builder create() { return builder.literal("create") .permission("worlds.command.world.create") .required("name", StringParser.stringParser(), @@ -59,17 +60,17 @@ Command.Builder create() { .filter(s -> Bukkit.getWorld(s) == null) .map(Suggestion::suggestion) .toList())) - .flag(CommandFlag.builder("type").withAliases("t") + .flag(CommandFlag.builder("type").withAliases("t") .withDescription(RichDescription.of(Component.text("The world type"))) - .withComponent(TypedCommandComponent.ofType(WorldType.class, "type") + .withComponent(TypedCommandComponent.ofType(WorldType.class, "type") .parser(EnumParser.enumParser(WorldType.class)))) - .flag(CommandFlag.builder("environment").withAliases("e") + .flag(CommandFlag.builder("environment").withAliases("e") .withDescription(RichDescription.of(Component.text("The environment"))) - .withComponent(TypedCommandComponent.ofType(Environment.class, "environment") + .withComponent(TypedCommandComponent.ofType(Environment.class, "environment") .parser(EnumParser.enumParser(Environment.class)))) - .flag(CommandFlag.builder("generator").withAliases("g") + .flag(CommandFlag.builder("generator").withAliases("g") .withDescription(RichDescription.of(Component.text("The generator plugin"))) - .withComponent(TypedCommandComponent.ofType(String.class, "generator") + .withComponent(TypedCommandComponent.ofType(String.class, "generator") .parser(StringParser.greedyFlagYieldingStringParser()) .suggestionProvider(SuggestionProvider.blocking((context, input) -> Arrays.stream(Bukkit.getPluginManager().getPlugins()) @@ -78,13 +79,13 @@ Command.Builder create() { .map(Plugin::getName) .map(Suggestion::suggestion) .toList())))) - .flag(CommandFlag.builder("base").withAliases("b") + .flag(CommandFlag.builder("base").withAliases("b") .withDescription(RichDescription.of(Component.text("The world to clone"))) - .withComponent(TypedCommandComponent.ofType(World.class, "world") + .withComponent(TypedCommandComponent.ofType(World.class, "world") .parser(WorldParser.worldParser()))) - .flag(CommandFlag.builder("preset") + .flag(CommandFlag.builder("preset") .withDescription(RichDescription.of(Component.text("The preset to use"))) - .withComponent(TypedCommandComponent.ofType(String.class, "preset") + .withComponent(TypedCommandComponent.ofType(String.class, "preset") .parser(StringParser.greedyFlagYieldingStringParser()) .suggestionProvider(SuggestionProvider.blocking((context, input) -> PresetFile.findPresets(plugin.presetsFolder()).stream() @@ -92,42 +93,45 @@ Command.Builder create() { .map(name -> name.contains(" ") ? "\"" + name + "\"" : name) .map(Suggestion::suggestion) .toList())))) - .flag(CommandFlag.builder("deletion").withAliases("d") + .flag(CommandFlag.builder("deletion").withAliases("d") .withDescription(RichDescription.of(Component.text("What to do with the world on shutdown"))) - .withComponent(TypedCommandComponent.ofType(DeletionType.class, "deletion") + .withComponent(TypedCommandComponent.ofType(DeletionType.class, "deletion") .parser(EnumParser.enumParser(DeletionType.class)))) - .flag(CommandFlag.builder("identifier").withAliases("i") + .flag(CommandFlag.builder("identifier").withAliases("i") .withDescription(RichDescription.of(Component.text("The identifier of the world generator"))) - .withComponent(TypedCommandComponent.ofType(String.class, "identifier") + .withComponent(TypedCommandComponent.ofType(String.class, "identifier") .parser(StringParser.greedyFlagYieldingStringParser()))) - .flag(CommandFlag.builder("key") + .flag(CommandFlag.builder("key") .withDescription(RichDescription.of(Component.text("The namespaced key"))) - .withComponent(TypedCommandComponent.ofType(NamespacedKey.class, "key") + .withComponent(TypedCommandComponent.ofType(NamespacedKey.class, "key") .parser(NamespacedKeyParser.namespacedKeyParser(true)))) - .flag(CommandFlag.builder("seed").withAliases("s") + .flag(CommandFlag.builder("seed").withAliases("s") .withDescription(RichDescription.of(Component.text("The seed"))) - .withComponent(TypedCommandComponent.ofType(String.class, "seed") + .withComponent(TypedCommandComponent.ofType(String.class, "seed") .parser(StringParser.greedyFlagYieldingStringParser()))) - .flag(CommandFlag.builder("auto-save") + .flag(CommandFlag.builder("auto-save") .withDescription(RichDescription.of(Component.text("Whether the world should auto-save"))) - .withComponent(TypedCommandComponent.ofType(boolean.class, "auto-save") + .withComponent(TypedCommandComponent.ofType(boolean.class, "auto-save") .parser(BooleanParser.booleanParser()))) - .flag(CommandFlag.builder("structures") + .flag(CommandFlag.builder("structures") .withDescription(RichDescription.of(Component.text("Whether structures should generate"))) - .withComponent(TypedCommandComponent.ofType(boolean.class, "structures") + .withComponent(TypedCommandComponent.ofType(boolean.class, "structures") .parser(BooleanParser.booleanParser()))) - .flag(CommandFlag.builder("hardcore") + .flag(CommandFlag.builder("hardcore") .withDescription(RichDescription.of(Component.text("Whether hardcore is enabled")))) - .flag(CommandFlag.builder("load-manual") + .flag(CommandFlag.builder("load-manual") .withDescription(RichDescription.of(Component.text("Whether the world must be loaded manual on startup")))) .handler(this::execute); } - private void execute(CommandContext context) { + @SuppressWarnings("deprecation") + private void execute(CommandContext context) { var name = context.get("name"); + var sender = context.sender().getSender(); + if (Bukkit.getWorld(name) != null) { - plugin.bundle().sendMessage(context.sender(), "world.known", Placeholder.parsed("world", name)); + plugin.bundle().sendMessage(sender, "world.known", Placeholder.parsed("world", name)); return; } @@ -135,7 +139,7 @@ private void execute(CommandContext context) { var environment = context.flags().getValue("environment").orElse(Environment.NORMAL); if (environment.equals(Environment.CUSTOM)) { - plugin.bundle().sendMessage(context.sender(), "environment.custom"); + plugin.bundle().sendMessage(sender, "environment.custom"); return; } @@ -167,12 +171,12 @@ private void execute(CommandContext context) { JsonObject settings = null; if (preset != null && generator != null) { - plugin.bundle().sendMessage(context.sender(), "command.flag.combination", + plugin.bundle().sendMessage(sender, "command.flag.combination", Placeholder.parsed("flag-1", "generator"), Placeholder.parsed("flag-2", "preset")); return; } else if (preset != null && !Objects.equals(type, WorldType.FLAT)) { - plugin.bundle().sendMessage(context.sender(), "world.preset.flat"); + plugin.bundle().sendMessage(sender, "world.preset.flat"); return; } else if (preset != null) { final var fileName = preset + ".json"; @@ -185,8 +189,8 @@ private void execute(CommandContext context) { base.ifPresent(world -> { var placeholder = Placeholder.parsed("world", world.getName()); if (copy(world.getWorldFolder(), new File(Bukkit.getWorldContainer(), name))) - plugin.bundle().sendMessage(context.sender(), "world.clone.success", placeholder); - else plugin.bundle().sendMessage(context.sender(), "world.clone.failed", placeholder); + plugin.bundle().sendMessage(sender, "world.clone.success", placeholder); + else plugin.bundle().sendMessage(sender, "world.clone.failed", placeholder); }); var image = plugin.imageProvider().load(plugin.imageProvider().createWorldImage() @@ -195,8 +199,8 @@ private void execute(CommandContext context) { .hardcore(hardcore).loadOnStart(!loadManual).seed(seed)); var message = image != null ? "world.create.success" : "world.create.failed"; - plugin.bundle().sendMessage(context.sender(), message, Placeholder.parsed("world", name)); - if (image == null || !(context.sender() instanceof Entity entity)) return; + plugin.bundle().sendMessage(sender, message, Placeholder.parsed("world", name)); + if (image == null || !(sender instanceof Entity entity)) return; entity.teleportAsync(image.getWorld().getSpawnLocation(), PlayerTeleportEvent.TeleportCause.COMMAND); } diff --git a/plugin/src/main/java/net/thenextlvl/worlds/command/WorldDeleteCommand.java b/plugin/src/main/java/net/thenextlvl/worlds/command/WorldDeleteCommand.java index a8d3531..2ceb581 100644 --- a/plugin/src/main/java/net/thenextlvl/worlds/command/WorldDeleteCommand.java +++ b/plugin/src/main/java/net/thenextlvl/worlds/command/WorldDeleteCommand.java @@ -1,21 +1,22 @@ package net.thenextlvl.worlds.command; +import io.papermc.paper.command.brigadier.CommandSourceStack; import lombok.RequiredArgsConstructor; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import net.thenextlvl.worlds.Worlds; import org.bukkit.World; -import org.bukkit.command.CommandSender; import org.incendo.cloud.Command; import org.incendo.cloud.bukkit.parser.WorldParser; import org.incendo.cloud.context.CommandContext; import org.incendo.cloud.parser.flag.CommandFlag; @RequiredArgsConstructor +@SuppressWarnings("UnstableApiUsage") class WorldDeleteCommand { private final Worlds plugin; - private final Command.Builder builder; + private final Command.Builder builder; - Command.Builder create() { + Command.Builder create() { return builder.literal("delete") .permission("worlds.command.world.delete") .required("world", WorldParser.worldParser()) @@ -26,9 +27,10 @@ Command.Builder create() { .handler(this::execute); } - private void execute(CommandContext context) { + @SuppressWarnings("deprecation") + private void execute(CommandContext context) { if (!context.flags().contains("confirm")) { - plugin.bundle().sendMessage(context.sender(), "command.confirmation", + plugin.bundle().sendMessage(context.sender().getSender(), "command.confirmation", Placeholder.parsed("action", "/" + context.rawInput().input()), Placeholder.parsed("confirmation", "/" + context.rawInput().input() + " --confirm")); return; @@ -39,7 +41,7 @@ private void execute(CommandContext context) { var schedule = context.flags().contains("schedule"); var image = plugin.imageProvider().getOrDefault(world); var result = image.delete(keepImage, keepWorld, schedule); - plugin.bundle().sendMessage(context.sender(), result.getMessage(), + plugin.bundle().sendMessage(context.sender().getSender(), result.getMessage(), Placeholder.parsed("world", world.getName()), Placeholder.parsed("image", image.getWorldImage().name())); } diff --git a/plugin/src/main/java/net/thenextlvl/worlds/command/WorldExportCommand.java b/plugin/src/main/java/net/thenextlvl/worlds/command/WorldExportCommand.java index 160b13c..f5e71d7 100644 --- a/plugin/src/main/java/net/thenextlvl/worlds/command/WorldExportCommand.java +++ b/plugin/src/main/java/net/thenextlvl/worlds/command/WorldExportCommand.java @@ -1,10 +1,10 @@ package net.thenextlvl.worlds.command; +import io.papermc.paper.command.brigadier.CommandSourceStack; import lombok.RequiredArgsConstructor; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import net.thenextlvl.worlds.Worlds; import org.bukkit.World; -import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.incendo.cloud.Command; import org.incendo.cloud.bukkit.parser.WorldParser; @@ -14,27 +14,29 @@ import java.util.List; @RequiredArgsConstructor +@SuppressWarnings("UnstableApiUsage") class WorldExportCommand { private final Worlds plugin; - private final Command.Builder builder; + private final Command.Builder builder; - Command.Builder create() { + Command.Builder create() { return builder.literal("export", "save") .permission("worlds.command.world.export") .optional("world", WorldParser.worldParser()) .handler(this::execute); } - private void execute(CommandContext context) { - var world = context.optional("world").orElse(context.sender() instanceof Player self ? self.getWorld() : null); + private void execute(CommandContext context) { + var sender = context.sender().getSender(); + var world = context.optional("world").orElse(sender instanceof Player self ? self.getWorld() : null); if (world == null) throw new InvalidSyntaxException("world export [world]", context.sender(), List.of()); var placeholder = Placeholder.parsed("world", world.getName()); try { world.save(); - plugin.bundle().sendMessage(context.sender(), "world.save.success", placeholder); + plugin.bundle().sendMessage(sender, "world.save.success", placeholder); } catch (Exception e) { - plugin.bundle().sendMessage(context.sender(), "world.save.failed", placeholder); - e.printStackTrace(); + plugin.bundle().sendMessage(sender, "world.save.failed", placeholder); + plugin.getComponentLogger().error("Failed to save world {}", world.getName(), e); } } } diff --git a/plugin/src/main/java/net/thenextlvl/worlds/command/WorldImportCommand.java b/plugin/src/main/java/net/thenextlvl/worlds/command/WorldImportCommand.java index 75f8b00..226a147 100644 --- a/plugin/src/main/java/net/thenextlvl/worlds/command/WorldImportCommand.java +++ b/plugin/src/main/java/net/thenextlvl/worlds/command/WorldImportCommand.java @@ -1,11 +1,11 @@ package net.thenextlvl.worlds.command; +import io.papermc.paper.command.brigadier.CommandSourceStack; import lombok.RequiredArgsConstructor; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import net.thenextlvl.worlds.Worlds; import net.thenextlvl.worlds.image.WorldImage; import org.bukkit.Bukkit; -import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.event.player.PlayerTeleportEvent; import org.incendo.cloud.Command; @@ -15,11 +15,13 @@ import org.incendo.cloud.suggestion.SuggestionProvider; @RequiredArgsConstructor +@SuppressWarnings("UnstableApiUsage") class WorldImportCommand { private final Worlds plugin; - private final Command.Builder builder; + private final Command.Builder builder; - Command.Builder create() { + @SuppressWarnings("deprecation") + Command.Builder create() { return builder.literal("import") .permission("worlds.command.world.import") .required("image", StringParser.greedyStringParser(), @@ -31,7 +33,9 @@ Command.Builder create() { .handler(this::execute); } - private void execute(CommandContext context) { + private void execute(CommandContext context) { + var sender = context.sender().getSender(); + var imageName = context.get("image"); var image = plugin.imageProvider().findImageFiles().stream() .filter(file -> { @@ -43,21 +47,21 @@ private void execute(CommandContext context) { .orElse(null); if (image == null) { - plugin.bundle().sendMessage(context.sender(), "image.exists.not", Placeholder.parsed("image", imageName)); + plugin.bundle().sendMessage(sender, "image.exists.not", Placeholder.parsed("image", imageName)); return; } var world = Bukkit.getWorld(image.name()); var placeholder = Placeholder.parsed("world", world != null ? world.getName() : image.name()); if (world != null) { - plugin.bundle().sendMessage(context.sender(), "world.known", placeholder); + plugin.bundle().sendMessage(sender, "world.known", placeholder); return; } var result = plugin.imageProvider().load(image); var message = result != null ? "world.import.success" : "world.import.failed"; - plugin.bundle().sendMessage(context.sender(), message, placeholder); - if (result == null || !(context.sender() instanceof Player player)) return; + plugin.bundle().sendMessage(sender, message, placeholder); + if (result == null || !(sender instanceof Player player)) return; player.teleportAsync(result.getWorld().getSpawnLocation(), PlayerTeleportEvent.TeleportCause.COMMAND); } } diff --git a/plugin/src/main/java/net/thenextlvl/worlds/command/WorldInfoCommand.java b/plugin/src/main/java/net/thenextlvl/worlds/command/WorldInfoCommand.java index 2e28abf..e2932a8 100644 --- a/plugin/src/main/java/net/thenextlvl/worlds/command/WorldInfoCommand.java +++ b/plugin/src/main/java/net/thenextlvl/worlds/command/WorldInfoCommand.java @@ -1,11 +1,11 @@ package net.thenextlvl.worlds.command; +import io.papermc.paper.command.brigadier.CommandSourceStack; import lombok.RequiredArgsConstructor; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import net.thenextlvl.worlds.Worlds; import org.bukkit.World; import org.bukkit.WorldType; -import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.incendo.cloud.Command; import org.incendo.cloud.bukkit.parser.WorldParser; @@ -16,11 +16,12 @@ import java.util.List; @RequiredArgsConstructor +@SuppressWarnings("UnstableApiUsage") class WorldInfoCommand { private final Worlds plugin; - private final Command.Builder builder; + private final Command.Builder builder; - Command.Builder create() { + Command.Builder create() { return builder.literal("info") .permission("worlds.command.world.info") .optional("world", WorldParser.worldParser()) @@ -28,22 +29,23 @@ Command.Builder create() { } @SuppressWarnings("deprecation") - private void execute(CommandContext context) { - var world = context.optional("world").orElse(context.sender() instanceof Player self ? self.getWorld() : null); - if (world == null) throw new InvalidSyntaxException("world info [world]", context.sender(), List.of()); + private void execute(CommandContext context) { + var sender = context.sender().getSender(); + var world = context.optional("world").orElse(sender instanceof Player self ? self.getWorld() : null); + if (world == null) throw new InvalidSyntaxException("world info [world]", sender, List.of()); var volume = plugin.imageProvider().getOrDefault(world); - plugin.bundle().sendMessage(context.sender(), "world.info.name", + plugin.bundle().sendMessage(sender, "world.info.name", Placeholder.parsed("world", world.getName())); - plugin.bundle().sendMessage(context.sender(), "world.info.players", + plugin.bundle().sendMessage(sender, "world.info.players", Placeholder.parsed("players", String.valueOf(world.getPlayers().size()))); - plugin.bundle().sendMessage(context.sender(), "world.info.type", + plugin.bundle().sendMessage(sender, "world.info.type", Placeholder.parsed("type", getName(notnull(world.getWorldType(), WorldType.NORMAL)))); - plugin.bundle().sendMessage(context.sender(), "world.info.environment", + plugin.bundle().sendMessage(sender, "world.info.environment", Placeholder.parsed("environment", getName(world.getEnvironment()))); - plugin.bundle().sendMessage(context.sender(), "world.info.generator", + plugin.bundle().sendMessage(sender, "world.info.generator", Placeholder.parsed("generator", String.valueOf(notnull( volume.getWorldImage().generator(), "Vanilla")))); - plugin.bundle().sendMessage(context.sender(), "world.info.seed", + plugin.bundle().sendMessage(sender, "world.info.seed", Placeholder.parsed("seed", String.valueOf(world.getSeed()))); } diff --git a/plugin/src/main/java/net/thenextlvl/worlds/command/WorldLinkCommand.java b/plugin/src/main/java/net/thenextlvl/worlds/command/WorldLinkCommand.java index 2f898d6..ba55c01 100644 --- a/plugin/src/main/java/net/thenextlvl/worlds/command/WorldLinkCommand.java +++ b/plugin/src/main/java/net/thenextlvl/worlds/command/WorldLinkCommand.java @@ -1,12 +1,12 @@ package net.thenextlvl.worlds.command; +import io.papermc.paper.command.brigadier.CommandSourceStack; import lombok.RequiredArgsConstructor; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import net.thenextlvl.worlds.Worlds; import net.thenextlvl.worlds.link.Link; import org.bukkit.PortalType; import org.bukkit.World; -import org.bukkit.command.CommandSender; import org.incendo.cloud.Command; import org.incendo.cloud.bukkit.parser.WorldParser; import org.incendo.cloud.context.CommandContext; @@ -18,24 +18,25 @@ import org.incendo.cloud.suggestion.SuggestionProvider; @RequiredArgsConstructor +@SuppressWarnings("UnstableApiUsage") abstract class WorldLinkCommand { protected final Worlds plugin; - protected final Command.Builder builder; + protected final Command.Builder builder; - protected final Command.Builder linkCommand() { + protected final Command.Builder linkCommand() { return builder.literal("link") .commandDescription(Description.description("link portals between dimensions")); } - abstract Command.Builder create(); + abstract Command.Builder create(); static class Create extends WorldLinkCommand { - public Create(Worlds plugin, Command.Builder builder) { + public Create(Worlds plugin, Command.Builder builder) { super(plugin, builder); } @Override - Command.Builder create() { + Command.Builder create() { return linkCommand().literal("create") .permission("worlds.command.link.create") .required("source", WorldParser.worldParser()) @@ -44,28 +45,30 @@ Command.Builder create() { .handler(this::execute); } - private void execute(CommandContext context) { + private void execute(CommandContext context) { handleCreate(context); } - private void handleCreate(CommandContext context) { + private void handleCreate(CommandContext context) { var source = context.get("source"); var destination = context.get("destination"); var portalType = context.optional("portal-type") .orElse(getPortalType(source.getEnvironment(), destination.getEnvironment())); + var sender = context.sender().getSender(); + if (portalType == null) throw new InvalidSyntaxException( "world link create [source] [destination] [portal-type]", - context.sender(), java.util.List.of() + sender, java.util.List.of() ); var link = new Link(portalType, source, destination); if (plugin.linkRegistry().register(link)) { - plugin.bundle().sendMessage(context.sender(), "link.created", + plugin.bundle().sendMessage(sender, "link.created", Placeholder.parsed("type", link.portalType().name().toLowerCase()), Placeholder.parsed("source", link.source().getName()), Placeholder.parsed("destination", link.destination().getName())); - } else plugin.bundle().sendMessage(context.sender(), "link.exists", + } else plugin.bundle().sendMessage(sender, "link.exists", Placeholder.parsed("type", link.portalType().name().toLowerCase()), Placeholder.parsed("source", link.source().getName()), Placeholder.parsed("destination", link.destination().getName())); @@ -94,12 +97,12 @@ private PortalType getPortalType(World.Environment source, World.Environment des } static class Delete extends WorldLinkCommand { - public Delete(Worlds plugin, Command.Builder builder) { + public Delete(Worlds plugin, Command.Builder builder) { super(plugin, builder); } @Override - Command.Builder create() { + Command.Builder create() { return linkCommand().literal("delete") .permission("worlds.command.link.delete") .required("link", StringParser.greedyStringParser(), @@ -110,38 +113,40 @@ Command.Builder create() { .handler(this::execute); } - private void execute(CommandContext context) { + private void execute(CommandContext context) { + var sender = context.sender().getSender(); var linkName = context.get("link"); var link = plugin.linkRegistry().getLinks() .filter(link1 -> link1.toString().equals(linkName)) .findFirst() .orElse(null); if (link != null && plugin.linkRegistry().unregister(link)) { - plugin.bundle().sendMessage(context.sender(), "link.deleted", + plugin.bundle().sendMessage(sender, "link.deleted", Placeholder.parsed("type", link.portalType().name().toLowerCase()), Placeholder.parsed("source", link.source().getName()), Placeholder.parsed("destination", link.destination().getName())); - } else plugin.bundle().sendMessage(context.sender(), "link.exists.not", + } else plugin.bundle().sendMessage(sender, "link.exists.not", Placeholder.parsed("link", linkName)); } } static class List extends WorldLinkCommand { - public List(Worlds plugin, Command.Builder builder) { + public List(Worlds plugin, Command.Builder builder) { super(plugin, builder); } @Override - Command.Builder create() { + Command.Builder create() { return linkCommand().literal("list") .permission("worlds.command.link.list") .handler(this::execute); } - private void execute(CommandContext context) { + private void execute(CommandContext context) { + var sender = context.sender().getSender(); var links = plugin.linkRegistry().getLinks().map(Link::toString).toList(); - if (links.isEmpty()) plugin.bundle().sendMessage(context.sender(), "link.list.empty"); - else plugin.bundle().sendMessage(context.sender(), "link.list", + if (links.isEmpty()) plugin.bundle().sendMessage(sender, "link.list.empty"); + else plugin.bundle().sendMessage(sender, "link.list", Placeholder.parsed("links", String.join(", ", links)), Placeholder.parsed("amount", String.valueOf(links.size()))); } diff --git a/plugin/src/main/java/net/thenextlvl/worlds/command/WorldListCommand.java b/plugin/src/main/java/net/thenextlvl/worlds/command/WorldListCommand.java index 2a66cd3..471e085 100644 --- a/plugin/src/main/java/net/thenextlvl/worlds/command/WorldListCommand.java +++ b/plugin/src/main/java/net/thenextlvl/worlds/command/WorldListCommand.java @@ -1,5 +1,6 @@ package net.thenextlvl.worlds.command; +import io.papermc.paper.command.brigadier.CommandSourceStack; import lombok.RequiredArgsConstructor; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.JoinConfiguration; @@ -8,24 +9,24 @@ import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import net.thenextlvl.worlds.Worlds; import org.bukkit.Bukkit; -import org.bukkit.command.CommandSender; import org.bukkit.generator.WorldInfo; import org.incendo.cloud.Command; import org.incendo.cloud.context.CommandContext; @RequiredArgsConstructor +@SuppressWarnings("UnstableApiUsage") class WorldListCommand { private final Worlds plugin; - private final Command.Builder builder; + private final Command.Builder builder; - Command.Builder create() { + Command.Builder create() { return builder.literal("list") .permission("worlds.command.world.list") .handler(this::execute); } - private void execute(CommandContext context) { - var sender = context.sender(); + private void execute(CommandContext context) { + var sender = context.sender().getSender(); var worlds = Bukkit.getWorlds().stream().map(WorldInfo::getName).toList(); var joined = Component.join(JoinConfiguration.commas(true), worlds.stream() .map(world -> Component.text(world) diff --git a/plugin/src/main/java/net/thenextlvl/worlds/command/WorldSetSpawnCommand.java b/plugin/src/main/java/net/thenextlvl/worlds/command/WorldSetSpawnCommand.java index b70d1d1..8fc5da8 100644 --- a/plugin/src/main/java/net/thenextlvl/worlds/command/WorldSetSpawnCommand.java +++ b/plugin/src/main/java/net/thenextlvl/worlds/command/WorldSetSpawnCommand.java @@ -1,49 +1,59 @@ package net.thenextlvl.worlds.command; +import io.papermc.paper.command.brigadier.CommandSourceStack; import lombok.RequiredArgsConstructor; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import net.thenextlvl.worlds.Worlds; import org.bukkit.Location; -import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.incendo.cloud.Command; import org.incendo.cloud.bukkit.parser.location.LocationParser; import org.incendo.cloud.component.DefaultValue; import org.incendo.cloud.context.CommandContext; +import org.incendo.cloud.exception.InvalidCommandSenderException; import org.incendo.cloud.parser.standard.FloatParser; +import java.util.List; + import static org.bukkit.event.player.PlayerTeleportEvent.TeleportCause.COMMAND; @RequiredArgsConstructor +@SuppressWarnings("UnstableApiUsage") class WorldSetSpawnCommand { private final Worlds plugin; - private final Command.Builder builder; + private final Command.Builder builder; - Command.Builder create() { + Command.Builder create() { return builder.literal("setspawn") .permission("worlds.command.world.setspawn") - .senderType(Player.class) + // .senderType(Player.class) .optional("position", LocationParser.locationParser(), DefaultValue.dynamic(context -> context.sender().getLocation())) .optional("angle", FloatParser.floatParser(-360, 360), - DefaultValue.dynamic(context -> context.sender().getYaw())) + DefaultValue.dynamic(context -> { + var executor = context.sender().getExecutor(); + return executor != null ? executor.getYaw() : 0; + })) .handler(this::execute); } - private void execute(CommandContext context) { + private void execute(CommandContext context) { + if (!(context.sender().getSender() instanceof Player player)) + throw new InvalidCommandSenderException(context.sender(), Player.class, List.of(), context.command()); + var location = context.get("position"); float angle = context.get("angle"); - var success = context.sender().getWorld().setSpawnLocation( + var success = player.getWorld().setSpawnLocation( location.getBlockX(), location.getBlockY(), location.getBlockZ(), angle ); - if (success) context.sender().teleportAsync(context.sender().getWorld().getSpawnLocation(), COMMAND); + if (success) player.teleportAsync(player.getWorld().getSpawnLocation(), COMMAND); var message = success ? "world.spawn.set.success" : "world.spawn.set.failed"; - plugin.bundle().sendMessage(context.sender(), message, + plugin.bundle().sendMessage(player, message, Placeholder.parsed("x", String.valueOf(location.getBlockX())), Placeholder.parsed("y", String.valueOf(location.getBlockY())), Placeholder.parsed("z", String.valueOf(location.getBlockZ())), diff --git a/plugin/src/main/java/net/thenextlvl/worlds/command/WorldTeleportCommand.java b/plugin/src/main/java/net/thenextlvl/worlds/command/WorldTeleportCommand.java index 6aaba47..f2b79c9 100644 --- a/plugin/src/main/java/net/thenextlvl/worlds/command/WorldTeleportCommand.java +++ b/plugin/src/main/java/net/thenextlvl/worlds/command/WorldTeleportCommand.java @@ -1,10 +1,10 @@ package net.thenextlvl.worlds.command; +import io.papermc.paper.command.brigadier.CommandSourceStack; import lombok.RequiredArgsConstructor; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import net.thenextlvl.worlds.Worlds; import org.bukkit.World; -import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.event.player.PlayerTeleportEvent; import org.incendo.cloud.Command; @@ -16,11 +16,12 @@ import java.util.List; @RequiredArgsConstructor +@SuppressWarnings("UnstableApiUsage") class WorldTeleportCommand { private final Worlds plugin; - private final Command.Builder builder; + private final Command.Builder builder; - Command.Builder create() { + Command.Builder create() { return builder.literal("teleport", "tp") .permission("worlds.command.world.teleport") .required("world", WorldParser.worldParser()) @@ -28,8 +29,8 @@ Command.Builder create() { .handler(this::execute); } - private void execute(CommandContext context) { - var sender = context.sender(); + private void execute(CommandContext context) { + var sender = context.sender().getSender(); var world = context.get("world"); var player = context.optional("player").orElse(sender instanceof Player self ? self : null); if (player == null) throw new InvalidSyntaxException("world teleport [world] [player]", sender, List.of());