From 1440e81ea40ec4a74bd10081ea0d4203782db84a Mon Sep 17 00:00:00 2001 From: Brady Date: Fri, 4 Oct 2019 18:12:36 -0500 Subject: [PATCH] IArgParser API --- .../java/baritone/api/IBaritoneProvider.java | 12 +++- .../api/utils/command/ICommandSystem.java | 29 +++++++++ .../utils/command/argparser/IArgParser.java | 4 -- .../command/argparser/IArgParserManager.java | 65 +++++++++++++++++++ src/main/java/baritone/BaritoneProvider.java | 7 ++ .../baritone/utils/command/CommandSystem.java | 35 ++++++++++ .../command/argparser/ArgParserManager.java | 64 +++++++----------- .../command/argparser/DefaultArgParsers.java | 3 +- .../command/argument/CommandArgument.java | 7 +- 9 files changed, 177 insertions(+), 49 deletions(-) create mode 100644 src/api/java/baritone/api/utils/command/ICommandSystem.java create mode 100644 src/api/java/baritone/api/utils/command/argparser/IArgParserManager.java create mode 100644 src/main/java/baritone/utils/command/CommandSystem.java rename src/{api/java/baritone/api => main/java/baritone}/utils/command/argparser/ArgParserManager.java (53%) rename src/{api/java/baritone/api => main/java/baritone}/utils/command/argparser/DefaultArgParsers.java (97%) diff --git a/src/api/java/baritone/api/IBaritoneProvider.java b/src/api/java/baritone/api/IBaritoneProvider.java index c5c6cf39..bdd396d4 100644 --- a/src/api/java/baritone/api/IBaritoneProvider.java +++ b/src/api/java/baritone/api/IBaritoneProvider.java @@ -18,13 +18,15 @@ package baritone.api; import baritone.api.cache.IWorldScanner; +import baritone.api.utils.command.Command; +import baritone.api.utils.command.ICommandSystem; import net.minecraft.client.entity.EntityPlayerSP; import java.util.List; import java.util.Objects; /** - * Provides the present {@link IBaritone} instances + * Provides the present {@link IBaritone} instances, as well as non-baritone instance related APIs. * * @author leijurv */ @@ -72,4 +74,12 @@ public interface IBaritoneProvider { * @return The {@link IWorldScanner} instance. */ IWorldScanner getWorldScanner(); + + /** + * Returns the {@link ICommandSystem} instance. This is not bound to a specific {@link IBaritone} + * instance because {@link ICommandSystem} itself controls global behavior for {@link Command}s. + * + * @return The {@link ICommandSystem} instance. + */ + ICommandSystem getCommandSystem(); } diff --git a/src/api/java/baritone/api/utils/command/ICommandSystem.java b/src/api/java/baritone/api/utils/command/ICommandSystem.java new file mode 100644 index 00000000..22055b57 --- /dev/null +++ b/src/api/java/baritone/api/utils/command/ICommandSystem.java @@ -0,0 +1,29 @@ +/* + * This file is part of Baritone. + * + * Baritone is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Baritone 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Baritone. If not, see . + */ + +package baritone.api.utils.command; + +import baritone.api.utils.command.argparser.IArgParserManager; + +/** + * @author Brady + * @since 10/4/2019 + */ +public interface ICommandSystem { + + IArgParserManager getParserManager(); +} diff --git a/src/api/java/baritone/api/utils/command/argparser/IArgParser.java b/src/api/java/baritone/api/utils/command/argparser/IArgParser.java index c1f615f6..cf87258b 100644 --- a/src/api/java/baritone/api/utils/command/argparser/IArgParser.java +++ b/src/api/java/baritone/api/utils/command/argparser/IArgParser.java @@ -28,8 +28,6 @@ public interface IArgParser { /** * A stateless argument parser is just that. It takes a {@link ICommandArgument} and outputs its type. - * - * @see ArgParserManager#REGISTRY */ interface Stateless extends IArgParser { @@ -45,8 +43,6 @@ public interface IArgParser { /** * A stated argument parser is similar to a stateless one. It also takes a {@link ICommandArgument}, but it also * takes a second argument that can be any type, referred to as the state. - * - * @see ArgParserManager#REGISTRY */ interface Stated extends IArgParser { diff --git a/src/api/java/baritone/api/utils/command/argparser/IArgParserManager.java b/src/api/java/baritone/api/utils/command/argparser/IArgParserManager.java new file mode 100644 index 00000000..e714d30e --- /dev/null +++ b/src/api/java/baritone/api/utils/command/argparser/IArgParserManager.java @@ -0,0 +1,65 @@ +/* + * This file is part of Baritone. + * + * Baritone is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Baritone 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Baritone. If not, see . + */ + +package baritone.api.utils.command.argparser; + +import baritone.api.utils.command.argument.ICommandArgument; +import baritone.api.utils.command.exception.CommandInvalidTypeException; +import baritone.api.utils.command.registry.Registry; + +/** + * @author Brady + * @since 10/4/2019 + */ +public interface IArgParserManager { + + /** + * @param type The type trying to be parsed + * @return A parser that can parse arguments into this class, if found. + */ + IArgParser.Stateless getParserStateless(Class type); + + /** + * @param type The type trying to be parsed + * @return A parser that can parse arguments into this class, if found. + */ + IArgParser.Stated getParserStated(Class type, Class stateKlass); + + /** + * Attempt to parse the specified argument with a stateless {@link IArgParser} that outputs the specified class. + * + * @param type The type to try and parse the argument into. + * @param arg The argument to parse. + * @return An instance of the specified class. + * @throws CommandInvalidTypeException If the parsing failed + */ + T parseStateless(Class type, ICommandArgument arg) throws CommandInvalidTypeException; + + /** + * Attempt to parse the specified argument with a stated {@link IArgParser} that outputs the specified class. + * + * @param type The type to try and parse the argument into. + * @param arg The argument to parse. + * @param state The state to pass to the {@link IArgParser.Stated}. + * @return An instance of the specified class. + * @throws CommandInvalidTypeException If the parsing failed + * @see IArgParser.Stated + */ + T parseStated(Class type, Class stateKlass, ICommandArgument arg, S state) throws CommandInvalidTypeException; + + Registry getRegistry(); +} diff --git a/src/main/java/baritone/BaritoneProvider.java b/src/main/java/baritone/BaritoneProvider.java index 416f1cae..3f6f8283 100644 --- a/src/main/java/baritone/BaritoneProvider.java +++ b/src/main/java/baritone/BaritoneProvider.java @@ -20,8 +20,10 @@ package baritone; import baritone.api.IBaritone; import baritone.api.IBaritoneProvider; import baritone.api.cache.IWorldScanner; +import baritone.api.utils.command.ICommandSystem; import baritone.utils.command.BaritoneChatControl; import baritone.cache.WorldScanner; +import baritone.utils.command.CommandSystem; import java.util.Collections; import java.util.List; @@ -57,4 +59,9 @@ public final class BaritoneProvider implements IBaritoneProvider { public IWorldScanner getWorldScanner() { return WorldScanner.INSTANCE; } + + @Override + public ICommandSystem getCommandSystem() { + return CommandSystem.INSTANCE; + } } diff --git a/src/main/java/baritone/utils/command/CommandSystem.java b/src/main/java/baritone/utils/command/CommandSystem.java new file mode 100644 index 00000000..56d977a2 --- /dev/null +++ b/src/main/java/baritone/utils/command/CommandSystem.java @@ -0,0 +1,35 @@ +/* + * This file is part of Baritone. + * + * Baritone is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Baritone 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Baritone. If not, see . + */ + +package baritone.utils.command; + +import baritone.api.utils.command.ICommandSystem; +import baritone.utils.command.argparser.ArgParserManager; +import baritone.api.utils.command.argparser.IArgParserManager; + +/** + * @author Brady + * @since 10/4/2019 + */ +public enum CommandSystem implements ICommandSystem { + INSTANCE; + + @Override + public IArgParserManager getParserManager() { + return ArgParserManager.INSTANCE; + } +} diff --git a/src/api/java/baritone/api/utils/command/argparser/ArgParserManager.java b/src/main/java/baritone/utils/command/argparser/ArgParserManager.java similarity index 53% rename from src/api/java/baritone/api/utils/command/argparser/ArgParserManager.java rename to src/main/java/baritone/utils/command/argparser/ArgParserManager.java index 2b864f42..75c16cd7 100644 --- a/src/api/java/baritone/api/utils/command/argparser/ArgParserManager.java +++ b/src/main/java/baritone/utils/command/argparser/ArgParserManager.java @@ -15,28 +15,28 @@ * along with Baritone. If not, see . */ -package baritone.api.utils.command.argparser; +package baritone.utils.command.argparser; +import baritone.api.utils.command.argparser.IArgParser; +import baritone.api.utils.command.argparser.IArgParserManager; import baritone.api.utils.command.argument.ICommandArgument; import baritone.api.utils.command.exception.CommandInvalidTypeException; import baritone.api.utils.command.exception.CommandNoParserForTypeException; import baritone.api.utils.command.registry.Registry; -public class ArgParserManager { +public enum ArgParserManager implements IArgParserManager { + INSTANCE; - public static final Registry REGISTRY = new Registry<>(); + public final Registry registry = new Registry<>(); - static { - DefaultArgParsers.ALL.forEach(REGISTRY::register); + ArgParserManager() { + DefaultArgParsers.ALL.forEach(this.registry::register); } - /** - * @param type The type trying to be parsed - * @return A parser that can parse arguments into this class, if found. - */ - public static IArgParser.Stateless getParserStateless(Class type) { + @Override + public IArgParser.Stateless getParserStateless(Class type) { //noinspection unchecked - return REGISTRY.descendingStream() + return this.registry.descendingStream() .filter(IArgParser.Stateless.class::isInstance) .map(IArgParser.Stateless.class::cast) .filter(parser -> parser.getTarget().isAssignableFrom(type)) @@ -44,13 +44,10 @@ public class ArgParserManager { .orElse(null); } - /** - * @param type The type trying to be parsed - * @return A parser that can parse arguments into this class, if found. - */ - public static IArgParser.Stated getParserStated(Class type, Class stateKlass) { + @Override + public IArgParser.Stated getParserStated(Class type, Class stateKlass) { //noinspection unchecked - return REGISTRY.descendingStream() + return this.registry.descendingStream() .filter(IArgParser.Stated.class::isInstance) .map(IArgParser.Stated.class::cast) .filter(parser -> parser.getTarget().isAssignableFrom(type)) @@ -60,16 +57,9 @@ public class ArgParserManager { .orElse(null); } - /** - * Attempt to parse the specified argument with a stateless {@link IArgParser} that outputs the specified class. - * - * @param type The type to try and parse the argument into. - * @param arg The argument to parse. - * @return An instance of the specified class. - * @throws CommandInvalidTypeException If the parsing failed - */ - public static T parseStateless(Class type, ICommandArgument arg) throws CommandInvalidTypeException { - IArgParser.Stateless parser = getParserStateless(type); + @Override + public T parseStateless(Class type, ICommandArgument arg) throws CommandInvalidTypeException { + IArgParser.Stateless parser = this.getParserStateless(type); if (parser == null) { throw new CommandNoParserForTypeException(type); } @@ -80,18 +70,9 @@ public class ArgParserManager { } } - /** - * Attempt to parse the specified argument with a stated {@link IArgParser} that outputs the specified class. - * - * @param type The type to try and parse the argument into. - * @param arg The argument to parse. - * @param state The state to pass to the {@link IArgParser.Stated}. - * @return An instance of the specified class. - * @throws CommandInvalidTypeException If the parsing failed - * @see IArgParser.Stated - */ - public static T parseStated(Class type, Class stateKlass, ICommandArgument arg, S state) throws CommandInvalidTypeException { - IArgParser.Stated parser = getParserStated(type, stateKlass); + @Override + public T parseStated(Class type, Class stateKlass, ICommandArgument arg, S state) throws CommandInvalidTypeException { + IArgParser.Stated parser = this.getParserStated(type, stateKlass); if (parser == null) { throw new CommandNoParserForTypeException(type); } @@ -101,4 +82,9 @@ public class ArgParserManager { throw new CommandInvalidTypeException(arg, type.getSimpleName()); } } + + @Override + public Registry getRegistry() { + return this.registry; + } } diff --git a/src/api/java/baritone/api/utils/command/argparser/DefaultArgParsers.java b/src/main/java/baritone/utils/command/argparser/DefaultArgParsers.java similarity index 97% rename from src/api/java/baritone/api/utils/command/argparser/DefaultArgParsers.java rename to src/main/java/baritone/utils/command/argparser/DefaultArgParsers.java index b734f47d..41c95af7 100644 --- a/src/api/java/baritone/api/utils/command/argparser/DefaultArgParsers.java +++ b/src/main/java/baritone/utils/command/argparser/DefaultArgParsers.java @@ -15,8 +15,9 @@ * along with Baritone. If not, see . */ -package baritone.api.utils.command.argparser; +package baritone.utils.command.argparser; +import baritone.api.utils.command.argparser.IArgParser; import baritone.api.utils.command.argument.ICommandArgument; import java.util.Arrays; diff --git a/src/main/java/baritone/utils/command/argument/CommandArgument.java b/src/main/java/baritone/utils/command/argument/CommandArgument.java index 9c374248..d1b7a157 100644 --- a/src/main/java/baritone/utils/command/argument/CommandArgument.java +++ b/src/main/java/baritone/utils/command/argument/CommandArgument.java @@ -17,9 +17,8 @@ package baritone.utils.command.argument; -import baritone.api.utils.command.argparser.ArgParserManager; +import baritone.utils.command.argparser.ArgParserManager; import baritone.api.utils.command.argument.ICommandArgument; -import baritone.api.utils.command.exception.CommandInvalidArgumentException; import baritone.api.utils.command.exception.CommandInvalidTypeException; import java.util.stream.Stream; @@ -66,7 +65,7 @@ class CommandArgument implements ICommandArgument { @Override public T getAs(Class type) throws CommandInvalidTypeException { - return ArgParserManager.parseStateless(type, this); + return ArgParserManager.INSTANCE.parseStateless(type, this); } @Override @@ -82,7 +81,7 @@ class CommandArgument implements ICommandArgument { @SuppressWarnings("UnusedReturnValue") @Override public T getAs(Class type, Class stateType, S state) throws CommandInvalidTypeException { - return ArgParserManager.parseStated(type, stateType, this, state); + return ArgParserManager.INSTANCE.parseStated(type, stateType, this, state); } @Override