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

    event.register(
        Commands.literal("clairvoyance")
		.executes(ctx => {
						
						let player = ctx.source.player
						let server = ctx.source.getServer()
						if (abilityUtil.isEnabled(player, 'heroes:clairvoyance', 'genetic_marker')) {
						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();
                        let position = ctx.source.getPosition()
                        let dimension = ctx.source.getLevel().getDimension()
						
						let entity = find_random(player)
						let item = player.mainHandItem;
						let item_off = player.offHandItem;
						if (item == null) {
						server.runCommandSilent(`execute as ${player.uuid} at @s run tellraw @s {"text":"You require a focus..","bold":true,"color":"red"}`)
						}
						
						if (item != null && entity == false) {
						server.runCommandSilent(`execute as ${player.uuid} at @s run tellraw @s {"text":"You try as you might but are unable to find the target..","bold":true,"color":"red"}`)
						}
						if (item != null && entity != false) {
						let dimension = entity.getLevel().getDimension();
						let true_dimension = dimension.toString()
						let entity_x = entity.x
						let entity_y = entity.y
						let entity_z = entity.z
						let entity_name = entity.name.getString()
						if (abilityUtil.isEnabled(player, 'heroes:clairvoyance', 'Clairvoyance_Cords')) {
							if (item == "paper" && item_off == "ink_sac" && value >= 50) {
								server.runCommandSilent(`execute as ${username} at @s run scoreboard players add @s Heroes.Stamina.Remove 50`)
						server.runCommandSilent(`execute as ${username} at @s run item modify entity @s weapon.mainhand heroes:item_remove`)
						server.runCommandSilent(`execute as ${username} at @s run item modify entity @s weapon.offhand heroes:item_remove`)
						server.runCommandSilent(`execute as ${username} at @s run give @s minecraft:paper{display:{Name:'[{"text":"${entity_name} was at X:${Math.ceil(entity_x)} Y:${Math.ceil(entity_y)} Z:${Math.ceil(entity_z)} in ${true_dimension}","italic":false}]'},HideFlags:1}`)
									}
						}
						if (item == "compass" && value >= 25) {
							server.runCommandSilent(`execute as ${username} at @s run scoreboard players add @s Heroes.Stamina.Remove 25`)
						server.runCommandSilent(`execute as ${username} at @s run item modify entity @s weapon.mainhand heroes:item_remove`)
						server.runCommandSilent(`execute as ${username} at @s run give @s minecraft:compass{display:{Name:'[{"text":"${entity_name}s Last Location Compass","italic":false}]'},HideFlags:1,LodestoneDimension:"${true_dimension}", LodestoneTracked:0b, LodestonePos:{X:${entity_x}, Y:${entity_y}, Z:${entity_z}}}`)
									}
								}
						} else {
							server.runCommandSilent(`execute as ${player.uuid} at @s run tellraw @s {"text":"You lack the ability to use this power..","bold":true,"color":"dark_purple"}`)
						}
                    return 1
                })
				
		.then(Commands.argument('entity_type', Arguments.RESOURCE_LOCATION.create(event))
                    .executes(ctx => {
						const entity_type = Arguments.RESOURCE_LOCATION.getResult(ctx, "entity_type")
						
						let player = ctx.source.player
						let server = ctx.source.getServer()
						if (abilityUtil.isEnabled(player, 'heroes:clairvoyance', 'Clairvoyance')) {
						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();
                        let position = ctx.source.getPosition()
                        let dimension = ctx.source.getLevel().getDimension()
						
						let entity = entityfind(entity_type, player)
						let item = player.mainHandItem;
						let item_off = player.offHandItem;
						if (item == null) {
						server.runCommandSilent(`execute as ${player.uuid} at @s run tellraw @s {"text":"You require a focus..","bold":true,"color":"red"}`)
						}
						
						if (item != null && entity == false) {
						server.runCommandSilent(`execute as ${player.uuid} at @s run tellraw @s {"text":"You try as you might but are unable to find the target..","bold":true,"color":"red"}`)
						}
						if (item != null && entity != false) {
						let dimension = entity.getLevel().getDimension();
						let true_dimension = dimension.toString()
						let entity_x = entity.x
						let entity_y = entity.y
						let entity_z = entity.z
						let entity_name = entity.name.getString()
						if (abilityUtil.isEnabled(player, 'heroes:clairvoyance', 'Clairvoyance_Cords')) {
							if (item == "paper" && item_off == "ink_sac" && value >= 75) {
								server.runCommandSilent(`execute as ${username} at @s run scoreboard players add @s Heroes.Stamina.Remove 75`)
						server.runCommandSilent(`execute as ${username} at @s run item modify entity @s weapon.mainhand heroes:item_remove`)
						server.runCommandSilent(`execute as ${username} at @s run item modify entity @s weapon.offhand heroes:item_remove`)
						server.runCommandSilent(`execute as ${username} at @s run give @s minecraft:paper{display:{Name:'[{"text":"${entity_name} was at X:${Math.ceil(entity_x)} Y:${Math.ceil(entity_y)} Z:${Math.ceil(entity_z)} in ${true_dimension}","italic":false}]'},HideFlags:1}`)
									}
						}
						if (item == "compass" && value >= 50) {
							server.runCommandSilent(`execute as ${username} at @s run scoreboard players add @s Heroes.Stamina.Remove 50`)
						server.runCommandSilent(`execute as ${username} at @s run item modify entity @s weapon.mainhand heroes:item_remove`)
						server.runCommandSilent(`execute as ${username} at @s run give @s minecraft:compass{display:{Name:'[{"text":"${entity_name}s Last Location Compass","italic":false}]'},HideFlags:1,LodestoneDimension:"${true_dimension}", LodestoneTracked:0b, LodestonePos:{X:${entity_x}, Y:${entity_y}, Z:${entity_z}}}`)
									}
								}
						} else {
							server.runCommandSilent(`execute as ${player.uuid} at @s run tellraw @s {"text":"You lack the ability to use this power..","bold":true,"color":"dark_purple"}`)
						}
                    return 1
                })
				.then(Commands.argument('name', Arguments.STRING.create(event))
				 .executes(ctx => {
						const entity_type = Arguments.RESOURCE_LOCATION.getResult(ctx, "entity_type")
						const name = Arguments.STRING.getResult(ctx, "name")
						
						let player = ctx.source.player
						let server = ctx.source.getServer()
						if (abilityUtil.isEnabled(player, 'heroes:clairvoyance', 'Clairvoyance_Name')) {
						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();
                        let position = ctx.source.getPosition()
                        let dimension = ctx.source.getLevel().getDimension()
						
						let entity = entityfindName(entity_type, name, player)
						if (entity == false) {
						server.runCommandSilent(`execute as ${player.uuid} at @s run tellraw @s {"text":"You try as you might but are unable to find the target..","bold":true,"color":"red"}`)
						}
                        let item = player.mainHandItem;
						let item_off = player.offHandItem;
						if (item != null && entity != false) {
						let dimension = entity.getLevel().getDimension();
						let true_dimension = dimension.toString()
						let entity_x = entity.x
						let entity_y = entity.y
						let entity_z = entity.z
						let entity_name = entity.name.getString()
						if (abilityUtil.isEnabled(player, 'heroes:clairvoyance', 'Clairvoyance_Cords')) {
							if (item == "paper" && item_off == "ink_sac" && value >= 100) {
								server.runCommandSilent(`execute as ${username} at @s run scoreboard players add @s Heroes.Stamina.Remove 100`)
						server.runCommandSilent(`execute as ${username} at @s run item modify entity @s weapon.mainhand heroes:item_remove`)
						server.runCommandSilent(`execute as ${username} at @s run item modify entity @s weapon.offhand heroes:item_remove`)
						server.runCommandSilent(`execute as ${username} at @s run give @s minecraft:paper{display:{Name:'[{"text":"${entity_name} was at X:${Math.ceil(entity_x)} Y:${Math.ceil(entity_y)} Z:${Math.ceil(entity_z)} in ${true_dimension}","italic":false}]'},HideFlags:1}`)
									}
						}
						if (item == "compass" && value >= 75) {
						server.runCommandSilent(`execute as ${username} at @s run scoreboard players add @s Heroes.Stamina.Remove 75`)
						server.runCommandSilent(`execute as ${username} at @s run clear @s minecraft:compass 1`)
						server.runCommandSilent(`execute as ${username} at @s run give @s minecraft:compass{display:{Name:'[{"text":"${entity_name}s Last Location Compass","italic":false}]'},HideFlags:1,LodestoneDimension:"${true_dimension}", LodestoneTracked:0b, LodestonePos:{X:${entity_x}, Y:${entity_y}, Z:${entity_z}}}`)
								}
							}
						} else {
							server.runCommandSilent(`execute as ${player.uuid} at @s run tellraw @s {"text":"You lack the ability to use this power..","bold":true,"color":"dark_purple"}`)
						}
						
						
                    return 1
                })
			)
		)
    )
})
function find_random(source) {
    let entities = Utils.server.getEntities();
	let random_power = (getRandomInt(0, entities.size()));
	for (let i = 0; i < entities.size(); i++) {
	if (i === random_power) {
	return entities.get(i)
						}
			}
    return false;
}

function entityfind(type, source) {
    let entities = Utils.server.getEntities();
	let check = 0
	let ticks = 0
	randomloop: for (let random = 0; random < 3; random++) {
	let random_power = (getRandomInt(0, entities.size()));
	for (let i = random_power; i < entities.size(); i++) {
	if (entities.get(i).type === type) {
	check = 1
	return entities.get(i)
						}
					}
	if (check == 0) {
		ticks += 1
		if (ticks <= 2) {
		random = -1
		continue randomloop;
		ticks
					}
				}
	}
    return false;
}

function entityfindName(type, name, source) {
    let entities = Utils.server.getEntities();
	for (let i = 0; i < entities.size(); i++) {
	if (entities.get(i).type === type && entities.get(i).name.getString() == name) {
	return entities.get(i)
						}
			}
    return false;
}

function getRandomInt(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min) + min); // The maximum is exclusive and the minimum is inclusive
}