CommandMessage

This is a guide to learn how to get easily your command messages.

What is CommandMessage ?

CommandMessage is a class which allows you to get your command messages at a specific path from a json file. This class is very useful to centralize all your command messages in a single file without using a config file.

How to create CommandMessage ?

At this point, you should have something like this in your command resource file.

Remember that my command's name is item so I will write the following codes into the command_item.json file.

{
  "item": {}
}

To create a CommandMessage, write the following code into the item section:

"commandMessage": {}

So you should have something like this:

{
  "item": {
    "commandMessage": {}
  }
}

How to use CommandMessage ?

You've created your CommandMessage. Now, let's use it. First of all, to get a message stored in the commandMessage section in the json file, you must retrieve the getCommandMessage() method from the SmartCommand class.

This is an example in the onCommand() method:

CommandMessage commandMessage = command.getCommandMessage();

Let's see the available methods of this class:

Method

Type

Description

getString(String path)

String

Get a String at the specified path

getByte(String path)

byte

Get a byte at the specified path

getShort(String path)

short

Get a short at the specified path

getInt(String path)

int

Get an int at the specified path

getDouble(String path)

double

Get a double at the specified path

getFloat(String path)

float

Get a float at the specified path

getLong(String path)

long

Get a long at the specified path

getList<?>(String path)

List<?>

Get a List<?> at the specified path

getEasyComponent(String path)

EasyComponent

Get an EasyComponent at the specified path

getTextComponent(String path)

TextComponent

Get a TextComponent at the specified path

get(String path)

JsonElement

Get a JsonElement at the specified path

How it works ?

To get our command messages, we need to specify an access path. Indeed, a JsonObject stores values from keys so we will use them to create a path that points to our values.

Example 1

To get the value "It's a test", which is directly in the commandMessage object, the path is "test".

"commandMessage": {
    "test": "It's a test !"
}

So the code to retrieve this message is:

String message = commandMessage.getString("test");

Example 2

Here, we have two values which are in a sub object of commandMessage. So to get them, we have to specify the sub object and the key where our values is stored.

For example, the path of "The first message" value if "messages.firstMessage" and that of "Another message" is "messages.anotherMessage".

"commandMessage": {
    "messages": {
        "firstMessage": "The first message"
        "anotherMessage": "Another message"
    }
}

Let's see the code to retrieve these two messages:

String firstMessage = commandMessage.getString("messages.firstMessage");
String anotherMessage = commandMessage.getString("messages.anotherMessage");

Last updated