// Events!
// Hopefully I won't have that many.

import crafttweaker.api.events.CTEventManager;
import crafttweaker.api.event.entity.player.interact.MCEntityInteractEvent;
import crafttweaker.api.event.entity.living.MCLivingHurtEvent;
import crafttweaker.api.event.living.MCLivingUpdateEvent;
import crafttweaker.api.entity.MCLivingEntity;
import crafttweaker.api.player.MCPlayerEntity;
import crafttweaker.api.inventory.PlayerInventory;

// Turning Trolls into Functioning Members of Society
// Also Turtles
// This took forever to get working. I'm quite proud of it!
// ---------------------------------------------------------------------------------------------------------------------------------------------
CTEventManager.register<crafttweaker.api.event.entity.player.interact.MCEntityInteractEvent>((event) => {
    val target = event.target;
    val player = event.player;
    val world = player.world;
    val item = event.getItemStack();
    if world.remote {
        return;
    }
    if target.type.name == "Cave Troll" && item.getDefinition().getDefaultInstance() == <item:twilightforest:transformation_powder> {
        if world.random.nextInt(5) == 4 {
            world.asServerWorld().server.executeCommand("execute in twilightforest:twilightforest as " + target.getUUID() + " run summon minecraft:wandering_trader " + target.getPosition().x + " " + target.getPosition().y + " " + target.getPosition().z + "", true);
        }
        else {
            world.asServerWorld().server.executeCommand("execute in twilightforest:twilightforest as " + target.getUUID() + " run summon minecraft:villager " + target.getPosition().x + " " + target.getPosition().y + " " + target.getPosition().z + "", true);
        }
        world.asServerWorld().server.executeCommand("execute in twilightforest:twilightforest as " + target.getUUID() + " run playsound minecraft:entity.zombie_villager.cure neutral " + player.getName() + " " + target.getPosition().x + " " + target.getPosition().y + " " + target.getPosition().z + "", true);
        world.asServerWorld().server.executeCommand("execute in twilightforest:twilightforest as " + target.getUUID() + " run particle minecraft:poof " + target.getPosition().x + " " + target.getPosition().y + " " + target.getPosition().z + " 0 1 0 0 50", true);
        world.asServerWorld().server.executeCommand("execute in twilightforest:twilightforest as " + target.getUUID() + " run tp @s ~ ~-300 ~", true);
        if player.isCreative() == false {
            player.setHeldItem(event.getHand(), item.shrink());
        }
    }
    if target.type.name == "Turtle" && item.getDefinition().getDefaultInstance() == <item:twilightforest:transformation_powder> {
        world.asServerWorld().server.executeCommand("execute in twilightforest:twilightforest as " + target.getUUID() + " run summon quark:toretoise " + target.getPosition().x + " " + target.getPosition().y + " " + target.getPosition().z + "", true);
        world.asServerWorld().server.executeCommand("execute in twilightforest:twilightforest as " + target.getUUID() + " run playsound minecraft:entity.zombie_villager.cure neutral " + player.getName() + " " + target.getPosition().x + " " + target.getPosition().y + " " + target.getPosition().z + "", true);
        world.asServerWorld().server.executeCommand("execute in twilightforest:twilightforest as " + target.getUUID() + " run particle minecraft:poof " + target.getPosition().x + " " + target.getPosition().y + " " + target.getPosition().z + " 0 1 0 0 50", true);
        world.asServerWorld().server.executeCommand("execute in twilightforest:twilightforest as " + target.getUUID() + " run tp @s ~ ~-300 ~", true);
        if player.isCreative() == false {
            player.setHeldItem(event.getHand(), item.shrink());
        }
    }
});
// ---------------------------------------------------------------------------------------------------------------------------------------------


// Beware the Necromancer, and their reflexive summoning of your past foes to haunt you.
// ---------------------------------------------------------------------------------------------------------------------------------------------
import crafttweaker.api.event.entity.living.MCLivingHurtEvent;

