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
Async Command with GUI
Create async commands that open custom GUIs. Use CompletableFuture.runAsync with world executor.
Example Code
public class MenuCommand extends AbstractAsyncCommand {
public MenuCommand() {
super("menu", "Opens the menu GUI");
this.requirePermission("myplugin.menu");
}
@Override
protected CompletableFuture<Void> executeAsync(CommandContext context) {
CommandSender sender = context.sender();
if (sender instanceof Player player) {
Ref<EntityStore> ref = player.getReference();
if (ref != null && ref.isValid()) {
Store<EntityStore> store = ref.getStore();
World world = store.getExternalData().getWorld();
return CompletableFuture.runAsync(() -> {
PlayerRef playerRef = store.getComponent(ref, PlayerRef.getComponentType());
player.getPageManager().openCustomPage(
ref, store,
new MyMenuGui(playerRef, CustomPageLifetime.CanDismiss)
);
}, world); // Run on world executor
}
}
return CompletableFuture.completedFuture(null);
}
}
Thread Safety
Use CompletableFuture.runAsync(lambda, world) to run on world thread.
Common Mistakes
Opening GUI outside world executor. Not checking ref.isValid().