Code Patterns

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

← Back to Patterns
ecs

Exploration Player Setup System

RefSystem implementation

Example Code

java
package dev.myplugin.example;

import com.hypixel.hytale.component.*;
import com.hypixel.hytale.component.query.Query;
import com.hypixel.hytale.component.system.RefSystem;
import com.hypixel.hytale.server.core.entity.entities.Player;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
// import dev.myplugin.example.BetterMap;  // Anonymized
// import dev.myplugin.example.ExplorationComponent;  // Anonymized

import javax.annotation.Nonnull;

/**
 * specific System that attaches the ExplorationComponent to players.
 */
public class ExampleSystem extends RefSystem<EntityStore> {

    @Nonnull
    private final ComponentType<EntityStore, ExplorationComponent> explorationComponentType;
    @Nonnull
    private final Query<EntityStore> query;

    /**
     * Constructs the setup system.
     */
    public ExampleSystem() {
        this.explorationComponentType = BetterMap.get().getExplorationComponentType();
        ComponentType<EntityStore, Player> playerComponentType = Player.getComponentType();
        this.query = Query.and(playerComponentType);
    }

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

    /**
     * Called when a player entity is added. Ensures they have the ExplorationComponent.
     *
     * @param ref           The entity reference.
     * @param reason        Reason for addition.
     * @param store         The entity store.
     * @param commandBuffer The command buffer.
     */
    @Override
    public void onEntityAdded(
            @Nonnull Ref<EntityStore> ref,
            @Nonnull AddReason reason,
            @Nonnull Store<EntityStore> store,
            @Nonnull CommandBuffer<EntityStore> commandBuffer
    ) {
        commandBuffer.ensureComponent(ref, this.explorationComponentType);
    }

    @Override
    public void onEntityRemove(
            @Nonnull Ref<EntityStore> ref,
            @Nonnull RemoveReason reason,
            @Nonnull Store<EntityStore> store,
            @Nonnull CommandBuffer<EntityStore> commandBuffer
    ) {
    }
}