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);
        }
    }
}

@@ -293,288 +293,22 @@
 mon.
-MinecraftForge;%0Aimport net.minecraftforge.common.capabilities.Capability;%0Aimport net.minecraftforge.common.capabilities.CapabilityInject;%0Aimport net.minecraftforge.common.capabilities.ICapabilitySerializable;%0Aimport net.minecraftforge.common.capabilities.RegisterCapabilitiesEvent
+capabilities.*
 ;%0Aim
@@ -522,66 +522,41 @@
 ort 
-net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext
+org.jetbrains.annotations.NotNull
 ;%0Aim
@@ -710,41 +710,8 @@
 r;%0A%0A
-import javax.annotation.Nonnull;%0A
 impo
@@ -900,53 +900,8 @@
 h%22);
-%0A%0A    @CapabilityInject(IHealthManager.class)
 %0A   
@@ -962,56 +962,60 @@
 LITY
-;%0A%0A    static %7B%0A        CAPABILITY = null;%0A    %7D
+ = CapabilityManager.get(new CapabilityToken%3C%3E()%7B%7D);
 %0A%0A  
@@ -1218,187 +1218,8 @@
 new 
-CapabilitySerializable%3C%3E(HealthCapability.CAPABILITY, new HealthManager()));%0A        %7D%0A    %7D%0A%0A    private static class CapabilitySerializable%3CC extends IHealthManager%3E implements 
 ICap
@@ -1250,16 +1250,18 @@
 oundTag%3E
+()
  %7B%0A%0A    
@@ -1268,241 +1268,76 @@
     
-private final Capability%3CC%3E capability;%0A        private final C instance;%0A%0A        private CapabilitySerializable(Capability%3CC%3E capability, C instance) %7B%0A            this.capability = capability;%0A            this.instance = instance;
+        private final HealthManager instance = new HealthManager();%0A
 %0A   
@@ -1333,35 +1333,32 @@
 ger();%0A%0A        
-%7D%0A%0A
         @Nonnull
@@ -1352,18 +1352,18 @@
      @No
-nn
+tN
 ull%0A    
@@ -1358,32 +1358,40 @@
 NotNull%0A        
+    
+    
 @Override%0A      
@@ -1388,24 +1388,32 @@
 ide%0A        
+     
+   
 public %3CT%3E L
@@ -1444,18 +1444,18 @@
 lity(@No
-nn
+tN
 ull Capa
@@ -1500,12 +1500,11 @@
 ion 
-side
+arg
 ) %7B%0A
@@ -1519,21 +1519,30 @@
     
+   
+     
 return 
-this.c
+HealthC
 apab
@@ -1547,16 +1547,27 @@
 ability.
+CAPABILITY.
 orEmpty(
@@ -1617,16 +1617,24 @@
 ance));%0A
+        
         
@@ -1636,32 +1636,40 @@
      %7D%0A%0A        
+  
+      
 @Override%0A      
@@ -1654,32 +1654,40 @@
       @Override%0A
+        
         public C
@@ -1726,16 +1726,24 @@
         
+    
+    
 Compound
@@ -1775,32 +1775,40 @@
 ();%0A            
+        
 this.instance.se
@@ -1829,24 +1829,32 @@
             
+     
+   
 return tag;%0A
@@ -1853,32 +1853,40 @@
 rn tag;%0A        
+      
+  
 %7D%0A%0A        @Over
@@ -1872,32 +1872,40 @@
      %7D%0A%0A        
+       
+ 
 @Override%0A      
@@ -1890,32 +1890,40 @@
       @Override%0A
+        
         public v
@@ -1956,24 +1956,32 @@
 dTag nbt) %7B%0A
+        
             
@@ -2012,16 +2012,50 @@
 e(nbt);%0A
+                %7D%0A            %7D);%0A
         

Use CapabilityToken for getting the Capability instance