package net.miarma.hystuff.component;

import com.hypixel.hytale.codec.Codec;
import com.hypixel.hytale.codec.KeyedCodec;
import com.hypixel.hytale.codec.builder.BuilderCodec;
import com.hypixel.hytale.component.Component;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import org.checkerframework.checker.nullness.compatqual.NullableDecl;

public class PlaytimeComponent implements Component<EntityStore> {
    public static final BuilderCodec<PlaytimeComponent> CODEC;

    private long playtime;

    public PlaytimeComponent() {
        this.playtime = 0L;
    }

    public PlaytimeComponent(long playtime) {
        this.playtime = playtime;
    }

    public void tick(float dt) {
        playtime += dt;
    }

    public long getPlaytime() {
        return playtime;
    }

    public void setPlaytime(long playtime) {
        this.playtime = playtime;
    }

    @NullableDecl
    @Override
    public Component<EntityStore> clone() {
        return new PlaytimeComponent(this.getPlaytime());
    }

    static {
        CODEC = BuilderCodec.builder(PlaytimeComponent.class, PlaytimeComponent::new)
            .append(
                new KeyedCodec<>("Playtime", Codec.LONG),
                    PlaytimeComponent::setPlaytime,
                    PlaytimeComponent::getPlaytime
            ).add()
            .build();
    }
}
