tutorial:commands
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
tutorial:commands [2019/11/16 02:14] – Simple Custom ArgumentType using UUIDArgumentType i509vcb | tutorial:commands [2019/11/16 17:01] – Line numbers i509vcb | ||
---|---|---|---|
Line 439: | Line 439: | ||
</ | </ | ||
- | ===== Custom Arguments | + | ===== Custom Arguments ===== |
- | Brigadier has support for custom argument types and this section goes into showing how to create a simple argument type. For this example we will create a UUIDArgumentType | + | Brigadier has support for custom argument types and this section goes into showing how to create a simple argument type. |
+ | |||
+ | Warning: Custom arguments require client mod installation to be registered correctly! If you are making a server plugin, consider using existing argument type and a custom suggestions provider instead. | ||
+ | |||
+ | For this example we will create a UUIDArgumentType. | ||
First create a class which extends '' | First create a class which extends '' | ||
- | <code java> | + | <code java [enable_line_numbers=" |
public class UUIDArgumentType implements ArgumentType< | public class UUIDArgumentType implements ArgumentType< | ||
</ | </ | ||
Line 459: | Line 463: | ||
Next you will store the current position of the cursor, this is so you can substring out only the specific argument. This will always be at the beginning of where your argument appears on the command line. | Next you will store the current position of the cursor, this is so you can substring out only the specific argument. This will always be at the beginning of where your argument appears on the command line. | ||
- | <code java> | + | <code java [enable_line_numbers=" |
int argBeginning = reader.getCursor(); | int argBeginning = reader.getCursor(); | ||
if (!reader.canRead()) { | if (!reader.canRead()) { | ||
Line 468: | Line 472: | ||
Now we grab the entire argument. Depending on your argument type, you may have a different criteria or be similar to some arguments where detecting a '' | Now we grab the entire argument. Depending on your argument type, you may have a different criteria or be similar to some arguments where detecting a '' | ||
- | <code java> | + | <code java [enable_line_numbers=" |
while (reader.canRead() && reader.peek() != ' ') { // peek provides the character at the current cursor position. | while (reader.canRead() && reader.peek() != ' ') { // peek provides the character at the current cursor position. | ||
reader.skip(); | reader.skip(); | ||
Line 476: | Line 480: | ||
Then we will ask the StringReader what the current position of the cursor is an substring our argument out of the command line. | Then we will ask the StringReader what the current position of the cursor is an substring our argument out of the command line. | ||
- | <code java> | + | <code java [enable_line_numbers=" |
Now finally we check if our argument is correct and parse the specific argument to our liking, and throwing an exception if the parsing fails. | Now finally we check if our argument is correct and parse the specific argument to our liking, and throwing an exception if the parsing fails. | ||
- | <code java> | + | <code java [enable_line_numbers=" |
try { | try { | ||
UUID uuid = UUID.fromString(uuidString); | UUID uuid = UUID.fromString(uuidString); | ||
Line 495: | Line 499: | ||
Within your ModInitializer (Not only client or server) you will add this so the client can recognize the argument when the command tree is sent. For more complex argument types, you may need to create your own ArgumentSerializer. | Within your ModInitializer (Not only client or server) you will add this so the client can recognize the argument when the command tree is sent. For more complex argument types, you may need to create your own ArgumentSerializer. | ||
- | <code java> | + | <code java [enable_line_numbers=" |
ArgumentTypes.register(" | ArgumentTypes.register(" | ||
// The argument should be what will create the ArgumentType. | // The argument should be what will create the ArgumentType. | ||
Line 502: | Line 506: | ||
And here is the whole ArgumentType: | And here is the whole ArgumentType: | ||
- | <file java UUIDArgumentType.java> | + | <file java UUIDArgumentType.java |
import com.mojang.brigadier.StringReader; | import com.mojang.brigadier.StringReader; |
tutorial/commands.txt · Last modified: 2024/02/23 14:22 by allen1210