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: