Custom Sources
Custom Sources
In Imperat, a command source represents the entity that initiates a command execution.
By default, Imperat provides a command source for the platform you are using (for example, BukkitCommandSource for Bukkit/Spigot).
However, you can also create and use your own custom command sources that wraps around these platform-specific sources,
allowing you to add additional functionality or context to the command source.
Creating a Custom Command Source
To create a custom command source, you need to define a class that implements the CommandSource interface.
public class CustomCommandCommandSource implements CommandSource {
private final BukkitCommandSource platformSource;
public CustomCommandCommandSource(BukkitCommandSource platformSource) {
this.platformSource = platformSource;
}
public void greet() {
reply("Hello Sir!");
}
@Override
public String name() {
return platformSource.name();
}
@Override
public Object origin() {
return platformSource.origin();
}
@Override
public void reply(String message) {
platformSource.reply(message);
}
@Override
public void warn(String message) {
platformSource.warn(message);
}
@Override
public void error(String message) {
platformSource.error(message);
}
@Override
public boolean isConsole() {
return platformSource.isConsole();
}
}
Registering a SourceProvider for the Custom Command Source
Then, you have to register a SourceProvider for that CustomCommandSource in your PLATFORMIMPERAT builder,
which defines how to create an instance of the custom source from the platform-specific source.
PLATFORMIMPERAT imperat = PLATFORMIMPERAT.builder()
.sourceProvider(CustomCommandSource.class, (source, context) -> new CustomCommandSource(source))
.build();
Usage example
and now you can use CustomCommandSource as a parameter in your pathway methods , it shall represent the command source,
and Imperat will automatically create an instance of it using the registered SourceProvider when executing the command.
@RootCommand("greet")
public class GreetCommand {
@Execute
public void greet(CustomCommandSource source) {
source.greet(); // This will send "Hello Sir!" to the command source.
}
}