import com.mojang.brigadier.StringReader; import com.mojang.brigadier.arguments.ArgumentType; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; import net.minecraft.text.Text; import java.util.ArrayList; import java.util.Collection; import java.util.UUID; /** * Represents an ArgumentType that will return a UUID. */ public class UuidArgumentType implements ArgumentType { public static final DynamicCommandExceptionType INVALID_UUID = new DynamicCommandExceptionType(o -> Text.literal("Invalid uuid: " + o)); public static UuidArgumentType uuid() { return new UuidArgumentType(); } public static UUID getUuid(String name, CommandContext context) { // Note that you should assume the CommandSource wrapped inside of the CommandContext will always be a generic type. // If you need to access the ServerCommandSource make sure you verify the source is a server command source before casting. return context.getArgument(name, UUID.class); } private static final Collection EXAMPLES = List.of( "765e5d33-c991-454f-8775-b6a7a394c097", // i509VCB: Username The_1_gamers "069a79f4-44e9-4726-a5be-fca90e38aaf5", // Notch "61699b2e-d327-4a01-9f1e-0ea8c3f06bc6" // Dinnerbone ); @Override public UUID parse(StringReader reader) throws CommandSyntaxException { int argBeginning = reader.getCursor(); // The starting position of the cursor is at the beginning of the argument. if (!reader.canRead()) { reader.skip(); } // Now we check the contents of the argument till either we hit the end of the // command line (when ''canRead'' becomes false) // Otherwise we go till reach reach a character that cannot compose a UUID while (reader.canRead() && (Character.isLetterOrDigit(reader.peek()) || reader.peek() == '-')) { // peek provides the character at the current cursor position. reader.skip(); // Tells the StringReader to move it's cursor to the next position. } // Now we substring the specific part we want to see using the starting cursor // position and the ends where the next argument starts. String uuidString = reader.getString().substring(argBeginning, reader.getCursor()); try { UUID uuid = UUID.fromString(uuidString); // Now our actual logic. return uuid; // And we return our type, in this case the parser will consider this // argument to have parsed properly and then move on. } catch (InvalidArgumentException ex) { // UUIDs can throw an exception when made by a string, so we catch the exception and repackage it into a CommandSyntaxException type. // Create with context tells Brigadier to supply some context to tell the user where the command failed at. // Though normal create method could be used. reader.setCursor(argBeginning); throw INVALID_UUID.createWithContext(reader, ex.getMessage()); } } @Override public Collection getExamples() { // Brigadier has support to show examples for what the argument should look like, // this should contain a Collection of only the argument this type will return. // This is mainly used to detect ambiguity, which means an argument of this type may be parsed as another type. return EXAMPLES; } }