2020-05-06 11:48:49 +02:00
|
|
|
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
2017-12-20 23:50:26 +01:00
|
|
|
From: Aikar <aikar@aikar.co>
|
|
|
|
Date: Wed, 20 Dec 2017 17:36:49 -0500
|
|
|
|
Subject: [PATCH] Ability to apply mending to XP API
|
|
|
|
|
|
|
|
This allows plugins that give players the ability to apply the experience
|
|
|
|
points to the Item Mending formula, which will repair an item instead
|
|
|
|
of giving the player experience points.
|
|
|
|
|
|
|
|
Both an API To standalone mend, and apply mending logic to .giveExp has been added.
|
|
|
|
|
2021-03-16 08:19:45 +01:00
|
|
|
diff --git a/src/main/java/net/minecraft/world/entity/EntityExperienceOrb.java b/src/main/java/net/minecraft/world/entity/EntityExperienceOrb.java
|
2021-03-16 16:50:45 +01:00
|
|
|
index e3dfb018b06c0139594ddbb88fab2ca8d43ab12f..3387a19044b3ee2a1ef549c328c8bc354a5b6d23 100644
|
2021-03-16 08:19:45 +01:00
|
|
|
--- a/src/main/java/net/minecraft/world/entity/EntityExperienceOrb.java
|
|
|
|
+++ b/src/main/java/net/minecraft/world/entity/EntityExperienceOrb.java
|
2021-03-16 16:50:45 +01:00
|
|
|
@@ -265,10 +265,12 @@ public class EntityExperienceOrb extends Entity {
|
2017-12-20 23:50:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-02 07:39:36 +02:00
|
|
|
+ public final int durToXp(int i) { return b(i); } // Paper OBFHELPER
|
2018-07-18 06:52:33 +02:00
|
|
|
private int b(int i) {
|
2017-12-20 23:50:26 +01:00
|
|
|
return i / 2;
|
|
|
|
}
|
|
|
|
|
2020-08-02 07:39:36 +02:00
|
|
|
+ public final int xpToDur(int i) { return c(i); } // Paper OBFHELPER
|
2018-07-18 06:52:33 +02:00
|
|
|
private int c(int i) {
|
2017-12-20 23:50:26 +01:00
|
|
|
return i * 2;
|
|
|
|
}
|
2021-03-16 08:19:45 +01:00
|
|
|
diff --git a/src/main/java/net/minecraft/world/item/enchantment/EnchantmentManager.java b/src/main/java/net/minecraft/world/item/enchantment/EnchantmentManager.java
|
|
|
|
index d313b02f41e4f4a90676cbb37afce4e92dd4d664..72afbf8f537770540e90a2880ea81de137ea10f5 100644
|
|
|
|
--- a/src/main/java/net/minecraft/world/item/enchantment/EnchantmentManager.java
|
|
|
|
+++ b/src/main/java/net/minecraft/world/item/enchantment/EnchantmentManager.java
|
|
|
|
@@ -269,8 +269,8 @@ public class EnchantmentManager {
|
|
|
|
return getEnchantmentLevel(Enchantments.CHANNELING, itemstack) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
- @Nullable
|
|
|
|
- public static Entry<EnumItemSlot, ItemStack> b(Enchantment enchantment, EntityLiving entityliving) {
|
|
|
|
+ public static @javax.annotation.Nonnull ItemStack getRandomEquippedItemWithEnchant(Enchantment enchantment, EntityLiving entityliving) { Entry<EnumItemSlot, ItemStack> entry = b(enchantment, entityliving); return entry != null ? entry.getValue() : ItemStack.NULL_ITEM; } // Paper - OBFHELPER
|
|
|
|
+ @Nullable public static Entry<EnumItemSlot, ItemStack> b(Enchantment enchantment, EntityLiving entityliving) {
|
|
|
|
return a(enchantment, entityliving, (itemstack) -> {
|
|
|
|
return true;
|
|
|
|
});
|
2017-12-20 23:50:26 +01:00
|
|
|
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
|
2021-03-16 16:50:45 +01:00
|
|
|
index cf9ae60db30ef09bb4c89935a42632e562b6d61e..4766a78a0562e5ae6e7d4850bd7b5d71425c3a0c 100644
|
2017-12-20 23:50:26 +01:00
|
|
|
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
|
|
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
|
2021-03-16 16:50:45 +01:00
|
|
|
@@ -58,13 +58,17 @@ import net.minecraft.server.level.WorldServer;
|
|
|
|
import net.minecraft.server.network.PlayerConnection;
|
|
|
|
import net.minecraft.server.players.WhiteListEntry;
|
|
|
|
import net.minecraft.world.entity.Entity;
|
|
|
|
+import net.minecraft.world.entity.EntityExperienceOrb;
|
|
|
|
import net.minecraft.world.entity.EntityLiving;
|
|
|
|
+import net.minecraft.world.entity.EntityTypes;
|
|
|
|
import net.minecraft.world.entity.ai.attributes.AttributeMapBase;
|
|
|
|
import net.minecraft.world.entity.ai.attributes.AttributeModifiable;
|
|
|
|
import net.minecraft.world.entity.ai.attributes.GenericAttributes;
|
|
|
|
import net.minecraft.world.entity.player.EntityHuman;
|
|
|
|
import net.minecraft.world.inventory.Container;
|
|
|
|
import net.minecraft.world.item.EnumColor;
|
|
|
|
+import net.minecraft.world.item.enchantment.EnchantmentManager;
|
|
|
|
+import net.minecraft.world.item.enchantment.Enchantments;
|
|
|
|
import net.minecraft.world.level.EnumGamemode;
|
|
|
|
import net.minecraft.world.level.block.entity.TileEntitySign;
|
|
|
|
import net.minecraft.world.level.saveddata.maps.MapIcon;
|
|
|
|
@@ -1180,8 +1184,37 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
|
2017-12-20 23:50:26 +01:00
|
|
|
return GameMode.getByValue(getHandle().playerInteractManager.getGameMode().getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
+ // Paper start
|
|
|
|
@Override
|
|
|
|
- public void giveExp(int exp) {
|
|
|
|
+ public int applyMending(int amount) {
|
|
|
|
+ EntityPlayer handle = getHandle();
|
|
|
|
+ // Logic copied from EntityExperienceOrb and remapped to unobfuscated methods/properties
|
2021-03-16 16:50:45 +01:00
|
|
|
+ net.minecraft.world.item.ItemStack itemstack = EnchantmentManager.getRandomEquippedItemWithEnchant(Enchantments.MENDING, handle);
|
2018-07-19 06:42:43 +02:00
|
|
|
+ if (!itemstack.isEmpty() && itemstack.getItem().usesDurability()) {
|
2017-12-20 23:50:26 +01:00
|
|
|
+
|
2021-03-16 16:50:45 +01:00
|
|
|
+ EntityExperienceOrb orb = EntityTypes.EXPERIENCE_ORB.create(handle.world);
|
2017-12-20 23:50:26 +01:00
|
|
|
+ orb.value = amount;
|
|
|
|
+ orb.spawnReason = org.bukkit.entity.ExperienceOrb.SpawnReason.CUSTOM;
|
2019-12-12 19:45:00 +01:00
|
|
|
+ orb.setPositionRaw(handle.locX(), handle.locY(), handle.locZ());
|
2017-12-20 23:50:26 +01:00
|
|
|
+
|
|
|
|
+ int i = Math.min(orb.xpToDur(amount), itemstack.getDamage());
|
|
|
|
+ org.bukkit.event.player.PlayerItemMendEvent event = org.bukkit.craftbukkit.event.CraftEventFactory.callPlayerItemMendEvent(handle, orb, itemstack, i);
|
|
|
|
+ i = event.getRepairAmount();
|
|
|
|
+ orb.dead = true;
|
|
|
|
+ if (!event.isCancelled()) {
|
|
|
|
+ amount -= orb.durToXp(i);
|
|
|
|
+ itemstack.setDamage(itemstack.getDamage() - i);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return amount;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void giveExp(int exp, boolean applyMending) {
|
|
|
|
+ if (applyMending) {
|
|
|
|
+ exp = this.applyMending(exp);
|
|
|
|
+ }
|
|
|
|
+ // Paper end
|
|
|
|
getHandle().giveExp(exp);
|
|
|
|
}
|
|
|
|
|