diff --git a/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/ArcCommandManager.java b/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/ArcCommandManager.java index 7cfb665c..5b4425da 100644 --- a/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/ArcCommandManager.java +++ b/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/ArcCommandManager.java @@ -18,14 +18,16 @@ */ package com.xpdustry.distributor.command.cloud; +import com.xpdustry.distributor.command.cloud.parser.PlayerInfoParser; +import com.xpdustry.distributor.command.cloud.parser.PlayerParser; +import com.xpdustry.distributor.command.cloud.parser.TeamParser; +import com.xpdustry.distributor.command.cloud.specifier.AllTeams; import com.xpdustry.distributor.core.command.CommandSender; import com.xpdustry.distributor.core.plugin.MindustryPlugin; import com.xpdustry.distributor.core.plugin.PluginAware; import io.leangen.geantyref.TypeToken; import java.text.MessageFormat; import mindustry.game.Team; -import mindustry.gen.Player; -import mindustry.net.Administration; import org.incendo.cloud.CloudCapability; import org.incendo.cloud.CommandManager; import org.incendo.cloud.SenderMapper; @@ -57,6 +59,7 @@ public ArcCommandManager( this.registerDefaultExceptionHandlers(); + /* TODO Set Distributor caption registry this.captionRegistry().registerProvider((caption, sender) -> { final var source = DistributorProvider.get().getGlobalLocalizationSource(); final var locale = @@ -64,6 +67,7 @@ public ArcCommandManager( final var format = source.localize(caption.getKey(), locale); return format != null ? format.toPattern() : "???" + caption.getKey() + "???"; }); + */ this.captionFormatter((key, recipient, caption, variables) -> { final var arguments = variables.toArray(); @@ -75,25 +79,19 @@ public ArcCommandManager( } }); + this.parserRegistry() + .registerParser(PlayerParser.playerParser()) + .registerParser(PlayerInfoParser.playerInfoParser()); + this.parserRegistry() .registerAnnotationMapper( AllTeams.class, (annotation, typeToken) -> - ParserParameters.single(ArcParserParameters.TEAM_MODE, TeamMode.ALL)); - - this.parserRegistry() - .registerParserSupplier(TypeToken.get(Player.class), params -> new PlayerArgument.PlayerParser<>()); - - this.parserRegistry() - .registerParserSupplier( - TypeToken.get(Administration.PlayerInfo.class), - params -> new PlayerInfoArgument.PlayerInfoParser<>()); - - this.parserRegistry() + ParserParameters.single(ArcParserParameters.TEAM_MODE, TeamParser.TeamMode.ALL)) .registerParserSupplier( TypeToken.get(Team.class), - params -> new TeamArgument.TeamParser<>( - params.get(ArcParserParameters.TEAM_MODE, TeamMode.BASE))); + params -> + new TeamParser<>(params.get(ArcParserParameters.TEAM_MODE, TeamParser.TeamMode.BASE))); } @Override diff --git a/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/ArcParserParameters.java b/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/ArcParserParameters.java new file mode 100644 index 00000000..c8c2ab14 --- /dev/null +++ b/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/ArcParserParameters.java @@ -0,0 +1,38 @@ +/* + * Distributor, a feature-rich framework for Mindustry plugins. + * + * Copyright (C) 2024 Xpdustry + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.xpdustry.distributor.command.cloud; + +import com.xpdustry.distributor.command.cloud.parser.TeamParser; +import io.leangen.geantyref.TypeToken; +import org.incendo.cloud.parser.ParserParameter; + +/** + * A collection of {@link ParserParameter} used by Distributor to resolve Mindustry types in the + * {@link org.incendo.cloud.parser.ParserRegistry}. + */ +public final class ArcParserParameters { + + /** + * Whether a {@link mindustry.game.Team} argument should include all the teams or only the base ones. + */ + public static final ParserParameter TEAM_MODE = + new ParserParameter<>("team_mode", TypeToken.get(TeamParser.TeamMode.class)); + + private ArcParserParameters() {} +} diff --git a/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/PlayerInfoParser.java b/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/PlayerInfoParser.java index 031250d3..11a1636e 100644 --- a/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/PlayerInfoParser.java +++ b/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/PlayerInfoParser.java @@ -18,17 +18,34 @@ */ package com.xpdustry.distributor.command.cloud.parser; +import arc.Core; import com.xpdustry.distributor.core.DistributorProvider; +import com.xpdustry.distributor.core.collection.ArcCollections; import com.xpdustry.distributor.core.player.PlayerLookup; import java.util.concurrent.CompletableFuture; +import mindustry.gen.Groups; +import mindustry.gen.Player; import mindustry.net.Administration; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.incendo.cloud.component.CommandComponent; 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.Suggestion; +import org.incendo.cloud.suggestion.SuggestionProvider; public final class PlayerInfoParser implements ArgumentParser.FutureArgumentParser { + public static ParserDescriptor playerInfoParser() { + return ParserDescriptor.of(new PlayerInfoParser<>(), Administration.PlayerInfo.class); + } + + public static CommandComponent.Builder playerInfoComponent() { + return CommandComponent.builder().parser(playerInfoParser()); + } + @Override public CompletableFuture> parseFuture( final CommandContext ctx, final CommandInput input) { @@ -49,4 +66,14 @@ public CompletableFuture> parseFu } }); } + + @Override + public @NonNull SuggestionProvider suggestionProvider() { + return (ctx, input) -> CompletableFuture.supplyAsync( + () -> ArcCollections.immutableList(Groups.player).stream() + .map(Player::plainName) + .map(Suggestion::simple) + .toList(), + Core.app::post); + } } diff --git a/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/PlayerParser.java b/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/PlayerParser.java index 933a6c16..c3431b03 100644 --- a/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/PlayerParser.java +++ b/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/PlayerParser.java @@ -20,17 +20,31 @@ import arc.Core; import com.xpdustry.distributor.core.DistributorProvider; +import com.xpdustry.distributor.core.collection.ArcCollections; import com.xpdustry.distributor.core.player.PlayerLookup; import java.util.concurrent.CompletableFuture; +import mindustry.gen.Groups; import mindustry.gen.Player; import org.checkerframework.checker.nullness.qual.NonNull; +import org.incendo.cloud.component.CommandComponent; 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.Suggestion; +import org.incendo.cloud.suggestion.SuggestionProvider; public final class PlayerParser implements ArgumentParser { + public static ParserDescriptor playerParser() { + return ParserDescriptor.of(new PlayerParser<>(), Player.class); + } + + public static CommandComponent.Builder playerComponent() { + return CommandComponent.builder().parser(playerParser()); + } + @Override public ArgumentParseResult parse(final CommandContext ctx, final CommandInput input) { final var query = input.readString(); @@ -52,4 +66,14 @@ public ArgumentParseResult parse(final CommandContext ctx, final Comm final CommandContext ctx, final CommandInput input) { return CompletableFuture.supplyAsync(() -> this.parse(ctx, input), Core.app::post); } + + @Override + public @NonNull SuggestionProvider suggestionProvider() { + return (ctx, input) -> CompletableFuture.supplyAsync( + () -> ArcCollections.immutableList(Groups.player).stream() + .map(Player::plainName) + .map(Suggestion::simple) + .toList(), + Core.app::post); + } } diff --git a/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/TeamParser.java b/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/TeamParser.java index 72ac2aec..e99de192 100644 --- a/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/TeamParser.java +++ b/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/TeamParser.java @@ -27,11 +27,13 @@ import mindustry.game.Team; import org.checkerframework.checker.nullness.qual.NonNull; import org.incendo.cloud.caption.CaptionVariable; +import org.incendo.cloud.component.CommandComponent; import org.incendo.cloud.context.CommandContext; import org.incendo.cloud.context.CommandInput; import org.incendo.cloud.exception.parsing.ParserException; import org.incendo.cloud.parser.ArgumentParseResult; import org.incendo.cloud.parser.ArgumentParser; +import org.incendo.cloud.parser.ParserDescriptor; import org.incendo.cloud.suggestion.SuggestionProvider; public final class TeamParser implements ArgumentParser { @@ -42,6 +44,18 @@ public final class TeamParser implements ArgumentParser { private static final Map ALL_TEAMS = Arrays.stream(Team.all) .collect(Collectors.toUnmodifiableMap(t -> t.name.toLowerCase(Locale.ROOT), Function.identity())); + public static ParserDescriptor teamParser() { + return teamParser(TeamMode.BASE); + } + + public static @NonNull ParserDescriptor teamParser(final TeamMode teamMode) { + return ParserDescriptor.of(new TeamParser<>(teamMode), Team.class); + } + + public static CommandComponent.Builder teamComponent() { + return CommandComponent.builder().parser(teamParser()); + } + private final TeamMode teamMode; public TeamParser(final TeamMode teamMode) { diff --git a/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/specifier/AllTeams.java b/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/specifier/AllTeams.java new file mode 100644 index 00000000..d5c3c40d --- /dev/null +++ b/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/specifier/AllTeams.java @@ -0,0 +1,34 @@ +/* + * Distributor, a feature-rich framework for Mindustry plugins. + * + * Copyright (C) 2024 Xpdustry + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.xpdustry.distributor.command.cloud.specifier; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Annotation used to specify that a {@link mindustry.game.Team} command argument can represent all the teams instead of the 6 base + * teams. + * + * @see com.xpdustry.distributor.command.cloud.parser.TeamParser.TeamMode#ALL + */ +@Target(ElementType.PARAMETER) +@Retention(RetentionPolicy.RUNTIME) +public @interface AllTeams {}