From 72415cdae5d9978f3d92b14b5b5ad4f1c29c5809 Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Wed, 15 Feb 2023 12:17:04 +0800 Subject: [PATCH 1/2] feat: Implement list command --- .../MultiverseCore/MultiverseCore.java | 2 + .../MultiverseCore/commands/ListCommand.java | 126 ++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 src/main/java/com/onarandombox/MultiverseCore/commands/ListCommand.java diff --git a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java index fdb5432d..0b5b5d16 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java @@ -33,6 +33,7 @@ import com.onarandombox.MultiverseCore.commands.CreateCommand; import com.onarandombox.MultiverseCore.commands.DebugCommand; import com.onarandombox.MultiverseCore.commands.DeleteCommand; import com.onarandombox.MultiverseCore.commands.GameruleCommand; +import com.onarandombox.MultiverseCore.commands.ListCommand; import com.onarandombox.MultiverseCore.commands.LoadCommand; import com.onarandombox.MultiverseCore.commands.RegenCommand; import com.onarandombox.MultiverseCore.commands.ReloadCommand; @@ -204,6 +205,7 @@ public class MultiverseCore extends JavaPlugin implements MVCore { this.commandManager.registerCommand(new DebugCommand(this)); this.commandManager.registerCommand(new DeleteCommand(this)); this.commandManager.registerCommand(new GameruleCommand(this)); + this.commandManager.registerCommand(new ListCommand(this)); this.commandManager.registerCommand(new LoadCommand(this)); this.commandManager.registerCommand(new RegenCommand(this)); this.commandManager.registerCommand(new ReloadCommand(this)); diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/ListCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/ListCommand.java new file mode 100644 index 00000000..b91b424b --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/ListCommand.java @@ -0,0 +1,126 @@ +package com.onarandombox.MultiverseCore.commands; + +import java.util.ArrayList; +import java.util.List; + +import co.aikar.commands.BukkitCommandIssuer; +import co.aikar.commands.InvalidCommandArgument; +import co.aikar.commands.annotation.CommandAlias; +import co.aikar.commands.annotation.CommandCompletion; +import co.aikar.commands.annotation.CommandPermission; +import co.aikar.commands.annotation.Description; +import co.aikar.commands.annotation.Subcommand; +import co.aikar.commands.annotation.Syntax; +import com.onarandombox.MultiverseCore.MultiverseCore; +import com.onarandombox.MultiverseCore.api.MVWorld; +import com.onarandombox.MultiverseCore.commandtools.flags.CommandFlagGroup; +import com.onarandombox.MultiverseCore.commandtools.flags.CommandValueFlag; +import com.onarandombox.MultiverseCore.commandtools.flags.ParsedCommandFlags; +import com.onarandombox.MultiverseCore.display.ContentDisplay; +import com.onarandombox.MultiverseCore.display.filters.ContentFilter; +import com.onarandombox.MultiverseCore.display.filters.DefaultContentFilter; +import com.onarandombox.MultiverseCore.display.filters.RegexContentFilter; +import com.onarandombox.MultiverseCore.display.handlers.PagedSendHandler; +import com.onarandombox.MultiverseCore.display.parsers.ListContentProvider; +import org.bukkit.ChatColor; +import org.bukkit.World; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +@CommandAlias("mv") +public class ListCommand extends MultiverseCoreCommand { + public ListCommand(@NotNull MultiverseCore plugin) { + super(plugin); + + registerFlagGroup(CommandFlagGroup.builder("mvlist") + .add(CommandValueFlag.builder("--filter", ContentFilter.class) + .addAlias("-f") + .context((value) -> { + try { + return RegexContentFilter.fromString(value); + } catch (IllegalArgumentException e) { + throw new InvalidCommandArgument("Invalid filter: " + value); + } + }) + .build()) + .add(CommandValueFlag.builder("--page", Integer.class) + .addAlias("-p") + .context((value) -> { + try { + return Integer.parseInt(value); + } catch (NumberFormatException e) { + throw new InvalidCommandArgument("Invalid page number: " + value); + } + }) + .build()) + .build()); + } + + @Subcommand("list") + @CommandPermission("multiverse.core.list.worlds") + @CommandCompletion("@flags:groupName=mvlist") + @Syntax("--filter [filter] --page [page]") + @Description("Displays a listing of all worlds that you can enter.") + public void onListCommand(BukkitCommandIssuer issuer, + + @Syntax("--filter [filter] --page [page]") + @Description("Filters the list of worlds by the given regex and displays the given page.") + String[] flags + ) { + ParsedCommandFlags parsedFlags = parseFlags(flags); + ContentDisplay.create() + .addContent(ListContentProvider.forContent(getListContents(issuer))) + .withSendHandler(PagedSendHandler.create() + .withHeader("%s====[ Multiverse World List ]====", ChatColor.GOLD) + .withTargetPage(parsedFlags.flagValue("--page", 1, Integer.class)) + .withFilter(parsedFlags.flagValue("--filter", DefaultContentFilter.get(), ContentFilter.class)) + .withLinesPerPage(4)) //TODO Change back after testing + .send(issuer); + } + + private List getListContents(BukkitCommandIssuer issuer) { + Player player = issuer.isPlayer() ? issuer.getPlayer() : null; + List worldList = new ArrayList<>(); + + this.plugin.getMVWorldManager().getMVWorlds().stream() + .filter(world -> player == null || plugin.getMVPerms().canEnterWorld(player, world)) + .filter(world -> canSeeWorld(player, world)) + .map(world -> hiddenText(world) + world.getColoredWorldString() + " - " + parseColouredEnvironment(world.getEnvironment())) + .sorted() + .forEach(worldList::add); + + this.plugin.getMVWorldManager().getUnloadedWorlds().stream() + .filter(world -> plugin.getMVPerms().hasPermission(issuer.getIssuer(), "multiverse.access." + world, true)) + .map(world -> ChatColor.GRAY + world + " - UNLOADED") + .sorted() + .forEach(worldList::add); + + return worldList; + } + + private boolean canSeeWorld(Player player, MVWorld world) { + return !world.isHidden() + || player == null + || this.plugin.getMVPerms().hasPermission(player, "multiverse.core.modify", true); + } + + private String hiddenText(MVWorld world) { + return (world.isHidden()) ? String.format("%s[H] ", ChatColor.GRAY) : ""; + } + + private String parseColouredEnvironment(World.Environment env) { + ChatColor color = ChatColor.GOLD; + switch (env) { + case NETHER: + color = ChatColor.RED; + break; + case NORMAL: + color = ChatColor.GREEN; + break; + case THE_END: + color = ChatColor.AQUA; + break; + } + return color + env.toString(); + } +} From da1d059b848d0627b062fde30fe2431271eb5d48 Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Thu, 31 Aug 2023 23:55:40 +0800 Subject: [PATCH 2/2] Update list command to new api changes --- .../MultiverseCore/commands/ListCommand.java | 35 ++++++++++++++----- .../multiverse/core/inject/InjectionTest.kt | 2 +- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/ListCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/ListCommand.java index b91b424b..cc595b6f 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/ListCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/ListCommand.java @@ -13,6 +13,9 @@ import co.aikar.commands.annotation.Subcommand; import co.aikar.commands.annotation.Syntax; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MVWorld; +import com.onarandombox.MultiverseCore.api.MVWorldManager; +import com.onarandombox.MultiverseCore.commandtools.MVCommandManager; +import com.onarandombox.MultiverseCore.commandtools.MultiverseCommand; import com.onarandombox.MultiverseCore.commandtools.flags.CommandFlagGroup; import com.onarandombox.MultiverseCore.commandtools.flags.CommandValueFlag; import com.onarandombox.MultiverseCore.commandtools.flags.ParsedCommandFlags; @@ -22,15 +25,31 @@ import com.onarandombox.MultiverseCore.display.filters.DefaultContentFilter; import com.onarandombox.MultiverseCore.display.filters.RegexContentFilter; import com.onarandombox.MultiverseCore.display.handlers.PagedSendHandler; import com.onarandombox.MultiverseCore.display.parsers.ListContentProvider; +import com.onarandombox.MultiverseCore.utils.UnsafeCallWrapper; +import com.onarandombox.MultiverseCore.world.entrycheck.WorldEntryCheckerProvider; +import jakarta.inject.Inject; import org.bukkit.ChatColor; import org.bukkit.World; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; +import org.jvnet.hk2.annotations.Service; +@Service @CommandAlias("mv") -public class ListCommand extends MultiverseCoreCommand { - public ListCommand(@NotNull MultiverseCore plugin) { - super(plugin); +public class ListCommand extends MultiverseCommand { + + private final MVWorldManager worldManager; + private final WorldEntryCheckerProvider worldEntryCheckerProvider; + + @Inject + public ListCommand( + @NotNull MVCommandManager commandManager, + @NotNull MVWorldManager worldManager, + @NotNull WorldEntryCheckerProvider worldEntryCheckerProvider + ) { + super(commandManager); + this.worldManager = worldManager; + this.worldEntryCheckerProvider = worldEntryCheckerProvider; registerFlagGroup(CommandFlagGroup.builder("mvlist") .add(CommandValueFlag.builder("--filter", ContentFilter.class) @@ -82,15 +101,15 @@ public class ListCommand extends MultiverseCoreCommand { Player player = issuer.isPlayer() ? issuer.getPlayer() : null; List worldList = new ArrayList<>(); - this.plugin.getMVWorldManager().getMVWorlds().stream() - .filter(world -> player == null || plugin.getMVPerms().canEnterWorld(player, world)) + worldManager.getMVWorlds().stream() + .filter(world -> player == null || worldEntryCheckerProvider.forSender(player).canAccessWorld(world).isSuccess()) .filter(world -> canSeeWorld(player, world)) .map(world -> hiddenText(world) + world.getColoredWorldString() + " - " + parseColouredEnvironment(world.getEnvironment())) .sorted() .forEach(worldList::add); - this.plugin.getMVWorldManager().getUnloadedWorlds().stream() - .filter(world -> plugin.getMVPerms().hasPermission(issuer.getIssuer(), "multiverse.access." + world, true)) + worldManager.getUnloadedWorlds().stream() + .filter(world -> issuer.hasPermission("multiverse.access." + world)) // TODO: Refactor stray permission check .map(world -> ChatColor.GRAY + world + " - UNLOADED") .sorted() .forEach(worldList::add); @@ -101,7 +120,7 @@ public class ListCommand extends MultiverseCoreCommand { private boolean canSeeWorld(Player player, MVWorld world) { return !world.isHidden() || player == null - || this.plugin.getMVPerms().hasPermission(player, "multiverse.core.modify", true); + || player.hasPermission("multiverse.core.modify"); // TODO: Refactor stray permission check } private String hiddenText(MVWorld world) { diff --git a/src/test/java/org/mvplugins/multiverse/core/inject/InjectionTest.kt b/src/test/java/org/mvplugins/multiverse/core/inject/InjectionTest.kt index 68901a7e..ab3d1f61 100644 --- a/src/test/java/org/mvplugins/multiverse/core/inject/InjectionTest.kt +++ b/src/test/java/org/mvplugins/multiverse/core/inject/InjectionTest.kt @@ -127,7 +127,7 @@ class InjectionTest : TestWithMockBukkit() { fun `Commands are available as services`() { val commands = multiverseCore.getAllServices(MultiverseCommand::class.java) // TODO come up with a better way to test this like via actually testing the effect of calling each command - assertEquals(17, commands.size) + assertEquals(18, commands.size) } @Test