Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/lucko/commodore

Utility for using Minecraft's 1.13 'brigadier' library in Bukkit plugins.
https://github.com/lucko/commodore

Last synced: 14 days ago
JSON representation

Utility for using Minecraft's 1.13 'brigadier' library in Bukkit plugins.

Awesome Lists containing this project

README

        

# commodore [![Javadocs](https://javadoc.io/badge/me.lucko/commodore.svg)](https://javadoc.io/doc/me.lucko/commodore) [![Maven Central](https://img.shields.io/maven-metadata/v/https/repo1.maven.org/maven2/me/lucko/commodore/maven-metadata.xml.svg?label=maven%20central&colorB=brightgreen)](https://search.maven.org/artifact/me.lucko/commodore)

commodore is a utility for using Minecraft's 1.13 [brigadier](https://github.com/Mojang/brigadier) library in Bukkit plugins. It allows you to easily register command completion data for your plugin's commands.

If you have questions, feel free to ask in [Discord](https://discord.gg/AEqagwA).

#### Example
![](https://i.imgur.com/4VElTNG.png)
![](https://i.imgur.com/o3AcyVY.png)

___

### Approaches to registering completion data

commodore supports:
* Registering completions using brigadier's `LiteralCommandNode` builder API
* Registering completions using commodore's `.commodore` file format

For example, implementing completions for [Minecraft's `/time` command](https://minecraft.gamepedia.com/Commands/time):

#### Using brigadier's `LiteralCommandNode` builder API
```java
LiteralCommandNode> timeCommand = LiteralArgumentBuilder.literal("time")
.then(LiteralArgumentBuilder.literal("set")
.then(LiteralArgumentBuilder.literal("day"))
.then(LiteralArgumentBuilder.literal("noon"))
.then(LiteralArgumentBuilder.literal("night"))
.then(LiteralArgumentBuilder.literal("midnight"))
.then(RequiredArgumentBuilder.argument("time", IntegerArgumentType.integer())))
.then(LiteralArgumentBuilder.literal("add")
.then(RequiredArgumentBuilder.argument("time", IntegerArgumentType.integer())))
.then(LiteralArgumentBuilder.literal("query")
.then(LiteralArgumentBuilder.literal("daytime"))
.then(LiteralArgumentBuilder.literal("gametime"))
.then(LiteralArgumentBuilder.literal("day"))
).build();

commodore.register(bukkitCommand, timeCommand);
```

#### Using commodore's `.commodore` file format
```
time {
set {
day;
noon;
night;
midnight;
time brigadier:integer;
}
add {
time brigadier:integer;
}
query {
daytime;
gametime;
day;
}
}
```
```java
// assuming the file above is stored as "time.commodore" in the plugin jar
LiteralCommandNode> timeCommand = CommodoreFileFormat.parse(plugin.getResource("time.commodore"));
commodore.register(bukkitCommand, timeCommand);
```

Using the `.commodore` file format is recommended. In my opinion it is much easier to read/understand/update than the Node Builder API provided by brigadier.

Another example of a `.commodore` file can be found [here](https://github.com/lucko/LuckPerms/blob/master/bukkit/src/main/resources/luckperms.commodore), for the [LuckPerms](https://luckperms.net/) plugin commands. The corresponding code used to register the completions is [here](https://github.com/lucko/LuckPerms/blob/master/bukkit/src/main/java/me/lucko/luckperms/bukkit/brigadier/LuckPermsBrigadier.java).

## Usage

This guide assumes your plugin is built using Maven or Gradle though.

#### 1) Configure your build script to shade commodore into your plugin jar

Maven

You need to add (or merge) the following sections into your `pom.xml` file.
```xml



org.apache.maven.plugins
maven-shade-plugin
3.2.1


package

shade




me.lucko:commodore




me.lucko.commodore

com.yourdomain.yourplugin.commodore







me.lucko
commodore

{version}
compile


minecraft-repo
https://libraries.minecraft.net/

```

Gradle

You need to add (or merge) the following sections into your `build.gradle` file.
```gradle
plugins {
id 'com.github.johnrengelman.shadow' version '6.1.0'
}

repositories {
mavenCentral()
maven { url 'https://libraries.minecraft.net/' }
}

dependencies {
/* vvv Replace with the latest commodore version vvv */
implementation 'me.lucko:commodore:{version}'
}

shadowJar {
dependencies {
exclude(dependency('com.mojang:brigadier'))
}

/* vvv Replace with the package of your plugin vvv */
relocate 'me.lucko.commodore', 'com.yourdomain.yourplugin.commodore'
}
```

Replace `{version}` with the latest version: [![latest version](https://img.shields.io/maven-metadata/v/https/repo1.maven.org/maven2/me/lucko/commodore/maven-metadata.xml.svg?label=latest%20version&colorB=brightgreen)](https://search.maven.org/artifact/me.lucko/commodore)

#### 2) Setup commodore in your plugin

```java
package me.lucko.example;

import com.mojang.brigadier.arguments.BoolArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;

import me.lucko.commodore.Commodore;
import me.lucko.commodore.CommodoreProvider;

import org.bukkit.command.PluginCommand;
import org.bukkit.plugin.java.JavaPlugin;

public class TestPlugin extends JavaPlugin {

@Override
public void onEnable() {

// register your command executor as normal.
PluginCommand command = getCommand("mycommand");
command.setExecutor(new MyCommandExecutor());

// check if brigadier is supported
if (CommodoreProvider.isSupported()) {

// get a commodore instance
Commodore commodore = CommodoreProvider.getCommodore(this);

// register your completions.
registerCompletions(commodore, command);
}
}

// You will need to put this method inside another class to prevent classloading
// errors when your plugin loads on pre 1.13 versions.
private static void registerCompletions(Commodore commodore, PluginCommand command) {
commodore.register(command, LiteralArgumentBuilder.literal("mycommand")
.then(RequiredArgumentBuilder.argument("some-argument", StringArgumentType.string()))
.then(RequiredArgumentBuilder.argument("some-other-argument", BoolArgumentType.bool()))
);
}
}
```

The `com.mojang.brigadier` packages will be automatically imported into your classpath when you add the commodore dependency, but they should not be shaded into your plugins jar file.