(function () {
    let ClientboundSetEntityMotionPacket = Java.loadClass('net.minecraft.network.protocol.game.ClientboundSetEntityMotionPacket');

    StartupEvents.registry('palladium:abilities', (event) => {
        event.create('heroes:base_power')
            .icon(palladium.createItemIcon('palladium:vibranium_circuit'))

            .tick((entity, entry, holder, enabled) => {
                if (enabled) {
                    let username = entity.getGameProfile().getName();
					let username_true = username.toLowerCase();
                    let scoreboard = Utils.server.scoreboard;
					let Power_Tol = 3
					if (abilityUtil.isEnabled(entity, "heroes:ability_movement", "genetic_marker")) {
					Power_Tol += 5
					}
					if (abilityUtil.isEnabled(entity, "heroes:parallel_processing", "genetic_marker")) {
					Power_Tol += 1
					}
					if (abilityUtil.isEnabled(entity, "heroes:ability_replication", "genetic_marker")) {
					let scoreboard_slots = scoreboard.getObjective('Heroes.Replication.Slots');
                    let score_slots = scoreboard.getOrCreatePlayerScore(username, scoreboard_slots);
                    let value_slots = score_slots.getScore();
					Power_Tol += value_slots
					}
					
					let scoreboard_abil = scoreboard.getObjective('Heroes.AbilityTot');
                    let score_abil = scoreboard.getOrCreatePlayerScore(username, scoreboard_abil);
                    let value_abil = score_abil.getScore();
					
					entity.persistentData.Ability_Limit = 0
					if (abilityUtil.isEnabled(entity, "heroes:base_power", "genetic_marker")) {
					if (value_abil > Power_Tol - 1) {
						entity.persistentData.Ability_Limit = 1
					}
					if (value_abil > Power_Tol) {
						entity.persistentData.Ability_Limit = (value_abil - Power_Tol) + 1
						entity.server.runCommandSilent("execute as " + username + " at @s run scoreboard players add @s Heroes.Concentration.Hold " + (value_abil - Power_Tol) +"");
					}
					if (value_abil > Power_Tol + 1) {
						entity.persistentData.Ability_Limit = (value_abil - Power_Tol) + 1
						entity.server.runCommandSilent("execute as " + username + " at @s run scoreboard players add @s Heroes.Concentration.Hold " + (value_abil - Power_Tol) +"");
						}
					}
					
					let scoreboard_stam = scoreboard.getObjective('Heroes.Stamina');
                    let score_stam = scoreboard.getOrCreatePlayerScore(username, scoreboard_stam);
                    let value_stam = score_stam.getScore();
					
                    let scoreboard_max = scoreboard.getObjective('Heroes.Stamina.Max');
                    let score_max = scoreboard.getOrCreatePlayerScore(username, scoreboard_max);
                    let value_max = score_max.getScore();
					
					let scoreboard_hold = scoreboard.getObjective('Heroes.Stamina.Hold');
                    let score_hold = scoreboard.getOrCreatePlayerScore(username, scoreboard_hold);
                    let value_hold = score_hold.getScore();
					
					let scoreboard_remove = scoreboard.getObjective('Heroes.Stamina.Remove');
                    let score_remove = scoreboard.getOrCreatePlayerScore(username, scoreboard_remove);
                    let value_remove = score_remove.getScore();
					
					let scoreboard_conmax = scoreboard.getObjective('Heroes.Concentration.Max');
                    let score_conmax = scoreboard.getOrCreatePlayerScore(username, scoreboard_conmax);
                    let value_conmax = score_conmax.getScore();
					
					let scoreboard_basemax = scoreboard.getObjective('Heroes.Concentration.Max.Base');
                    let score_basemax = scoreboard.getOrCreatePlayerScore(username, scoreboard_basemax);
                    let value_basemax = score_basemax.getScore();
					
					let scoreboard_pot = scoreboard.getObjective('Heroes.Concentration.Potential');
                    let score_pot = scoreboard.getOrCreatePlayerScore(username, scoreboard_pot);
                    let value_pot = score_pot.getScore();
					
					let scoreboard_conmax_hold = scoreboard.getObjective('Heroes.Concentration.Hold');
                    let score_conmax_hold = scoreboard.getOrCreatePlayerScore(username, scoreboard_conmax_hold);
                    let value_conmax_hold = score_conmax_hold.getScore();
					
					let scoreboard_conxp = scoreboard.getObjective('Heroes.Concentration.Xp');
                    let score_conxp = scoreboard.getOrCreatePlayerScore(username, scoreboard_conxp);
                    let value_conxp = score_conxp.getScore();
					
					let scoreboard_conxpmax = scoreboard.getObjective('Heroes.Concentration.Xp.Max');
                    let score_conxpmax = scoreboard.getOrCreatePlayerScore(username, scoreboard_conxpmax);
                    let value_conxpmax = score_conxpmax.getScore();
					
					let scoreboard_usable = scoreboard.getObjective('Heroes.Concentration.Usable');
                    let score_usable = scoreboard.getOrCreatePlayerScore(username, scoreboard_usable);
                    let value_usable = score_usable.getScore();
					
					
					
					
					if (value_usable <= 0) {
						entity.server.runCommandSilent("execute as " + username + " at @s run tag @s add Heroes.BurnOut");
					}
					if (value_stam <= 0) {
						entity.server.runCommandSilent("execute as " + username + " at @s run tag @s add Heroes.Stam.BurnOut");
					}
	
					if (containsTag(entity.getTags().toArray(), 'Heroes.BurnOut')) {
					value_conmax_hold = value_conmax;
					}
					entity.server.runCommandSilent("execute as " + username + " at @s run scoreboard players set @s[tag=!Heroes.BurnOut] Heroes.Concentration.Usable " + ((value_conmax + 1) - value_conmax_hold) + "");
					if (!containsTag(entity.getTags().toArray(), 'Heroes.BurnOut')) {
					if (value_conmax_hold >= 1) {
						if (value_basemax <= value_pot) {
					entity.server.runCommandSilent("execute as " + username + " at @s run scoreboard players add @s Heroes.Concentration.Xp 1");
						}
						}
					}
					
					if (value_basemax <= value_pot && value_conxp >= value_conxpmax) {
					entity.server.runCommandSilent("execute as " + username + " at @s run scoreboard players add @s Heroes.Concentration.Max.Base 1");
					entity.server.runCommandSilent("execute as " + username + " at @s run scoreboard players set @s Heroes.Concentration.Xp 0");
					entity.server.runCommandSilent("execute as " + username + " at @s run scoreboard players add @s Heroes.Concentration.Xp.Max 50000");
					}
					entity.server.runCommandSilent("execute as " + username + " at @s run scoreboard players set @s Heroes.Concentration " + (12 - value_conmax_hold) + "");
					entity.server.runCommandSilent("execute as " + username + " at @s run scoreboard players set @s Heroes.Concentration.Hold 0");
					
					
					for (let stam_loop = 1; stam_loop <= 80; stam_loop++) {
						if (((value_max / 80) * stam_loop) >= value_stam) {
					entity.server.runCommandSilent("execute as " + username + " at @s run scoreboard players set @s Heroes.Stamina.Percent " + stam_loop + "");
					break;
						}
					}
					if (value_remove >= 1) {
					entity.server.runCommandSilent("execute as " + username + " at @s run scoreboard players add @s Heroes.Stamina.Hold " + value_remove + "");
					entity.server.runCommandSilent("execute as " + username + " at @s run scoreboard players remove @s Heroes.Stamina " + value_remove + "");
					entity.server.runCommandSilent("execute as " + username + " at @s run scoreboard players set @s Heroes.Stamina.Remove 0");
					}			
					if (value_hold >= (value_max * 5)) {
						if (value_max < 250) {
							entity.server.runCommandSilent("execute as " + username + " at @s run scoreboard players add @s Heroes.Stamina.Max 5");
						}
						if (value_max >= 250 && value_max < 500) {
							entity.server.runCommandSilent("execute as " + username + " at @s run scoreboard players add @s Heroes.Stamina.Max 4");
						}
						if (value_max >= 500 && value_max < 750) {
							entity.server.runCommandSilent("execute as " + username + " at @s run scoreboard players add @s Heroes.Stamina.Max 3");
						}
						if (value_max >= 750 && value_max < 1000) {
							entity.server.runCommandSilent("execute as " + username + " at @s run scoreboard players add @s Heroes.Stamina.Max 2");
						}
						if (value_max >= 1000) {
							entity.server.runCommandSilent("execute as " + username + " at @s run scoreboard players add @s Heroes.Stamina.Max 1");
						}
					entity.server.runCommandSilent("execute as " + username + " at @s run scoreboard players remove @s Heroes.Stamina.Hold " + value_max + "");
					}
				}
            });
    });
})();