package me.km;

import me.km.blocks.ModBlocks;
import me.km.blocks.cookingpot.TileEntityCookingPot;
import me.km.entities.ModEntities;
import me.km.items.ModItems;
import me.km.world.WorldManager;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.entity.EntityType;
import net.minecraft.item.Item;
import net.minecraft.item.Items;
import net.minecraft.tileentity.TileEntityType;
import net.minecraftforge.common.ModDimension;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber(bus=Mod.EventBusSubscriber.Bus.MOD)
public class ObjectRegistry 
{
    @SubscribeEvent
    public static void onBlockRegistry(RegistryEvent.Register<Block> e) 
    {
        ModBlocks.initBlocks(e.getRegistry());
    }
    
    @SubscribeEvent
    public static void onBlockMissing(RegistryEvent.MissingMappings<Block> e) 
    {
        e.getMappings().stream()
                .forEach((missing) -> 
                {
                    String key = missing.key.toString();
                    if(key.contains("cauldron"))
                    {
                        missing.remap(ModBlocks.cookingPotCopper);   
                    }
                    else if(key.contains("campfire"))
                    {
                        missing.remap(Blocks.CAMPFIRE);   
                    }
                    else if(key.contains("spikes"))
                    {
                        missing.remap(ModBlocks.spikesCopper);   
                    }
                    else if(key.contains("campfire"))
                    {
                        missing.remap(Blocks.CAMPFIRE);   
                    }
                    else if(key.contains("barrel"))
                    {
                        missing.remap(Blocks.BARREL);   
                    }
                    else if(key.contains("poison") || key.contains("honey"))
                    {
                        missing.remap(Blocks.WATER);   
                    }
                    else
                    {
                        missing.remap(Blocks.STONE);   
                    }
                });
    }
    
    @SubscribeEvent
    public static void onItemMissing(RegistryEvent.MissingMappings<Item> e) 
    {
        e.getMappings().stream()
                .forEach((missing) -> 
                {
                    missing.remap(Items.APPLE);   
                });
    }

    @SubscribeEvent
    public static void onItemRegistry(RegistryEvent.Register<Item> e) 
    {
        ModItems.init(e.getRegistry());
        ModBlocks.initItemBlocks(e.getRegistry());
    }
    
    @SubscribeEvent
    public static void onEntityTypeRegistry(RegistryEvent.Register<EntityType<?>> e) 
    {
        ModEntities.init(e.getRegistry());
    }
    
    @SubscribeEvent
    public static void onTileEntityTypeRegistry(RegistryEvent.Register<TileEntityType<?>> e) 
    {
        e.getRegistry().register(TileEntityCookingPot.COOKING_POT);
    }
    
    @SubscribeEvent
    public static void onModDimensionRegistry(RegistryEvent.Register<ModDimension> e) 
    {
        e.getRegistry().register(WorldManager.MOD_DIMENSION);
    }
}
