mirror of https://github.com/Minestom/Minestom.git
Arg range, resource loc, nbt done & array reader
This commit is contained in:
parent
56e99424e0
commit
24ea5bc9f6
|
@ -91,6 +91,36 @@ public final class CommandReader {
|
|||
return -1;
|
||||
}
|
||||
|
||||
public int getClosingIndexOfJsonArray(int fromOffset) {
|
||||
int count = 1;
|
||||
boolean insideString = false;
|
||||
boolean lastWasEscape = false;
|
||||
final int start = nextIndexOf('[', fromOffset);
|
||||
if (start == -1) return -1;
|
||||
for (int i = start+1; i < input.length(); i++) {
|
||||
final char current = getCharAt(i);
|
||||
if (insideString) {
|
||||
if (current == '"')
|
||||
if (lastWasEscape)
|
||||
lastWasEscape = false;
|
||||
else
|
||||
insideString = false;
|
||||
else if (current == '\\')
|
||||
lastWasEscape = !lastWasEscape;
|
||||
else
|
||||
lastWasEscape = false;
|
||||
} else {
|
||||
if (current == '[')
|
||||
count++;
|
||||
else if (current == ']' && --count == 0)
|
||||
return i;
|
||||
else if (current == '"')
|
||||
insideString = true;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public String get(int exclusiveAbsoluteEnd) {
|
||||
if (!hasRemaining()) throw new BufferUnderflowException();
|
||||
final String s = input.subSequence(cursor, exclusiveAbsoluteEnd).toString();
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package net.minestom.server.command.builder.arguments.minecraft;
|
||||
|
||||
import net.minestom.server.command.CommandReader;
|
||||
import net.minestom.server.command.builder.arguments.Argument;
|
||||
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
@ -20,21 +21,27 @@ public class ArgumentNbtCompoundTag extends Argument<NBTCompound> {
|
|||
public static final int INVALID_NBT = 1;
|
||||
|
||||
public ArgumentNbtCompoundTag(String id) {
|
||||
super(id, true);
|
||||
super(id);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public NBTCompound parse(@NotNull String input) throws ArgumentSyntaxException {
|
||||
try {
|
||||
NBT nbt = new SNBTParser(new StringReader(input)).parse();
|
||||
public @NotNull NBTCompound parse(CommandReader reader) throws ArgumentSyntaxException {
|
||||
int end = reader.getClosingIndexOfJsonObject(0);
|
||||
if (end == -1) {
|
||||
throw new ArgumentSyntaxException("Invalid NBT", "", INVALID_NBT);
|
||||
} else {
|
||||
final String input = reader.get(end);
|
||||
reader.consume();
|
||||
try {
|
||||
NBT nbt = new SNBTParser(new StringReader(input)).parse();
|
||||
|
||||
if (!(nbt instanceof NBTCompound))
|
||||
if (!(nbt instanceof NBTCompound))
|
||||
throw new ArgumentSyntaxException("NBTCompound is invalid", input, INVALID_NBT);
|
||||
|
||||
return (NBTCompound) nbt;
|
||||
} catch (NBTException e) {
|
||||
throw new ArgumentSyntaxException("NBTCompound is invalid", input, INVALID_NBT);
|
||||
|
||||
return (NBTCompound) nbt;
|
||||
} catch (NBTException e) {
|
||||
throw new ArgumentSyntaxException("NBTCompound is invalid", input, INVALID_NBT);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package net.minestom.server.command.builder.arguments.minecraft;
|
||||
|
||||
import net.minestom.server.command.CommandReader;
|
||||
import net.minestom.server.command.builder.arguments.Argument;
|
||||
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
@ -21,16 +22,25 @@ public class ArgumentNbtTag extends Argument<NBT> {
|
|||
public static final int INVALID_NBT = 1;
|
||||
|
||||
public ArgumentNbtTag(String id) {
|
||||
super(id, true);
|
||||
super(id);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public NBT parse(@NotNull String input) throws ArgumentSyntaxException {
|
||||
try {
|
||||
return new SNBTParser(new StringReader(input)).parse();
|
||||
} catch (NBTException e) {
|
||||
throw new ArgumentSyntaxException("Invalid NBT", input, INVALID_NBT);
|
||||
public @NotNull NBT parse(CommandReader reader) throws ArgumentSyntaxException {
|
||||
int end = reader.getClosingIndexOfJsonObject(0);
|
||||
if (end == -1) {
|
||||
end = reader.getClosingIndexOfJsonArray(0);
|
||||
}
|
||||
if (end == -1) {
|
||||
throw new ArgumentSyntaxException("Invalid NBT", "", INVALID_NBT);
|
||||
} else {
|
||||
final String input = reader.get(end);
|
||||
reader.consume();
|
||||
try {
|
||||
return new SNBTParser(new StringReader(input)).parse();
|
||||
} catch (NBTException e) {
|
||||
throw new ArgumentSyntaxException("Invalid NBT", input, INVALID_NBT);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package net.minestom.server.command.builder.arguments.minecraft;
|
||||
|
||||
import net.minestom.server.command.CommandReader;
|
||||
import net.minestom.server.command.builder.arguments.Argument;
|
||||
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
|
||||
import net.minestom.server.utils.math.Range;
|
||||
|
@ -30,13 +31,14 @@ public abstract class ArgumentRange<T extends Range<N>, N extends Number> extend
|
|||
this.rangeConstructor = rangeConstructor;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public T parse(@NotNull String input) throws ArgumentSyntaxException {
|
||||
public @NotNull T parse(CommandReader reader) throws ArgumentSyntaxException {
|
||||
final String input = reader.getWord();
|
||||
try {
|
||||
final String[] split = input.split(Pattern.quote(".."), -1);
|
||||
|
||||
if (split.length == 2) {
|
||||
reader.consume();
|
||||
final N min;
|
||||
final N max;
|
||||
if (split[0].length() == 0 && split[1].length() > 0) {
|
||||
|
@ -58,6 +60,7 @@ public abstract class ArgumentRange<T extends Range<N>, N extends Number> extend
|
|||
return rangeConstructor.apply(min, max);
|
||||
} else if (split.length == 1) {
|
||||
final N number = parser.apply(input);
|
||||
reader.consume();
|
||||
return rangeConstructor.apply(number, number);
|
||||
}
|
||||
} catch (NumberFormatException e2) {
|
||||
|
|
|
@ -1,24 +1,20 @@
|
|||
package net.minestom.server.command.builder.arguments.minecraft;
|
||||
|
||||
import net.minestom.server.command.CommandReader;
|
||||
import net.minestom.server.command.builder.arguments.Argument;
|
||||
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
|
||||
import net.minestom.server.utils.StringUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class ArgumentResourceLocation extends Argument<String> {
|
||||
|
||||
public static final int SPACE_ERROR = 1;
|
||||
|
||||
public ArgumentResourceLocation(@NotNull String id) {
|
||||
super(id);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String parse(@NotNull String input) throws ArgumentSyntaxException {
|
||||
if (input.contains(StringUtils.SPACE))
|
||||
throw new ArgumentSyntaxException("Resource location cannot contain space character", input, SPACE_ERROR);
|
||||
|
||||
public @NotNull String parse(CommandReader reader) throws ArgumentSyntaxException {
|
||||
final String input = reader.getWord();
|
||||
reader.consume();
|
||||
return input;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue