Skip to content

Commit

Permalink
chore: Some more docs and code changes
Browse files Browse the repository at this point in the history
  • Loading branch information
phinner committed May 10, 2024
1 parent 9d71f26 commit 2d18ede
Show file tree
Hide file tree
Showing 28 changed files with 299 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ final class CloudCommandFacade<C> extends CommandHandler.Command implements Comm
this.cloudDescription = description;
}

/**
* Returns the real name of the command, without the prefix.
*/
@Override
public String getRealName() {
return this.realName;
Expand All @@ -81,9 +78,6 @@ public DescriptionFacade getDescription() {
return this.manager.descriptionMapper().map(this.cloudDescription);
}

/**
* Returns whether this command is an alias.
*/
@Override
public boolean isAlias() {
return this.alias;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A command manager for Mindustry plugins.
*
* @param <C> the command sender type
*/
public class MindustryCommandManager<C> extends CommandManager<C>
implements PluginAware, SenderMapperHolder<CommandSender, C> {

Expand All @@ -55,6 +60,14 @@ public class MindustryCommandManager<C> extends CommandManager<C>
private DescriptionMapper<Description> descriptionMapper = DescriptionMapper.text(Description::textDescription);
private @Nullable CommandHandler handler = null;

/**
* Constructs a new {@link MindustryCommandManager}.
*
* @param plugin the owning plugin
* @param coordinator the execution coordinator
* @param senderMapper the sender mapper
* @see CommandManager#CommandManager(ExecutionCoordinator, CommandRegistrationHandler)
*/
public MindustryCommandManager(
final MindustryPlugin plugin,
final ExecutionCoordinator<C> coordinator,
Expand Down Expand Up @@ -109,12 +122,17 @@ public final void initialize(final CommandHandler handler) {
}

/**
* Returns the {@code DescriptionMapper} of this command manager.
* Returns the {@link DescriptionMapper} of this command manager.
*/
public final DescriptionMapper<Description> descriptionMapper() {
return this.descriptionMapper;
}

/**
* Sets the {@link DescriptionMapper} of this command manager.
*
* @param descriptionMapper the new description mapper
*/
public final void descriptionMapper(final DescriptionMapper<Description> descriptionMapper) {
this.descriptionMapper = descriptionMapper;
}
Expand All @@ -135,13 +153,16 @@ public final MindustryPlugin getPlugin() {
return this.senderMapper;
}

/**
* Registers the default exception handlers for this command manager.
*/
protected void registerDefaultExceptionHandlers() {
this.registerDefaultExceptionHandlers(
triplet -> {
final var context = triplet.first();
senderMapper()
.reverse(context.sender())
.sendWarning(context.formatCaption(triplet.second(), triplet.third()));
.error(context.formatCaption(triplet.second(), triplet.third()));
},
pair -> logger.error(pair.first(), pair.second()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@
import org.incendo.cloud.parser.ArgumentParser;
import org.incendo.cloud.parser.ParserDescriptor;

/**
* A parser for {@link MappableContent content} arguments.
* Will only succeed on exact matches.
*
* @param <C> the command sender type
* @param <T> the content type
*/
public final class ContentParser<C, T extends MappableContent> implements ArgumentParser<C, T> {

public static <C, T extends MappableContent> ParserDescriptor<C, T> contentParser(
Expand Down Expand Up @@ -60,11 +67,21 @@ public ArgumentParseResult<T> parse(final CommandContext<C> ctx, final CommandIn
: ArgumentParseResult.success((T) content);
}

/**
* An exception thrown when a content argument could not be parsed.
*/
public static final class ContentParseException extends ParserException {

private final String input;
private final ContentTypeKey<?> contentType;

/**
* Creates a new {@link ContentParseException}.
*
* @param input the input string
* @param ctx the command context
* @param contentType the content type
*/
public ContentParseException(
final CommandContext<?> ctx, final String input, final ContentTypeKey<?> contentType) {
super(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package com.xpdustry.distributor.api.command.cloud.parser;

import arc.Core;
import com.xpdustry.distributor.api.Distributor;
import com.xpdustry.distributor.api.DistributorProvider;
import com.xpdustry.distributor.api.collection.MindustryCollections;
import com.xpdustry.distributor.api.command.cloud.MindustryCaptionKeys;
Expand All @@ -41,6 +42,12 @@
import org.incendo.cloud.suggestion.Suggestion;
import org.incendo.cloud.suggestion.SuggestionProvider;

/**
* A parser for {@link Player} arguments.
* Uses {@link Distributor#getPlayerLookup()} to find players.
*
* @param <C> the command sender type
*/
public final class PlayerParser<C> implements ArgumentParser<C, Player> {

public static <C> ParserDescriptor<C, Player> playerParser() {
Expand All @@ -63,9 +70,9 @@ public ArgumentParseResult<Player> parse(final CommandContext<C> ctx, final Comm
final var query = queryBuilder.setFields(fields).build();
final var players = DistributorProvider.get().getPlayerLookup().findOnlinePlayers(query);
if (players.isEmpty()) {
return ArgumentParseResult.failure(new PlayerParseException.PlayerNotFound(query.getInput(), ctx));
return ArgumentParseResult.failure(new PlayerParseException.PlayerNotFound(ctx, query.getInput()));
} else if (players.size() > 1) {
return ArgumentParseResult.failure(new PlayerParseException.TooManyPlayers(query.getInput(), ctx));
return ArgumentParseResult.failure(new PlayerParseException.TooManyPlayers(ctx, query.getInput()));
} else {
return ArgumentParseResult.success(players.iterator().next());
}
Expand Down Expand Up @@ -97,11 +104,11 @@ public static sealed class PlayerParseException extends ParserException {
/**
* Creates a new {@link PlayerParseException}.
*
* @param input the input string
* @param ctx the command context
* @param input the input string
* @param caption the error caption of this exception
*/
public PlayerParseException(final String input, final CommandContext<?> ctx, final Caption caption) {
public PlayerParseException(final CommandContext<?> ctx, final String input, final Caption caption) {
super(PlayerParser.class, ctx, caption, CaptionVariable.of("input", input));
this.input = input;
}
Expand All @@ -121,11 +128,11 @@ public static final class TooManyPlayers extends PlayerParseException {
/**
* Creates a new {@link TooManyPlayers}.
*
* @param input the input string
* @param ctx the command context
* @param input the input string
*/
public TooManyPlayers(final String input, final CommandContext<?> ctx) {
super(input, ctx, MindustryCaptionKeys.ARGUMENT_PARSE_FAILURE_PLAYER_TOO_MANY);
public TooManyPlayers(final CommandContext<?> ctx, final String input) {
super(ctx, input, MindustryCaptionKeys.ARGUMENT_PARSE_FAILURE_PLAYER_TOO_MANY);
}
}

Expand All @@ -137,11 +144,11 @@ public static final class PlayerNotFound extends PlayerParseException {
/**
* Creates a new {@link PlayerNotFound}.
*
* @param input the input string
* @param ctx the command context
* @param input the input string
*/
public PlayerNotFound(final String input, final CommandContext<?> ctx) {
super(input, ctx, MindustryCaptionKeys.ARGUMENT_PARSE_FAILURE_PLAYER_NOT_FOUND);
public PlayerNotFound(final CommandContext<?> ctx, final String input) {
super(ctx, input, MindustryCaptionKeys.ARGUMENT_PARSE_FAILURE_PLAYER_NOT_FOUND);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
import org.incendo.cloud.parser.ParserDescriptor;
import org.incendo.cloud.suggestion.SuggestionProvider;

/**
* A parser for {@link Team} arguments.
*
* @param <C> the command sender type
*/
public final class TeamParser<C> implements ArgumentParser<C, Team> {

public static <C> ParserDescriptor<C, Team> teamParser() {
Expand Down Expand Up @@ -66,7 +71,7 @@ public ArgumentParseResult<Team> parse(final CommandContext<C> ctx, final Comman
if (this.getTeamIndex().containsKey(name)) {
return ArgumentParseResult.success(this.getTeamIndex().get(name));
} else {
return ArgumentParseResult.failure(new TeamParseException(name, ctx, this.teamMode));
return ArgumentParseResult.failure(new TeamParseException(ctx, name, this.teamMode));
}
}

Expand Down Expand Up @@ -114,11 +119,11 @@ public static final class TeamParseException extends ParserException {
/**
* Creates a new {@link TeamParseException}.
*
* @param input the input string
* @param ctx the command context
* @param input the input string
* @param teamMode the team mode
*/
public TeamParseException(final String input, final CommandContext<?> ctx, final TeamMode teamMode) {
public TeamParseException(final CommandContext<?> ctx, final String input, final TeamMode teamMode) {
super(
TeamParser.class,
ctx,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
*/
package com.xpdustry.distributor.api.command;

import com.xpdustry.distributor.api.permission.PermissionHolder;
import com.xpdustry.distributor.api.translation.LocaleHolder;
import com.xpdustry.distributor.api.permission.TriState;
import java.util.Locale;
import mindustry.gen.Player;

/**
* Represents an entity that can send commands.
*/
public interface CommandSender extends PermissionHolder, LocaleHolder {
public interface CommandSender {

/**
* Wraps a player into a command sender.
Expand Down Expand Up @@ -54,14 +54,14 @@ static CommandSender server() {
*
* @param text the message to send
*/
void sendMessage(final String text);
void reply(final String text);

/**
* Sends a warning message to the sender.
* Sends a error message to the sender.
*
* @param text the message to send
*/
void sendWarning(final String text);
void error(final String text);

/**
* Returns whether this sender is a player.
Expand All @@ -80,4 +80,17 @@ static CommandSender server() {
* @throws UnsupportedOperationException if this sender is not a player
*/
Player getPlayer();

/**
* Returns the permission state of the given permission.
*
* @param permission the permission to check
* @return the permission state
*/
TriState getPermission(final String permission);

/**
* Returns the locale of this command sender.
*/
Locale getLocale();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package com.xpdustry.distributor.api.command;

import com.xpdustry.distributor.api.translation.LocaleHolder;
import com.xpdustry.distributor.internal.annotation.DistributorDataClass;
import java.util.Locale;
import org.immutables.value.Value;
Expand Down Expand Up @@ -62,10 +61,10 @@ static DescriptionFacade translated(final String key, final Locale defaultLocale
/**
* Returns the description text for the given locale holder.
*
* @param holder the locale holder
* @param sender the command sender
* @return the description text
*/
default String getText(final LocaleHolder holder) {
default String getText(final CommandSender sender) {
return this.getText();
}

Expand All @@ -79,10 +78,10 @@ default boolean isEmpty() {
/**
* Returns whether this description is empty for the given locale holder.
*
* @param holder the locale holder
* @param sender the command sender
* @return whether this description is empty
*/
default boolean isEmpty(final LocaleHolder holder) {
return this.getText(holder).isEmpty();
default boolean isEmpty(final CommandSender sender) {
return this.getText(sender).isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ record PlayerCommandSender(Player player) implements CommandSender {

@Override
public String getName() {
return this.player.name();
return this.player.coloredName();
}

@Override
public void sendMessage(final String text) {
public void reply(final String text) {
this.player.sendMessage(text);
}

@Override
public void sendWarning(final String text) {
public void error(final String text) {
this.player.sendMessage("[red]" + text);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ public String getName() {
}

@Override
public void sendMessage(final String text) {
public void reply(final String text) {
for (final var line : text.split("\n", -1)) {
Log.info(line);
}
}

@Override
public void sendWarning(final String text) {
public void error(final String text) {
for (final var line : text.split("\n", -1)) {
Log.warn(line);
}
Expand All @@ -60,7 +60,7 @@ public boolean isServer() {

@Override
public Player getPlayer() {
throw new UnsupportedOperationException();
throw new UnsupportedOperationException("Cannot get player from server command sender");
}

@Override
Expand Down
Loading

0 comments on commit 2d18ede

Please sign in to comment.