Code Patterns

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

← Back to Patterns
ecs

store-ref-pattern

The Store<EntityStore> and Ref<EntityStore> pattern is the core way to access entity components in Hytale ECS. Store is your accessor, Ref identifies which entity.

Example Code

java
// Store<EntityStore> - Your accessor to the ECS database
// Ref<EntityStore> - Identifies a specific entity

// Reading a component:
Player player = store.getComponent(ref, Player.getComponentType());

// Adding a component to an entity:
store.addComponent(ref, Teleport.getComponentType(), new Teleport(destination));

// Checking if entity has a component:
if (store.hasComponent(ref, HealthComponent.getComponentType())) {
    // Entity has health
}

// ComponentType<T> - Type descriptor for compile-time safety
// Get it via: SomeComponent.getComponentType()

// Full example in a command:
@Override
protected void execute(CommandContext context, Store<EntityStore> store,
                       Ref<EntityStore> ref, PlayerRef playerRef, World world) {
    // Get player component
    Player player = store.getComponent(ref, Player.getComponentType());

    // Get transform to read position
    TransformComponent transform = player.getTransformComponent();
    Vector3d pos = transform.getPosition();

    // Add a teleport component (will be processed by teleport system)
    Vector3d destination = new Vector3d(pos.x + 10, pos.y, pos.z);
    store.addComponent(ref, Teleport.getComponentType(),
        new Teleport(destination, Vector3f.ZERO, TeleportCause.COMMAND));
}

Thread Safety

Store operations are thread-safe when used within Systems. Outside of systems, use world.execute() for safe access.

Common Mistakes

Trying to store Store or Ref references - they are only valid within their execution context. Always get fresh references.