Skip to content

Command File

Redempt edited this page Nov 27, 2021 · 11 revisions

It's well-known that writing commands the long way is a repetitive, verbose, and error-prone task. This is largely due to the conflation of command metadata and structure with implementation. When these two are not separated, the code can become very messy.

The core idea of the RedCommands framework is keeping command structure and implementation separate. This means keeping the layout of your commands entirely separate from your code. Many other command libraries feature builder or annotation based command structures, but this still leaves the command structure intermingled with code.

By removing this metadata from Java code entirely and putting it in its own file in a custom format, you can achieve much more with far more clarity and far less space. The rdcml file format is how you will be specifying how your commands should be used, along with plenty of metadata.

The rdcml format has an intellij extension which provides syntax highlighting and error checking, which can be found at https://github.com/Shuaiouke/RedLib-Command-File or in the intellij marketplace under RedLib-Command-File.

The format of the command file is simple: You begin with a command's name, followed by curly braces, much like a code block.

commandname {
	// Command info here
}

Inside the braces, you will be adding more metadata. For example, commands typically require a certain permission to run them. Within these braces, we can use "tags" to specify certain things about the command. The first of these tags that you will learn is the permission tag:

helloworld {
	permission helloworld.use
}

Here, we have created a command /helloworld which requires the helloworld.use permission to be run. But there's a lot more properties of a command that are often needed and can be specified in the command file.

name1,name2 {
	permission example.use
	help An example command
	user player
}

In this example, a few new concepts have been introduced. Firstly, you can specify multiple names separated by commas when defining a command to create aliases. This command can be run using /name1 or /name2. It has a help message, which will be shown if the player uses it incorrectly or prompts for the help menu with /name1 help. If you want a command to not have this auto-generated help subcommand, you can use the nohelp tag in its body. This command also has the user player tag, so it can only be used by players, not console.

But commands rarely exist in this simplistic form. Frequently, you will need subcommands. To specify a subcommand, all you need to do is add another command within the same block, similarly to nesting code bodies:

base {
	help This is a base command!
	child {
		help This is a child command!
	}
}

This child command can now be run like /base child. Similarly, you can define multiple different base (or child) commands with subsequent blocks:

smite {
	help Smites you with a lightning bolt
}
spawn {
	help Teleports you to the spawn of the world
	user player
}
coins {
	info {
		help Get info about the coins system
	}
	balance {
		help Check your balance of coins
	}
}

This will create /smite, /spawn, /coins, /coins info, and /coins balance as commands.

Now, defining these commands is all well and good, but they won't actually do anything on their own. For them to be functional, we need to define a hook name and a method hook which will be called when the command is run. To do that, we first must define the hook tag:

smite {
	help Smites you
	hook smite
}

When registered, this command will now look for a method annotated with @CommandHook("smite") to run. To learn how to write and register command method hooks, read on to the Command Hooks page.

Clone this wiki locally