package org.cyclops.commoncapabilities.modcompat.ic2.capability.tesla;

import ic2.api.item.IElectricItem;
import ic2.api.tile.IEnergyStorage;
import ic2.core.block.TileEntityBlock;
import net.darkhax.tesla.api.ITeslaHolder;
import net.darkhax.tesla.capability.TeslaCapabilities;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import org.cyclops.commoncapabilities.CommonCapabilities;
import org.cyclops.cyclopscore.modcompat.capabilities.CapabilityConstructorRegistry;
import org.cyclops.cyclopscore.modcompat.capabilities.ICapabilityConstructor;
import org.cyclops.cyclopscore.modcompat.capabilities.MultipleCapabilityProvider;
import org.cyclops.cyclopscore.modcompat.capabilities.SimpleCapabilityConstructor;

import javax.annotation.Nullable;

/**
 * Loader for IC2 Tesla capabilities.
 * @author rubensworks
 */
public class Ic2TeslaIntegration {

    public static void load() {
        CapabilityConstructorRegistry registry = CommonCapabilities._instance.getCapabilityConstructorRegistry();

        // Tiles
        registry.registerInheritableTile(IEnergyStorage.class,
                new SimpleCapabilityConstructor<ITeslaHolder, IEnergyStorage>() {
                    @Override
                    public Capability<ITeslaHolder> getCapability() {
                        return TeslaCapabilities.CAPABILITY_HOLDER;
                    }

                    @Nullable
                    @Override
                    public ICapabilityProvider createProvider(IEnergyStorage host) {
                        HolderProducerConsumerEnergyStorage capabilityImplementation = new HolderProducerConsumerEnergyStorage(host);
                        return MultipleCapabilityProvider.of(
                                TeslaCapabilities.CAPABILITY_HOLDER, capabilityImplementation,
                                TeslaCapabilities.CAPABILITY_PRODUCER, capabilityImplementation,
                                TeslaCapabilities.CAPABILITY_CONSUMER, capabilityImplementation
                        );
                    }
                });
        registry.registerInheritableTile(TileEntityBlock.class,
                new SimpleCapabilityConstructor<ITeslaHolder, TileEntityBlock>() {
                    @Override
                    public Capability<ITeslaHolder> getCapability() {
                        return TeslaCapabilities.CAPABILITY_HOLDER;
                    }

                    @Nullable
                    @Override
                    public ICapabilityProvider createProvider(TileEntityBlock host) {
                        HolderProducerConsumerBlock capabilityImplementation = new HolderProducerConsumerBlock(host);
                        return MultipleCapabilityProvider.of(
                                TeslaCapabilities.CAPABILITY_HOLDER, capabilityImplementation,
                                TeslaCapabilities.CAPABILITY_PRODUCER, capabilityImplementation,
                                TeslaCapabilities.CAPABILITY_CONSUMER, capabilityImplementation
                        );
                    }
                });

        // Items
        registry.registerInheritableItem(IElectricItem.class,
                new ICapabilityConstructor<ITeslaHolder, IElectricItem, ItemStack>() {
                    @Override
                    public Capability<ITeslaHolder> getCapability() {
                        return TeslaCapabilities.CAPABILITY_HOLDER;
                    }

                    @Nullable
                    @Override
                    public ICapabilityProvider createProvider(IElectricItem hostType, final ItemStack host) {
                        HolderProducerConsumerElectricItem capabilityImplementation = new HolderProducerConsumerElectricItem(hostType, host);
                        return MultipleCapabilityProvider.of(
                                TeslaCapabilities.CAPABILITY_HOLDER, capabilityImplementation,
                                TeslaCapabilities.CAPABILITY_PRODUCER, capabilityImplementation,
                                TeslaCapabilities.CAPABILITY_CONSUMER, capabilityImplementation
                        );
                    }
                });
    }

}

@@ -2636,32 +2636,101 @@
 tyBlock host) %7B%0A
+                        if (!(host instanceof IEnergyStorage)) %7B%0A    
                 
@@ -2847,32 +2847,36 @@
                 
+    
 return MultipleC
@@ -2921,32 +2921,36 @@
                 
+   
+ 
 TeslaCapabilitie
@@ -2988,32 +2988,36 @@
 Implementation,%0A
+    
                 
@@ -3121,32 +3121,36 @@
                 
+    
 TeslaCapabilitie
@@ -3213,33 +3213,100 @@
                 
+ 
-)
+   );%0A                        %7D%0A                        return null
 ;%0A              

Fix duplicate Tesla capability on IC2 storage tiles