Skip to content

Commit

Permalink
feat: Finish base argument parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
phinner committed Feb 29, 2024
1 parent d417c68 commit 24a5036
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -57,13 +59,15 @@ public ArcCommandManager(

this.registerDefaultExceptionHandlers();

/* TODO Set Distributor caption registry
this.captionRegistry().registerProvider((caption, sender) -> {
final var source = DistributorProvider.get().getGlobalLocalizationSource();
final var locale =
this.getBackwardsCommandSenderMapper().apply(sender).getLocale();
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();
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/
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<TeamParser.TeamMode> TEAM_MODE =
new ParserParameter<>("team_mode", TypeToken.get(TeamParser.TeamMode.class));

private ArcParserParameters() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<C> implements ArgumentParser.FutureArgumentParser<C, Administration.PlayerInfo> {

public static <C> ParserDescriptor<C, Administration.PlayerInfo> playerInfoParser() {
return ParserDescriptor.of(new PlayerInfoParser<>(), Administration.PlayerInfo.class);
}

public static <C> CommandComponent.Builder<C, Administration.PlayerInfo> playerInfoComponent() {
return CommandComponent.<C, Administration.PlayerInfo>builder().parser(playerInfoParser());
}

@Override
public CompletableFuture<ArgumentParseResult<Administration.PlayerInfo>> parseFuture(
final CommandContext<C> ctx, final CommandInput input) {
Expand All @@ -49,4 +66,14 @@ public CompletableFuture<ArgumentParseResult<Administration.PlayerInfo>> parseFu
}
});
}

@Override
public @NonNull SuggestionProvider<C> suggestionProvider() {
return (ctx, input) -> CompletableFuture.supplyAsync(
() -> ArcCollections.immutableList(Groups.player).stream()
.map(Player::plainName)
.map(Suggestion::simple)
.toList(),
Core.app::post);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<C> implements ArgumentParser<C, Player> {

public static <C> ParserDescriptor<C, Player> playerParser() {
return ParserDescriptor.of(new PlayerParser<>(), Player.class);
}

public static <C> CommandComponent.Builder<C, Player> playerComponent() {
return CommandComponent.<C, Player>builder().parser(playerParser());
}

@Override
public ArgumentParseResult<Player> parse(final CommandContext<C> ctx, final CommandInput input) {
final var query = input.readString();
Expand All @@ -52,4 +66,14 @@ public ArgumentParseResult<Player> parse(final CommandContext<C> ctx, final Comm
final CommandContext<C> ctx, final CommandInput input) {
return CompletableFuture.supplyAsync(() -> this.parse(ctx, input), Core.app::post);
}

@Override
public @NonNull SuggestionProvider<C> suggestionProvider() {
return (ctx, input) -> CompletableFuture.supplyAsync(
() -> ArcCollections.immutableList(Groups.player).stream()
.map(Player::plainName)
.map(Suggestion::simple)
.toList(),
Core.app::post);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<C> implements ArgumentParser<C, Team> {
Expand All @@ -42,6 +44,18 @@ public final class TeamParser<C> implements ArgumentParser<C, Team> {
private static final Map<String, Team> ALL_TEAMS = Arrays.stream(Team.all)
.collect(Collectors.toUnmodifiableMap(t -> t.name.toLowerCase(Locale.ROOT), Function.identity()));

public static <C> ParserDescriptor<C, Team> teamParser() {
return teamParser(TeamMode.BASE);
}

public static <C> @NonNull ParserDescriptor<C, Team> teamParser(final TeamMode teamMode) {
return ParserDescriptor.of(new TeamParser<>(teamMode), Team.class);
}

public static <C> CommandComponent.Builder<C, Team> teamComponent() {
return CommandComponent.<C, Team>builder().parser(teamParser());
}

private final TeamMode teamMode;

public TeamParser(final TeamMode teamMode) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/
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 {}

0 comments on commit 24a5036

Please sign in to comment.