Code Patterns

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

← Back to Patterns
ecs

Hud System

EntityTickingSystem implementation

Example Code

java
package dev.myplugin.example;

import com.hypixel.hytale.component.ArchetypeChunk;
import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.component.Holder;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.component.query.Query;
import com.hypixel.hytale.component.system.tick.EntityTickingSystem;
import com.hypixel.hytale.server.core.entity.EntityUtils;
import com.hypixel.hytale.server.core.entity.entities.Player;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
// import dev.myplugin.example.PartyPlugin;  // Anonymized
// import dev.myplugin.example.PartyHudProvider;  // Anonymized

import javax.annotation.Nonnull;

/**
 * A ticking system that updates party member HUDs for all players.
 * Runs each tick and updates the party indicators on screen.
 */
public class ExampleSystem extends EntityTickingSystem<EntityStore> {

    private final Query<EntityStore> query;

    public ExampleSystem() {
        this.query = Query.and(Player.getComponentType());
    }

    @Override
    @Nonnull
    public Query<EntityStore> getQuery() {
        return this.query;
    }

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

        Holder<EntityStore> holder = EntityUtils.toHolder(index, archetypeChunk);
        Player player = holder.getComponent(Player.getComponentType());
        PlayerRef playerRef = holder.getComponent(PlayerRef.getComponentType());

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

        PartyHudProvider provider = PartyPlugin.getInstance().getPartyHudProvider();
        if (provider != null) {
            provider.updateHud(dt, index, archetypeChunk, store, commandBuffer);
        }
    }
}