Code Patterns

Copy-paste examples for common plugin tasks. Commands, events, ECS, GUI, and more.

← Back to Patterns
ecs

Entity Creation

Create new entities using Holder blueprint pattern. Collect all components first, then add to store.

Example Code

java
// Create entity blueprint
Holder<EntityStore> holder = EntityStore.REGISTRY.newHolder();

// Add required components
holder.addComponent(TransformComponent.getComponentType(),
    new TransformComponent(position, rotation));
holder.addComponent(ModelComponent.getComponentType(),
    new ModelComponent(model));
holder.addComponent(BoundingBox.getComponentType(),
    new BoundingBox(model.getBoundingBox()));
holder.addComponent(NetworkId.getComponentType(),
    new NetworkId(store.getExternalData().takeNextNetworkId()));

// Ensure required marker components
holder.ensureComponent(UUIDComponent.getComponentType());
holder.ensureComponent(Interactable.getComponentType());

// Spawn entity in world (must be on world thread)
world.execute(() -> {
    Ref<EntityStore> entity = store.addEntity(holder, AddReason.SPAWN);
});