From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Jake Potrebic 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 a80f09d61b3b0d8bc5c5bec7c9df17327bd94b57..29a463ebceabf2d7ae22c26178f969cf1c1e5a21 100644 --- a/src/main/java/net/minecraft/world/item/ItemStack.java +++ b/src/main/java/net/minecraft/world/item/ItemStack.java @@ -639,7 +639,7 @@ public final class ItemStack implements DataComponentHolder { return (Integer) this.getOrDefault(DataComponents.MAX_DAMAGE, 0); } - public void hurtAndBreak(int amount, RandomSource random, @Nullable ServerPlayer player, Runnable breakCallback) { + public void hurtAndBreak(int amount, RandomSource random, @Nullable LivingEntity player, Runnable breakCallback) { // Paper - Add EntityDamageItemEvent if (this.isDamageableItem()) { int j; @@ -655,8 +655,8 @@ public final class ItemStack implements DataComponentHolder { 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()) { @@ -667,6 +667,14 @@ public final class ItemStack implements DataComponentHolder { } 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) { @@ -674,8 +682,8 @@ public final class ItemStack implements DataComponentHolder { } } - 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; @@ -708,7 +716,7 @@ public final class ItemStack implements DataComponentHolder { entityplayer = null; } - this.hurtAndBreak(amount, randomsource, entityplayer, () -> { + this.hurtAndBreak(amount, randomsource, entity, () -> { // Paper - Add EntityDamageItemEvent entity.broadcastBreakEvent(slot); Item item = this.getItem(); // CraftBukkit start - Check for item breaking