ItemStack damage API

Adds methods notify clients about item breaks and
to simulate damage done to an itemstack and all
the logic associated with damaging them

== AT ==
public net.minecraft.world.entity.LivingEntity entityEventForEquipmentBreak(Lnet/minecraft/world/entity/EquipmentSlot;)B
This commit is contained in:
Jake Potrebic 2022-05-08 13:35:45 -07:00
parent 0e9f28fe68
commit 4a416ca85a
2 changed files with 101 additions and 32 deletions

View file

@ -349,19 +349,25 @@
ItemStack itemstack = this.copy(); ItemStack itemstack = this.copy();
boolean flag = this.getUseDuration(user) <= 0; boolean flag = this.getUseDuration(user) <= 0;
InteractionResult enuminteractionresult = this.getItem().use(world, user, hand); InteractionResult enuminteractionresult = this.getItem().use(world, user, hand);
@@ -490,8 +697,32 @@ @@ -490,27 +697,66 @@
return this.isDamageableItem() && this.getDamageValue() >= this.getMaxDamage() - 1; 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 ServerPlayer player, Consumer<Item> breakCallback) {
- int j = this.processDurabilityChange(amount, world, player);
+ public void hurtAndBreak(int amount, ServerLevel world, @Nullable LivingEntity player, Consumer<Item> breakCallback) { // Paper - Add EntityDamageItemEvent + public void hurtAndBreak(int amount, ServerLevel world, @Nullable LivingEntity player, Consumer<Item> breakCallback) { // Paper - Add EntityDamageItemEvent
+ // Paper start - add force boolean overload
+ this.hurtAndBreak(amount, world, player, breakCallback, false);
+ }
+ public void hurtAndBreak(int amount, ServerLevel world, @Nullable LivingEntity player, Consumer<Item> breakCallback, boolean force) { // Paper - Add EntityDamageItemEvent
+ // Paper end
+ int originalDamage = amount; // Paper - Expand PlayerItemDamageEvent + int originalDamage = amount; // Paper - Expand PlayerItemDamageEvent
int j = this.processDurabilityChange(amount, world, player); + int j = this.processDurabilityChange(amount, world, player, force); // Paper
+ // CraftBukkit start + // CraftBukkit start
+ if (player instanceof final ServerPlayer serverPlayer) { // Paper - Add EntityDamageItemEvent + if (player instanceof final ServerPlayer serverPlayer) { // Paper - Add EntityDamageItemEvent
+ PlayerItemDamageEvent event = new PlayerItemDamageEvent(serverPlayer.getBukkitEntity(), CraftItemStack.asCraftMirror(this), j, originalDamage); // Paper - Add EntityDamageItemEvent + PlayerItemDamageEvent event = new PlayerItemDamageEvent(serverPlayer.getBukkitEntity(), CraftItemStack.asCraftMirror(this), j, originalDamage); // Paper - Add EntityDamageItemEvent
+ event.getPlayer().getServer().getPluginManager().callEvent(event); + event.getPlayer().getServer().getPluginManager().callEvent(event);
+
+ if (j != event.getDamage() || event.isCancelled()) { + if (j != event.getDamage() || event.isCancelled()) {
+ event.getPlayer().updateInventory(); + event.getPlayer().updateInventory();
+ } + }
@ -380,18 +386,23 @@
+ // Paper end - Add EntityDamageItemEvent + // Paper end - Add EntityDamageItemEvent
+ } + }
+ // CraftBukkit end + // CraftBukkit end
+
if (j != 0) { if (j != 0) {
this.applyDamage(this.getDamageValue() + j, player, breakCallback); this.applyDamage(this.getDamageValue() + j, player, breakCallback);
@@ -499,18 +730,23 @@ }
} }
- private int processDurabilityChange(int baseDamage, ServerLevel world, @Nullable ServerPlayer player) { - 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)); - 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 + 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 + // Paper start - itemstack damage api
+ return processDurabilityChange(baseDamage, world, player, false);
} }
+ private int processDurabilityChange(int baseDamage, ServerLevel world, @Nullable LivingEntity player, boolean force) {
+ return !this.isDamageableItem() ? 0 : (player instanceof ServerPlayer && player.hasInfiniteMaterials() && !force ? 0 : (baseDamage > 0 ? EnchantmentHelper.processDurabilityChange(world, this, baseDamage) : baseDamage)); // Paper - Add EntityDamageItemEvent
+ // Paper end - itemstack damage api
+ }
- private void applyDamage(int damage, @Nullable ServerPlayer player, Consumer<Item> breakCallback) { - private void applyDamage(int damage, @Nullable ServerPlayer player, Consumer<Item> breakCallback) {
- if (player != null) { - if (player != null) {
@ -412,7 +423,7 @@
this.shrink(1); this.shrink(1);
breakCallback.accept(item); breakCallback.accept(item);
@@ -518,7 +754,7 @@ @@ -518,7 +764,7 @@
} }
@ -421,16 +432,32 @@
if (player instanceof ServerPlayer entityplayer) { if (player instanceof ServerPlayer entityplayer) {
int j = this.processDurabilityChange(amount, entityplayer.serverLevel(), entityplayer); int j = this.processDurabilityChange(amount, entityplayer.serverLevel(), entityplayer);
@@ -546,7 +782,7 @@ @@ -535,6 +781,11 @@
}
public void hurtAndBreak(int amount, LivingEntity entity, EquipmentSlot slot) {
+ // Paper start - add param to skip infinite mats check
+ this.hurtAndBreak(amount, entity, slot, false);
+ }
+ public void hurtAndBreak(int amount, LivingEntity entity, EquipmentSlot slot, boolean force) {
+ // Paper end - add param to skip infinite mats check
Level world = entity.level();
if (world instanceof ServerLevel worldserver) {
@@ -546,9 +797,9 @@
entityplayer = null; entityplayer = null;
} }
- this.hurtAndBreak(amount, worldserver, entityplayer, (item) -> { - this.hurtAndBreak(amount, worldserver, entityplayer, (item) -> {
- entity.onEquippedItemBroken(item, slot);
- });
+ this.hurtAndBreak(amount, worldserver, entity, (item) -> { // Paper - Add EntityDamageItemEvent + this.hurtAndBreak(amount, worldserver, entity, (item) -> { // Paper - Add EntityDamageItemEvent
entity.onEquippedItemBroken(item, slot); + if (slot != null) entity.onEquippedItemBroken(item, slot); // Paper - itemstack damage API - do not process entity related callbacks when damaging from API
}); + }, force); // Paper - itemstack damage API
} }
@@ -580,11 +816,11 @@
}
@@ -580,11 +831,11 @@
return this.getItem().getBarColor(this); return this.getItem().getBarColor(this);
} }
@ -444,7 +471,7 @@
return this.getItem().overrideOtherStackedOnMe(this, stack, slot, clickType, player, cursorStackReference); return this.getItem().overrideOtherStackedOnMe(this, stack, slot, clickType, player, cursorStackReference);
} }
@@ -592,8 +828,8 @@ @@ -592,8 +843,8 @@
Item item = this.getItem(); Item item = this.getItem();
if (item.hurtEnemy(this, target, user)) { if (item.hurtEnemy(this, target, user)) {
@ -455,7 +482,7 @@
entityhuman.awardStat(Stats.ITEM_USED.get(item)); entityhuman.awardStat(Stats.ITEM_USED.get(item));
} }
@@ -608,7 +844,7 @@ @@ -608,7 +859,7 @@
this.getItem().postHurtEnemy(this, target, user); this.getItem().postHurtEnemy(this, target, user);
} }
@ -464,7 +491,7 @@
Item item = this.getItem(); Item item = this.getItem();
if (item.mineBlock(this, world, state, pos, miner)) { if (item.mineBlock(this, world, state, pos, miner)) {
@@ -617,11 +853,11 @@ @@ -617,11 +868,11 @@
} }
@ -478,7 +505,7 @@
return this.getItem().interactLivingEntity(this, user, entity, hand); return this.getItem().interactLivingEntity(this, user, entity, hand);
} }
@@ -736,7 +972,7 @@ @@ -736,7 +987,7 @@
} }
@ -487,7 +514,7 @@
player.awardStat(Stats.ITEM_CRAFTED.get(this.getItem()), amount); player.awardStat(Stats.ITEM_CRAFTED.get(this.getItem()), amount);
this.getItem().onCraftedBy(this, world, player); this.getItem().onCraftedBy(this, world, player);
} }
@@ -770,6 +1006,12 @@ @@ -770,6 +1021,12 @@
return this.getItem().useOnRelease(this); return this.getItem().useOnRelease(this);
} }
@ -500,13 +527,10 @@
@Nullable @Nullable
public <T> T set(DataComponentType<? super T> type, @Nullable T value) { public <T> T set(DataComponentType<? super T> type, @Nullable T value) {
return this.components.set(type, value); return this.components.set(type, value);
@@ -803,8 +1045,27 @@ @@ -806,6 +1063,25 @@
this.components.restorePatch(datacomponentpatch1); }
} else { }
this.getItem().verifyComponentsAfterLoad(this);
+ }
+ }
+
+ // Paper start - (this is just a good no conflict location) + // Paper start - (this is just a good no conflict location)
+ public org.bukkit.inventory.ItemStack asBukkitMirror() { + public org.bukkit.inventory.ItemStack asBukkitMirror() {
+ return CraftItemStack.asCraftMirror(this); + return CraftItemStack.asCraftMirror(this);
@ -521,14 +545,15 @@
+ public org.bukkit.inventory.ItemStack getBukkitStack() { + public org.bukkit.inventory.ItemStack getBukkitStack() {
+ if (bukkitStack == null || bukkitStack.handle != this) { + if (bukkitStack == null || bukkitStack.handle != this) {
+ bukkitStack = org.bukkit.craftbukkit.inventory.CraftItemStack.asCraftMirror(this); + bukkitStack = org.bukkit.craftbukkit.inventory.CraftItemStack.asCraftMirror(this);
} + }
+ return bukkitStack; + return bukkitStack;
} + }
+ // Paper end + // Paper end
+
public void applyComponents(DataComponentPatch changes) { public void applyComponents(DataComponentPatch changes) {
this.components.applyPatch(changes); this.components.applyPatch(changes);
@@ -858,7 +1119,7 @@ this.getItem().verifyComponentsAfterLoad(this);
@@ -858,7 +1134,7 @@
} }
private <T extends TooltipProvider> void addToTooltip(DataComponentType<T> componentType, Item.TooltipContext context, Consumer<Component> textConsumer, TooltipFlag type) { private <T extends TooltipProvider> void addToTooltip(DataComponentType<T> componentType, Item.TooltipContext context, Consumer<Component> textConsumer, TooltipFlag type) {
@ -537,7 +562,7 @@
if (t0 != null) { if (t0 != null) {
t0.addToTooltip(context, textConsumer, type); t0.addToTooltip(context, textConsumer, type);
@@ -866,7 +1127,7 @@ @@ -866,7 +1142,7 @@
} }
@ -546,7 +571,7 @@
boolean flag = this.getItem().shouldPrintOpWarning(this, player); boolean flag = this.getItem().shouldPrintOpWarning(this, player);
if (!type.isCreative() && this.has(DataComponents.HIDE_TOOLTIP)) { if (!type.isCreative() && this.has(DataComponents.HIDE_TOOLTIP)) {
@@ -941,7 +1202,7 @@ @@ -941,7 +1217,7 @@
} }
} }
@ -555,7 +580,7 @@
ItemAttributeModifiers itemattributemodifiers = (ItemAttributeModifiers) this.getOrDefault(DataComponents.ATTRIBUTE_MODIFIERS, ItemAttributeModifiers.EMPTY); ItemAttributeModifiers itemattributemodifiers = (ItemAttributeModifiers) this.getOrDefault(DataComponents.ATTRIBUTE_MODIFIERS, ItemAttributeModifiers.EMPTY);
if (itemattributemodifiers.showInTooltip()) { if (itemattributemodifiers.showInTooltip()) {
@@ -966,7 +1227,7 @@ @@ -966,7 +1242,7 @@
} }
} }
@ -564,7 +589,7 @@
double d0 = modifier.amount(); double d0 = modifier.amount();
boolean flag = false; boolean flag = false;
@@ -1091,6 +1352,14 @@ @@ -1091,6 +1367,14 @@
EnchantmentHelper.forEachModifier(this, slot, attributeModifierConsumer); EnchantmentHelper.forEachModifier(this, slot, attributeModifierConsumer);
} }
@ -579,7 +604,7 @@
public Component getDisplayName() { public Component getDisplayName() {
MutableComponent ichatmutablecomponent = Component.empty().append(this.getHoverName()); MutableComponent ichatmutablecomponent = Component.empty().append(this.getHoverName());
@@ -1153,7 +1422,7 @@ @@ -1153,7 +1437,7 @@
} }
public void consume(int amount, @Nullable LivingEntity entity) { public void consume(int amount, @Nullable LivingEntity entity) {

View file

@ -1154,4 +1154,48 @@ public class CraftLivingEntity extends CraftEntity implements LivingEntity {
this.getHandle().knockback(strength, directionX, directionZ); this.getHandle().knockback(strength, directionX, directionZ);
}; };
// Paper end - knockback API // Paper end - knockback API
// Paper start - ItemStack damage API
public void broadcastSlotBreak(final org.bukkit.inventory.EquipmentSlot slot) {
this.getHandle().level().broadcastEntityEvent(this.getHandle(), net.minecraft.world.entity.LivingEntity.entityEventForEquipmentBreak(org.bukkit.craftbukkit.CraftEquipmentSlot.getNMS(slot)));
}
@Override
public void broadcastSlotBreak(final org.bukkit.inventory.EquipmentSlot slot, final Collection<org.bukkit.entity.Player> players) {
if (players.isEmpty()) {
return;
}
final net.minecraft.network.protocol.game.ClientboundEntityEventPacket packet = new net.minecraft.network.protocol.game.ClientboundEntityEventPacket(
this.getHandle(),
net.minecraft.world.entity.LivingEntity.entityEventForEquipmentBreak(org.bukkit.craftbukkit.CraftEquipmentSlot.getNMS(slot))
);
players.forEach(player -> ((CraftPlayer) player).getHandle().connection.send(packet));
}
@Override
public ItemStack damageItemStack(ItemStack stack, final int amount) {
final net.minecraft.world.item.ItemStack nmsStack;
if (stack instanceof final CraftItemStack craftItemStack) {
if (craftItemStack.handle == null || craftItemStack.handle.isEmpty()) {
return stack;
}
nmsStack = craftItemStack.handle;
} else {
nmsStack = CraftItemStack.asNMSCopy(stack);
stack = CraftItemStack.asCraftMirror(nmsStack); // mirror to capture changes in hurt logic & events
}
this.damageItemStack0(nmsStack, amount, null);
return stack;
}
@Override
public void damageItemStack(final org.bukkit.inventory.EquipmentSlot slot, final int amount) {
final net.minecraft.world.entity.EquipmentSlot nmsSlot = org.bukkit.craftbukkit.CraftEquipmentSlot.getNMS(slot);
this.damageItemStack0(this.getHandle().getItemBySlot(nmsSlot), amount, nmsSlot);
}
private void damageItemStack0(final net.minecraft.world.item.ItemStack nmsStack, final int amount, final net.minecraft.world.entity.EquipmentSlot slot) {
nmsStack.hurtAndBreak(amount, this.getHandle(), slot, true);
}
// Paper end - ItemStack damage API
} }