Improve gamerules command with filter and page flag

This commit is contained in:
Ben Woo 2023-09-10 13:56:50 +08:00
parent 95a1fddbe5
commit b8c10c412d
No known key found for this signature in database
GPG Key ID: FB2A3645536E12C8

View File

@ -1,22 +1,26 @@
package com.onarandombox.MultiverseCore.commands; package com.onarandombox.MultiverseCore.commands;
import co.aikar.commands.BukkitCommandIssuer;
import co.aikar.commands.CommandIssuer; import co.aikar.commands.CommandIssuer;
import co.aikar.commands.InvalidCommandArgument;
import co.aikar.commands.MessageType; import co.aikar.commands.MessageType;
import co.aikar.commands.annotation.CommandAlias; import co.aikar.commands.annotation.CommandAlias;
import co.aikar.commands.annotation.CommandCompletion; import co.aikar.commands.annotation.CommandCompletion;
import co.aikar.commands.annotation.CommandPermission; import co.aikar.commands.annotation.CommandPermission;
import co.aikar.commands.annotation.Default;
import co.aikar.commands.annotation.Description; import co.aikar.commands.annotation.Description;
import co.aikar.commands.annotation.Flags;
import co.aikar.commands.annotation.Optional; import co.aikar.commands.annotation.Optional;
import co.aikar.commands.annotation.Subcommand; import co.aikar.commands.annotation.Subcommand;
import co.aikar.commands.annotation.Syntax; import co.aikar.commands.annotation.Syntax;
import com.dumptruckman.minecraft.util.Logging;
import com.onarandombox.MultiverseCore.api.MVWorld; import com.onarandombox.MultiverseCore.api.MVWorld;
import com.onarandombox.MultiverseCore.api.MVWorldManager; import com.onarandombox.MultiverseCore.commandtools.MVCommandIssuer;
import com.onarandombox.MultiverseCore.commandtools.MVCommandManager; import com.onarandombox.MultiverseCore.commandtools.MVCommandManager;
import com.onarandombox.MultiverseCore.commandtools.MultiverseCommand; import com.onarandombox.MultiverseCore.commandtools.MultiverseCommand;
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.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.handlers.PagedSendHandler;
import com.onarandombox.MultiverseCore.display.parsers.MapContentProvider; import com.onarandombox.MultiverseCore.display.parsers.MapContentProvider;
import com.onarandombox.MultiverseCore.utils.MVCorei18n; import com.onarandombox.MultiverseCore.utils.MVCorei18n;
@ -37,50 +41,71 @@ import java.util.Map;
@CommandAlias("mv") @CommandAlias("mv")
public class GamerulesCommand extends MultiverseCommand { public class GamerulesCommand extends MultiverseCommand {
private final CommandValueFlag<Integer> PAGE_FLAG = flag(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());
private final CommandValueFlag<ContentFilter> FILTER_FLAG = flag(CommandValueFlag
.builder("--filter", ContentFilter.class)
.addAlias("-f")
.context(value -> {
try {
return RegexContentFilter.fromString(value);
} catch (IllegalArgumentException e) {
throw new InvalidCommandArgument("Invalid filter: " + value);
}
})
.build());
@Inject @Inject
public GamerulesCommand(@NotNull MVCommandManager commandManager) { GamerulesCommand(@NotNull MVCommandManager commandManager) {
super(commandManager); super(commandManager);
} }
@Subcommand("gamerules|rules") @Subcommand("gamerules|rules")
@CommandPermission("multiverse.core.gamerule.list") @CommandPermission("multiverse.core.gamerule.list")
@CommandCompletion("@mvworlds @range:1-6") @CommandCompletion("@mvworlds|@flags:groupName=mvgamerulescommand @flags:groupName=mvgamerulescommand")
@Syntax("[world] [page]") @Syntax("[world] [--page <page>] [--filter <filter>]")
@Description("{@@mv-core.gamerules.description}") @Description("{@@mv-core.gamerules.description}")
public void onGamerulesCommand(@NotNull BukkitCommandIssuer issuer, public void onGamerulesCommand(
@Optional @NotNull MVCommandIssuer issuer,
@Flags("resolve=issuerAware")
@Syntax("<world>") @Syntax("<world>")
@Description("{@@mv-core.gamerules.description.world}") @Description("{@@mv-core.gamerules.description.world}")
MVWorld world, MVWorld world,
@Optional @Optional
@Default("1") @Syntax("[--page <page>] [--filter <filter>]")
@Syntax("<page>")
@Description("{@@mv-core.gamerules.description.page}") @Description("{@@mv-core.gamerules.description.page}")
int page String[] flags
) { ) {
Logging.finer("Page is: " + page); ParsedCommandFlags parsedFlags = parseFlags(flags);
ContentDisplay.create() ContentDisplay.create()
.addContent( .addContent(new MapContentProvider<>(getGameRuleMap(world.getCBWorld()))
new MapContentProvider<>(getGameRuleMap(world.getCBWorld()))
.withKeyColor(ChatColor.AQUA) .withKeyColor(ChatColor.AQUA)
.withValueColor(ChatColor.WHITE) .withValueColor(ChatColor.WHITE))
) .withSendHandler(new PagedSendHandler()
.withSendHandler( .withHeader(this.getTitle(issuer, world.getCBWorld()))
new PagedSendHandler() .doPagination(true)
.withHeader(this.getTitle(issuer, world.getCBWorld())) .withTargetPage(parsedFlags.flagValue(PAGE_FLAG, 1))
.doPagination(true) .withFilter(parsedFlags.flagValue(FILTER_FLAG, DefaultContentFilter.get())))
.withLinesPerPage(8)
.withTargetPage(page)
)
.send(issuer); .send(issuer);
} }
/** /**
* Gets all the gamerules and their values for a given world * Gets all the gamerules and their values for a given world.
* @param world The world to find gamerules for *
* @param world The world to find gamerules for.
* @return A map of the gamerules and their values * @return A map of the gamerules and their values
*/ */
private Map<String, String> getGameRuleMap(World world) { private Map<String, String> getGameRuleMap(World world) {