|
| 1 | +--- |
| 2 | +order: 1 |
| 3 | +authors: |
| 4 | + - JorelAli |
| 5 | + - DerEchtePilz |
| 6 | + - willkroboth |
| 7 | +--- |
| 8 | + |
| 9 | +# Command conversion |
| 10 | + |
| 11 | +:::info |
| 12 | + |
| 13 | +If you're a server owner, you're probably lost! This section is for developer command conversion. If you're looking for how to convert plugins with the `config.yml` file, you want [Configuration for server owners](../user-setup/config#plugins-to-convert). |
| 14 | + |
| 15 | +::: |
| 16 | + |
| 17 | +The CommandAPI has the ability to convert plugin commands to vanilla Minecraft commands using its `config.yml`'s `plugins-to-convert` option. Nevertheless, the API for command conversion is not hidden and you're free to use it as you see fit! |
| 18 | + |
| 19 | +Before you continue, let's clear up a few naming conventions which is used in the following sections! |
| 20 | + |
| 21 | +- **Target plugin** - This refers to a non-CommandAPI plugin which registers normal Bukkit commands. This typically uses the old `boolean onCommand(CommandSender ... )` method |
| 22 | +- **Your plugin** - This refers to your plugin, the one that uses the CommandAPI and wants to add compatibility to a target plugin |
| 23 | + |
| 24 | +## Entire plugins |
| 25 | + |
| 26 | +To register all commands that are declared by a target plugin, the `Converter.convert(Plugin)` method can be used. This attempts to register all commands declared in a target plugin's `plugin.yml` file, as well as any aliases or permissions stated in the `plugin.yml` file. |
| 27 | + |
| 28 | +::::tip Example – Converting commands for a target plugin |
| 29 | + |
| 30 | +Say you have some `plugin.yml` file for a target plugin that adds some basic functionality to a server. The target plugin in this example is called "TargetPlugin": |
| 31 | + |
| 32 | +```yaml |
| 33 | +name: TargetPlugin |
| 34 | +main: some.random.package.Main |
| 35 | +version: 1.0 |
| 36 | +commands: |
| 37 | + gmc: |
| 38 | + aliases: gm1 |
| 39 | + gms: |
| 40 | + i: |
| 41 | + permission: item.permission |
| 42 | +``` |
| 43 | +
|
| 44 | +As you can see, it declares 3 commands: `/gmc`, `/gms` and `/i`. We can now begin writing your plugin that uses the CommandAPI converter. We will call this plugin "YourPlugin": |
| 45 | + |
| 46 | +:::tabs |
| 47 | +===Java |
| 48 | +<<< @/../reference-code/src/main/java/utils/Conversion.java#simpleConvertExample |
| 49 | +===Kotlin |
| 50 | +<<< @/../reference-code/src/main/kotlin/utils/Conversion.kt#simpleConvertExample |
| 51 | +::: |
| 52 | + |
| 53 | +When this is run, the commands `/gmc`, `/gm1`, `/gms` and `/i` will all be registered by the CommandAPI. |
| 54 | + |
| 55 | +:::: |
| 56 | + |
| 57 | +## Only specific commands |
| 58 | + |
| 59 | +In addition to converting all commands from a target plugin, the CommandAPI allows you to convert single commands at a time using the following methods from the `Converter` class: |
| 60 | + |
| 61 | +```java |
| 62 | +public static convert(Plugin plugin, String cmdName); |
| 63 | +public static convert(Plugin plugin, String cmdName, List<Argument> arguments); |
| 64 | +public static convert(Plugin plugin, String cmdName, Argument... arguments); |
| 65 | +``` |
| 66 | + |
| 67 | +In these commands, the `plugin` refers to the plugin which has the command you want to convert and `cmdName` is the name of the command declared in the target plugin's `plugin.yml` file (just the main command, not the aliases!). |
| 68 | + |
| 69 | +The `List<Argument>` or `Argument...` can be used to provide argument checks that lets you apply the command UI to a bukkit command. |
| 70 | + |
| 71 | +::::tip Example – Converting EssentialsX's speed command |
| 72 | + |
| 73 | +Say we want to convert EssentialsX's `/speed` command using the CommandAPI. The `plugin.yml` entry for the `/speed` command is the following: |
| 74 | + |
| 75 | +```yaml |
| 76 | + speed: |
| 77 | + description: Change your speed limits. |
| 78 | + usage: /<command> [type] <speed> [player] |
| 79 | + aliases: [flyspeed,eflyspeed,fspeed,efspeed,espeed,walkspeed,ewalkspeed,wspeed,ewspeed] |
| 80 | +``` |
| 81 | + |
| 82 | +From this, we can determine that there are the following commands, where "walk" and "fly" are the different types that the command can take: |
| 83 | + |
| 84 | +```mccmd |
| 85 | +/speed <speed> |
| 86 | +/speed <speed> <target> |
| 87 | +/speed <walk/fly> <speed> |
| 88 | +/speed <walk/fly> <speed> <target> |
| 89 | +``` |
| 90 | + |
| 91 | +With the EssentialsX plugin, the `<speed>` value can only take numbers between 0 and 10. As such, we'll ensure to apply these limits using the `IntegerArgument`. In addition, since the speed type can only be "walk" or "fly", we'll add that to our converter as well using a `MultiLiteralArgument`: |
| 92 | + |
| 93 | +:::tabs |
| 94 | +===Java |
| 95 | +<<< @/../reference-code/src/main/java/utils/Conversion.java#convertSpeedCommandExample |
| 96 | +===Kotlin |
| 97 | +<<< @/../reference-code/src/main/kotlin/utils/Conversion.kt#convertSpeedCommandExample |
| 98 | +::: |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | +:::: |
0 commit comments