Package net.forthecrown.grenadier
Class AbstractCommand
java.lang.Object
net.forthecrown.grenadier.Nodes
net.forthecrown.grenadier.AbstractCommand
- All Implemented Interfaces:
Predicate<CommandSource>
,CommandTreeBuilder
public abstract class AbstractCommand
extends Nodes
implements CommandTreeBuilder, Predicate<CommandSource>
An abstract class that can be easily extended to create commands.
Use example:
public class HelloWorldCommand extends AbstractCommand {
public HelloWorldCommand() {
super("hello_world");
// Aliases, description and permission can be define here
setAliases("helloworld");
setDescription("Says hello world");
// Required for the command to be created
register();
}
@Override
public void createCommand(GrenadierCommand command) {
command.execute(context -> {
CommandSource source = context.getSource();
source.sendMessage("Hello, world!");
return 0;
});
}
}
You can also override the test(CommandSource)
method to change the
requirement for command sources that want to execute this command. Here's an
example that only allows a command to be executed in a world named
command_world
:
public class WorldLimitedCommand extends AbstractCommand {
public WorldLimitedCommand() {
super("world_limited");
register();
}
@Override
public boolean test(CommandSource source) {
World world = source.getWorld();
return world.getName().equals("command_world");
}
@Override
public void createCommand(GrenadierCommand command) {
// ...
}
}
Note: if you call setPermission(String)
, then the permission test
will always be run, regardless of if you've overridden
test(CommandSource)
or not- See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionfinal boolean
canUse
(CommandSource source) Tests if the specifiedsource
can use this command.final boolean
canUse
(CommandSender sender) Delegate forcanUse(CommandSource)
.final GrenadierCommandNode
Gets the built grenadier command node.getName()
Gets the command's name.final void
register()
Builds the command tree usingCommandTreeBuilder.createCommand(GrenadierCommand)
and then registers it.setAliases
(String... strings) setAliases
(Collection<String> strings) setDescription
(String description) setPermission
(String permission) setPermission
(Permission permission) boolean
test
(CommandSource source) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface net.forthecrown.grenadier.CommandTreeBuilder
createCommand
-
Constructor Details
-
AbstractCommand
Creates a new abstract command instance.Initializes the
getCommand()
by callingGrenadier.createCommand(String)
- Parameters:
name
- Command name
-
-
Method Details
-
getName
Gets the command's name.- Returns:
- Command name
-
register
public final void register()Builds the command tree usingCommandTreeBuilder.createCommand(GrenadierCommand)
and then registers it.If this has already been called then nothing happens
-
getBuiltNode
Gets the built grenadier command node.- Returns:
- Built node, or
null
, ifregister()
has not been called
-
canUse
Tests if the specifiedsource
can use this command.For this function to return
true
, the source must first pass a permission check, if a permission is set, and then pass thetest(CommandSource)
method, which can be overridden by subclasses of this class.- Parameters:
source
- Source to test- Returns:
true
if the source can use this command,false
otherwise
-
canUse
Delegate forcanUse(CommandSource)
. Calls the delegate method by converting the specifiedsender
to aCommandSource
by callingGrenadier.createSource(CommandSender, GrenadierCommandNode)
- Parameters:
sender
- Sender to test- Returns:
true
if the source can use this command,false
otherwise
-
test
- Specified by:
test
in interfacePredicate<CommandSource>
-
getCommand
-
getDescription
-
setDescription
-
getPermission
-
setPermission
-
setPermission
-
getAliases
-
setAliases
-
setAliases
-