From e0c05f226d2b9681166ca97ab94a975d2e4eac76 Mon Sep 17 00:00:00 2001 From: phinner <62483793+Phinner@users.noreply.github.com> Date: Thu, 7 Mar 2024 10:15:26 +0100 Subject: [PATCH] feat(command-cloud): Added content type parsers --- .../command/cloud/ArcCaptionKeys.java | 5 ++ .../command/cloud/ArcCommandManager.java | 16 +++- .../cloud/parser/PlayerParseException.java | 3 + .../command/cloud/parser/TeamParser.java | 1 + .../cloud/parser/content/BlockParser.java | 39 +++++++++ .../cloud/parser/content/ContentParser.java | 82 +++++++++++++++++++ .../cloud/parser/content/ItemParser.java | 39 +++++++++ .../cloud/parser/content/LiquidParser.java | 39 +++++++++ .../cloud/parser/content/PlanetParser.java | 39 +++++++++ .../cloud/parser/content/StatusParser.java | 39 +++++++++ .../cloud/parser/content/UnitParser.java | 39 +++++++++ .../cloud/parser/content/WeatherParser.java | 39 +++++++++ .../cloud/parser/content/package-info.java | 4 + 13 files changed, 383 insertions(+), 1 deletion(-) create mode 100644 distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/content/BlockParser.java create mode 100644 distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/content/ContentParser.java create mode 100644 distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/content/ItemParser.java create mode 100644 distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/content/LiquidParser.java create mode 100644 distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/content/PlanetParser.java create mode 100644 distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/content/StatusParser.java create mode 100644 distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/content/UnitParser.java create mode 100644 distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/content/WeatherParser.java create mode 100644 distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/content/package-info.java diff --git a/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/ArcCaptionKeys.java b/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/ArcCaptionKeys.java index 70a9897f..10d196b3 100644 --- a/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/ArcCaptionKeys.java +++ b/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/ArcCaptionKeys.java @@ -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) { 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 b384ffbe..65a7d59d 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 @@ -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; @@ -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( diff --git a/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/PlayerParseException.java b/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/PlayerParseException.java index 8b1e9504..df5322e8 100644 --- a/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/PlayerParseException.java +++ b/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/PlayerParseException.java @@ -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; @@ -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 { /** @@ -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 { /** 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 e99de192..53d1e6d1 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 @@ -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; diff --git a/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/content/BlockParser.java b/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/content/BlockParser.java new file mode 100644 index 00000000..50784017 --- /dev/null +++ b/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/content/BlockParser.java @@ -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 . + */ +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 extends ContentParser { + + public static ParserDescriptor blockParser() { + return ParserDescriptor.of(new BlockParser<>(), Block.class); + } + + public static CommandComponent.Builder blockComponent() { + return CommandComponent.builder().parser(blockParser()); + } + + public BlockParser() { + super(ContentType.block); + } +} diff --git a/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/content/ContentParser.java b/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/content/ContentParser.java new file mode 100644 index 00000000..995d6d3a --- /dev/null +++ b/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/content/ContentParser.java @@ -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 . + */ +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 implements ArgumentParser + permits BlockParser, ItemParser, LiquidParser, PlanetParser, StatusParser, UnitParser, WeatherParser { + + private final ContentType contentType; + + ContentParser(final ContentType contentType) { + this.contentType = contentType; + } + + @SuppressWarnings("unchecked") + @Override + public ArgumentParseResult parse(final CommandContext 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 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; + } + } +} diff --git a/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/content/ItemParser.java b/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/content/ItemParser.java new file mode 100644 index 00000000..6cd0ca1b --- /dev/null +++ b/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/content/ItemParser.java @@ -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 . + */ +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 extends ContentParser { + + public static ParserDescriptor itemParser() { + return ParserDescriptor.of(new ItemParser<>(), Item.class); + } + + public static CommandComponent.Builder itemComponent() { + return CommandComponent.builder().parser(itemParser()); + } + + public ItemParser() { + super(ContentType.item); + } +} diff --git a/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/content/LiquidParser.java b/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/content/LiquidParser.java new file mode 100644 index 00000000..d0c185aa --- /dev/null +++ b/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/content/LiquidParser.java @@ -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 . + */ +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 extends ContentParser { + + public static ParserDescriptor liquidParser() { + return ParserDescriptor.of(new LiquidParser<>(), Liquid.class); + } + + public static CommandComponent.Builder liquidComponent() { + return CommandComponent.builder().parser(liquidParser()); + } + + public LiquidParser() { + super(ContentType.liquid); + } +} diff --git a/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/content/PlanetParser.java b/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/content/PlanetParser.java new file mode 100644 index 00000000..ec984373 --- /dev/null +++ b/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/content/PlanetParser.java @@ -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 . + */ +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 extends ContentParser { + + public static ParserDescriptor planetParser() { + return ParserDescriptor.of(new PlanetParser<>(), Planet.class); + } + + public static CommandComponent.Builder planetComponent() { + return CommandComponent.builder().parser(planetParser()); + } + + public PlanetParser() { + super(ContentType.planet); + } +} diff --git a/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/content/StatusParser.java b/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/content/StatusParser.java new file mode 100644 index 00000000..998005bc --- /dev/null +++ b/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/content/StatusParser.java @@ -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 . + */ +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 extends ContentParser { + + public static ParserDescriptor statusParser() { + return ParserDescriptor.of(new StatusParser<>(), StatusEffect.class); + } + + public static CommandComponent.Builder statusComponent() { + return CommandComponent.builder().parser(statusParser()); + } + + public StatusParser() { + super(ContentType.status); + } +} diff --git a/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/content/UnitParser.java b/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/content/UnitParser.java new file mode 100644 index 00000000..d0b136e6 --- /dev/null +++ b/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/content/UnitParser.java @@ -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 . + */ +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 extends ContentParser { + + public static ParserDescriptor unitParser() { + return ParserDescriptor.of(new UnitParser<>(), UnitType.class); + } + + public static CommandComponent.Builder unitComponent() { + return CommandComponent.builder().parser(unitParser()); + } + + public UnitParser() { + super(ContentType.unit); + } +} diff --git a/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/content/WeatherParser.java b/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/content/WeatherParser.java new file mode 100644 index 00000000..66054d83 --- /dev/null +++ b/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/content/WeatherParser.java @@ -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 . + */ +package com.xpdustry.distributor.command.cloud.parser.content; + +import mindustry.ctype.ContentType; +import mindustry.type.Weather; +import org.incendo.cloud.component.CommandComponent; +import org.incendo.cloud.parser.ParserDescriptor; + +public final class WeatherParser extends ContentParser { + + public static ParserDescriptor weatherParser() { + return ParserDescriptor.of(new WeatherParser<>(), Weather.class); + } + + public static CommandComponent.Builder weatherComponent() { + return CommandComponent.builder().parser(weatherParser()); + } + + public WeatherParser() { + super(ContentType.weather); + } +} diff --git a/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/content/package-info.java b/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/content/package-info.java new file mode 100644 index 00000000..939b01a8 --- /dev/null +++ b/distributor-command-cloud/src/main/java/com/xpdustry/distributor/command/cloud/parser/content/package-info.java @@ -0,0 +1,4 @@ +@NullMarked +package com.xpdustry.distributor.command.cloud.parser.content; + +import org.jspecify.annotations.NullMarked;