1# Commands 2 3In general, you should prefer to implement Brigadier `/` commands, but there are some cases where some additional flexibility is required. 4 5## Creating commands 6 7Creating new commands is easy, you'll have to implement the `Command` interface, create a [MapCodec](https://forge.gemwire.uk/wiki/Codecs) to serialize/deserialize the command and register the codec with `CommandTypes.register()`. 8 9Let's create a simple command which will print a string to standard output. 10 11```java 12public record DummyCommand(String text) implements Command { 13 14 @Override 15 public boolean execute(EventContext context) { 16 System.out.println(text()); 17 return true; //Return if execution was successful. 18 } 19 20 @Override 21 public CommandType type() { 22 return null; 23 } 24} 25``` 26 27Now create a [Codec](https://forge.gemwire.uk/wiki/Codecs) for your command. 28 29```java 30public static final MapCodec<DummyCommand> CODEC = Codec.STRING.fieldOf("text").xmap(DummyCommand::new, DummyCommand::text); 31``` 32 33With that done, we'll have to register the command to get our `CommandType`. 34 35```java 36public static final CommandType DUMMY = CommandType.register(new Identifier("modid", "print"), DummyCommand.CODEC); 37``` 38 39Now return this type in `type()`. 40 41```java 42@Override 43public CommandType type() { 44 return MyModInit.DUMMY; 45} 46``` 47 48## EventContext 49 50EventContext allows you to retrieve the `LootContext` which is passed with the event type. 51 52```java 53context.lootContext().getWorld(); //Returns a ServerWorld. 54 55context.lootContext().get(LootContextParameters.TOOL); //Returns the prameter or null if not present. 56 57context.lootContext().requireParameter(LootContextParameters.TOOL); //Returns the parameter or throws an exception if not present. 58```