import { world } from '@minecraft/server';
import { system } from '@minecraft/server';

import './carrinho';
import './car'
import { itemUseController } from "./item_use_controller";
import { entityHitEntityController } from "./entity_hit_entity_controller";

world.afterEvents.itemUse.subscribe(itemUseController);
world.afterEvents.projectileHitEntity.subscribe(entityHitEntityController);


world.afterEvents.playerSpawn.subscribe(event => {
    if (!event.player.hasTag("kt_ct:book")) {
        event.player.runCommand('gamerule sendcommandfeedback false')
        event.player.runCommand('give @s kt_ct:book_spawn_egg')
        event.player.runCommand('gamerule sendcommandfeedback true')
        event.player.addTag("kt_ct:book")
    }
})

system.afterEvents.scriptEventReceive.subscribe((x) => {
    if (x.id == "book:system") {
        const player = x.sourceEntity.dimension.getEntities({ location: x.sourceEntity.location, families: ['player'], closest: 1 })[0]
        if (!player) return

        x.sourceEntity.teleport({ x: x.sourceEntity.location.x, y: x.sourceEntity.location.y, z: x.sourceEntity.location.z }, { facingLocation: player.location })
    }
})