Paper/patches/server/0609-Add-EntityDamageItemEvent.patch

66 lines
3.8 KiB
Diff
Raw Normal View History

2021-08-10 07:04:44 +02:00
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
2021-08-10 07:04:44 +02:00
diff --git a/src/main/java/net/minecraft/world/item/ItemStack.java b/src/main/java/net/minecraft/world/item/ItemStack.java
index b4f2b75960674e81c8189dc908523c56ae2e5079..4624fc8eb4a5e71d7874d25ca0a25975f65a6919 100644
2021-08-10 07:04:44 +02:00
--- a/src/main/java/net/minecraft/world/item/ItemStack.java
+++ b/src/main/java/net/minecraft/world/item/ItemStack.java
2023-12-06 04:57:46 +01:00
@@ -622,7 +622,7 @@ public final class ItemStack {
2021-08-10 07:04:44 +02:00
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
2021-08-10 07:04:44 +02:00
if (!this.isDamageableItem()) {
return false;
} else {
2023-12-06 04:57:46 +01:00
@@ -640,8 +640,8 @@ public final class ItemStack {
2021-08-10 07:04:44 +02:00
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
2021-08-10 07:04:44 +02:00
event.getPlayer().getServer().getPluginManager().callEvent(event);
if (amount != event.getDamage() || event.isCancelled()) {
2023-12-06 04:57:46 +01:00
@@ -652,6 +652,14 @@ public final class ItemStack {
2021-08-10 07:04:44 +02:00
}
amount = event.getDamage();
+ // Paper start - Add EntityDamageItemEvent
2021-08-10 07:04:44 +02:00
+ } 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
2021-08-10 07:04:44 +02:00
}
// CraftBukkit end
if (amount <= 0) {
2023-12-06 04:57:46 +01:00
@@ -659,8 +667,8 @@ public final class ItemStack {
2021-08-10 07:04:44 +02:00
}
}
- 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
2021-08-10 07:04:44 +02:00
}
j = this.getDamageValue() + amount;
2023-12-06 04:57:46 +01:00
@@ -672,7 +680,7 @@ public final class ItemStack {
2021-08-10 07:04:44 +02:00
public <T extends LivingEntity> void hurtAndBreak(int amount, T entity, Consumer<T> breakCallback) {
2023-06-08 04:04:01 +02:00
if (!entity.level().isClientSide && (!(entity instanceof net.minecraft.world.entity.player.Player) || !((net.minecraft.world.entity.player.Player) entity).getAbilities().instabuild)) {
2021-08-10 07:04:44 +02:00
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
2021-08-10 07:04:44 +02:00
breakCallback.accept(entity);
Item item = this.getItem();
// CraftBukkit start - Check for item breaking