StartupEvents.registry('palladium:abilities', (event) => {

    // ID of the ability will be: 'kubejs:radius/damage'
    event.create('heroes:aoe_speed_damage')

        .icon(palladium.createItemIcon('palladium:vibranium_circuit'))

        .addProperty('tag', 'string', 'damage_tag', 'Tag that is applied to the projectile')
        .addProperty("score_value", "string", "Heroes.Speed.Damage", "Scoreboard Name")
        .addProperty('radius_te', 'integer', 10, 'Radius in which surrounding entities will be damaged')
        .tick((entity, entry, holder, enabled) => {
            if (enabled) {
                const tag = entry.getPropertyByName('tag');
                let username = entity.getGameProfile().getName();
                    let scoreboard = Utils.server.scoreboard;
                    let scoreboard_obj = scoreboard.getObjective(entry.getPropertyByName("score_value"));
                    let score = scoreboard.getOrCreatePlayerScore(username, scoreboard_obj);
                    let damage = score.getScore();

                const radius = entry.getPropertyByName('radius_te') * 2.0;

                const targets = entity.serverLevel().getEntities(entity, AABB.ofSize(entity.position(), 100, 100, 100)).toArray(); 

                for (let j = 0; j < targets.length; j++) {
                    let en = targets[j];
                    if (containsTag(en.getTags().toArray(), tag)) {
                        let box = AABB.ofSize(en.position(), radius, radius, radius);
                        let entities = en.level().getEntities(en, box).toArray();
                        for (let i = 0; i < entities.length; i++) {
                            if (entities[i] !== entity && !containsTag(entities[i].getTags().toArray(), tag)) {
                                entities[i].attack(damage);
                            }
                        }
                    }
                }
            }
        })
});

function containsTag(tags, tag) {
    for (let i = 0; i < tags.length; i++) {
        if(tags[i].equals(tag)) {
            return true;
        }
    }
    return false;
}