ServerEvents.commandRegistry(event => {
    const { commands: Commands, arguments: Arguments} = event

    event.register(
        Commands.literal("spatialtravel")
        .then(Commands.argument('x', Arguments.STRING.create(event))
            .then(Commands.argument('y', Arguments.STRING.create(event))
				.then(Commands.argument('z', Arguments.STRING.create(event))
                    .executes(ctx => {
                    let x = Arguments.STRING.getResult(ctx, "x")
                    let y = Arguments.STRING.getResult(ctx, "y")
					let z = Arguments.STRING.getResult(ctx, "z")


                        let server = ctx.source.getServer()
                        let player = ctx.source.player
						let dim = player.getLevel().getDimension()
						let current_x = player.getX();
						let current_y = player.getY();
						let current_z = player.getZ();
						
						let username = player.getGameProfile().getName();
						let scoreboard = Utils.server.scoreboard;
					
						let scoreboard_obj = scoreboard.getObjective('Heroes.Stamina');
						let score = scoreboard.getOrCreatePlayerScore(username, scoreboard_obj);
						let value = score.getScore();
						
						if (dim != "heroes:pocket_dimension") {
						
						server.runCommandSilent(`execute as ${username} at @s store result score @s Heroes.Spatial.Count run execute if entity @e[distance=0.01..3,type=!item,type=!armor_stand,type=!palladium:suit_stand,type=!item_frame,type=!glow_item_frame]`)
						let scoreboard_obj_spa = scoreboard.getObjective('Heroes.Spatial.Count');
						let score_spa = scoreboard.getOrCreatePlayerScore(username, scoreboard_obj_spa);
						let value_spa = score_spa.getScore();
						
						let stam_cost_og = JavaMath.ceil(Math.abs(current_x -x) + Math.abs(current_y -y) + Math.abs(current_z -z))
						let stam_cost = JavaMath.ceil(stam_cost_og / 25 + (value_spa * 25))
					
						
						if (!abilityUtil.isEnabled(player, 'heroes:space_time_manipulation', 'Spatial_Travel')) {
						server.runCommandSilent(`execute as ${username} at @s run tellraw @s {"text":"You lack the ability to use this power..","bold":true,"color":"dark_purple"}`)
						}
						if (abilityUtil.isEnabled(player, 'heroes:space_time_manipulation', 'Spatial_Travel') && value >= stam_cost) {
						server.runCommandSilent(`execute as ${username} at @s run playsound heroes:heroes.space_time_sfx player @a[distance=..10]`)
                        server.runCommandSilent(`execute as ${username} at @s run scoreboard players add @s Heroes.Stamina.Remove ${stam_cost}`)
						server.runCommandSilent(`execute as ${username} at @s positioned ${x} ${y} ${z} run playsound heroes:heroes.space_time_sfx player @a[distance=..10]`)
                        server.runCommandSilent(`execute as ${username} at @s as @e[distance=..3] run tp @s ${x} ${y} ${z}`)
						}
						if (abilityUtil.isEnabled(player, 'heroes:space_time_manipulation', 'Spatial_Travel') && value < stam_cost) {
						server.runCommandSilent(`execute as ${username} at @s run tellraw @s {"text":"You attempt to travel but dont have the required energy. Requires : (${stam_cost}) You have (${value})","bold":true,"color":"red"}`)
							}
						}
                    return 1
                })
            )
	)
        )
    )
})