Code Patterns

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

← Back to Patterns
ecs

Exploration Component

Custom EntityStore component

Example Code

java
package dev.myplugin.example;

import com.hypixel.hytale.codec.KeyedCodec;
import com.hypixel.hytale.codec.builder.BuilderCodec;
import com.hypixel.hytale.codec.codecs.array.LongArrayCodec;
import com.hypixel.hytale.component.Component;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
import it.unimi.dsi.fastutil.longs.LongSet;

import javax.annotation.Nonnull;

/**
 * Component that stores the exploration data for a player.
 * It tracks which chunks have been explored.
 */
public class ExampleClass implements Component<EntityStore> {

    /**
     * Codec for serializing and deserializing the ExampleClass.
     */
    public static final BuilderCodec<ExampleClass> CODEC = BuilderCodec.builder(ExampleClass.class, ExampleClass::new)
            .append(
                    new KeyedCodec<>("ExploredChunks", new LongArrayCodec()),
                    (component, chunks) -> {
                        if (chunks != null) {
                            component.exploredChunks = new LongOpenHashSet(chunks);
                        } else {
                            component.exploredChunks = new LongOpenHashSet();
                        }
                    },
                    component -> component.exploredChunks.toLongArray()
            )
            .add()
            .build();

    private LongSet exploredChunks = new LongOpenHashSet();

    /**
     * Constructs a new ExampleClass.
     */
    public ExampleClass() {
    }

    /**
     * Gets the set of explored chunk indices.
     *
     * @return The set of explored chunk indices.
     */
    public LongSet getExploredChunks() {
        return exploredChunks;
    }

    /**
     * Marks a chunk as explored.
     *
     * @param chunkIndex The index of the chunk to mark as explored.
     */
    public void addExploredChunk(long chunkIndex) {
        exploredChunks.add(chunkIndex);
    }

    /**
     * Checks if a chunk is explored.
     *
     * @param chunkIndex The index of the chunk.
     * @return True if the chunk is explored, false otherwise.
     */
    public boolean isExplored(long chunkIndex) {
        return exploredChunks.contains(chunkIndex);
    }

    /**
     * Creates a clone of this component.
     *
     * @return A deep copy of the component.
     */
    @Nonnull
    @Override
    public Component<EntityStore> clone() {
        ExampleClass clone = new ExampleClass();
        clone.exploredChunks = new LongOpenHashSet(this.exploredChunks);
        return clone;
    }
}