IArgParser API

This commit is contained in:
Brady 2019-10-04 18:12:36 -05:00
parent bfd8773efa
commit 1440e81ea4
No known key found for this signature in database
GPG Key ID: 73A788379A197567
9 changed files with 177 additions and 49 deletions

View File

@ -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();
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.command;
import baritone.api.utils.command.argparser.IArgParserManager;
/**
* @author Brady
* @since 10/4/2019
*/
public interface ICommandSystem {
IArgParserManager getParserManager();
}

View File

@ -28,8 +28,6 @@ public interface IArgParser<T> {
/**
* A stateless argument parser is just that. It takes a {@link ICommandArgument} and outputs its type.
*
* @see ArgParserManager#REGISTRY
*/
interface Stateless<T> extends IArgParser<T> {
@ -45,8 +43,6 @@ public interface IArgParser<T> {
/**
* 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<T, S> extends IArgParser<T> {

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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.
*/
<T> IArgParser.Stateless<T> getParserStateless(Class<T> type);
/**
* @param type The type trying to be parsed
* @return A parser that can parse arguments into this class, if found.
*/
<T, S> IArgParser.Stated<T, S> getParserStated(Class<T> type, Class<S> 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> T parseStateless(Class<T> 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, S> T parseStated(Class<T> type, Class<S> stateKlass, ICommandArgument arg, S state) throws CommandInvalidTypeException;
Registry<IArgParser> getRegistry();
}

View File

@ -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;
}
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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;
}
}

View File

@ -15,28 +15,28 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
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<IArgParser> REGISTRY = new Registry<>();
public final Registry<IArgParser> 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 <T> IArgParser.Stateless<T> getParserStateless(Class<T> type) {
@Override
public <T> IArgParser.Stateless<T> getParserStateless(Class<T> 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 <T, S> IArgParser.Stated<T, S> getParserStated(Class<T> type, Class<S> stateKlass) {
@Override
public <T, S> IArgParser.Stated<T, S> getParserStated(Class<T> type, Class<S> 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> T parseStateless(Class<T> type, ICommandArgument arg) throws CommandInvalidTypeException {
IArgParser.Stateless<T> parser = getParserStateless(type);
@Override
public <T> T parseStateless(Class<T> type, ICommandArgument arg) throws CommandInvalidTypeException {
IArgParser.Stateless<T> 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, S> T parseStated(Class<T> type, Class<S> stateKlass, ICommandArgument arg, S state) throws CommandInvalidTypeException {
IArgParser.Stated<T, S> parser = getParserStated(type, stateKlass);
@Override
public <T, S> T parseStated(Class<T> type, Class<S> stateKlass, ICommandArgument arg, S state) throws CommandInvalidTypeException {
IArgParser.Stated<T, S> 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<IArgParser> getRegistry() {
return this.registry;
}
}

View File

@ -15,8 +15,9 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
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;

View File

@ -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> T getAs(Class<T> 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, S> T getAs(Class<T> type, Class<S> stateType, S state) throws CommandInvalidTypeException {
return ArgParserManager.parseStated(type, stateType, this, state);
return ArgParserManager.INSTANCE.parseStated(type, stateType, this, state);
}
@Override