package xyz.brassgoggledcoders.modularutilities.modules.ender;

import com.teamacronymcoders.base.items.ItemBase;
import com.teamacronymcoders.base.modulesystem.Module;
import com.teamacronymcoders.base.modulesystem.ModuleBase;
import com.teamacronymcoders.base.util.ItemStackUtils;
import net.minecraft.block.Block;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumParticleTypes;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.living.LivingDropsEvent;
import net.minecraftforge.event.world.BlockEvent.HarvestDropsEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import xyz.brassgoggledcoders.modularutilities.ModularUtilities;

import java.util.ArrayList;
import java.util.Iterator;

@Module(ModularUtilities.MODID)
public class EnderModule extends ModuleBase {
    public static Item ender_glove, ender_pocket;

    public static Block ender_proxy;

    @Override
    public String getName() {
        return "Ender";
    }

    @Override
    public void preInit(FMLPreInitializationEvent event) {
        // TODO EnderStorage compatibility.
        ender_glove = new ItemBase("ender_glove").setMaxStackSize(1);
        getItemRegistry().register(ender_glove);
        ender_pocket = new ItemEnderPocket();
        getItemRegistry().register(ender_pocket);
        // TODO Ender Totem (experience) and Ender Dispenser/Dropper. Also inverse ender glove...

        ender_proxy = new BlockEnderChestProxy();
        getBlockRegistry().register(ender_proxy);

        MinecraftForge.EVENT_BUS.register(this);
    }

    @SubscribeEvent
    public void onLivingDrops(LivingDropsEvent event) {
        if (event.getSource().getDamageType() == "player") {
            EntityPlayer player = (EntityPlayer) event.getSource().getTrueSource();
            if (ItemStackUtils.doItemsMatch(player.getHeldItemOffhand(), ender_glove)) {
                Iterator<EntityItem> drops = event.getDrops().iterator();
                ArrayList<EntityItem> toRemove = new ArrayList<EntityItem>();
                while (drops.hasNext()) {
                    EntityItem current = drops.next();
                    if (player.getInventoryEnderChest().addItem(current.getItem()) == null)
                        toRemove.add(current);
                }
                event.getDrops().removeAll(toRemove);
            }
        }
    }

    @SubscribeEvent
    public void onBlockDrops(HarvestDropsEvent event) {
        if (event.getHarvester() != null) {
            EntityPlayer player = event.getHarvester();
            if (ItemStackUtils.doItemsMatch(player.getHeldItemOffhand(), ender_glove)) {
                Iterator<ItemStack> drops = event.getDrops().iterator();
                ArrayList<ItemStack> toRemove = new ArrayList<ItemStack>();
                while (drops.hasNext()) {
                    ItemStack current = drops.next();
                    if (player.getInventoryEnderChest().addItem(current) == null) {
                        toRemove.add(current);
                        ModularUtilities.proxy.spawnFX(EnumParticleTypes.PORTAL, event.getPos());
                    }
                }
                event.getDrops().removeAll(toRemove);
            }
        }
    }
}

@@ -1908,20 +1908,34 @@
 geType()
- == 
+.equalsIgnoreCase(
 %22player%22
@@ -1935,19 +1935,101 @@
 player%22)
+)
  %7B%0A
+            if (event.getSource().getTrueSource() instanceof EntityPlayer) %7B%0A    
         
@@ -2108,32 +2108,36 @@
 ();%0A            
+    
 if (ItemStackUti
@@ -2143,50 +2143,25 @@
 ils.
-doItemsMatch(player.getHeldItemOffhand(), 
+isPlayerCarrying(
 ende
@@ -2159,37 +2159,49 @@
 ying(ender_glove
+, player
 )) %7B%0A
+    
                 
@@ -2250,32 +2250,36 @@
 s().iterator();%0A
+    
                 
@@ -2329,23 +2329,17 @@
 ist%3C
-EntityItem
 %3E();%0A
+    
     
@@ -2392,24 +2392,28 @@
             
+   
+ 
 EntityItem c
@@ -2427,32 +2427,36 @@
 = drops.next();%0A
+    
                 
@@ -2525,18 +2525,26 @@
 m())
- == null)%0A
+.isEmpty()) %7B%0A    
     
@@ -2594,34 +2594,68 @@
                 
+        
 %7D%0A
+                    %7D%0A    
                 
@@ -2684,32 +2684,50 @@
 eAll(toRemove);%0A
+                %7D%0A
             %7D%0A  
@@ -3327,16 +3327,18 @@
 ent)
- == null
+.isEmpty()
 ) %7B%0A

Fix dups related to null itemstacks