package joshie.harvest.core.handlers;

import joshie.harvest.core.util.annotations.HFEvents;
import joshie.harvest.core.util.holders.HolderRegistrySet;
import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemHoe;
import net.minecraft.item.ItemStack;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.event.entity.player.UseHoeEvent;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.fml.common.eventhandler.Event.Result;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import static joshie.harvest.animals.HFAnimals.DISABLE_SPAWN_CHICKEN;
import static joshie.harvest.crops.HFCrops.*;
import static net.minecraft.init.Items.EGG;

public class DisableHandler {
    //Crop Blocks
    public static final Set<Block> CROPS = new HashSet<>();
    public static final Set<Block> GRASS = new HashSet<>();
    public static final HolderRegistrySet SEEDS_BLACKLIST = new HolderRegistrySet();
    public static final HolderRegistrySet HOE_BLACKLIST = new HolderRegistrySet();


    /* Disables vanilla seeds from being able to be planted **/
    @HFEvents
    @SuppressWarnings("unused")
    public static class VanillaSeeds {
        public static boolean register() { return DISABLE_VANILLA_SEEDS; }

        @SubscribeEvent
        public void onRightClickBlock(PlayerInteractEvent.RightClickBlock event) {
            ItemStack held = event.getItemStack();
            if (held != null) {
                if (SEEDS_BLACKLIST.contains(held)) {
                    event.setUseItem(Result.DENY);
                }
            }
        }
    }

    /* Disables vanilla crops from growing **/
    @HFEvents
    @SuppressWarnings("unused")
    public static class VanillaGrowth {
        public static boolean register() { return DISABLE_VANILLA_GROWTH; }

        @SubscribeEvent
        public void onRightClickBlock(PlayerInteractEvent.RightClickBlock event) {
            if (CROPS.contains(event.getWorld().getBlockState(event.getPos()).getBlock())) {
                event.setUseItem(Result.DENY);
            }
        }
    }

    /* Disables vanilla crops from dropping **/
    @HFEvents
    @SuppressWarnings("unused")
    public static class VanillaDrops {
        public static boolean register() { return DISABLE_VANILLA_DROPS; }

        @SubscribeEvent
        public void onHarvestBlock(BlockEvent.HarvestDropsEvent event) {
            if (CROPS.contains(event.getState().getBlock())) {
                event.getDrops().clear();
            }
        }
    }

    /* Disables vanilla eggs from spawning chickens **/
    @HFEvents
    @SuppressWarnings("unused")
    public static class EggSpawning {
        public static boolean register() { return DISABLE_SPAWN_CHICKEN; }

        @SubscribeEvent
        public void onRightClickItem(PlayerInteractEvent.RightClickItem event) {
            ItemStack held = event.getItemStack();
            if (held != null) {
                if (held.getItem() == EGG) {
                    event.setResult(Result.DENY);
                }
            }
        }
    }

    /* Disables the use of vanilla hoes **/
    @HFEvents
    @SuppressWarnings("unused")
    public static class VanillaHoes {
        public static boolean register() { return DISABLE_VANILLA_HOE; }

        @SubscribeEvent
        public void onUseHoe(UseHoeEvent event) {
            if (DISABLE_VANILLA_HOE && (event.getCurrent().getItem() instanceof ItemHoe || HOE_BLACKLIST.contains(event.getCurrent()))) {
                event.setCanceled(true);
            }
        }
    }

    /* Disables seeds from dropping from grass **/
    @HFEvents
    @SuppressWarnings("unused")
    public static class SeedDrops {
        public static boolean register() { return DISABLE_VANILLA_WHEAT_SEEDS; }

        @SubscribeEvent
        public void onItemDropping(BlockEvent.HarvestDropsEvent event) {
            if (event.getState().getBlock() == Blocks.TALLGRASS || event.getState().getBlock() == Blocks.DOUBLE_PLANT || GRASS.contains(event.getState().getBlock())) {
                Iterator<ItemStack> it = event.getDrops().iterator();
                while (it.hasNext()) {
                    if (SEEDS_BLACKLIST.contains(it.next())) it.remove();
                }
            }
        }
    }
}

@@ -32,16 +32,51 @@
 dlers;%0A%0A
+import joshie.harvest.core.HFCore;%0A
 import j
@@ -4336,32 +4336,426 @@
                 
+ItemStack stack = it.next();%0A                    if (stack == null) %7B%0A                        if (HFCore.DEBUG_MODE) System.out.println(event.getState() + %22 had a null itemstack somehow...%22);%0A                    %7D else if (stack.getItem() == null) %7B%0A                        if (HFCore.DEBUG_MODE) System.out.println(event.getState() + %22 had a null item somehow...%22);%0A                    %7D else 
 if (SEEDS_BLACKL
@@ -4767,25 +4767,21 @@
 ontains(
-it.next()
+stack
 )) it.re

Debug code for fun.