Skip to content

Commit

Permalink
Refactor the filter arguments in the info command
Browse files Browse the repository at this point in the history
  • Loading branch information
md5sha256 committed Jun 16, 2024
1 parent 1b1e0c7 commit a33e25d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import me.wiefferink.areashop.adapters.platform.OfflinePlayerHelper;
import me.wiefferink.areashop.commands.util.AreaShopCommandException;
import me.wiefferink.areashop.commands.util.AreashopCommandBean;
import me.wiefferink.areashop.commands.util.ParserWrapper;
import me.wiefferink.areashop.commands.util.RegionGroupParser;
import me.wiefferink.areashop.commands.util.RegionInfoUtil;
import me.wiefferink.areashop.managers.IFileManager;
Expand Down Expand Up @@ -48,7 +49,6 @@ public class InfoCommand extends AreashopCommandBean {
private static final CommandFlag<Integer> FLAG_PAGE = CommandFlag.builder("page")
.withComponent(IntegerParser.integerParser(1))
.build();
private static final CloudKey<String> KEY_FILTER_ARG = CloudKey.of("filterArg", String.class);
private static final CloudKey<RegionStateFilterType> KEY_TYPE = CloudKey.of("type", RegionStateFilterType.class);

private final MessageBridge messageBridge;
Expand All @@ -58,6 +58,7 @@ public class InfoCommand extends AreashopCommandBean {
private final BukkitSchedulerExecutor executor;

private final CommandFlag<RegionGroup> filterGroupFlag;
private final CommandFlag<String> filterArgFlag;

@Inject
public InfoCommand(
Expand All @@ -74,6 +75,9 @@ public InfoCommand(
.withComponent(ParserDescriptor.of(new RegionGroupParser<>(fileManager, "info-noFiltergroup"),
RegionGroup.class))
.build();
this.filterArgFlag = CommandFlag.builder("filterArg")
.withComponent(ParserWrapper.wrap(StringParser.stringParser(), this::suggestFilterArg))
.build();
this.offlinePlayerHelper = offlinePlayerHelper;
this.executor = executor;
}
Expand All @@ -95,8 +99,9 @@ public String getHelpKey(CommandSender target) {
protected @Nonnull Command.Builder<? extends CommandSender> configureCommand(@Nonnull Command.Builder<CommandSender> builder) {
return builder.literal("info")
.required(KEY_TYPE, EnumParser.enumParser(RegionStateFilterType.class))
.optional(KEY_FILTER_ARG, StringParser.stringParser(), this::suggestFilterArg)
.flag(FLAG_PAGE)
.flag(this.filterGroupFlag)
.flag(filterArgFlag)
.handler(this::handleCommand);
}

Expand Down Expand Up @@ -183,7 +188,7 @@ private void processOtherFilters(@Nonnull CommandContext<CommandSender> context,

private void processWithPlayerFilter(@Nonnull CommandContext<CommandSender> context) {
CommandSender sender = context.sender();
Optional<String> optionalArg = context.optional(KEY_FILTER_ARG);
Optional<String> optionalArg = context.flags().getValue(filterArgFlag);
if (optionalArg.isEmpty()) {
throw new AreaShopCommandException("info-playerHelp");
}
Expand All @@ -202,7 +207,7 @@ private void processWithPlayerFilter(@Nonnull CommandContext<CommandSender> cont
private void processWithRegionFilter(@Nonnull CommandContext<CommandSender> context) {
CommandSender sender = context.sender();
// Region info
Optional<String> optionalArg = context.optional(KEY_FILTER_ARG);
Optional<String> optionalArg = context.flags().getValue(filterArgFlag);
GeneralRegion region;
if (optionalArg.isPresent()) {
region = this.fileManager.getRegion(optionalArg.get());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package me.wiefferink.areashop.commands.util;

import org.checkerframework.checker.nullness.qual.NonNull;
import org.incendo.cloud.context.CommandContext;
import org.incendo.cloud.context.CommandInput;
import org.incendo.cloud.parser.ArgumentParseResult;
import org.incendo.cloud.parser.ArgumentParser;
import org.incendo.cloud.parser.ParserDescriptor;
import org.incendo.cloud.suggestion.SuggestionProvider;
import org.jetbrains.annotations.NotNull;

import javax.annotation.Nonnull;

public class ParserWrapper<C, T> implements ArgumentParser<C, T> {

private final SuggestionProvider<C> suggestionProvider;
private final ParserDescriptor<C, T> parser;

public static <C, T> ParserDescriptor<C, T> wrap(@Nonnull ParserDescriptor<C, T> parser, @Nonnull SuggestionProvider<C> suggestionProvider) {
ArgumentParser<C, T> wrapped = new ParserWrapper<>(parser, suggestionProvider);
return ParserDescriptor.of(wrapped, parser.valueType());
}

public ParserWrapper(@Nonnull ParserDescriptor<C, T> parser, @Nonnull SuggestionProvider<C> suggestionProvider) {
this.parser = parser;
this.suggestionProvider = suggestionProvider;
}

@Override
public @NonNull ArgumentParseResult<@NonNull T> parse(@NonNull CommandContext<@NonNull C> commandContext,
@NonNull CommandInput commandInput) {
return this.parser.parser().parse(commandContext, commandInput);
}

@NotNull
@Override
public SuggestionProvider<C> suggestionProvider() {
return this.suggestionProvider;
}
}

0 comments on commit a33e25d

Please sign in to comment.