CTEventManager.register<crafttweaker.api.event.entity.living.MCLivingHurtEvent>((event) => {
    val entity = event.entity;
    val world = entity.world;
    val health = event.entityLiving.getHealth() as int;
    var summonable_a = ["twilightforest:skeleton_druid", "twilightforest:death_tome", "twilightforest:slime_beetle", "twilightforest:kobold", "twilightforest:mini_ghast", "twilightforest:helmet_crab", "twilightforest:stable_ice_core" , "druidcraft:dreadfish"] as string[];
    var summonable_b = ["twilightforest:redcap", "twilightforest:wraith", "twilightforest:yeti", "twilightforest:blockchain_goblin", "twilightforest:minotaur" , "twilightforest:fire_beetle"] as string[];
    var summonable_c = ["twilightforest:pinch_beetle", "twilightforest:tower_golem" , "druidcraft:beetle" , "eidolon:wraith"] as string[];
    if world.remote {
        return;
    }
    if entity.type.name == "Necromancer" {
        if world.random.nextInt(300) > health {
            val monster_difficulty = world.random.nextInt(3);
            if monster_difficulty == 0 {
                val monster_choice = world.random.nextInt(8);
                world.asServerWorld().server.executeCommand("execute in twilightforest:twilightforest as " + entity.getUUID() + " run summon " + summonable_a[monster_choice] + " " + (entity.getPosition().x + world.random.nextInt(7) - 3) + " " + entity.getPosition().y + " " + (entity.getPosition().z + world.random.nextInt(7) - 3) + "", true);
                world.asServerWorld().server.executeCommand("execute in twilightforest:twilightforest as " + entity.getUUID() + " run summon " + summonable_a[monster_choice] + " " + (entity.getPosition().x + world.random.nextInt(7) - 3) + " " + entity.getPosition().y + " " + (entity.getPosition().z + world.random.nextInt(7) - 3) + "", true);
                world.asServerWorld().server.executeCommand("execute in twilightforest:twilightforest as " + entity.getUUID() + " run summon " + summonable_a[monster_choice] + " " + (entity.getPosition().x + world.random.nextInt(7) - 3) + " " + entity.getPosition().y + " " + (entity.getPosition().z + world.random.nextInt(7) - 3) + "", true);
            }
            if monster_difficulty == 1 {
                val monster_choice = world.random.nextInt(6);
                world.asServerWorld().server.executeCommand("execute in twilightforest:twilightforest as " + entity.getUUID() + " run summon " + summonable_b[monster_choice] + " " + (entity.getPosition().x + world.random.nextInt(7) - 3) + " " + entity.getPosition().y + " " + (entity.getPosition().z + world.random.nextInt(7) - 3) + "", true);
                world.asServerWorld().server.executeCommand("execute in twilightforest:twilightforest as " + entity.getUUID() + " run summon " + summonable_b[monster_choice] + " " + (entity.getPosition().x + world.random.nextInt(7) - 3) + " " + entity.getPosition().y + " " + (entity.getPosition().z + world.random.nextInt(7) - 3) + "", true);
            }
            if monster_difficulty == 2 {
                val monster_choice = world.random.nextInt(4);
                world.asServerWorld().server.executeCommand("execute in twilightforest:twilightforest as " + entity.getUUID() + " run summon " + summonable_c[monster_choice] + " " + (entity.getPosition().x + world.random.nextInt(7) - 3) + " " + entity.getPosition().y + " " + (entity.getPosition().z + world.random.nextInt(7) - 3) + "", true);
            }
            world.asServerWorld().server.executeCommand("execute in twilightforest:twilightforest as " + entity.getUUID() + " run playsound eidolon:cast_soulfire neutral @a " + entity.getPosition().x + " " + entity.getPosition().y + " " + entity.getPosition().z + "", true);
            world.asServerWorld().server.executeCommand("execute in twilightforest:twilightforest as " + entity.getUUID() + " run particle minecraft:witch " + entity.getPosition().x + " " + entity.getPosition().y + " " + entity.getPosition().z + " 3 0 3 1 150", true);
        }
    }
});
// ---------------------------------------------------------------------------------------------------------------------------------------------


// Astral Anchoring.
// ---------------------------------------------------------------------------------------------------------------------------------------------

CTEventManager.register<crafttweaker.api.event.living.MCLivingUpdateEvent>((event) => {
    var player = event.entityLiving;
    if !(event.entityLiving is MCPlayerEntity) {
        return;
    }
    else {
        var player = event.entityLiving as MCPlayerEntity;
    }
    
    val world = player.world;
    if world.remote {
        return;
    }
    if (player is MCPlayerEntity && (player as MCPlayerEntity).getInventory().hasIItemStack(<item:contenttweaker:astral_anchor>) && player.isPotionActive(<effect:astral:astral_travel>)) {
        player.addPotionEffect(<effect:astral:astral_travel>.newInstance(100, player.getActivePotionEffect(<effect:astral:astral_travel>).amplifier));
    }
});

// ---------------------------------------------------------------------------------------------------------------------------------------------





