@EventHandler
public void onDamage(EntityDamageByEntityEvent event)
{
if ((event.getEntity() instanceof Player)) {
Player p = (Player) event.getEntity();
int finalLvl = 0;
int finalPoint = 0;
if (p.getEquipment().getHelmet() != null){
ItemStack Helmet = p.getEquipment().getHelmet();
Material type = Helmet.getType();
if (type == Material.LEATHER_HELMET || type == Material.CHAINMAIL_HELMET || type == Material.IRON_HELMET){
finalPoint = finalPoint + 2;
}
if (type == Material.GOLD_HELMET || type == Material.DIAMOND_HELMET){
finalPoint = finalPoint + 3;
}
if (Helmet.containsEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL)){
finalLvl = finalLvl + Helmet.getEnchantmentLevel(Enchantment.PROTECTION_ENVIRONMENTAL);
}
if (Helmet.hasItemMeta() && Helmet.getItemMeta().hasLore() &&
Helmet.getItemMeta().getLore().get(0).equals(ChatColor.DARK_GRAY + "Armor")){
ItemMeta meta = Helmet.getItemMeta();
List<String> Lore = Helmet.getItemMeta().getLore();
String durability = ChatColor.stripColor(Lore.get(1));
Integer dur = Integer.valueOf(durability.split("/")[0]);
Integer maxdur = Integer.valueOf(durability.split("/")[1]);
if (dur <= 0) {
p.getEquipment().setHelmet(null);
}
else{
Random rand = new Random();
int r = rand.nextInt(100) + 1;
if (Helmet.getEnchantments().containsKey(Enchantment.DURABILITY)){
float chance = 100/(Helmet.getEnchantmentLevel(Enchantment.DURABILITY) + 1);
if (r > 0 && r < (int) chance){
dur = dur - 1;
}
}
else {
dur = dur - 1;
}
durability = ChatColor.WHITE + Integer.toString(dur) + "/" + Integer.toString(maxdur);
Lore.set(1, durability);
meta.setLore(Lore);
Helmet.setItemMeta(meta);
Helmet.setDurability((short) 1);
}
}
}
if (p.getEquipment().getChestplate() != null){
ItemStack Chestplate = p.getEquipment().getChestplate();
Material type = Chestplate.getType();
if (type == Material.LEATHER_CHESTPLATE){
finalPoint = finalPoint + 5;
}
if (type == Material.CHAINMAIL_CHESTPLATE || type == Material.IRON_CHESTPLATE){
finalPoint = finalPoint + 6;
}
if (type == Material.GOLD_CHESTPLATE || type == Material.DIAMOND_CHESTPLATE){
finalPoint = finalPoint + 8;
}
if (Chestplate.containsEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL)){
finalLvl = finalLvl + Chestplate.getEnchantmentLevel(Enchantment.PROTECTION_ENVIRONMENTAL);
}
if (Chestplate.hasItemMeta() && Chestplate.getItemMeta().hasLore() &&
Chestplate.getItemMeta().getLore().get(0).equals(ChatColor.DARK_GRAY + "Armor")){
ItemMeta meta = Chestplate.getItemMeta();
List<String> Lore = Chestplate.getItemMeta().getLore();
String durability = ChatColor.stripColor(Lore.get(1));
Integer dur = Integer.valueOf(durability.split("/")[0]);
Integer maxdur = Integer.valueOf(durability.split("/")[1]);
if (dur <= 0) {
p.getEquipment().setChestplate(null);
}
else{
Random rand = new Random();
int r = rand.nextInt(100) + 1;
if (Chestplate.getEnchantments().containsKey(Enchantment.DURABILITY)){
float chance = 100/(Chestplate.getEnchantmentLevel(Enchantment.DURABILITY) + 1);
if (r > 0 && r < (int) chance){
dur = dur - 1;
}
}
else {
dur = dur - 1;
}
durability = ChatColor.WHITE + Integer.toString(dur) + "/" + Integer.toString(maxdur);
Lore.set(1, durability);
meta.setLore(Lore);
Chestplate.setItemMeta(meta);
Chestplate.setDurability((short) 1);
}
}
}
if (p.getEquipment().getLeggings() != null){
ItemStack Leggings = p.getEquipment().getLeggings();
Material type = Leggings.getType();
if (type == Material.LEATHER_LEGGINGS){
finalPoint = finalPoint + 4;
}
if (type == Material.CHAINMAIL_LEGGINGS || type == Material.IRON_LEGGINGS){
finalPoint = finalPoint + 5;
}
if (type == Material.GOLD_LEGGINGS || type == Material.DIAMOND_LEGGINGS){
finalPoint = finalPoint + 6;
}
if (Leggings.containsEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL)){
finalLvl = finalLvl + Leggings.getEnchantmentLevel(Enchantment.PROTECTION_ENVIRONMENTAL);
}
if (Leggings.hasItemMeta() && Leggings.getItemMeta().hasLore() &&
Leggings.getItemMeta().getLore().get(0).equals(ChatColor.DARK_GRAY + "Armor")){
ItemMeta meta = Leggings.getItemMeta();
List<String> Lore = Leggings.getItemMeta().getLore();
String durability = ChatColor.stripColor(Lore.get(1));
Integer dur = Integer.valueOf(durability.split("/")[0]);
Integer maxdur = Integer.valueOf(durability.split("/")[1]);
if (dur <= 0) {
p.getEquipment().setLeggings(null);
}
else{
Random rand = new Random();
int r = rand.nextInt(100) + 1;
if (Leggings.getEnchantments().containsKey(Enchantment.DURABILITY)){
float chance = 100/(Leggings.getEnchantmentLevel(Enchantment.DURABILITY) + 1);
if (r > 0 && r < (int) chance){
dur = dur - 1;
}
}
else {
dur = dur - 1;
}
durability = ChatColor.WHITE + Integer.toString(dur) + "/" + Integer.toString(maxdur);
Lore.set(1, durability);
meta.setLore(Lore);
Leggings.setItemMeta(meta);
Leggings.setDurability((short) 1);
}
}
}
if (p.getEquipment().getBoots() != null){
ItemStack Boots = p.getEquipment().getBoots();
Material type = Boots.getType();
if (type == Material.LEATHER_BOOTS){
finalPoint = finalPoint + 1;
}
if (type == Material.CHAINMAIL_BOOTS || type == Material.IRON_BOOTS){
finalPoint = finalPoint + 2;
}
if (type == Material.GOLD_BOOTS || type == Material.DIAMOND_BOOTS){
finalPoint = finalPoint + 3;
}
if (Boots.containsEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL)){
finalLvl = finalLvl + Boots.getEnchantmentLevel(Enchantment.PROTECTION_ENVIRONMENTAL);
}
if (Boots.hasItemMeta() && Boots.getItemMeta().hasLore() &&
Boots.getItemMeta().getLore().get(0).equals(ChatColor.DARK_GRAY + "Armor")){
ItemMeta meta = Boots.getItemMeta();
List<String> Lore = Boots.getItemMeta().getLore();
String durability = ChatColor.stripColor(Lore.get(1));
Integer dur = Integer.valueOf(durability.split("/")[0]);
Integer maxdur = Integer.valueOf(durability.split("/")[1]);
if (dur <= 0) {
p.getEquipment().setBoots(null);
}
else{
Random rand = new Random();
int r = rand.nextInt(100) + 1;
if (Boots.getEnchantments().containsKey(Enchantment.DURABILITY)){
float chance = 100/(Boots.getEnchantmentLevel(Enchantment.DURABILITY) + 1);
if (r > 0 && r < (int) chance){
dur = dur - 1;
}
}
else {
dur = dur - 1;
}
durability = ChatColor.WHITE + Integer.toString(dur) + "/" + Integer.toString(maxdur);
Lore.set(1, durability);
meta.setLore(Lore);
Boots.setItemMeta(meta);
Boots.setDurability((short) 1);
}
}
}
double health = p.getHealth();
double defence = finalLvl / 2 + finalPoint / 1.5;
//getLogger().info("defence: " + Double.toString(defence));
//getLogger().info("damage: " + Double.toString(damage));
if ((defence > damage || defence == damage) & damage != 0){
Random rand = new Random();
int r = rand.nextInt(100) + 1;
if (r > 0 && r < 15 - (int) finalLvl / 4){
health = p.getHealth() - 1;
}
damage = 0;
}
//getLogger().info("bound: " + Double.toString(damage - defence));
if ((damage > defence) & defence != 0) {
if (finalLvl != 0){
health = p.getHealth() - (damage - defence) / (finalLvl / 4);
} else
if (finalPoint >= 10){
health = p.getHealth() - (damage - defence) / (finalPoint / 10);
getLogger().info("point = " + Double.toString(finalPoint / 10) + "; final = " + Integer.toString(finalPoint));
getLogger().info("HelthD = " + Double.toString((damage - defence) / (finalPoint / 10)));
}
else {
health = p.getHealth() - damage;
}
getLogger().info("level: " + Double.toString(finalLvl / 4));
damage = 0;
}
if (health >= 0){ p.setHealth(health);}
else {p.setHealth(0);}
}
event.setDamage(damage);
}
}