aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/server/0607-Add-EntityDamageItemEvent.patch
blob: 99df36aea012975f711b8878526af7deceef023c (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
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 9d2d305a5e66b9f3d94f6464736f5bb40adae591..d5ee83e6538fbd067388272fa9895e17859be642 100644
--- a/src/main/java/net/minecraft/world/item/ItemStack.java
+++ b/src/main/java/net/minecraft/world/item/ItemStack.java
@@ -624,7 +624,7 @@ public final class ItemStack {
         return this.getItem().getMaxDamage();
     }
 
-    public boolean hurt(int amount, RandomSource random, @Nullable ServerPlayer player) {
+    public boolean hurt(int amount, RandomSource random, @Nullable LivingEntity player) { // Paper - Add EntityDamageItemEvent
         if (!this.isDamageableItem()) {
             return false;
         } else {
@@ -642,8 +642,8 @@ public final class ItemStack {
 
                 amount -= k;
                 // CraftBukkit start
-                if (player != null) {
-                    PlayerItemDamageEvent event = new PlayerItemDamageEvent(player.getBukkitEntity(), CraftItemStack.asCraftMirror(this), amount);
+                if (player instanceof ServerPlayer serverPlayer) { // Paper - Add EntityDamageItemEvent
+                    PlayerItemDamageEvent event = new PlayerItemDamageEvent(serverPlayer.getBukkitEntity(), CraftItemStack.asCraftMirror(this), amount); // Paper - Add EntityDamageItemEvent
                     event.getPlayer().getServer().getPluginManager().callEvent(event);
 
                     if (amount != event.getDamage() || event.isCancelled()) {
@@ -654,6 +654,14 @@ public final class ItemStack {
                     }
 
                     amount = 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 false;
+                    }
+                    amount = event.getDamage();
+                    // Paper end - Add EntityDamageItemEvent
                 }
                 // CraftBukkit end
                 if (amount <= 0) {
@@ -661,8 +669,8 @@ public final class ItemStack {
                 }
             }
 
-            if (player != null && amount != 0) {
-                CriteriaTriggers.ITEM_DURABILITY_CHANGED.trigger(player, this, this.getDamageValue() + amount);
+            if (player instanceof ServerPlayer serverPlayer && amount != 0) { // Paper - Add EntityDamageItemEvent
+                CriteriaTriggers.ITEM_DURABILITY_CHANGED.trigger(serverPlayer, this, this.getDamageValue() + amount); // Paper - Add EntityDamageItemEvent
             }
 
             j = this.getDamageValue() + amount;
@@ -674,7 +682,7 @@ public final class ItemStack {
     public <T extends LivingEntity> void hurtAndBreak(int amount, T entity, Consumer<T> breakCallback) {
         if (!entity.level().isClientSide && (!(entity instanceof net.minecraft.world.entity.player.Player) || !((net.minecraft.world.entity.player.Player) entity).getAbilities().instabuild)) {
             if (this.isDamageableItem()) {
-                if (this.hurt(amount, entity.getRandom(), entity instanceof ServerPlayer ? (ServerPlayer) entity : null)) {
+                if (this.hurt(amount, entity.getRandom(), entity /*instanceof ServerPlayer ? (ServerPlayer) entity : null*/)) { // Paper - Add EntityDamageItemEvent
                     breakCallback.accept(entity);
                     Item item = this.getItem();
                     // CraftBukkit start - Check for item breaking