|
| 1 | +--- |
| 2 | +order: 2 |
| 3 | +authors: |
| 4 | + - JorelAli |
| 5 | + - willkroboth |
| 6 | + - DerEchtePilz |
| 7 | + - Sytm |
| 8 | +--- |
| 9 | + |
| 10 | +# Using the DSL |
| 11 | + |
| 12 | +## Defining a simple message command |
| 13 | + |
| 14 | +As a first example and to take a first look at the Kotlin DSL syntax, we will first create a simple command to send messages to a player. |
| 15 | + |
| 16 | +::::tip Example – Sending a message to a player using the Kotlin DSL |
| 17 | + |
| 18 | +We want to create a command that lets us send a message to a player. To do this, we want to register a command with the following syntax: |
| 19 | + |
| 20 | +```mccmd |
| 21 | +/sendmessageto <player> <msg> |
| 22 | +``` |
| 23 | + |
| 24 | +We can then use the following command registration: |
| 25 | + |
| 26 | +:::tabs key:dsl-usage-page |
| 27 | +===CommandTree |
| 28 | +<<< @/../reference-code/src/main/kotlin/kotlindsl/Usage.kt#dslTreeExample |
| 29 | +===CommandAPICommand |
| 30 | +<<< @/../reference-code/src/main/kotlin/kotlindsl/Usage.kt#dslExample |
| 31 | +::: |
| 32 | + |
| 33 | + |
| 34 | +Here you can see some interesting things: |
| 35 | + |
| 36 | +- You do not need to call the `.register()` method when using the DSL |
| 37 | +- You do not need to initialise any arguments |
| 38 | + |
| 39 | +:::: |
| 40 | + |
| 41 | +## Executors |
| 42 | + |
| 43 | +The Kotlin DSL also provides executors to execute your command. You've seen the `anyExecutor` in the example above. |
| 44 | + |
| 45 | +To find out, which DSL executor corresponds to "normal" executors, you can refer to the table below: |
| 46 | + |
| 47 | +| DSL normal executor | DSL resulting executor | DSL normal execution info | DSL resulting execution info | "normal" Executor | |
| 48 | +|---------------------------|------------------------------------|--------------------------------|-----------------------------------------|---------------------------| |
| 49 | +| `anyExecutor()` | `anyResultingExecutor()` | `anyExecutionInfo()` | `anyResultingExecutionInfo` | `executes()` | |
| 50 | +| `playerExecutor()` | `playerResultingExecutor()` | `playerExecutionInfo()` | `playerResultingExecutionInfo()` | `executesPlayer()` | |
| 51 | +| `entityExecutor()` | `entityResultingExecutor()` | `entityExecutionInfo()` | `entityResultingExecutionInfo()` | `executesEntity()` | |
| 52 | +| `consoleExecutor()` | `consoleResultingExecutor()` | `consoleExecutionInfo()` | `consoleResultingExecutionInfo()` | `executesConsole()` | |
| 53 | +| `commandBlockExecutor()` | `commandBlockResultingExecutor()` | `commandBlockExecutionInfo()` | `commandBlockResultingExecutionInfo()` | `executesCommandBlock()` | |
| 54 | +| `proxyExecutor()` | `proxyResultingExecutor()` | `proxyExecutionInfo()` | `proxyResultingExecutionInfo()` | `executesProxy()` | |
| 55 | +| `nativeExecutor()` | `nativeResultingExecutor()` | `nativeExecutionInfo()` | `nativeResultingExecutionInfo()` | `executesNative()` | |
| 56 | +| `remoteConsoleExecutor()` | `remoteConsoleResultingExecutor()` | `remoteConsoleExecutionInfo()` | `remoteConsoleResultingExecutionInfo()` | `executesRemoteConsole()` | |
| 57 | + |
| 58 | +## Arguments |
| 59 | + |
| 60 | +The DSL implements almost every argument with a method. You've seen the `playerArgument()` and the `greedyStringArgument()` method in the example at the top of this page. |
| 61 | + |
| 62 | +The way arguments are implemented is pretty straight forward: It's basically the argument class' name, but as a method. So if you wanted to use a `ItemStackArgument` in your command, you would use the `itemStackArgument()` method of the DSL. |
| 63 | + |
| 64 | +One thing to note is that the DSL also features every existing constructor. This means if you want to use an `IntegerArgument` with a minimum of `0` and a maximum of `10`, you normally would implement it like this: |
| 65 | + |
| 66 | +```java |
| 67 | +new IntegerArgument("integer", 0, 10) |
| 68 | +``` |
| 69 | + |
| 70 | +However, when using this DSL it is implemented like this: |
| 71 | + |
| 72 | +```kotlin |
| 73 | +integerArgument("integer", 0, 10) |
| 74 | +``` |
| 75 | + |
| 76 | +:::warning Developer's Note: |
| 77 | + |
| 78 | +There are a few arguments not having a method which directly corresponds to their respective argument. |
| 79 | + |
| 80 | +These arguments most likely use a builder pattern and because of that require further implementation by the user. |
| 81 | + |
| 82 | +To use these arguments, the DSL also provides the `argument()` method which takes in any argument as a parameter. |
| 83 | + |
| 84 | +::: |
| 85 | + |
| 86 | +## Editing arguments |
| 87 | + |
| 88 | +When using the DSL, you might want to modify the behaviour of certain arguments by adding requirements or suggestions to them. |
| 89 | + |
| 90 | +To give you a general idea how you could accomplish that, the `sendMessageTo` command is adding a broadcast option which should only be executed by server operators. |
| 91 | + |
| 92 | +:::tabs key:dsl-usage-page |
| 93 | +===CommandTree |
| 94 | +<<< @/../reference-code/src/main/kotlin/kotlindsl/Usage.kt#argumentRequirementsTreeExample |
| 95 | +===CommandAPICommand |
| 96 | +<<< @/../reference-code/src/main/kotlin/kotlindsl/Usage.kt#argumentRequirementsExample |
| 97 | +::: |
| 98 | + |
| 99 | +Notice how you can just add the requirement in a CommandTree by adding it to the argument block where you also define the next arguments and the executor. |
| 100 | + |
| 101 | +However, when modifying the behaviour of an argument in a CommandAPICommand you have to add an extra block where you can implement the additional behaviour. |
| 102 | + |
| 103 | +### Adding requirements to commands |
| 104 | + |
| 105 | +Expanding on the previous example where we added a requirement to a single argument, we now also want to add a requirement to a whole command. |
| 106 | + |
| 107 | +This works similar to how argument behaviour is modified in a CommandTree: |
| 108 | + |
| 109 | +:::tabs key:dsl-usage-page |
| 110 | +===CommandTree |
| 111 | +<<< @/../reference-code/src/main/kotlin/kotlindsl/Usage.kt#commandRequirementsTreeExample |
| 112 | +===CommandAPICommand |
| 113 | +<<< @/../reference-code/src/main/kotlin/kotlindsl/Usage.kt#commandRequirementsExample |
| 114 | +::: |
| 115 | + |
| 116 | +## More examples |
| 117 | + |
| 118 | +Now, a few more examples are shown to demonstrate the use of this DSL a little more: |
| 119 | + |
| 120 | +::::tip Example – Implementing optional arguments with the Kotlin DSL |
| 121 | + |
| 122 | +We want to create a `/give` command with the following syntax: |
| 123 | + |
| 124 | +```mccmd |
| 125 | +/optionalArgument give <item> |
| 126 | +/optionalArgument give <item> <amount> |
| 127 | +``` |
| 128 | + |
| 129 | +To declare an argument as optional you need to set the `optional` value to `true`: |
| 130 | + |
| 131 | +:::tabs key:dsl-usage-page |
| 132 | +===CommandTree |
| 133 | +<<< @/../reference-code/src/main/kotlin/kotlindsl/Usage.kt#optionalArgumentsTreeExample |
| 134 | +===CommandAPICommand |
| 135 | +<<< @/../reference-code/src/main/kotlin/kotlindsl/Usage.kt#optionalArgumentsExample |
| 136 | +::: |
| 137 | + |
| 138 | +:::: |
| 139 | + |
| 140 | +::::tip Example – Replacing suggestions using the Kotlin DSL |
| 141 | + |
| 142 | +We want to create a command with the following syntax to demonstrate replacing suggestions using the Kotlin DSL: |
| 143 | + |
| 144 | +```mccmd |
| 145 | +/replaceSuggestions <strings> |
| 146 | +``` |
| 147 | + |
| 148 | +Replacing suggestions works similar to how you would add a requirement to an argument as shown in [Editing arguments](#editing-arguments). |
| 149 | + |
| 150 | +You just have to use the `replaceSuggestions` method this time: |
| 151 | + |
| 152 | +:::tabs key:dsl-usage-page |
| 153 | +===CommandTree |
| 154 | +<<< @/../reference-code/src/main/kotlin/kotlindsl/Usage.kt#replaceSuggestionsTreeExample |
| 155 | +===CommandAPICommand |
| 156 | +<<< @/../reference-code/src/main/kotlin/kotlindsl/Usage.kt#replaceSuggestionsExample |
| 157 | +::: |
| 158 | + |
| 159 | +:::: |
0 commit comments