IArgParser API

This commit is contained in:
Brady
2019-10-04 18:12:36 -05:00
parent bfd8773efa
commit 1440e81ea4
9 changed files with 177 additions and 49 deletions

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

@@ -0,0 +1,90 @@
/*
* 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.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 enum ArgParserManager implements IArgParserManager {
INSTANCE;
public final Registry<IArgParser> registry = new Registry<>();
ArgParserManager() {
DefaultArgParsers.ALL.forEach(this.registry::register);
}
@Override
public <T> IArgParser.Stateless<T> getParserStateless(Class<T> type) {
//noinspection unchecked
return this.registry.descendingStream()
.filter(IArgParser.Stateless.class::isInstance)
.map(IArgParser.Stateless.class::cast)
.filter(parser -> parser.getTarget().isAssignableFrom(type))
.findFirst()
.orElse(null);
}
@Override
public <T, S> IArgParser.Stated<T, S> getParserStated(Class<T> type, Class<S> stateKlass) {
//noinspection unchecked
return this.registry.descendingStream()
.filter(IArgParser.Stated.class::isInstance)
.map(IArgParser.Stated.class::cast)
.filter(parser -> parser.getTarget().isAssignableFrom(type))
.filter(parser -> parser.getStateType().isAssignableFrom(stateKlass))
.map(IArgParser.Stated.class::cast)
.findFirst()
.orElse(null);
}
@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);
}
try {
return parser.parseArg(arg);
} catch (Exception exc) {
throw new CommandInvalidTypeException(arg, type.getSimpleName());
}
}
@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);
}
try {
return parser.parseArg(arg, state);
} catch (Exception exc) {
throw new CommandInvalidTypeException(arg, type.getSimpleName());
}
}
@Override
public Registry<IArgParser> getRegistry() {
return this.registry;
}
}

View File

@@ -0,0 +1,124 @@
/*
* 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.argparser;
import baritone.api.utils.command.argparser.IArgParser;
import baritone.api.utils.command.argument.ICommandArgument;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
public class DefaultArgParsers {
public enum IntArgumentParser implements IArgParser.Stateless<Integer> {
INSTANCE;
@Override
public Class<Integer> getTarget() {
return Integer.class;
}
@Override
public Integer parseArg(ICommandArgument arg) throws RuntimeException {
return Integer.parseInt(arg.getValue());
}
}
public enum LongArgumentParser implements IArgParser.Stateless<Long> {
INSTANCE;
@Override
public Class<Long> getTarget() {
return Long.class;
}
@Override
public Long parseArg(ICommandArgument arg) throws RuntimeException {
return Long.parseLong(arg.getValue());
}
}
public enum FloatArgumentParser implements IArgParser.Stateless<Float> {
INSTANCE;
@Override
public Class<Float> getTarget() {
return Float.class;
}
@Override
public Float parseArg(ICommandArgument arg) throws RuntimeException {
String value = arg.getValue();
if (!value.matches("^([+-]?(?:\\d+(?:\\.\\d*)?|\\.\\d+)|)$")) {
throw new IllegalArgumentException("failed float format check");
}
return Float.parseFloat(value);
}
}
public enum DoubleArgumentParser implements IArgParser.Stateless<Double> {
INSTANCE;
@Override
public Class<Double> getTarget() {
return Double.class;
}
@Override
public Double parseArg(ICommandArgument arg) throws RuntimeException {
String value = arg.getValue();
if (!value.matches("^([+-]?(?:\\d+(?:\\.\\d*)?|\\.\\d+)|)$")) {
throw new IllegalArgumentException("failed double format check");
}
return Double.parseDouble(value);
}
}
public static class BooleanArgumentParser implements IArgParser.Stateless<Boolean> {
public static final BooleanArgumentParser INSTANCE = new BooleanArgumentParser();
public static final List<String> TRUTHY_VALUES = Arrays.asList("1", "true", "yes", "t", "y", "on", "enable");
public static final List<String> FALSY_VALUES = Arrays.asList("0", "false", "no", "f", "n", "off", "disable");
@Override
public Class<Boolean> getTarget() {
return Boolean.class;
}
@Override
public Boolean parseArg(ICommandArgument arg) throws RuntimeException {
String value = arg.getValue();
if (TRUTHY_VALUES.contains(value.toLowerCase(Locale.US))) {
return true;
} else if (FALSY_VALUES.contains(value.toLowerCase(Locale.US))) {
return false;
} else {
throw new IllegalArgumentException("invalid boolean");
}
}
}
public static final List<IArgParser<?>> ALL = Arrays.asList(
IntArgumentParser.INSTANCE,
LongArgumentParser.INSTANCE,
FloatArgumentParser.INSTANCE,
DoubleArgumentParser.INSTANCE,
BooleanArgumentParser.INSTANCE
);
}

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