Mending mends most damages equipment first

This commit is contained in:
William Blake Galbreath
2025-01-05 19:16:43 -08:00
committed by granny
parent 1f36eb12ed
commit 3e4de1e628
5 changed files with 87 additions and 130 deletions

View File

@@ -0,0 +1,11 @@
--- a/net/minecraft/world/entity/ExperienceOrb.java
+++ b/net/minecraft/world/entity/ExperienceOrb.java
@@ -339,7 +_,7 @@
}
private int repairPlayerItems(ServerPlayer player, int value) {
- Optional<EnchantedItemInUse> randomItemWith = EnchantmentHelper.getRandomItemWith(
+ Optional<EnchantedItemInUse> randomItemWith = level().purpurConfig.useBetterMending ? EnchantmentHelper.getMostDamagedItemWith(EnchantmentEffectComponents.REPAIR_WITH_XP, player) : EnchantmentHelper.getRandomItemWith( // Purpur - Add option to mend the most damaged equipment first
EnchantmentEffectComponents.REPAIR_WITH_XP, player, ItemStack::isDamaged
);
if (randomItemWith.isPresent()) {

View File

@@ -0,0 +1,29 @@
--- a/net/minecraft/world/item/ItemStack.java
+++ b/net/minecraft/world/item/ItemStack.java
@@ -627,6 +_,26 @@
return this.isDamageableItem() && this.getDamageValue() > 0;
}
+ // Purpur start - Add option to mend the most damaged equipment first
+ public float getDamagePercent() {
+ if (this.has(DataComponents.UNBREAKABLE)) {
+ return 0.0F;
+ }
+
+ final int maxDamage = this.getOrDefault(DataComponents.MAX_DAMAGE, 0);
+ if (maxDamage == 0) {
+ return 0.0F;
+ }
+
+ final int damage = this.getOrDefault(DataComponents.DAMAGE, 0);
+ if (damage == 0) {
+ return 0.0F;
+ }
+
+ return (float) damage / maxDamage;
+ }
+ // Purpur end - Add option to mend the most damaged equipment first
+
public int getDamageValue() {
return Mth.clamp(this.getOrDefault(DataComponents.DAMAGE, Integer.valueOf(0)), 0, this.getMaxDamage());
}

View File

@@ -1,6 +1,6 @@
--- a/net/minecraft/world/item/enchantment/EnchantmentHelper.java
+++ b/net/minecraft/world/item/enchantment/EnchantmentHelper.java
@@ -602,4 +_,14 @@
@@ -602,4 +_,58 @@
interface EnchantmentVisitor {
void accept(Holder<Enchantment> enchantment, int level);
}
@@ -14,4 +14,48 @@
+ return getItemEnchantmentLevel(getEnchantmentHolder(enchantment), stack);
+ }
+ // Purpur end - Enchantment convenience methods
+
+ // Purpur start - Add option to mend the most damaged equipment first
+ public static Optional<EnchantedItemInUse> getMostDamagedItemWith(DataComponentType<?> componentType, LivingEntity entity) {
+ ItemStack maxStack = null;
+ EquipmentSlot maxSlot = null;
+ float maxPercent = 0.0F;
+
+ equipmentSlotLoop:
+ for (EquipmentSlot equipmentSlot : EquipmentSlot.values()) {
+ ItemStack stack = entity.getItemBySlot(equipmentSlot);
+
+ // do not even check enchantments for item with lower or equal damage percent
+ float percent = stack.getDamagePercent();
+ if (percent <= maxPercent) {
+ continue;
+ }
+
+ ItemEnchantments itemEnchantments = stack.getOrDefault(DataComponents.ENCHANTMENTS, ItemEnchantments.EMPTY);
+
+ for (Entry<Holder<Enchantment>> entry : itemEnchantments.entrySet()) {
+ Enchantment enchantment = entry.getKey().value();
+
+ net.minecraft.core.component.DataComponentMap effects = enchantment.effects();
+ if (!effects.has(componentType)) {
+ // try with another enchantment
+ continue;
+ }
+
+ if (enchantment.matchingSlot(equipmentSlot)) {
+ maxStack = stack;
+ maxSlot = equipmentSlot;
+ maxPercent = percent;
+
+ // check another slot now
+ continue equipmentSlotLoop;
+ }
+ }
+ }
+
+ return maxStack != null
+ ? Optional.of(new EnchantedItemInUse(maxStack, maxSlot, entity))
+ : Optional.empty();
+ }
+ // Purpur end - Add option to mend the most damaged equipment first
}

View File

@@ -84,11 +84,13 @@ public class PurpurWorldConfig {
armorstandStepHeight = (float) getDouble("gameplay-mechanics.armorstand.step-height", armorstandStepHeight);
}
public boolean useBetterMending = false;
public boolean boatEjectPlayersOnLand = false;
public boolean disableDropsOnCrammingDeath = false;
public boolean milkCuresBadOmen = true;
public double tridentLoyaltyVoidReturnHeight = 0.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);