package com.cjm721.overloaded.storage;

import com.google.common.collect.Maps;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;
import net.minecraftforge.common.capabilities.CapabilityManager;
import net.minecraftforge.common.util.INBTSerializable;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Map;

public class GenericDataStorage implements IGenericDataStorage, INBTSerializable<NBTTagCompound>, Capability.IStorage<IGenericDataStorage> {

    @CapabilityInject(IGenericDataStorage.class)
    public static Capability<IGenericDataStorage> GENERIC_DATA_STORAGE = null;
    private final Map<String, Integer> integerMap;
    private final Map<String, Boolean> booleanMap;
    private final Map<String, Double> doubleMap;

    public GenericDataStorage() {
        integerMap = Maps.newHashMap();
        booleanMap = Maps.newHashMap();
        doubleMap = Maps.newHashMap();
    }

    public static void register() {
        CapabilityManager.INSTANCE.register(IGenericDataStorage.class,
                new GenericDataStorage(),
                GenericDataStorage::new
        );
    }

    @Nonnull
    @Override
    public Map<String, Integer> getIntegerMap() {
        return integerMap;
    }

    @Nonnull
    @Override
    public Map<String, Boolean> getBooleanMap() {
        return booleanMap;
    }

    @Nonnull
    @Override
    public Map<String, Double> getDoubleMap() {
        return doubleMap;
    }

    @Override
    public NBTTagCompound serializeNBT() {
        return writeNBT(null, this, null);
    }

    @Override
    public void deserializeNBT(NBTTagCompound tagCompound) {
        readNBT(null, this, null, tagCompound);
    }

    @Nullable
    @Override
    public NBTTagCompound writeNBT(Capability<IGenericDataStorage> capability, IGenericDataStorage instance, EnumFacing side) {
        NBTTagCompound tagCompound = new NBTTagCompound();
        Map<String, Integer> integers = instance.getIntegerMap();
        Map<String, Boolean> booleans = instance.getBooleanMap();
        Map<String, Double> doubles = instance.getDoubleMap();

        for (String key : integers.keySet()) {
            tagCompound.setInteger(key, integers.get(key));
        }

        for (String key : booleans.keySet()) {
            tagCompound.setBoolean(key, booleans.get(key));
        }

        for (String key : doubles.keySet()) {
            tagCompound.setDouble(key, doubles.get(key));
        }

        return tagCompound;
    }

    @Override
    public void readNBT(Capability<IGenericDataStorage> capability, IGenericDataStorage instance, EnumFacing side, NBTBase nbt) {
        if (!(nbt instanceof NBTTagCompound))
            return;

        NBTTagCompound tagCompound = ((NBTTagCompound) nbt);
        Map<String, Integer> integers = instance.getIntegerMap();
        Map<String, Boolean> booleans = instance.getBooleanMap();
        Map<String, Double> doubles = instance.getDoubleMap();

        for (String key : tagCompound.getKeySet()) {
            integers.put(key, tagCompound.getInteger(key));
        }

        for (String key : tagCompound.getKeySet()) {
            booleans.put(key, tagCompound.getBoolean(key));
        }

        for (String key : tagCompound.getKeySet()) {
            doubles.put(key, tagCompound.getDouble(key));
        }
    }
}

@@ -101,56 +101,9 @@
 nbt.
-NBTBase;%0Aimport net.minecraft.nbt.NBTTagCompound
+*
 ;%0Aim
@@ -325,16 +325,65 @@
 anager;%0A
+import net.minecraftforge.common.util.Constants;%0A
 import n
@@ -1717,28 +1717,44 @@
 rn writeNBT(
-null
+GENERIC_DATA_STORAGE
 , this, null
@@ -1854,20 +1854,36 @@
 readNBT(
-null
+GENERIC_DATA_STORAGE
 , this, 
@@ -3219,32 +3219,132 @@
 .getKeySet()) %7B%0A
+            switch(tagCompound.getTagId(key)) %7B%0A                case Constants.NBT.TAG_INT:%0A        
             inte
@@ -3391,35 +3391,32 @@
 ));%0A        
-%7D%0A%0A
         
 for (String 
@@ -3407,53 +3407,72 @@
     
-for (String key : tagCompound.getKeySet()) %7B%0A
+    break;%0A                case Constants.NBT.TAG_BYTE:%0A        
     
@@ -3531,35 +3531,32 @@
 ));%0A        
-%7D%0A%0A
         
 for (String 
@@ -3547,53 +3547,74 @@
     
-for (String key : tagCompound.getKeySet()) %7B%0A
+    break;%0A                case Constants.NBT.TAG_DOUBLE:%0A        
     
@@ -3659,32 +3659,73 @@
 etDouble(key));%0A
+                    break;%0A            %7D%0A
         %7D%0A    %7D%0A

Fix subtle bug in generic data that caused it to override int's with doubles. Also its cleaner / faster