package terrails.statskeeper.forge.capability;

import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.common.capabilities.RegisterCapabilitiesEvent;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.event.AttachCapabilitiesEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import terrails.statskeeper.Constants;
import terrails.statskeeper.api.health.IHealthManager;
import terrails.statskeeper.feature.health.HealthManager;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

@Mod.EventBusSubscriber
public class HealthCapability {

    private static final ResourceLocation NAME = new ResourceLocation(Constants.MOD_ID, "health");

    @CapabilityInject(IHealthManager.class)
    public static final Capability<IHealthManager> CAPABILITY;

    static {
        CAPABILITY = null;
    }

    @SubscribeEvent
    public static void attachCapabilities(final AttachCapabilitiesEvent<Entity> event) {
        if (event.getObject() instanceof Player) {
            event.addCapability(NAME, new CapabilitySerializable<>(HealthCapability.CAPABILITY, new HealthManager()));
        }
    }

    private static class CapabilitySerializable<C extends IHealthManager> implements ICapabilitySerializable<CompoundTag> {

        private final Capability<C> capability;
        private final C instance;

        private CapabilitySerializable(Capability<C> capability, C instance) {
            this.capability = capability;
            this.instance = instance;
        }

        @Nonnull
        @Override
        public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> capability, @Nullable Direction side) {
            return this.capability.orEmpty(capability, LazyOptional.of(() -> this.instance));
        }

        @Override
        public CompoundTag serializeNBT() {
            CompoundTag tag = new CompoundTag();
            this.instance.serialize(tag);
            return tag;
        }

        @Override
        public void deserializeNBT(CompoundTag nbt) {
            this.instance.deserialize(nbt);
        }
    }
}

package terrails.statskeeper.forge.capability;

import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.common.capabilities.*;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.event.AttachCapabilitiesEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import org.jetbrains.annotations.NotNull;
import terrails.statskeeper.Constants;
import terrails.statskeeper.api.health.IHealthManager;
import terrails.statskeeper.feature.health.HealthManager;

import javax.annotation.Nullable;

@Mod.EventBusSubscriber
public class HealthCapability {

    private static final ResourceLocation NAME = new ResourceLocation(Constants.MOD_ID, "health");
    public static final Capability<IHealthManager> CAPABILITY = CapabilityManager.get(new CapabilityToken<>(){});

    @SubscribeEvent
    public static void attachCapabilities(final AttachCapabilitiesEvent<Entity> event) {
        if (event.getObject() instanceof Player) {
            event.addCapability(NAME, new ICapabilitySerializable<CompoundTag>() {

                private final HealthManager instance = new HealthManager();

                @NotNull
                @Override
                public <T> LazyOptional<T> getCapability(@NotNull Capability<T> capability, @Nullable Direction arg) {
                    return HealthCapability.CAPABILITY.orEmpty(capability, LazyOptional.of(() -> this.instance));
                }

                @Override
                public CompoundTag serializeNBT() {
                    CompoundTag tag = new CompoundTag();
                    this.instance.serialize(tag);
                    return tag;
                }

                @Override
                public void deserializeNBT(CompoundTag nbt) {
                    this.instance.deserialize(nbt);
                }
            });
        }
    }
}

Use CapabilityToken for getting the Capability instance