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

    StartupEvents.registry('palladium:abilities', (event) => {
        event.create('heroes:spatial_armaments')
            .icon(palladium.createItemIcon('palladium:vibranium_circuit'))
			.addProperty("Equip_Storage_Id", "string", "Heroes", "This should be a unique ID for each use of the ability unless you want them to share the same slot")
            .tick((entity, entry, holder, enabled) => {
                if (enabled) {
					const requipid_fake = entry.getPropertyByName('Equip_Storage_Id');
					let spatial_slot = palladium.getProperty(entity, "heroes:spatial_armaments_slot")
					let requipid = requipid_fake.toLowerCase();
                    let username = entity.getGameProfile().getName();
					let username_true = username.toLowerCase();
					let scoreboard = Utils.server.scoreboard;
					let scoreboard_obj = scoreboard.getObjective("Heroes.Stamina");
                    let score = scoreboard.getOrCreatePlayerScore(username, scoreboard_obj);
                    let value = score.getScore();
	if (abilityUtil.isUnlocked(entity, 'heroes:spatial_armaments', 'Spatial_Repair')) {
		if (entity.headArmorItem.nbt != null) {
		entity.headArmorItem.nbt.Damage = entity.headArmorItem.nbt.Damage - 10
		if (entity.headArmorItem.nbt.Damage < 0) {
				entity.headArmorItem.nbt.Damage = 0
			}
		}
		if (entity.chestArmorItem.nbt != null) {
		entity.chestArmorItem.nbt.Damage = entity.chestArmorItem.nbt.Damage - 10
		if (entity.chestArmorItem.nbt.Damage < 0) {
				entity.chestArmorItem.nbt.Damage = 0
			}
		}
		if (entity.legsArmorItem.nbt != null) {
		entity.legsArmorItem.nbt.Damage = entity.legsArmorItem.nbt.Damage - 10
		if (entity.legsArmorItem.nbt.Damage < 0) {
				entity.legsArmorItem.nbt.Damage = 0
			}
		}
		if (entity.feetArmorItem.nbt != null) {
		entity.feetArmorItem.nbt.Damage = entity.feetArmorItem.nbt.Damage - 10
		if (entity.feetArmorItem.nbt.Damage < 0) {
				entity.feetArmorItem.nbt.Damage = 0
			}
		}
	}
	entity.server.runCommandSilent("execute as " + username + " at @s if entity @s[nbt={Inventory:[{Slot:103b}]}] run tag @s add Heroes.Spatial.Cost");
    entity.server.runCommandSilent("execute as " + username + " at @s if entity @s[nbt={Inventory:[{Slot:102b}]}] run tag @s add Heroes.Spatial.Cost");
    entity.server.runCommandSilent("execute as " + username + " at @s if entity @s[nbt={Inventory:[{Slot:101b}]}] run tag @s add Heroes.Spatial.Cost");
    entity.server.runCommandSilent("execute as " + username + " at @s if entity @s[nbt={Inventory:[{Slot:100b}]}] run tag @s add Heroes.Spatial.Cost");
	
	
	entity.server.runCommandSilent("execute as " + username + " at @s run summon minecraft:armor_stand ~ ~ ~ {Invisible:1b,Marker:1b,CustomName:\'[{\"text\":\"" + requipid + "_" + spatial_slot + "_" + username_true + "\"}]\'}");
    entity.server.runCommandSilent("execute as " + username + " at @s run data modify entity @e[type=minecraft:armor_stand,name=" + requipid + "_" + spatial_slot + "_" + username_true + ",limit=1] ArmorItems set from storage minecraft:" + requipid + "_" + spatial_slot + "_" + username_true + " ArmorItems");


    entity.server.runCommandSilent("execute as " + username + " at @s if entity @s[nbt={Inventory:[{Slot:103b}]}] run tag @s add Armor_Swap_Load");
    entity.server.runCommandSilent("execute as " + username + " at @s if entity @s[nbt={Inventory:[{Slot:102b}]}] run tag @s add Armor_Swap_Load");
    entity.server.runCommandSilent("execute as " + username + " at @s if entity @s[nbt={Inventory:[{Slot:101b}]}] run tag @s add Armor_Swap_Load");
    entity.server.runCommandSilent("execute as " + username + " at @s if entity @s[nbt={Inventory:[{Slot:100b}]}] run tag @s add Armor_Swap_Load");

    entity.server.runCommandSilent("execute as " + username + " as @s[tag=Armor_Swap_Load] at @s run data remove storage minecraft:" + requipid + "_" + spatial_slot + "_" + username_true + " ArmorItems");
    entity.server.runCommandSilent("execute as " + username + " as @s[tag=Armor_Swap_Load] at @s run summon minecraft:armor_stand ~ ~ ~ {Invisible:1b,Marker:1b,CustomName:\'[{\"text\":\"" + requipid + "_" + spatial_slot + "_2" + username_true + "\"}]\'}");
    entity.server.runCommandSilent("execute as " + username + " as @s[tag=Armor_Swap_Load] at @s run item replace entity @e[type=minecraft:armor_stand,name=" + requipid + "_" + spatial_slot + "_2" + username_true + "] armor.head from entity @s armor.head");
    entity.server.runCommandSilent("execute as " + username + " as @s[tag=Armor_Swap_Load] at @s run item replace entity @e[type=minecraft:armor_stand,name=" + requipid + "_" + spatial_slot + "_2" + username_true + "] armor.chest from entity @s armor.chest");
    entity.server.runCommandSilent("execute as " + username + " as @s[tag=Armor_Swap_Load] at @s run item replace entity @e[type=minecraft:armor_stand,name=" + requipid + "_" + spatial_slot + "_2" + username_true + "] armor.legs from entity @s armor.legs");
    entity.server.runCommandSilent("execute as " + username + " as @s[tag=Armor_Swap_Load] at @s run item replace entity @e[type=minecraft:armor_stand,name=" + requipid + "_" + spatial_slot + "_2" + username_true + "] armor.feet from entity @s armor.feet");
    entity.server.runCommandSilent("execute as " + username + " as @s[tag=Armor_Swap_Load] at @s run data modify storage minecraft:" + requipid + "_" + spatial_slot + "_" + username_true + " ArmorItems insert 0 from entity @e[type=minecraft:armor_stand,limit=1,name=" + requipid + "_" + spatial_slot + "_2" + username_true + "] ArmorItems[]");
    entity.server.runCommandSilent("execute as " + username + " as @s[tag=Armor_Swap_Load] at @s run item replace entity @s armor.head with air");
    entity.server.runCommandSilent("execute as " + username + " as @s[tag=Armor_Swap_Load] at @s run item replace entity @s armor.chest with air");
    entity.server.runCommandSilent("execute as " + username + " as @s[tag=Armor_Swap_Load] at @s run item replace entity @s armor.legs with air");
    entity.server.runCommandSilent("execute as " + username + " as @s[tag=Armor_Swap_Load] at @s run item replace entity @s armor.feet with air");
    entity.server.runCommandSilent("execute as " + username + " as @s[tag=Armor_Swap_Load] at @s run kill @e[type=armor_stand,name=" + requipid + "_" + spatial_slot + "_2" + username_true + "]");





    entity.server.runCommandSilent("execute as " + username + " at @s run item replace entity @s armor.head from entity @e[type=minecraft:armor_stand,limit=1,name=" + requipid + "_" + spatial_slot + "_" + username_true + "] armor.head");
    entity.server.runCommandSilent("execute as " + username + " at @s run item replace entity @s armor.chest from entity @e[type=minecraft:armor_stand,limit=1,name=" + requipid + "_" + spatial_slot + "_" + username_true + "] armor.chest");
    entity.server.runCommandSilent("execute as " + username + " at @s run item replace entity @s armor.legs from entity @e[type=minecraft:armor_stand,limit=1,name=" + requipid + "_" + spatial_slot + "_" + username_true + "] armor.legs");
    entity.server.runCommandSilent("execute as " + username + " at @s run item replace entity @s armor.feet from entity @e[type=minecraft:armor_stand,limit=1,name=" + requipid + "_" + spatial_slot + "_" + username_true + "] armor.feet");
    entity.server.runCommandSilent("execute as " + username + " at @s run kill @e[type=armor_stand,name=" + requipid + "_" + spatial_slot + "_" + username_true + "]");
    entity.server.runCommandSilent("execute as " + username + " as @s[tag=!Armor_Swap_Load] at @s run data remove storage minecraft:" + requipid + "_" + spatial_slot + "_" + username_true + " ArmorItems");
    entity.server.runCommandSilent("execute as " + username + " as @s[tag=Armor_Swap_Load] at @s run tag @s remove Armor_Swap_Load");
    entity.server.runCommandSilent("execute as " + username + " as @s at @s run tag @s remove Armor_Store_Load");
	
	
	entity.server.runCommandSilent("execute as " + username + " at @s if entity @s[nbt={Inventory:[{Slot:103b}]}] run tag @s add Heroes.Spatial.Cost");
    entity.server.runCommandSilent("execute as " + username + " at @s if entity @s[nbt={Inventory:[{Slot:102b}]}] run tag @s add Heroes.Spatial.Cost");
    entity.server.runCommandSilent("execute as " + username + " at @s if entity @s[nbt={Inventory:[{Slot:101b}]}] run tag @s add Heroes.Spatial.Cost");
    entity.server.runCommandSilent("execute as " + username + " at @s if entity @s[nbt={Inventory:[{Slot:100b}]}] run tag @s add Heroes.Spatial.Cost");
	
    entity.server.runCommandSilent("execute as " + username + " at @s if entity @s[tag=Heroes.Spatial.Cost] run scoreboard players add @s Heroes.Concentration.Hold 1");
    entity.server.runCommandSilent("execute as " + username + " at @s if entity @s[tag=Heroes.Spatial.Cost] run scoreboard players add @s Heroes.Stamina.Remove 10");
    entity.server.runCommandSilent("execute as " + username + " at @s if entity @s[tag=Heroes.Spatial.Cost] run tag @s remove Heroes.Spatial.Cost");
	
    entity.server.runCommandSilent("execute as " + username + " as @s at @s run particle minecraft:end_rod ~ ~0.9 ~ 0.4 0.5 0.4 0 100");
    entity.server.runCommandSilent("execute as " + username + " as @s at @s run playsound minecraft:block.amethyst_block.resonate master @a ~ ~ ~ 1 2");
					}
            });
    });
})();