package top.r3944realms.lib39.core.sync;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.Identifier;
import org.jetbrains.annotations.NotNull;

/**
 * The type Nbt entity sync data.
 */
public abstract class NBTEntitySyncData implements IEntity, ISyncData<NBTEntitySyncData>, INBTSerializable<CompoundTag>, IUpdate {
    /**
     * The Dirty.
     */
    protected boolean dirty;
    /**
     * The Id.
     */
    protected final Identifier id;

    /**
     * Instantiates a new Nbt sync data.
     *
     * @param id the id
     */
    protected NBTEntitySyncData(Identifier id) {
        this.id = id;
    }

    @Override
    public Identifier id() {
        return id;
    }

    @Override
    public boolean isDirty() {
        return dirty;
    }

    @Override
    public void setDirty(boolean dirty) {
        this.dirty = dirty;
    }

    @Override
    public void copyFrom(@NotNull NBTEntitySyncData src) {
        this.dirty = src.isDirty();
    }

    @Override
    public void checkIfDirtyThenUpdate() {
        if (isDirty()) {
            update();
        }
        dirty = false;
    }

    @Override
    public NBTEntitySyncData getSyncData() {
        return this;
    }
}
