From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: William Blake Galbreath <blake.galbreath@gmail.com>
Date: Sun, 14 Jul 2019 19:52:47 -0500
Subject: [PATCH] Mending mends most damages equipment first


diff --git a/src/main/java/net/minecraft/world/entity/ExperienceOrb.java b/src/main/java/net/minecraft/world/entity/ExperienceOrb.java
index 1caf10ecf949e0f465ffe573f3bed1a3c5733a7f..8b6614bde99b17db2e161f3fe4ab2491ff7b679c 100644
--- a/src/main/java/net/minecraft/world/entity/ExperienceOrb.java
+++ b/src/main/java/net/minecraft/world/entity/ExperienceOrb.java
@@ -319,7 +319,7 @@ public class ExperienceOrb extends Entity {
     }
 
     private int repairPlayerItems(Player player, int amount) {
-        Entry<EquipmentSlot, ItemStack> entry = EnchantmentHelper.getRandomItemWith(Enchantments.MENDING, (LivingEntity) player, ItemStack::isDamaged);
+        Entry<EquipmentSlot, ItemStack> entry = level.purpurConfig.useBetterMending ? EnchantmentHelper.getMostDamagedEquipment(Enchantments.MENDING, player) : EnchantmentHelper.getRandomItemWith(Enchantments.MENDING, (LivingEntity) player, ItemStack::isDamaged); // Purpur
 
         if (entry != null) {
             ItemStack itemstack = (ItemStack) entry.getValue();
diff --git a/src/main/java/net/minecraft/world/item/ItemStack.java b/src/main/java/net/minecraft/world/item/ItemStack.java
index 3e5e358e24bd84a05785a9391526f316475e95ff..7b1393f3c1acf29318de8222ef54032a449b03f3 100644
--- a/src/main/java/net/minecraft/world/item/ItemStack.java
+++ b/src/main/java/net/minecraft/world/item/ItemStack.java
@@ -511,6 +511,16 @@ public final class ItemStack {
         return this.isDamageableItem() && this.getDamageValue() > 0;
     }
 
+    // Purpur start
+    public float getDamagePercent() {
+        if (isDamaged()) {
+            return (float) getDamageValue() / (float) getItem().getMaxDamage();
+        } else {
+            return 0F;
+        }
+    }
+    // Purpur end
+
     public int getDamageValue() {
         return this.tag == null ? 0 : this.tag.getInt("Damage");
     }
diff --git a/src/main/java/net/minecraft/world/item/enchantment/EnchantmentHelper.java b/src/main/java/net/minecraft/world/item/enchantment/EnchantmentHelper.java
index d439e8ce87bf7da03683a336941c7673b8b166e4..955e482e4bc83f2b777cd2e4254c40beef08853b 100644
--- a/src/main/java/net/minecraft/world/item/enchantment/EnchantmentHelper.java
+++ b/src/main/java/net/minecraft/world/item/enchantment/EnchantmentHelper.java
@@ -270,6 +270,29 @@ public class EnchantmentHelper {
         return getItemEnchantmentLevel(Enchantments.CHANNELING, stack) > 0;
     }
 
+    // Purpur start
+    @Nullable
+    public static Entry<EquipmentSlot, ItemStack> getMostDamagedEquipment(Enchantment enchantment, LivingEntity entity) {
+        Map<EquipmentSlot, ItemStack> map = enchantment.getSlotItems(entity);
+        if (map.isEmpty()) {
+            return null;
+        }
+        Entry<EquipmentSlot, ItemStack> item = null;
+        float maxPercent = 0F;
+        for (Entry<EquipmentSlot, ItemStack> entry : map.entrySet()) {
+            ItemStack itemstack = entry.getValue();
+            if (!itemstack.isEmpty() && itemstack.isDamaged() && getItemEnchantmentLevel(enchantment, itemstack) > 0) {
+                float percent = itemstack.getDamagePercent();
+                if (item == null || percent > maxPercent) {
+                    item = entry;
+                    maxPercent = percent;
+                }
+            }
+        }
+        return item;
+    }
+    // Purpur end
+
     @Deprecated public static @javax.annotation.Nonnull ItemStack getRandomEquippedItemWithEnchant(Enchantment enchantment, LivingEntity entityliving) {
         Entry<EquipmentSlot, ItemStack> entry = getRandomItemWith(enchantment, entityliving);
         return entry != null ? entry.getValue() : ItemStack.EMPTY;
diff --git a/src/main/java/net/pl3x/purpur/PurpurWorldConfig.java b/src/main/java/net/pl3x/purpur/PurpurWorldConfig.java
index 29f50b0144f28c03af1114bed4473b5bbcf34677..a5572f018bbc5f12d97a1466bd7e34f7999d99e8 100644
--- a/src/main/java/net/pl3x/purpur/PurpurWorldConfig.java
+++ b/src/main/java/net/pl3x/purpur/PurpurWorldConfig.java
@@ -211,6 +211,7 @@ public class PurpurWorldConfig {
         });
     }
 
+    public boolean useBetterMending = false;
     public boolean boatEjectPlayersOnLand = false;
     public boolean disableDropsOnCrammingDeath = false;
     public boolean milkCuresBadOmen = true;
@@ -218,6 +219,7 @@ public class PurpurWorldConfig {
     public double voidDamageHeight = -64.0D;
     public double voidDamageDealt = 4.0D;
     private void miscGameplayMechanicsSettings() {
+        useBetterMending = getBoolean("gameplay-mechanics.use-better-mending", useBetterMending);
         boatEjectPlayersOnLand = getBoolean("gameplay-mechanics.boat.eject-players-on-land", boatEjectPlayersOnLand);
         disableDropsOnCrammingDeath = getBoolean("gameplay-mechanics.disable-drops-on-cramming-death", disableDropsOnCrammingDeath);
         milkCuresBadOmen = getBoolean("gameplay-mechanics.milk-cures-bad-omen", milkCuresBadOmen);
