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 All Argument Types
Use various argument types: STRING, INTEGER, DOUBLE, BOOLEAN, PLAYER_REF, WORLD, ITEM_ASSET, BLOCK_TYPE_KEY, GAME_MODE, etc.
Example Code
public class GiveCommand extends AbstractPlayerCommand {
public GiveCommand() {
super("give", "Give items to a player");
this.requirePermission("server.give");
}
// Required arguments
RequiredArg<PlayerRef> targetArg = withRequiredArg("player", "target.player", ArgTypes.PLAYER_REF);
RequiredArg<ItemAsset> itemArg = withRequiredArg("item", "item.type", ArgTypes.ITEM_ASSET);
// Optional arguments
OptionalArg<Integer> amountArg = withOptionalArg("amount", "item.amount", ArgTypes.INTEGER);
@Override
protected void execute(CommandContext context, Store<EntityStore> store,
Ref<EntityStore> ref, PlayerRef playerRef, World world) {
PlayerRef target = targetArg.get(context);
ItemAsset item = itemArg.get(context);
Integer amount = amountArg.get(context); // May be null
int count = amount != null ? amount : 1;
// Give items...
context.sendMessage(Message.raw("Gave " + count + " items"));
}
}
// Available ArgTypes:
// ArgTypes.STRING - Text
// ArgTypes.INTEGER - Whole numbers
// ArgTypes.DOUBLE - Decimal numbers
// ArgTypes.BOOLEAN - true/false
// ArgTypes.PLAYER_REF - Online player
// ArgTypes.WORLD - World reference
// ArgTypes.ITEM_ASSET - Item type
// ArgTypes.BLOCK_TYPE_KEY - Block type
// ArgTypes.GAME_MODE - GameMode enum
// ArgTypes.RELATIVE_POSITION - ~x ~y ~z coordinates
// ArgTypes.ROTATION - Rotation values
// ArgTypes.ENTITY_ID - Entity network ID