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
}

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


	EntityEvents.hurt("minecraft:player",event =>{
		
	
	if (event.entity.isPlayer() && abilityUtil.isEnabled(event.player, 'heroes:ocular_psychogenesis', 'Future_Sight')) {
		let entity = event.entity;
		let entry = event.entity.entry
		let server = event.server;
		let username = entity.getGameProfile().getName();
		let scoreboard = Utils.server.scoreboard;
        let scoreboard_obj = scoreboard.getObjective("Heroes.Psychogenesis.Dodge_Chance");
        let score = scoreboard.getOrCreatePlayerScore(username, scoreboard_obj);
        let value = score.getScore();
		const chance = (getRandomInt(0, 100) >= (100 - value));
		if (event.source.actual != null) {
			if (chance == true) {
		server.runCommandSilent(`tag ${username} add Heroes.Psychogenesis.Dodge`);
		server.runCommandSilent(`scoreboard players add ${username} Heroes.Stamina.Remove 22`);
		event.cancel()
			}
		}
	}	
	
	if (event.entity.isPlayer() && abilityUtil.isEnabled(event.player, 'heroes:ocular_psychogenesis', 'Reflex_Dodge')) {
		let entity = event.entity;
		let entry = event.entity.entry
		let server = event.server;
		let username = entity.getGameProfile().getName();
		let scoreboard = Utils.server.scoreboard;
        let scoreboard_obj = scoreboard.getObjective("Heroes.Psychogenesis.Dodge_Chance");
        let score = scoreboard.getOrCreatePlayerScore(username, scoreboard_obj);
        let value = score.getScore();
		const chance = (getRandomInt(0, 100) >= (100 - value));
		if (event.source.actual != null) {
			if (chance == true) {
		server.runCommandSilent(`tag ${username} add Heroes.Psychogenesis.Dodge`);
		server.runCommandSilent(`scoreboard players add ${username} Heroes.Stamina.Remove 22`);
		event.cancel()
			}
		}
	}	
	
	if (event.entity.isPlayer() && abilityUtil.isEnabled(event.player, 'heroes:danger_sense', 'Danger_Sense')) {
		let entity = event.entity;
		let entry = event.entity.entry
		let server = event.server;
		let username = entity.getGameProfile().getName();
		let scoreboard = Utils.server.scoreboard;
        let scoreboard_obj = scoreboard.getObjective("Heroes.Danger_Sense.Dodge_Chance");
        let score = scoreboard.getOrCreatePlayerScore(username, scoreboard_obj);
        let value = score.getScore();
		const chance = (getRandomInt(0, 100) >= (100 - value));
		if (event.source.actual != null) {
			if (chance == true) {
		server.runCommandSilent(`tag ${username} add Heroes.Danger_Sense.Dodge`);
		server.runCommandSilent(`scoreboard players add ${username} Heroes.Stamina.Remove 14`);
		event.cancel()
			}
		}
	}	
	
	if (event.entity.isPlayer() && abilityUtil.isEnabled(event.player, 'heroes:space_time_manipulation', 'Abil_3_Spatial_Defence')) {
		let entity = event.entity;
		let entry = event.entity.entry
		let server = event.server;
		let username = entity.getGameProfile().getName();
		if (event.source.actual != null) {
		server.runCommandSilent(`execute as ${username} at @s run playsound heroes:heroes.space_time_sfx player @a[distance=..10] ~ ~ ~ 0.4 1.5`);
		server.runCommandSilent(`scoreboard players add ${username} Heroes.Stamina.Remove 18`);
		event.cancel()
		}
	}	

	if (event.entity.isPlayer() && abilityUtil.isEnabled(event.player, 'heroes:space_time_manipulation', 'Abil_3_Timeline_Erasure')) {
		let source = event.source
		let entity = event.entity;
		let entry = event.entity.entry
		let server = event.server;
		let username = entity.getGameProfile().getName();
		
	if (event.source.actual != null) {
		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 function heroes:powers/auto_dodge_location`);
		server.runCommandSilent(`scoreboard players add ${username} Heroes.Stamina.Remove 88`);
		event.cancel()
		}
	}
	
		if (event.entity.isPlayer() && abilityUtil.isUnlocked(event.player, 'heroes:geokinetic_armor', 'Geo_Orbit_Ability_Abil')) {
		let entity = event.entity;
		let entry = event.entity.entry
		let server = event.server;
		let source = event.source.actual
		let damage = event.damage
		let username = entity.getGameProfile().getName();
		
		if (source != null) {
				let current_x = entity.x
				let current_z = entity.z
				
				let its_x = source.x
				let its_z = source.z
				
				let motion_x = current_x - its_x
				let motion_z = current_z - its_z
				
		
		let scoreboard = Utils.server.scoreboard;
        let scoreboard_obj = scoreboard.getObjective("Heroes.Geo_Armor.Orbit.Amount");
        let score = scoreboard.getOrCreatePlayerScore(username, scoreboard_obj);
        let value = score.getScore();
		if (value >= 1) {
		event.source.actual.addMotion((1.5 * (motion_x + 0.0001)) * -1, 0.5, (1.5 * (motion_z + 0.0001)) * -1);
		if (source.isPlayer()) {
		source.connection.send(new ClientboundSetEntityMotionPacket(entity));
		}
		server.runCommandSilent(`execute as ${username} at @s run playsound minecraft:entity.generic.explode master @a ~ ~ ~ 0.6 1.2`);
		server.runCommandSilent(`execute as ${username} at @s run particle minecraft:explosion ~ ~1 ~ 0.5 0.5 0.5 0 3`);
		server.runCommandSilent(`execute as ${username} at @s as @e[distance=..4,name=!${username}] run damage @s 10 minecraft:player_explosion by ${username}`);
		server.runCommandSilent(`scoreboard players remove ${username} Heroes.Geo_Armor.Orbit.Amount 1`);
		event.cancel()
			}
		}

    }
	
	
	if (event.entity.isPlayer() && abilityUtil.isEnabled(event.player, 'heroes:augmented_movement_potential', 'Reflex_Dodge')) {
		let damage = event.damage
		let source = event.source
		let entity = event.entity;
		let entry = event.entity.entry
		let server = event.server;
		let health = entity.health;
		let username = entity.getGameProfile().getName();
		
	if (event.source.actual != null) {
		server.runCommandSilent(`scoreboard players add ${username} Heroes.Stamina.Remove 33`);
		server.runCommandSilent(`tag ${username} add Heroes.Speed.Dodge`);
		event.cancel()
		}
	}

if (event.entity.isPlayer() && abilityUtil.isEnabled(event.player, 'heroes:base_power', 'Death.State')) {
		let damage = event.damage
		let source = event.source
		let entity = event.entity;
		let entry = event.entity.entry
		let server = event.server;
		let health = entity.health;
		let username = entity.getGameProfile().getName();
	
		event.cancel()
	}	

})
})();



