wip (3) command client side command syntax check: exceptions (show cause)

This commit is contained in:
Bixilon 2020-12-10 20:31:23 +01:00
parent 4817dd30d5
commit 1c891cb471
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
34 changed files with 687 additions and 92 deletions

View File

@ -159,6 +159,7 @@
<w>strad</w>
<w>straymeow</w>
<w>stutterturn</w>
<w>summonable</w>
<w>suspiciousstew</w>
<w>sweepattack</w>
<w>takepotion</w>

View File

@ -14,6 +14,7 @@
package de.bixilon.minosoft.data.commands;
import de.bixilon.minosoft.data.commands.parser.CommandParser;
import de.bixilon.minosoft.data.commands.parser.exception.CommandParseException;
import de.bixilon.minosoft.data.commands.parser.properties.ParserProperties;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
import de.bixilon.minosoft.util.BitByte;
@ -59,11 +60,9 @@ public class CommandArgumentNode extends CommandLiteralNode {
}
@Override
public boolean isSyntaxCorrect(ImprovedStringReader stringReader) {
if (parser.isParsable(properties, stringReader)) {
return super.isSyntaxCorrect(stringReader);
}
return false;
public void isSyntaxCorrect(ImprovedStringReader stringReader) throws CommandParseException {
parser.isParsable(properties, stringReader);
super.isSyntaxCorrect(stringReader);
}
public enum SuggestionTypes {

View File

@ -14,6 +14,9 @@
package de.bixilon.minosoft.data.commands;
import com.google.errorprone.annotations.DoNotCall;
import de.bixilon.minosoft.data.commands.parser.exception.CommandParseException;
import de.bixilon.minosoft.data.commands.parser.exception.RequiresMoreArgumentsCommandParseException;
import de.bixilon.minosoft.data.commands.parser.exception.WrongArgumentCommandParseException;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition;
import de.bixilon.minosoft.util.BitByte;
@ -74,30 +77,42 @@ public abstract class CommandNode {
return childrenIds;
}
public boolean isSyntaxCorrect(ImprovedStringReader stringReader) {
public void isSyntaxCorrect(ImprovedStringReader stringReader) throws CommandParseException {
String nextArgument = stringReader.getUntilNextCommandArgument();
if (nextArgument.length() == 0) {
return isExecutable;
if (isExecutable) {
return;
}
throw new RequiresMoreArgumentsCommandParseException(stringReader);
}
if (literalChildren.containsKey(nextArgument)) {
stringReader.skip(nextArgument.length() + ProtocolDefinition.COMMAND_SEPARATOR.length());
return literalChildren.get(nextArgument).isSyntaxCorrect(stringReader);
literalChildren.get(nextArgument).isSyntaxCorrect(stringReader);
return;
}
CommandParseException lastException = null;
for (CommandArgumentNode argumentNode : argumentsChildren) {
int currentPosition = stringReader.getPosition();
if (argumentNode.isSyntaxCorrect(stringReader)) {
return true;
try {
argumentNode.isSyntaxCorrect(stringReader);
return;
} catch (CommandParseException e) {
lastException = e;
}
stringReader.setPosition(currentPosition);
}
return false;
if (lastException != null) {
throw lastException;
}
stringReader.skip(nextArgument.length());
throw new WrongArgumentCommandParseException(stringReader, nextArgument);
}
public boolean isSyntaxCorrect(String string) {
public void isSyntaxCorrect(String string) throws CommandParseException {
// replace multiple spaces with nothing
string = string.replaceAll("\\s{2,}", " ");
ImprovedStringReader stringReader = new ImprovedStringReader(string);
return isSyntaxCorrect(stringReader);
isSyntaxCorrect(stringReader);
}
public enum NodeTypes {

View File

@ -13,10 +13,27 @@
package de.bixilon.minosoft.data.commands;
import de.bixilon.minosoft.data.commands.parser.exception.CommandParseException;
import de.bixilon.minosoft.data.commands.parser.exception.UnknownCommandParseException;
import de.bixilon.minosoft.data.commands.parser.exception.WrongArgumentCommandParseException;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
import de.bixilon.minosoft.util.buffers.ImprovedStringReader;
public class CommandRootNode extends CommandNode {
public CommandRootNode(byte flags, InByteBuffer buffer) {
super(flags, buffer);
}
@Override
public void isSyntaxCorrect(ImprovedStringReader stringReader) throws CommandParseException {
try {
super.isSyntaxCorrect(stringReader);
} catch (WrongArgumentCommandParseException e) {
if (e.getStartIndex() == 0) {
// beginn of string
throw new UnknownCommandParseException(stringReader, stringReader.getString().substring(0, e.getEndIndex()));
}
throw e;
}
}
}

View File

@ -13,6 +13,8 @@
package de.bixilon.minosoft.data.commands.parser;
import de.bixilon.minosoft.data.commands.parser.exception.BooleanCommandParseException;
import de.bixilon.minosoft.data.commands.parser.exception.CommandParseException;
import de.bixilon.minosoft.data.commands.parser.properties.ParserProperties;
import de.bixilon.minosoft.util.buffers.ImprovedStringReader;
@ -22,8 +24,10 @@ public class BooleanParser extends CommandParser {
public static final BooleanParser BOOLEAN_PARSER = new BooleanParser();
@Override
public boolean isParsable(@Nullable ParserProperties properties, ImprovedStringReader stringReader) {
public void isParsable(@Nullable ParserProperties properties, ImprovedStringReader stringReader) throws CommandParseException {
String argument = stringReader.readUntilNextCommandArgument();
return argument.equals("true") || argument.equals("false");
if (!argument.equals("true") && !argument.equals("false")) {
throw new BooleanCommandParseException(stringReader, argument);
}
}
}

View File

@ -14,6 +14,7 @@
package de.bixilon.minosoft.data.commands.parser;
import com.google.common.collect.HashBiMap;
import de.bixilon.minosoft.data.commands.parser.exception.CommandParseException;
import de.bixilon.minosoft.data.commands.parser.properties.ParserProperties;
import de.bixilon.minosoft.data.mappings.ModIdentifier;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
@ -45,6 +46,6 @@ public abstract class CommandParser {
return null;
}
public abstract boolean isParsable(@Nullable ParserProperties properties, ImprovedStringReader stringReader);
public abstract void isParsable(@Nullable ParserProperties properties, ImprovedStringReader stringReader) throws CommandParseException;
}

View File

@ -13,6 +13,9 @@
package de.bixilon.minosoft.data.commands.parser;
import de.bixilon.minosoft.data.commands.parser.exception.CommandParseException;
import de.bixilon.minosoft.data.commands.parser.exception.number.DoubleCommandParseException;
import de.bixilon.minosoft.data.commands.parser.exception.number.ValueOutOfRangeCommandParseException;
import de.bixilon.minosoft.data.commands.parser.properties.DoubleParserProperties;
import de.bixilon.minosoft.data.commands.parser.properties.ParserProperties;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
@ -31,12 +34,16 @@ public class DoubleParser extends CommandParser {
}
@Override
public boolean isParsable(ParserProperties properties, ImprovedStringReader stringReader) {
public void isParsable(ParserProperties properties, ImprovedStringReader stringReader) throws CommandParseException {
String argument = stringReader.readUntilNextCommandArgument();
try {
return isValidValue((DoubleParserProperties) properties, Double.parseDouble(argument));
} catch (Exception ignored) {
return false;
double value = Double.parseDouble(argument);
DoubleParserProperties doubleParserProperties = (DoubleParserProperties) properties;
if (value < doubleParserProperties.getMinValue() && value > doubleParserProperties.getMaxValue()) {
throw new ValueOutOfRangeCommandParseException(stringReader, doubleParserProperties.getMinValue(), doubleParserProperties.getMaxValue(), value);
}
} catch (NumberFormatException exception) {
throw new DoubleCommandParseException(stringReader, argument, exception);
}
}
}

View File

@ -13,6 +13,7 @@
package de.bixilon.minosoft.data.commands.parser;
import de.bixilon.minosoft.data.commands.parser.exception.CommandParseException;
import de.bixilon.minosoft.data.commands.parser.properties.ParserProperties;
import de.bixilon.minosoft.util.buffers.ImprovedStringReader;
@ -23,7 +24,6 @@ public class DummyParser extends CommandParser {
public static final DummyParser DUMMY_PARSER = new DummyParser();
@Override
public boolean isParsable(@Nullable ParserProperties properties, ImprovedStringReader stringReader) {
return true;
public void isParsable(@Nullable ParserProperties properties, ImprovedStringReader stringReader) throws CommandParseException {
}
}

View File

@ -14,7 +14,10 @@
package de.bixilon.minosoft.data.commands.parser;
import de.bixilon.minosoft.data.commands.parser.entity.EntitySelectorArgumentParser;
import de.bixilon.minosoft.data.commands.parser.entity.NameEntitySelectorArgumentParser;
import de.bixilon.minosoft.data.commands.parser.entity.IntegerSelectorArgumentParser;
import de.bixilon.minosoft.data.commands.parser.entity.StringSelectorArgumentParser;
import de.bixilon.minosoft.data.commands.parser.exception.CommandParseException;
import de.bixilon.minosoft.data.commands.parser.exception.entity.*;
import de.bixilon.minosoft.data.commands.parser.properties.EntityParserProperties;
import de.bixilon.minosoft.data.commands.parser.properties.ParserProperties;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
@ -39,17 +42,17 @@ public class EntityParser extends CommandParser {
ENTITY_FILTER_PARAMETER_LIST.put("dz", null);
ENTITY_FILTER_PARAMETER_LIST.put("gamemode", null);
ENTITY_FILTER_PARAMETER_LIST.put("level", null);
ENTITY_FILTER_PARAMETER_LIST.put("limit", null);
*/
ENTITY_FILTER_PARAMETER_LIST.put("name", NameEntitySelectorArgumentParser.NAME_ENTITY_SELECTOR_ARGUMENT_PARSER);
ENTITY_FILTER_PARAMETER_LIST.put("name", StringSelectorArgumentParser.STRING_SELECTOR_ARGUMENT_PARSER);
ENTITY_FILTER_PARAMETER_LIST.put("team", StringSelectorArgumentParser.STRING_SELECTOR_ARGUMENT_PARSER);
ENTITY_FILTER_PARAMETER_LIST.put("limit", IntegerSelectorArgumentParser.INTEGER_SELECTOR_ARGUMENT_PARSER);
/*
ENTITY_FILTER_PARAMETER_LIST.put("nbt", null);
ENTITY_FILTER_PARAMETER_LIST.put("predicate", null);
ENTITY_FILTER_PARAMETER_LIST.put("scores", null);
ENTITY_FILTER_PARAMETER_LIST.put("sort", null);
ENTITY_FILTER_PARAMETER_LIST.put("tag", null);
ENTITY_FILTER_PARAMETER_LIST.put("team", null);
ENTITY_FILTER_PARAMETER_LIST.put("x", null);
ENTITY_FILTER_PARAMETER_LIST.put("x_rotation", null);
ENTITY_FILTER_PARAMETER_LIST.put("y", null);
@ -65,17 +68,18 @@ public class EntityParser extends CommandParser {
}
@Override
public boolean isParsable(ParserProperties properties, ImprovedStringReader stringReader) {
public void isParsable(ParserProperties properties, ImprovedStringReader stringReader) throws CommandParseException {
EntityParserProperties entityParserProperties = (EntityParserProperties) properties;
if (stringReader.getChar().equals("@")) {
// selector
if (((EntityParserProperties) properties).isOnlySingleEntity()) {
return false;
if (entityParserProperties.isOnlySingleEntity()) {
throw new SingleEntityOnlyEntityCommandParseException(stringReader, stringReader.getChar());
}
stringReader.skip(1); // skip @
String selectorChar = stringReader.readChar();
if (!selectorChar.equals("a") && !selectorChar.equals("e") && !selectorChar.equals("p") && !selectorChar.equals("r") && !selectorChar.equals("s")) {
// only @a, @e, @p, @r and @s possible
return false;
throw new UnknownMassSelectorEntityCommandParseException(stringReader, stringReader.getChar());
}
// parse entity selector
@ -83,7 +87,7 @@ public class EntityParser extends CommandParser {
// example: /msg @a[ name = "Bixilon" ] asd
if (!stringReader.getChar().equals("[")) {
// no meta data given, valid
return true;
return;
}
stringReader.skip(1);
@ -91,20 +95,19 @@ public class EntityParser extends CommandParser {
HashSet<String> parameters = new HashSet<>();
while (true) {
stringReader.skipSpaces();
String parameterName = stringReader.readUntil("=").key.replaceAll("\\s", "");
String parameterName = stringReader.readUntil("=").key.replaceAll("\\s", ""); // ToDo: only remove prefix and suffix spaces!
if (parameters.contains(parameterName)) {
return false;
throw new DuplicatedParameterEntityCommandParseException(stringReader, parameterName);
}
if (!ENTITY_FILTER_PARAMETER_LIST.containsKey(parameterName)) {
return false;
throw new UnknownParameterEntityCommandParseException(stringReader, parameterName);
}
stringReader.skipSpaces();
EntitySelectorArgumentParser parser = ENTITY_FILTER_PARAMETER_LIST.get(parameterName);
if (!parser.isParsable(stringReader)) {
return false;
}
parser.isParsable(stringReader);
stringReader.skipSpaces();
parameters.add(parameterName);
String nextChar = stringReader.getChar();
@ -117,20 +120,20 @@ public class EntityParser extends CommandParser {
}
}
stringReader.skipSpaces();
return true;
return;
}
String value = stringReader.readUntilNextCommandArgument();
if (ProtocolDefinition.MINECRAFT_NAME_VALIDATOR.matcher(value).matches()) {
return true;
return;
}
if (((EntityParserProperties) properties).isOnlyPlayers()) {
return false;
if (entityParserProperties.isOnlyPlayers()) {
throw new PlayerOnlyEntityCommandParseException(stringReader, value);
}
try {
Util.getUUIDFromString(value);
return true;
return;
} catch (Exception ignored) {
}
return false;
throw new UnknownEntitySelectorCommandParseException(stringReader, value);
}
}

View File

@ -13,6 +13,9 @@
package de.bixilon.minosoft.data.commands.parser;
import de.bixilon.minosoft.data.commands.parser.exception.CommandParseException;
import de.bixilon.minosoft.data.commands.parser.exception.number.FloatCommandParseException;
import de.bixilon.minosoft.data.commands.parser.exception.number.ValueOutOfRangeCommandParseException;
import de.bixilon.minosoft.data.commands.parser.properties.FloatParserProperties;
import de.bixilon.minosoft.data.commands.parser.properties.ParserProperties;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
@ -21,9 +24,6 @@ import de.bixilon.minosoft.util.buffers.ImprovedStringReader;
public class FloatParser extends CommandParser {
public static final FloatParser FLOAT_PARSER = new FloatParser();
public boolean isValidValue(FloatParserProperties properties, float value) {
return value >= properties.getMinValue() && value <= properties.getMaxValue();
}
@Override
public ParserProperties readParserProperties(InByteBuffer buffer) {
@ -31,12 +31,16 @@ public class FloatParser extends CommandParser {
}
@Override
public boolean isParsable(ParserProperties properties, ImprovedStringReader stringReader) {
public void isParsable(ParserProperties properties, ImprovedStringReader stringReader) throws CommandParseException {
String argument = stringReader.readUntilNextCommandArgument();
try {
return isValidValue((FloatParserProperties) properties, Float.parseFloat(argument));
} catch (Exception ignored) {
return false;
float value = Float.parseFloat(argument);
FloatParserProperties floatParserProperties = (FloatParserProperties) properties;
if (value < floatParserProperties.getMinValue() && value > floatParserProperties.getMaxValue()) {
throw new ValueOutOfRangeCommandParseException(stringReader, floatParserProperties.getMinValue(), floatParserProperties.getMaxValue(), value);
}
} catch (NumberFormatException exception) {
throw new FloatCommandParseException(stringReader, argument, exception);
}
}
}

View File

@ -13,6 +13,9 @@
package de.bixilon.minosoft.data.commands.parser;
import de.bixilon.minosoft.data.commands.parser.exception.CommandParseException;
import de.bixilon.minosoft.data.commands.parser.exception.number.IntegerCommandParseException;
import de.bixilon.minosoft.data.commands.parser.exception.number.ValueOutOfRangeCommandParseException;
import de.bixilon.minosoft.data.commands.parser.properties.IntegerParserProperties;
import de.bixilon.minosoft.data.commands.parser.properties.ParserProperties;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
@ -31,12 +34,16 @@ public class IntegerParser extends CommandParser {
}
@Override
public boolean isParsable(ParserProperties properties, ImprovedStringReader stringReader) {
public void isParsable(ParserProperties properties, ImprovedStringReader stringReader) throws CommandParseException {
String argument = stringReader.readUntilNextCommandArgument();
try {
return isValidValue((IntegerParserProperties) properties, Integer.parseInt(argument));
} catch (Exception ignored) {
return false;
int value = Integer.parseInt(argument);
IntegerParserProperties integerParserProperties = (IntegerParserProperties) properties;
if (value < integerParserProperties.getMinValue() && value > integerParserProperties.getMaxValue()) {
throw new ValueOutOfRangeCommandParseException(stringReader, integerParserProperties.getMinValue(), integerParserProperties.getMaxValue(), value);
}
} catch (NumberFormatException exception) {
throw new IntegerCommandParseException(stringReader, argument, exception);
}
}
}

View File

@ -13,6 +13,7 @@
package de.bixilon.minosoft.data.commands.parser;
import de.bixilon.minosoft.data.commands.parser.exception.CommandParseException;
import de.bixilon.minosoft.data.commands.parser.properties.ParserProperties;
import de.bixilon.minosoft.data.commands.parser.properties.StringParserProperties;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
@ -31,7 +32,7 @@ public class MessageParser extends StringParser {
}
@Override
public boolean isParsable(@Nullable ParserProperties properties, ImprovedStringReader stringReader) {
return super.isParsable(STRING_PARSER_PROPERTIES, stringReader);
public void isParsable(@Nullable ParserProperties properties, ImprovedStringReader stringReader) throws CommandParseException {
super.isParsable(STRING_PARSER_PROPERTIES, stringReader);
}
}

View File

@ -13,6 +13,7 @@
package de.bixilon.minosoft.data.commands.parser;
import de.bixilon.minosoft.data.commands.parser.exception.CommandParseException;
import de.bixilon.minosoft.data.commands.parser.properties.ParserProperties;
import de.bixilon.minosoft.data.commands.parser.properties.RangeParserProperties;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
@ -29,7 +30,7 @@ public class RangeParser extends CommandParser {
}
@Override
public boolean isParsable(@Nullable ParserProperties properties, ImprovedStringReader stringReader) {
return false; // ToDo
public void isParsable(@Nullable ParserProperties properties, ImprovedStringReader stringReader) throws CommandParseException {
// ToDo
}
}

View File

@ -13,6 +13,7 @@
package de.bixilon.minosoft.data.commands.parser;
import de.bixilon.minosoft.data.commands.parser.exception.CommandParseException;
import de.bixilon.minosoft.data.commands.parser.properties.ParserProperties;
import de.bixilon.minosoft.data.commands.parser.properties.ScoreHolderParserProperties;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
@ -29,7 +30,7 @@ public class ScoreHolderParser extends CommandParser {
}
@Override
public boolean isParsable(@Nullable ParserProperties properties, ImprovedStringReader stringReader) {
return false; // ToDo
public void isParsable(@Nullable ParserProperties properties, ImprovedStringReader stringReader) throws CommandParseException {
// ToDo
}
}

View File

@ -13,6 +13,8 @@
package de.bixilon.minosoft.data.commands.parser;
import de.bixilon.minosoft.data.commands.parser.exception.BlankStringCommandParseException;
import de.bixilon.minosoft.data.commands.parser.exception.CommandParseException;
import de.bixilon.minosoft.data.commands.parser.properties.ParserProperties;
import de.bixilon.minosoft.data.commands.parser.properties.StringParserProperties;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
@ -27,17 +29,10 @@ public class StringParser extends CommandParser {
}
@Override
public boolean isParsable(ParserProperties properties, ImprovedStringReader stringReader) {
// Why the hell can't I use yield here :?
public void isParsable(ParserProperties properties, ImprovedStringReader stringReader) throws CommandParseException {
StringParserProperties stringParserProperties = ((StringParserProperties) properties);
switch (stringParserProperties.getSetting()) {
case SINGLE_WORD -> {
String rest = stringReader.readUntilNextCommandArgument();
if (stringParserProperties.isAllowEmptyString()) {
return true;
}
return !rest.isBlank();
}
String string = switch (stringParserProperties.getSetting()) {
case SINGLE_WORD -> stringReader.readUntilNextCommandArgument();
case QUOTABLE_PHRASE -> {
if (stringReader.get(1).equals("\"")) {
stringReader.skip(1);
@ -50,24 +45,18 @@ public class StringParser extends CommandParser {
builder.append(stringReader.readUntil("\""));
currentString = builder.toString();
}
if (stringParserProperties.isAllowEmptyString()) {
return true;
}
return currentString.isBlank();
yield currentString;
}
if (stringParserProperties.isAllowEmptyString()) {
return true;
}
return !stringReader.readUntilNextCommandArgument().isBlank();
}
case GREEDY_PHRASE -> {
String rest = stringReader.readRest();
if (stringParserProperties.isAllowEmptyString()) {
return true;
}
return !rest.isBlank();
yield stringReader.readUntilNextCommandArgument();
}
case GREEDY_PHRASE -> stringReader.readRest();
};
if (stringParserProperties.isAllowEmptyString()) {
return;
}
if (string.isBlank()) {
throw new BlankStringCommandParseException(stringReader, string);
}
return false;
}
}

View File

@ -13,9 +13,10 @@
package de.bixilon.minosoft.data.commands.parser.entity;
import de.bixilon.minosoft.data.commands.parser.exception.CommandParseException;
import de.bixilon.minosoft.util.buffers.ImprovedStringReader;
public interface EntitySelectorArgumentParser {
boolean isParsable(ImprovedStringReader stringReader);
void isParsable(ImprovedStringReader stringReader) throws CommandParseException;
}

View File

@ -0,0 +1,38 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* 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/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.data.commands.parser.entity;
import de.bixilon.minosoft.data.commands.parser.exception.CommandParseException;
import de.bixilon.minosoft.data.commands.parser.exception.number.IntegerCommandParseException;
import de.bixilon.minosoft.util.Pair;
import de.bixilon.minosoft.util.buffers.ImprovedStringReader;
public class IntegerSelectorArgumentParser implements EntitySelectorArgumentParser {
public static final IntegerSelectorArgumentParser INTEGER_SELECTOR_ARGUMENT_PARSER = new IntegerSelectorArgumentParser();
@Override
public void isParsable(ImprovedStringReader stringReader) throws CommandParseException {
// read until next space or comma
Pair<String, String> match = stringReader.readUntil(",", " ", "]");
if (!match.value.equals(" ")) {
// set pointer to --
stringReader.skip(-1);
}
try {
Integer.parseInt(match.key);
} catch (Exception e) {
throw new IntegerCommandParseException(stringReader, match.key, e);
}
}
}

View File

@ -14,19 +14,21 @@
package de.bixilon.minosoft.data.commands.parser.entity;
import de.bixilon.minosoft.data.commands.parser.StringParser;
import de.bixilon.minosoft.data.commands.parser.exception.CommandParseException;
import de.bixilon.minosoft.data.commands.parser.properties.StringParserProperties;
import de.bixilon.minosoft.util.Pair;
import de.bixilon.minosoft.util.buffers.ImprovedStringReader;
public class NameEntitySelectorArgumentParser implements EntitySelectorArgumentParser {
public static final NameEntitySelectorArgumentParser NAME_ENTITY_SELECTOR_ARGUMENT_PARSER = new NameEntitySelectorArgumentParser();
public class StringSelectorArgumentParser implements EntitySelectorArgumentParser {
public static final StringSelectorArgumentParser STRING_SELECTOR_ARGUMENT_PARSER = new StringSelectorArgumentParser();
private static final StringParserProperties STRING_PARSER_PROPERTIES = new StringParserProperties(StringParserProperties.StringSettings.QUOTABLE_PHRASE, true);
@Override
public boolean isParsable(ImprovedStringReader stringReader) {
public void isParsable(ImprovedStringReader stringReader) throws CommandParseException {
// if it starts with a quote, it will end with a quote
if (stringReader.get(1).equals("\"")) {
return StringParser.STRING_PARSER.isParsable(STRING_PARSER_PROPERTIES, stringReader);
StringParser.STRING_PARSER.isParsable(STRING_PARSER_PROPERTIES, stringReader);
return;
}
// read until next space or comma
Pair<String, String> match = stringReader.readUntil(",", " ", "]");
@ -34,6 +36,5 @@ public class NameEntitySelectorArgumentParser implements EntitySelectorArgumentP
// set pointer to --
stringReader.skip(-1);
}
return true;
}
}

View File

@ -0,0 +1,29 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* 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/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.data.commands.parser.exception;
import de.bixilon.minosoft.util.buffers.ImprovedStringReader;
public class BlankStringCommandParseException extends CommandParseException {
private static final String ERROR_MESSAGE = "String is blank!";
public BlankStringCommandParseException(ImprovedStringReader command, String currentArgument) {
super(ERROR_MESSAGE, command, currentArgument);
}
public BlankStringCommandParseException(ImprovedStringReader command, String currentArgument, Throwable cause) {
super(ERROR_MESSAGE, command, currentArgument, cause);
}
}

View File

@ -0,0 +1,29 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* 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/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.data.commands.parser.exception;
import de.bixilon.minosoft.util.buffers.ImprovedStringReader;
public class BooleanCommandParseException extends CommandParseException {
private static final String ERROR_MESSAGE = "Unknown boolean value!";
public BooleanCommandParseException(ImprovedStringReader command, String currentArgument) {
super(ERROR_MESSAGE, command, currentArgument);
}
public BooleanCommandParseException(ImprovedStringReader command, String currentArgument, Throwable cause) {
super(ERROR_MESSAGE, command, currentArgument, cause);
}
}

View File

@ -0,0 +1,64 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* 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/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.data.commands.parser.exception;
import de.bixilon.minosoft.util.buffers.ImprovedStringReader;
public class CommandParseException extends Exception {
private final String errorMessage;
private final ImprovedStringReader command;
private final int startIndex;
private final int endIndex;
public CommandParseException(String errorMessage, ImprovedStringReader command, int startIndex) {
this(errorMessage, command, startIndex, null);
}
public CommandParseException(String errorMessage, ImprovedStringReader command, int startIndex, Throwable cause) {
super(String.format("%s <-- [%s] (%d): %s", command.getString(), command.getString().substring(startIndex), startIndex, errorMessage), cause);
this.errorMessage = errorMessage;
this.command = command;
this.startIndex = startIndex;
this.endIndex = command.getPosition();
}
public CommandParseException(String errorMessage, ImprovedStringReader command, String currentArgument) {
this(errorMessage, command, currentArgument, null);
}
public CommandParseException(String errorMessage, ImprovedStringReader command, String currentArgument, Throwable cause) {
super(String.format("%s <-- [%s] (%d): %s", command.getString(), currentArgument, (command.getPosition() - currentArgument.length()), errorMessage), cause);
this.errorMessage = errorMessage;
this.command = command;
this.startIndex = command.getPosition() - currentArgument.length();
this.endIndex = command.getPosition();
}
public String getErrorMessage() {
return errorMessage;
}
public ImprovedStringReader getCommand() {
return command;
}
public int getStartIndex() {
return startIndex;
}
public int getEndIndex() {
return endIndex;
}
}

View File

@ -0,0 +1,29 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* 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/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.data.commands.parser.exception;
import de.bixilon.minosoft.util.buffers.ImprovedStringReader;
public class RequiresMoreArgumentsCommandParseException extends CommandParseException {
private static final String ERROR_MESSAGE = "Command requires more arguments!";
public RequiresMoreArgumentsCommandParseException(ImprovedStringReader command) {
super(ERROR_MESSAGE, command, command.getPosition());
}
public RequiresMoreArgumentsCommandParseException(ImprovedStringReader command, Throwable cause) {
super(ERROR_MESSAGE, command, command.getPosition(), cause);
}
}

View File

@ -0,0 +1,29 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* 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/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.data.commands.parser.exception;
import de.bixilon.minosoft.util.buffers.ImprovedStringReader;
public class UnknownCommandParseException extends CommandParseException {
private static final String ERROR_MESSAGE = "Unknown command!";
public UnknownCommandParseException(ImprovedStringReader command, String currentArgument) {
super(ERROR_MESSAGE, command, currentArgument);
}
public UnknownCommandParseException(ImprovedStringReader command, String currentArgument, Throwable cause) {
super(ERROR_MESSAGE, command, currentArgument, cause);
}
}

View File

@ -0,0 +1,29 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* 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/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.data.commands.parser.exception;
import de.bixilon.minosoft.util.buffers.ImprovedStringReader;
public class WrongArgumentCommandParseException extends CommandParseException {
private static final String ERROR_MESSAGE = "Wrong argument given!";
public WrongArgumentCommandParseException(ImprovedStringReader command, String currentArgument) {
super(ERROR_MESSAGE, command, currentArgument);
}
public WrongArgumentCommandParseException(ImprovedStringReader command, String currentArgument, Throwable cause) {
super(ERROR_MESSAGE, command, currentArgument, cause);
}
}

View File

@ -0,0 +1,30 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* 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/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.data.commands.parser.exception.entity;
import de.bixilon.minosoft.data.commands.parser.exception.CommandParseException;
import de.bixilon.minosoft.util.buffers.ImprovedStringReader;
public class DuplicatedParameterEntityCommandParseException extends CommandParseException {
private static final String ERROR_MESSAGE = "Entity parameter is duplicated!";
public DuplicatedParameterEntityCommandParseException(ImprovedStringReader command, String currentArgument) {
super(ERROR_MESSAGE, command, currentArgument);
}
public DuplicatedParameterEntityCommandParseException(ImprovedStringReader command, String currentArgument, Throwable cause) {
super(ERROR_MESSAGE, command, currentArgument, cause);
}
}

View File

@ -0,0 +1,30 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* 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/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.data.commands.parser.exception.entity;
import de.bixilon.minosoft.data.commands.parser.exception.CommandParseException;
import de.bixilon.minosoft.util.buffers.ImprovedStringReader;
public class PlayerOnlyEntityCommandParseException extends CommandParseException {
private static final String ERROR_MESSAGE = "Only players allowed!";
public PlayerOnlyEntityCommandParseException(ImprovedStringReader command, String currentArgument) {
super(ERROR_MESSAGE, command, currentArgument);
}
public PlayerOnlyEntityCommandParseException(ImprovedStringReader command, String currentArgument, Throwable cause) {
super(ERROR_MESSAGE, command, currentArgument, cause);
}
}

View File

@ -0,0 +1,30 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* 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/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.data.commands.parser.exception.entity;
import de.bixilon.minosoft.data.commands.parser.exception.CommandParseException;
import de.bixilon.minosoft.util.buffers.ImprovedStringReader;
public class SingleEntityOnlyEntityCommandParseException extends CommandParseException {
private static final String ERROR_MESSAGE = "Only 1 entity allowed!";
public SingleEntityOnlyEntityCommandParseException(ImprovedStringReader command, String currentArgument) {
super(ERROR_MESSAGE, command, currentArgument);
}
public SingleEntityOnlyEntityCommandParseException(ImprovedStringReader command, String currentArgument, Throwable cause) {
super(ERROR_MESSAGE, command, currentArgument, cause);
}
}

View File

@ -0,0 +1,30 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* 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/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.data.commands.parser.exception.entity;
import de.bixilon.minosoft.data.commands.parser.exception.CommandParseException;
import de.bixilon.minosoft.util.buffers.ImprovedStringReader;
public class UnknownEntitySelectorCommandParseException extends CommandParseException {
private static final String ERROR_MESSAGE = "Unknown entity selector!";
public UnknownEntitySelectorCommandParseException(ImprovedStringReader command, String currentArgument) {
super(ERROR_MESSAGE, command, currentArgument);
}
public UnknownEntitySelectorCommandParseException(ImprovedStringReader command, String currentArgument, Throwable cause) {
super(ERROR_MESSAGE, command, currentArgument, cause);
}
}

View File

@ -0,0 +1,30 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* 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/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.data.commands.parser.exception.entity;
import de.bixilon.minosoft.data.commands.parser.exception.CommandParseException;
import de.bixilon.minosoft.util.buffers.ImprovedStringReader;
public class UnknownMassSelectorEntityCommandParseException extends CommandParseException {
private static final String ERROR_MESSAGE = "Unknown mass selector!";
public UnknownMassSelectorEntityCommandParseException(ImprovedStringReader command, String currentArgument) {
super(ERROR_MESSAGE, command, currentArgument);
}
public UnknownMassSelectorEntityCommandParseException(ImprovedStringReader command, String currentArgument, Throwable cause) {
super(ERROR_MESSAGE, command, currentArgument, cause);
}
}

View File

@ -0,0 +1,30 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* 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/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.data.commands.parser.exception.entity;
import de.bixilon.minosoft.data.commands.parser.exception.CommandParseException;
import de.bixilon.minosoft.util.buffers.ImprovedStringReader;
public class UnknownParameterEntityCommandParseException extends CommandParseException {
private static final String ERROR_MESSAGE = "Unknown entity parameter!";
public UnknownParameterEntityCommandParseException(ImprovedStringReader command, String currentArgument) {
super(ERROR_MESSAGE, command, currentArgument);
}
public UnknownParameterEntityCommandParseException(ImprovedStringReader command, String currentArgument, Throwable cause) {
super(ERROR_MESSAGE, command, currentArgument, cause);
}
}

View File

@ -0,0 +1,30 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* 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/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.data.commands.parser.exception.number;
import de.bixilon.minosoft.data.commands.parser.exception.CommandParseException;
import de.bixilon.minosoft.util.buffers.ImprovedStringReader;
public class DoubleCommandParseException extends CommandParseException {
private static final String ERROR_MESSAGE = "Unknown double value!";
public DoubleCommandParseException(ImprovedStringReader command, String currentArgument) {
super(ERROR_MESSAGE, command, currentArgument);
}
public DoubleCommandParseException(ImprovedStringReader command, String currentArgument, Throwable cause) {
super(ERROR_MESSAGE, command, currentArgument, cause);
}
}

View File

@ -0,0 +1,30 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* 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/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.data.commands.parser.exception.number;
import de.bixilon.minosoft.data.commands.parser.exception.CommandParseException;
import de.bixilon.minosoft.util.buffers.ImprovedStringReader;
public class FloatCommandParseException extends CommandParseException {
private static final String ERROR_MESSAGE = "Unknown float value!";
public FloatCommandParseException(ImprovedStringReader command, String currentArgument) {
super(ERROR_MESSAGE, command, currentArgument);
}
public FloatCommandParseException(ImprovedStringReader command, String currentArgument, Throwable cause) {
super(ERROR_MESSAGE, command, currentArgument, cause);
}
}

View File

@ -0,0 +1,30 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* 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/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.data.commands.parser.exception.number;
import de.bixilon.minosoft.data.commands.parser.exception.CommandParseException;
import de.bixilon.minosoft.util.buffers.ImprovedStringReader;
public class IntegerCommandParseException extends CommandParseException {
private static final String ERROR_MESSAGE = "Unknown int value!";
public IntegerCommandParseException(ImprovedStringReader command, String currentArgument) {
super(ERROR_MESSAGE, command, currentArgument);
}
public IntegerCommandParseException(ImprovedStringReader command, String currentArgument, Throwable cause) {
super(ERROR_MESSAGE, command, currentArgument, cause);
}
}

View File

@ -0,0 +1,26 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* 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/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.data.commands.parser.exception.number;
import de.bixilon.minosoft.data.commands.parser.exception.CommandParseException;
import de.bixilon.minosoft.util.buffers.ImprovedStringReader;
public class ValueOutOfRangeCommandParseException extends CommandParseException {
private static final String ERROR_MESSAGE = "Value out of range (min=%s, max=%s!";
public ValueOutOfRangeCommandParseException(ImprovedStringReader command, Object minValue, Object maxValue, Object value) {
super(String.format(ERROR_MESSAGE, minValue, maxValue), command, value.toString());
}
}