mirror of https://github.com/Minestom/Minestom.git
Arg component, color, word done
This commit is contained in:
parent
4284e059cb
commit
6f42da5a13
|
@ -1,7 +1,7 @@
|
|||
package net.minestom.server.command.builder.arguments;
|
||||
|
||||
import net.minestom.server.command.CommandReader;
|
||||
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
|
||||
import net.minestom.server.utils.StringUtils;
|
||||
import net.minestom.server.utils.binary.BinaryWriter;
|
||||
import net.minestom.server.utils.validate.Check;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
@ -15,8 +15,9 @@ import org.jetbrains.annotations.Nullable;
|
|||
* Example: hey
|
||||
*/
|
||||
public class ArgumentWord extends Argument<String> {
|
||||
|
||||
public static final int SPACE_ERROR = 1;
|
||||
private static final byte[] prop = BinaryWriter.makeArray(packetWriter -> {
|
||||
packetWriter.writeVarInt(0); // Single word
|
||||
});
|
||||
public static final int RESTRICTION_ERROR = 2;
|
||||
|
||||
protected String[] restrictions;
|
||||
|
@ -25,6 +26,24 @@ public class ArgumentWord extends Argument<String> {
|
|||
super(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String parse(CommandReader reader) throws ArgumentSyntaxException {
|
||||
final String word = reader.getWord();
|
||||
|
||||
// Check restrictions (acting as literal)
|
||||
if (hasRestrictions()) {
|
||||
for (String r : restrictions) {
|
||||
if (word.equals(r)) {
|
||||
reader.consume();
|
||||
return word;
|
||||
}
|
||||
}
|
||||
throw new ArgumentSyntaxException("Word needs to be in the restriction list", word, RESTRICTION_ERROR);
|
||||
}
|
||||
reader.consume();
|
||||
return word;
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to force the use of a few precise words instead of complete freedom.
|
||||
* <p>
|
||||
|
@ -49,24 +68,6 @@ public class ArgumentWord extends Argument<String> {
|
|||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String parse(@NotNull String input) throws ArgumentSyntaxException {
|
||||
if (input.contains(StringUtils.SPACE))
|
||||
throw new ArgumentSyntaxException("Word cannot contain space character", input, SPACE_ERROR);
|
||||
|
||||
// Check restrictions (acting as literal)
|
||||
if (hasRestrictions()) {
|
||||
for (String r : restrictions) {
|
||||
if (input.equals(r))
|
||||
return input;
|
||||
}
|
||||
throw new ArgumentSyntaxException("Word needs to be in the restriction list", input, RESTRICTION_ERROR);
|
||||
}
|
||||
|
||||
return input;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String parser() {
|
||||
return "brigadier:string";
|
||||
|
@ -74,9 +75,7 @@ public class ArgumentWord extends Argument<String> {
|
|||
|
||||
@Override
|
||||
public byte @Nullable [] nodeProperties() {
|
||||
return BinaryWriter.makeArray(packetWriter -> {
|
||||
packetWriter.writeVarInt(0); // Single word
|
||||
});
|
||||
return prop;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -2,6 +2,7 @@ package net.minestom.server.command.builder.arguments.minecraft;
|
|||
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.text.format.Style;
|
||||
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,18 +21,19 @@ public class ArgumentColor extends Argument<Style> {
|
|||
super(id);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Style parse(@NotNull String input) throws ArgumentSyntaxException {
|
||||
|
||||
public @NotNull Style parse(CommandReader reader) throws ArgumentSyntaxException {
|
||||
final String input = reader.getWord();
|
||||
// check for colour
|
||||
NamedTextColor color = NamedTextColor.NAMES.value(input);
|
||||
if (color != null) {
|
||||
reader.consume();
|
||||
return Style.style(color);
|
||||
}
|
||||
|
||||
// check for reset
|
||||
if (input.equals("reset")) {
|
||||
reader.consume();
|
||||
return Style.empty();
|
||||
}
|
||||
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
package net.minestom.server.command.builder.arguments.minecraft;
|
||||
|
||||
import com.google.gson.JsonParseException;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||
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;
|
||||
|
@ -12,16 +12,27 @@ public class ArgumentComponent extends Argument<Component> {
|
|||
public static final int INVALID_JSON_ERROR = 1;
|
||||
|
||||
public ArgumentComponent(@NotNull String id) {
|
||||
super(id, true);
|
||||
super(id);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Component parse(@NotNull String input) throws ArgumentSyntaxException {
|
||||
public @NotNull Component parse(CommandReader reader) throws ArgumentSyntaxException {
|
||||
final char start = reader.getNextChar();
|
||||
if (start != '{') throw new ArgumentSyntaxException("Invalid component start", start+"", INVALID_JSON_ERROR);
|
||||
final int end = reader.getClosingIndexOfJsonObject(0);
|
||||
|
||||
if (end == -1) {
|
||||
final String remaining = reader.getRemaining();
|
||||
reader.consume();
|
||||
throw new ArgumentSyntaxException("Invalid JSON", remaining, INVALID_JSON_ERROR);
|
||||
}
|
||||
|
||||
final String s = reader.get(end);
|
||||
reader.consume();
|
||||
try {
|
||||
return GsonComponentSerializer.gson().deserialize(input);
|
||||
} catch (JsonParseException e) {
|
||||
throw new ArgumentSyntaxException("Invalid JSON", input, INVALID_JSON_ERROR);
|
||||
return GsonComponentSerializer.gson().deserialize(s);
|
||||
} catch (Exception e) {
|
||||
throw new ArgumentSyntaxException("Invalid component", s, INVALID_JSON_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue