Code Patterns

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

← Back to Patterns
ecs

Custom Component

Create custom ECS components with data and clone support.

Example Code

java
public class PoisonComponent implements Component<EntityStore> {

    private float damagePerTick;
    private float tickInterval;
    private int remainingTicks;
    private float elapsedTime;

    // Default constructor (required for registration)
    public PoisonComponent() {
        this(5f, 1.0f, 10);
    }

    public PoisonComponent(float damagePerTick, float tickInterval, int totalTicks) {
        this.damagePerTick = damagePerTick;
        this.tickInterval = tickInterval;
        this.remainingTicks = totalTicks;
        this.elapsedTime = 0f;
    }

    // Copy constructor (used by clone)
    public PoisonComponent(PoisonComponent other) {
        this.damagePerTick = other.damagePerTick;
        this.tickInterval = other.tickInterval;
        this.remainingTicks = other.remainingTicks;
        this.elapsedTime = other.elapsedTime;
    }

    @Nullable
    @Override
    public Component<EntityStore> clone() {
        return new PoisonComponent(this);
    }

    // Getters and setters...
    public float getDamagePerTick() { return damagePerTick; }
    public boolean isExpired() { return remainingTicks <= 0; }
}

Thread Safety

Components are data-only. Logic goes in Systems.

Common Mistakes

Missing default constructor. Missing clone() implementation. Adding logic to components.