Merge pull request #2841 from Multiverse/list-command

feat: Implement list command
This commit is contained in:
Ben Woo 2023-08-31 23:58:56 +08:00 committed by GitHub
commit 1e9f19b01f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 146 additions and 1 deletions

View File

@ -0,0 +1,145 @@
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.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;
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 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 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)
.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<String> getListContents(BukkitCommandIssuer issuer) {
Player player = issuer.isPlayer() ? issuer.getPlayer() : null;
List<String> worldList = new ArrayList<>();
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);
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);
return worldList;
}
private boolean canSeeWorld(Player player, MVWorld world) {
return !world.isHidden()
|| player == null
|| player.hasPermission("multiverse.core.modify"); // TODO: Refactor stray permission check
}
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();
}
}

View File

@ -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