package me.km.entities;

import me.km.KajetansMod;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityClassification;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.ai.attributes.GlobalEntityTypeAttributes;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.fml.client.registry.RenderingRegistry;
import net.minecraftforge.fml.loading.FMLEnvironment;
import net.minecraftforge.registries.IForgeRegistry;

public class ModEntities {
    private static <T extends Entity> EntityType<T> build(EntityType.Builder<T> e, String key) {
        EntityType<T> type = e.build(String.format("%s:human", KajetansMod.MODID));
        type.setRegistryName(KajetansMod.MODID, key);
        return type;
    }

    public static final EntityType<EntityHuman> HUMAN = build(
            EntityType.Builder.<EntityHuman>create(EntityHuman::new, EntityClassification.CREATURE)
                    .size(0.6f, 1.8f).trackingRange(10),
            "human");
    public static final EntityType<EntityNobody> NOBODY =
            build(EntityType.Builder.create(EntityNobody::new, EntityClassification.MISC)
                    .size(0.5f, 0.5f).trackingRange(10), "nobody");

    public static void init(IForgeRegistry<EntityType<?>> r) {
        if(FMLEnvironment.dist == Dist.CLIENT) {
            initClient();
        }
        r.register(HUMAN);
        r.register(NOBODY);

        GlobalEntityTypeAttributes.put(HUMAN, EntityHuman.getAttributes().create());
        GlobalEntityTypeAttributes.put(NOBODY, EntityNobody.getAttributes().create());
    }

    @OnlyIn(Dist.CLIENT)
    private static void initClient() {
        RenderingRegistry.registerEntityRenderingHandler(HUMAN, (rm) -> {
            return new RenderHuman(rm);
        });
        RenderingRegistry.registerEntityRenderingHandler(NOBODY, (rm) -> {
            return new RenderNobody(rm);
        });
    }
}
