ItemEvents.entityInteracted(event => {
    let entity = event.entity;
    let target = event.target;

    if (target.getType() === 'minecraft:player') {

        let pData = entity.persistentData

        let username = entity.getGameProfile().getName()
        let target_username = target.getGameProfile().getName()

        let entity_spin = palladium.getProperty(entity, 'ninjago.jitzu.spinjitzu_active');
        let target_spin = palladium.getProperty(target, 'ninjago.jitzu.spinjitzu_active')
        if (entity_spin && target_spin) {
            // pData.ninjago_toc_x = target.getX()
            // pData.ninjago_toc_y = target.getY()
            // pData.ninjago_toc_z = target.getZ()
            pData.ninjago_toc_target = target_username
            //entity.tell(`${target.getX()} ${target.getY()} ${target.getZ()}`)

            command(entity, "summon minecraft:armor_stand ~ ~2 ~ {Invisible:1,Tags:[\"Ninjago.TOC\",\"" + target_username + "\"]}", target_username)
            command(entity, "team add Ninjago.Intangible", username)
            command(entity, "team modify Ninjago.Intangible collisionRule never", username)
            command(entity, "team join Ninjago.Intangible", username)
            addTag(entity, `Ninjago.Spinjitzu.On`)
            addTag(target, `Ninjago.Spinjitzu.Vehicle`)
        }
    }

});
PlayerEvents.tick(event => {
    let entity = event.entity;
    let pData = entity.persistentData
    let username = entity.getGameProfile().getName()
    let target_username = pData.ninjago_toc_target

    // pData.ninjago_toc_x = pData.ninjago_toc_target.getX()
    // pData.ninjago_toc_y = pData.ninjago_toc_target.getY()
    // pData.ninjago_toc_z = pData.ninjago_toc_target.getZ()
    let entity_spin = palladium.getProperty(entity, 'ninjago.jitzu.spinjitzu_active');

    command(entity, "tp @e[type=minecraft:armor_stand,tag=Ninjago.TOC,tag=" + username + "] ~ ~1 ~", username)
    command(entity, "tp @e[type=minecraft:armor_stand,tag=Ninjago.TOC,tag=" + target_username + ",limit=1,sort=nearest]", username)

    if (entity.isCrouching() || !entity_spin) {
        removeTag(entity, 'Ninjago.Spinjitzu.On')
        removeTag(entity, 'Ninjago.Spinjitzu.Vehicle')
        command(entity, 'tag @s remove Ninjago.Spinjitzu.Vehicle', target_username)
        pData.ninjago_toc_target = ''
        command(entity, "kill @e[type=minecraft:armor_stand,tag=Ninjago.TOC,tag=" + target_username + ",limit=2,sort=nearest]", username)
        command(entity, "team leave @s", username)
    }
    if (!entity_spin) {
        command(entity, "kill @e[type=minecraft:armor_stand,tag=Ninjago.TOC,tag=" + username + "]", username)
        removeTag(entity, 'Ninjago.Spinjitzu.On')
        removeTag(entity, 'Ninjago.Spinjitzu.Vehicle')
        command(entity, 'tag @s remove Ninjago.Spinjitzu.Vehicle', target_username)
    }
})