Code Patterns

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

← Back to Patterns
ecs

holder-pattern

Holder<EntityStore> wraps an entity reference with convenient component access methods. Created from ArchetypeChunk index using EntityUtils.toHolder().

Example Code

java
import com.hypixel.hytale.component.Holder;
import com.hypixel.hytale.component.ArchetypeChunk;
import com.hypixel.hytale.server.core.entity.EntityUtils;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;

// In an EntityTickingSystem:
@Override
public void processTick(float deltaTime, int index,
                       ArchetypeChunk<EntityStore> archetypeChunk,
                       Store<EntityStore> store,
                       CommandBuffer<EntityStore> commandBuffer) {

    // Convert index to Holder for convenient access
    Holder<EntityStore> holder = EntityUtils.toHolder(index, archetypeChunk);

    // Get components directly from holder
    Player player = holder.getComponent(Player.getComponentType());
    PlayerRef playerRef = holder.getComponent(PlayerRef.getComponentType());

    if (player == null || playerRef == null) return;

    // To get the Ref for store operations, use archetypeChunk:
    Ref<EntityStore> ref = archetypeChunk.getReferenceTo(index);

    // Now you can use store.addComponent(ref, ...) etc.
}

// Holder provides:
// - getComponent(ComponentType<T>) - Get a component
// - hasComponent(ComponentType<T>) - Check if component exists

// NOTE: Holder does NOT have getRef()!
// Use archetypeChunk.getReferenceTo(index) to get the Ref

Thread Safety

Holder is only valid within the current tick context.

Common Mistakes

Calling holder.getRef() which does not exist. Use archetypeChunk.getReferenceTo(index) instead.