Skip to content

Commit

Permalink
feat(command-cloud): Added content type parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
phinner committed Mar 7, 2024
1 parent 1932295 commit e0c05f2
Show file tree
Hide file tree
Showing 13 changed files with 383 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public final class ArcCaptionKeys {
*/
public static final Caption ARGUMENT_PARSE_FAILURE_TEAM = of("argument.parse.failure.team");

/**
* Variables: {@code {input}}, {@code {type}}.
*/
public static final Caption ARGUMENT_PARSE_FAILURE_CONTENT = of("argument.parse.failure.content");

private ArcCaptionKeys() {}

private static Caption of(final String key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
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.parser.content.BlockParser;
import com.xpdustry.distributor.command.cloud.parser.content.ItemParser;
import com.xpdustry.distributor.command.cloud.parser.content.LiquidParser;
import com.xpdustry.distributor.command.cloud.parser.content.PlanetParser;
import com.xpdustry.distributor.command.cloud.parser.content.StatusParser;
import com.xpdustry.distributor.command.cloud.parser.content.UnitParser;
import com.xpdustry.distributor.command.cloud.parser.content.WeatherParser;
import com.xpdustry.distributor.command.cloud.specifier.AllTeams;
import com.xpdustry.distributor.core.DistributorProvider;
import com.xpdustry.distributor.core.command.CommandSender;
Expand Down Expand Up @@ -82,7 +89,14 @@ public ArcCommandManager(

this.parserRegistry()
.registerParser(PlayerParser.playerParser())
.registerParser(PlayerInfoParser.playerInfoParser());
.registerParser(PlayerInfoParser.playerInfoParser())
.registerParser(BlockParser.blockParser())
.registerParser(ItemParser.itemParser())
.registerParser(LiquidParser.liquidParser())
.registerParser(PlanetParser.planetParser())
.registerParser(StatusParser.statusParser())
.registerParser(UnitParser.unitParser())
.registerParser(WeatherParser.weatherParser());

this.parserRegistry()
.registerAnnotationMapper(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
/**
* An exception thrown when a parsing error occurs while searching for a player.
*/
@SuppressWarnings("serial")
public sealed class PlayerParseException extends ParserException {

private final String input;
Expand Down Expand Up @@ -54,6 +55,7 @@ public final String getInput() {
/**
* An exception thrown when too many players are found for the given input.
*/
@SuppressWarnings("serial")
public static final class TooManyPlayers extends PlayerParseException {

/**
Expand All @@ -70,6 +72,7 @@ public TooManyPlayers(final Class<?> parser, final String input, final CommandCo
/**
* An exception thrown when no player was found for the given input.
*/
@SuppressWarnings("serial")
public static final class PlayerNotFound extends PlayerParseException {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public enum TeamMode {
/**
* Exception thrown when a team cannot be found for the given input and {@link TeamMode}.
*/
@SuppressWarnings("serial")
public static final class TeamParseException extends ParserException {

private final String input;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.parser.content;

import mindustry.ctype.ContentType;
import mindustry.world.Block;
import org.incendo.cloud.component.CommandComponent;
import org.incendo.cloud.parser.ParserDescriptor;

public final class BlockParser<C> extends ContentParser<C, Block> {

public static <C> ParserDescriptor<C, Block> blockParser() {
return ParserDescriptor.of(new BlockParser<>(), Block.class);
}

public static <C> CommandComponent.Builder<C, Block> blockComponent() {
return CommandComponent.<C, Block>builder().parser(blockParser());
}

public BlockParser() {
super(ContentType.block);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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.parser.content;

import com.xpdustry.distributor.command.cloud.ArcCaptionKeys;
import java.util.Locale;
import mindustry.Vars;
import mindustry.ctype.ContentType;
import mindustry.ctype.MappableContent;
import org.incendo.cloud.caption.CaptionVariable;
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;

public sealed class ContentParser<C, T extends MappableContent> implements ArgumentParser<C, T>
permits BlockParser, ItemParser, LiquidParser, PlanetParser, StatusParser, UnitParser, WeatherParser {

private final ContentType contentType;

ContentParser(final ContentType contentType) {
this.contentType = contentType;
}

@SuppressWarnings("unchecked")
@Override
public ArgumentParseResult<T> parse(final CommandContext<C> ctx, final CommandInput input) {
final var name = input.readString().toLowerCase(Locale.ROOT);
final var content = Vars.content.getByName(contentType, name);
return (content == null)
? ArgumentParseResult.failure(new ContentParseException(this.getClass(), ctx, name, contentType))
: ArgumentParseResult.success((T) content);
}

@SuppressWarnings("serial")
public static final class ContentParseException extends ParserException {

private final String input;
private final ContentType contentType;

@SuppressWarnings("rawtypes")
public ContentParseException(
final Class<? extends ContentParser> clazz,
final CommandContext<?> ctx,
final String input,
final ContentType contentType) {
super(
clazz,
ctx,
ArcCaptionKeys.ARGUMENT_PARSE_FAILURE_CONTENT,
CaptionVariable.of("input", input),
CaptionVariable.of("type", contentType.name()));
this.input = input;
this.contentType = contentType;
}

public String getInput() {
return input;
}

public ContentType getContentType() {
return contentType;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.parser.content;

import mindustry.ctype.ContentType;
import mindustry.type.Item;
import org.incendo.cloud.component.CommandComponent;
import org.incendo.cloud.parser.ParserDescriptor;

public final class ItemParser<C> extends ContentParser<C, Item> {

public static <C> ParserDescriptor<C, Item> itemParser() {
return ParserDescriptor.of(new ItemParser<>(), Item.class);
}

public static <C> CommandComponent.Builder<C, Item> itemComponent() {
return CommandComponent.<C, Item>builder().parser(itemParser());
}

public ItemParser() {
super(ContentType.item);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.parser.content;

import mindustry.ctype.ContentType;
import mindustry.type.Liquid;
import org.incendo.cloud.component.CommandComponent;
import org.incendo.cloud.parser.ParserDescriptor;

public final class LiquidParser<C> extends ContentParser<C, Liquid> {

public static <C> ParserDescriptor<C, Liquid> liquidParser() {
return ParserDescriptor.of(new LiquidParser<>(), Liquid.class);
}

public static <C> CommandComponent.Builder<C, Liquid> liquidComponent() {
return CommandComponent.<C, Liquid>builder().parser(liquidParser());
}

public LiquidParser() {
super(ContentType.liquid);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.parser.content;

import mindustry.ctype.ContentType;
import mindustry.type.Planet;
import org.incendo.cloud.component.CommandComponent;
import org.incendo.cloud.parser.ParserDescriptor;

public final class PlanetParser<C> extends ContentParser<C, Planet> {

public static <C> ParserDescriptor<C, Planet> planetParser() {
return ParserDescriptor.of(new PlanetParser<>(), Planet.class);
}

public static <C> CommandComponent.Builder<C, Planet> planetComponent() {
return CommandComponent.<C, Planet>builder().parser(planetParser());
}

public PlanetParser() {
super(ContentType.planet);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.parser.content;

import mindustry.ctype.ContentType;
import mindustry.type.StatusEffect;
import org.incendo.cloud.component.CommandComponent;
import org.incendo.cloud.parser.ParserDescriptor;

public final class StatusParser<C> extends ContentParser<C, StatusEffect> {

public static <C> ParserDescriptor<C, StatusEffect> statusParser() {
return ParserDescriptor.of(new StatusParser<>(), StatusEffect.class);
}

public static <C> CommandComponent.Builder<C, StatusEffect> statusComponent() {
return CommandComponent.<C, StatusEffect>builder().parser(statusParser());
}

public StatusParser() {
super(ContentType.status);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.parser.content;

import mindustry.ctype.ContentType;
import mindustry.type.UnitType;
import org.incendo.cloud.component.CommandComponent;
import org.incendo.cloud.parser.ParserDescriptor;

public final class UnitParser<C> extends ContentParser<C, UnitType> {

public static <C> ParserDescriptor<C, UnitType> unitParser() {
return ParserDescriptor.of(new UnitParser<>(), UnitType.class);
}

public static <C> CommandComponent.Builder<C, UnitType> unitComponent() {
return CommandComponent.<C, UnitType>builder().parser(unitParser());
}

public UnitParser() {
super(ContentType.unit);
}
}
Loading

0 comments on commit e0c05f2

Please sign in to comment.