Skip to content

Messages

Redempt edited this page Jan 7, 2021 · 3 revisions

Configurable messages are a common thing requested for plugins, but their implementation can be a hassle. Fortunately, RedLib's Messages helper makes it dead simple.

Messages serve a similar purpose to ConfigManager, but are more specialized. They don't use the YAML format, and will handle more things implicitly.

While the Messages format is similar to YAML, it is much more basic. Each line represents a message. No quotes are used, and there are no sub-paths, unlike in YAML. The resulting format looks like this:

noPermission: &cYou do not have perrmission to do that!
flightDisabled: &aFlight disabled!

Color codes are stored the same way most plugins accept them: Using & instead of the section symbol Minecraft uses. Messages files should be stored in the resources of a jar - If you are exporting manually, put it in the root project directory. If you are using a build system like Gradle or Maven, put it in the respective resources folder. Once you have your messages file - whose conventional name is messages.txt - you can simply load it with a single call.

@Override
public void onEnable() {
	Messages.load(this);
}

Calling Messages.load(Plugin) will assume that the resource exists within the jar and is named messages.txt. There are alternate calls available for if the messages are under a different name.

Once loaded, Messages will remember which messages belong to which plugin. If you later call Messages.msg("messageNameHere"), it will automatically determine the owning plugin of the calling class, and return the message for your plugin. Color codes are automatically converted when the message is retrieved.

When messages are loaded for the first time, they are copied to a file called messages.txt by default in your plugin's data folder. Every time they are loaded, Messages will ensure that all needed messages are present. This is useful for plugins which add new messages between versions, as any missing ones will simply be copied from the internal plugin resource to the file in the data folder. Messages in the file are prioritized, and ones from the internal resource will only replace them if they are absent.

Messages can also be integrated to make configurable help messages for your commands, as specified in your command file. To do this, simply pass the Messages to the constructor for CommandParser - new CommandParser(InputStream, Messages). If you do this, you may use the helpmsg tag to specify the name of a configurable message from your messages file rather than literal text. Example:

messages.txt:

helpExample: This is an example help message!

command.txt:

example {
	hook example
	helpmsg helpExample
}

The message shown in the help menu will be This is an example help message!, or whatever the user has configured it to be.

Clone this wiki locally