Code Patterns
Copy-paste examples for common plugin tasks. Commands, events, ECS, GUI, and more.
All
225
Command
60
Damage
2
Ecs
27
Entity
5
Gui
75
Interaction
2
Inventory
4
Moderation
3
Permission
2
Player
9
Plugin
4
Storage
2
Teleport
3
Ui
22
Util
5
← Back to Patterns
java
command
Command with Arguments
Create a command with required and optional arguments using ArgTypes.
Example Code
public class TeleportCommand extends AbstractPlayerCommand {
public TeleportCommand() {
super("tp", "Teleport to coordinates");
}
RequiredArg<Integer> xArg = this.withRequiredArg("x", "X coordinate", ArgTypes.INTEGER);
RequiredArg<Integer> yArg = this.withRequiredArg("y", "Y coordinate", ArgTypes.INTEGER);
RequiredArg<Integer> zArg = this.withRequiredArg("z", "Z coordinate", ArgTypes.INTEGER);
@Override
protected void execute(@Nonnull CommandContext context, @Nonnull Store<EntityStore> store,
@Nonnull Ref<EntityStore> ref, @Nonnull PlayerRef playerRef, @Nonnull World world) {
int x = xArg.get(context);
int y = yArg.get(context);
int z = zArg.get(context);
Player player = store.getComponent(ref, Player.getComponentType());
world.execute(() -> {
Teleport teleport = Teleport.createForPlayer(world, new Vector3d(x, y, z), new Vector3f(0, 0, 0));
store.addComponent(ref, Teleport.getComponentType(), teleport);
player.sendMessage(Message.raw("Teleported to " + x + ", " + y + ", " + z));
});
}
}
Thread Safety
AbstractPlayerCommand extends AbstractAsyncCommand - execute() runs off main thread. Use world.execute() for ECS modifications.
Common Mistakes
Forgetting world.execute() when modifying components. Not handling null arguments for optional args.