All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.minestom.server.command.builder.arguments.ArgumentString Maven / Gradle / Ivy

There is a newer version: 7320437640
Show newest version
package net.minestom.server.command.builder.arguments;

import net.minestom.server.command.CommandSender;
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
import net.minestom.server.utils.StringUtils;
import net.minestom.server.utils.binary.BinaryWriter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * Argument which will take a quoted string.
 * 

* Example: "Hey I am a string" */ public class ArgumentString extends Argument { private static final char BACKSLASH = '\\'; private static final char DOUBLE_QUOTE = '"'; private static final char QUOTE = '\''; public static final int QUOTE_ERROR = 1; public ArgumentString(String id) { super(id, true); } @NotNull @Override public String parse(@NotNull CommandSender sender, @NotNull String input) throws ArgumentSyntaxException { return staticParse(input); } @Override public String parser() { return "brigadier:string"; } @Override public byte @Nullable [] nodeProperties() { return BinaryWriter.makeArray(packetWriter -> { packetWriter.writeVarInt(1); // Quotable phrase }); } /** * @deprecated use {@link Argument#parse(CommandSender, Argument)} */ @Deprecated public static String staticParse(@NotNull String input) throws ArgumentSyntaxException { // Return if not quoted if (!input.contains(String.valueOf(DOUBLE_QUOTE)) && !input.contains(String.valueOf(QUOTE)) && !input.contains(StringUtils.SPACE)) { return input; } // Check if value start and end with quote final char first = input.charAt(0); final char last = input.charAt(input.length() - 1); final boolean quote = input.length() >= 2 && first == last && (first == DOUBLE_QUOTE || first == QUOTE); if (!quote) throw new ArgumentSyntaxException("String argument needs to start and end with quotes", input, QUOTE_ERROR); // Remove first and last characters (quotes) input = input.substring(1, input.length() - 1); // Verify backslashes for (int i = 1; i < input.length(); i++) { final char c = input.charAt(i); if (c == first) { final char lastChar = input.charAt(i - 1); if (lastChar != BACKSLASH) { throw new ArgumentSyntaxException("Non-escaped quote", input, QUOTE_ERROR); } } } return StringUtils.unescapeJavaString(input); } @Override public String toString() { return String.format("String<%s>", getId()); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy