Code Patterns

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

← Back to Patterns
ecs

entity-ticking-system-pattern

EntityTickingSystem processes entities every tick (frame). It receives an index into an ArchetypeChunk for efficient iteration. Use for per-entity per-frame logic like movement, AI, or HUD updates.

Example Code

java
import com.hypixel.hytale.component.ArchetypeChunk;
import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.server.core.ecs.system.EntityTickingSystem;
import com.hypixel.hytale.server.core.entity.entities.Player;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import javax.annotation.Nonnull;

public class MyTickingSystem implements EntityTickingSystem<EntityStore> {

    @Override
    public void processTick(float deltaTime, int index,
                           @Nonnull ArchetypeChunk<EntityStore> archetypeChunk,
                           @Nonnull Store<EntityStore> store,
                           @Nonnull CommandBuffer<EntityStore> commandBuffer) {

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

        // Get components from the holder
        Player player = holder.getComponent(Player.getComponentType());
        if (player == null) return;

        // Get entity reference for component operations
        Ref<EntityStore> ref = archetypeChunk.getReferenceTo(index);

        // Your per-tick logic here
        // deltaTime is time since last tick in seconds
    }

    @Override
    public Class<EntityStore> storeType() {
        return EntityStore.class;
    }
}

// Register in plugin setup():
this.getEntityStoreRegistry().registerSystem(new MyTickingSystem());

Thread Safety

EntityTickingSystem runs on the server tick thread - component access is safe without world.execute().

Common Mistakes

Using holder.getRef() which does not exist - use archetypeChunk.getReferenceTo(index) instead. Forgetting to check for null components.