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

    StartupEvents.registry('palladium:abilities', (event) => {
        event.create('heroes:spatial_armaments_weapon')
            .icon(palladium.createItemIcon('palladium:vibranium_circuit'))
			.addProperty("Equip_Storage_Id", "string", "Heroes_Item", "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.mainHandItem.nbt != null) {
		entity.mainHandItem.nbt.Damage = entity.mainHandItem.nbt.Damage - 10
		if (entity.mainHandItem.nbt.Damage < 0) {
				entity.mainHandItem.nbt.Damage = 0
			}
		}
		if (entity.offHandItem.nbt != null) {
		entity.offHandItem.nbt.Damage = entity.offHandItem.nbt.Damage - 10
		if (entity.offHandItem.nbt.Damage < 0) {
				entity.offHandItem.nbt.Damage = 0
			}
		}
	}
	entity.server.runCommandSilent("execute as " + username + " at @s if entity @s[nbt={SelectedItem:{}}] run tag @s add Heroes.Spatial.Cost");
	entity.server.runCommandSilent("execute as " + username + " at @s if entity @s[nbt=!{Inventory:[{Slot:-106}]}] 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] HandItems set from storage minecraft:" + requipid + "_" + spatial_slot + "_" + username_true + " HandItems");


    entity.server.runCommandSilent("execute as " + username + " at @s if entity @s[nbt={SelectedItem:{}}] run tag @s add Armor_Swap_Load");
    entity.server.runCommandSilent("execute as " + username + " at @s if entity @s[nbt=!{Inventory:[{Slot:-106}]}] 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 + " HandItems");
    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 + "] weapon.mainhand from entity @s weapon.mainhand");
    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 + "] weapon.offhand from entity @s weapon.offhand");
    entity.server.runCommandSilent("execute as " + username + " as @s[tag=Armor_Swap_Load] at @s run data modify storage minecraft:" + requipid + "_" + spatial_slot + "_" + username_true + " HandItems insert 0 from entity @e[type=minecraft:armor_stand,limit=1,name=" + requipid + "_" + spatial_slot + "_2" + username_true + "] HandItems[]");
    entity.server.runCommandSilent("execute as " + username + " as @s[tag=Armor_Swap_Load] at @s run item replace entity @s weapon.mainhand with air");
    entity.server.runCommandSilent("execute as " + username + " as @s[tag=Armor_Swap_Load] at @s run item replace entity @s weapon.offhand 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 weapon.mainhand from entity @e[type=minecraft:armor_stand,limit=1,name=" + requipid + "_" + spatial_slot + "_" + username_true + "] weapon.mainhand");
    entity.server.runCommandSilent("execute as " + username + " at @s run item replace entity @s weapon.offhand from entity @e[type=minecraft:armor_stand,limit=1,name=" + requipid + "_" + spatial_slot + "_" + username_true + "] weapon.offhand");
    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 + " HandItems");
    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={SelectedItem:{}}] run tag @s add Heroes.Spatial.Cost");
	entity.server.runCommandSilent("execute as " + username + " at @s if entity @s[nbt=!{Inventory:[{Slot:-106}]}] 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 5");
    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");
					}
            });
    });
})();