Time arguments
The TimeArgument
class represents in-game time, in the number of in-game ticks. This allows command senders to specify a certain number of ticks in a simpler way, by including the characters d
to specify the numbers of days, s
to specify the number of seconds or t
to specify a number of ticks.
The CommandAPI converts the inputs provided by the command sender into a number of ticks as an integer.
Note:
The TimeArgument
is only supported in Minecraft versions 1.14 and later, meaning it will not work on Minecraft versions 1.13, 1.13.1 or 1.13.2. This is due to the fact that Minecraft added the time argument in 1.14. Attempting to use the TimeArgument
on an incompatible version will throw a TimeArgumentException
.
Developer's Note:
The
TimeArgument
provides inputs such as2d
(2 in-game days),10s
(10 seconds) and20t
(20 ticks), but does not let you combine them, such as2d10s
.
Example - Displaying a server-wide announcement
Say we have a command bigmsg
that displays a title message to all players for a certain duration:
/bigmsg <duration> <message>
new CommandAPICommand("bigmsg")
.withArguments(new TimeArgument("duration"))
.withArguments(new GreedyStringArgument("message"))
.executes((sender, args) -> {
//Duration in ticks
int duration = (int) args[0];
String message = (String) args[1];
for(Player player : Bukkit.getOnlinePlayers()) {
//Display the message to all players, with the default fade in/out times (10 and 20).
player.sendTitle(message, "", 10, duration, 20);
}
})
.register();