aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/server/0567-Add-EntityDamageItemEvent.patch
blob: bfe6c8751ad5def9259ae2213973b6a8ff7f961b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
Date: Tue, 22 Dec 2020 13:52:48 -0800
Subject: [PATCH] Add EntityDamageItemEvent


diff --git a/src/main/java/net/minecraft/world/item/ItemStack.java b/src/main/java/net/minecraft/world/item/ItemStack.java
index 225206a055e2f6bf4dbb18434cb3401d02746387..e320264c2283c2c09910ea70606413c73f443b1f 100644
--- a/src/main/java/net/minecraft/world/item/ItemStack.java
+++ b/src/main/java/net/minecraft/world/item/ItemStack.java
@@ -697,11 +697,11 @@ public final class ItemStack implements DataComponentHolder {
         return this.isDamageableItem() && this.getDamageValue() >= this.getMaxDamage() - 1;
     }
 
-    public void hurtAndBreak(int amount, ServerLevel world, @Nullable ServerPlayer player, Consumer<Item> breakCallback) {
+    public void hurtAndBreak(int amount, ServerLevel world, @Nullable LivingEntity player, Consumer<Item> breakCallback) {  // Paper - Add EntityDamageItemEvent
         int j = this.processDurabilityChange(amount, world, player);
         // CraftBukkit start
-        if (player != null) {
-            PlayerItemDamageEvent event = new PlayerItemDamageEvent(player.getBukkitEntity(), CraftItemStack.asCraftMirror(this), j);
+        if (player instanceof final ServerPlayer serverPlayer) { // Paper - Add EntityDamageItemEvent
+            PlayerItemDamageEvent event = new PlayerItemDamageEvent(serverPlayer.getBukkitEntity(), CraftItemStack.asCraftMirror(this), j); // Paper - Add EntityDamageItemEvent
             event.getPlayer().getServer().getPluginManager().callEvent(event);
 
             if (j != event.getDamage() || event.isCancelled()) {
@@ -712,6 +712,14 @@ public final class ItemStack implements DataComponentHolder {
             }
 
             j = event.getDamage();
+            // Paper start - Add EntityDamageItemEvent
+        } else if (player != null) {
+            io.papermc.paper.event.entity.EntityDamageItemEvent event = new io.papermc.paper.event.entity.EntityDamageItemEvent(player.getBukkitLivingEntity(), CraftItemStack.asCraftMirror(this), amount);
+            if (!event.callEvent()) {
+                return;
+            }
+            j = event.getDamage();
+            // Paper end - Add EntityDamageItemEvent
         }
         // CraftBukkit end
 
@@ -721,21 +729,21 @@ public final class ItemStack implements DataComponentHolder {
 
     }
 
-    private int processDurabilityChange(int baseDamage, ServerLevel world, @Nullable ServerPlayer player) {
-        return !this.isDamageableItem() ? 0 : (player != null && player.hasInfiniteMaterials() ? 0 : (baseDamage > 0 ? EnchantmentHelper.processDurabilityChange(world, this, baseDamage) : baseDamage));
+    private int processDurabilityChange(int baseDamage, ServerLevel world, @Nullable LivingEntity player) {  // Paper - Add EntityDamageItemEvent
+        return !this.isDamageableItem() ? 0 : (player instanceof ServerPlayer && player.hasInfiniteMaterials() ? 0 : (baseDamage > 0 ? EnchantmentHelper.processDurabilityChange(world, this, baseDamage) : baseDamage));  // Paper - Add EntityDamageItemEvent
     }
 
-    private void applyDamage(int damage, @Nullable ServerPlayer player, Consumer<Item> breakCallback) {
-        if (player != null) {
-            CriteriaTriggers.ITEM_DURABILITY_CHANGED.trigger(player, this, damage);
+    private void applyDamage(int damage, @Nullable LivingEntity player, Consumer<Item> breakCallback) { // Paper - Add EntityDamageItemEvent
+        if (player instanceof final ServerPlayer serverPlayer) { // Paper - Add EntityDamageItemEvent
+            CriteriaTriggers.ITEM_DURABILITY_CHANGED.trigger(serverPlayer, this, damage); // Paper - Add EntityDamageItemEvent
         }
 
         this.setDamageValue(damage);
         if (this.isBroken()) {
             Item item = this.getItem();
             // CraftBukkit start - Check for item breaking
-            if (this.count == 1 && player != null) {
-                org.bukkit.craftbukkit.event.CraftEventFactory.callPlayerItemBreakEvent(player, this);
+            if (this.count == 1 && player instanceof final ServerPlayer serverPlayer) { // Paper - Add EntityDamageItemEvent
+                org.bukkit.craftbukkit.event.CraftEventFactory.callPlayerItemBreakEvent(serverPlayer, this); // Paper - Add EntityDamageItemEvent
             }
             // CraftBukkit end
 
@@ -773,7 +781,7 @@ public final class ItemStack implements DataComponentHolder {
                 entityplayer = null;
             }
 
-            this.hurtAndBreak(amount, worldserver, entityplayer, (item) -> {
+            this.hurtAndBreak(amount, worldserver, entity, (item) -> { // Paper - Add EntityDamageItemEvent
                 entity.onEquippedItemBroken(item, slot);
             });
         }
diff --git a/src/main/java/net/minecraft/world/item/enchantment/effects/ChangeItemDamage.java b/src/main/java/net/minecraft/world/item/enchantment/effects/ChangeItemDamage.java
index 0019c8548aa4901b248ced32cc1475ad14b725bf..7e21c8b10debd70c35a138c14b9b177ef6fff37a 100644
--- a/src/main/java/net/minecraft/world/item/enchantment/effects/ChangeItemDamage.java
+++ b/src/main/java/net/minecraft/world/item/enchantment/effects/ChangeItemDamage.java
@@ -21,9 +21,9 @@ public record ChangeItemDamage(LevelBasedValue amount) implements EnchantmentEnt
     public void apply(ServerLevel world, int level, EnchantedItemInUse context, Entity user, Vec3 pos) {
         ItemStack itemStack = context.itemStack();
         if (itemStack.has(DataComponents.MAX_DAMAGE) && itemStack.has(DataComponents.DAMAGE)) {
-            ServerPlayer serverPlayer2 = context.owner() instanceof ServerPlayer serverPlayer ? serverPlayer : null;
+            // ServerPlayer serverPlayer2 = context.owner() instanceof ServerPlayer serverPlayer ? serverPlayer : null; // Paper - EntityDamageItemEvent - always pass in entity
             int i = (int)this.amount.calculate(level);
-            itemStack.hurtAndBreak(i, world, serverPlayer2, context.onBreak());
+            itemStack.hurtAndBreak(i, world, context.owner(), context.onBreak()); // Paper - EntityDamageItemEvent - always pass in entity
         }
     }