Skip to content

feat: added a item damage event - #39

Open
maxencelobry wants to merge 9 commits into
altayofficial:masterfrom
maxencelobry:item-damage-event
Open

feat: added a item damage event#39
maxencelobry wants to merge 9 commits into
altayofficial:masterfrom
maxencelobry:item-damage-event

Conversation

@maxencelobry

@maxencelobry maxencelobry commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

Introduction

At the moment, when we want to modify the durability of an item, we have to override items

But overriding items can be inconvenient in several cases (sometimes)

The event here allows you to change the durability of items when they are damaged, but could be even more developed (for example, to modify durability via a position where the item is in use)

If you need more questions or additions to the event to be "more relevant", ask me

@maxencelobry
maxencelobry marked this pull request as ready for review August 16, 2026 14:38
@xRookieFight

Copy link
Copy Markdown
Contributor

@maxencelobry
maxencelobry marked this pull request as draft August 16, 2026 16:58
@maxencelobry

maxencelobry commented Aug 16, 2026

Copy link
Copy Markdown
Contributor Author

Reworked the implementation to provide proper context to ItemDamageEvent

The event now exposes the damage cause, holder/user, target, inventory and slot in addition to the damage and Unbreaking reduction. Core durability changes now pass context from the higher-level code where it is available, while applyDamage() remains available for direct plugin usage

onAttackEntity() and onDestroyBlock() signatures were kept unchanged for backwards compatibility; contextual wrappers are used instead

This addresses the main limitation of the previous implementation: listeners can now determine not only that an item was damaged, but also how, where and by whom it was damaged

(btw, dylan originally asked me to add this pmmp/PocketMine-MP#6240 (comment))

@maxencelobry
maxencelobry marked this pull request as ready for review August 16, 2026 18:08
@maxencelobry

Copy link
Copy Markdown
Contributor Author

The implementation was also tested in-game with a simple plugin listening to ItemDamageEvent, and the different durability paths, event cancellation and custom durability changes all worked as expected

@xRookieFight

xRookieFight commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

I think Dylan's comment on this before is really necessary. I'll leave it to another maintainers but my strongly opinion is this event shouldn't exist or should get a better usage. There always could be a item damage cause addition in future. This means maintaining the cause constants would be a pain in future too. Leaving to other maintainers for their reviews.

@maxencelobry

Copy link
Copy Markdown
Contributor Author

I actually added item damage causes in the latest revision of the PR, precisely to address this concern.

I understand the maintenance concern around adding new cause constants over time, but I still think the event itself is useful. One of the main use cases is allowing plugins to implement items with custom durability behaviour while still going through PocketMine's normal durability logic instead of having to replace or duplicate it.

For example, this is something I've already had to implement manually in one of my projects by extending/reworking the durability logic:

https://github.com/nitrofaction/kitmap/blob/main/src/item/Durable.php

and then handling the custom behaviour separately here:

https://github.com/nitrofaction/kitmap/blob/80cde4fdc626de0287247d3ad1ab523242bc43a5/src/listener/EventListener.php#L570

Having ItemDamageEvent in the core makes this kind of use case much cleaner: plugins can modify/cancel durability damage and Unbreaking reduction while still respecting PocketMine's existing item durability flow.

The new implementation also provides the context that was missing from the original PR (cause, entity, target, inventory and slot), rather than firing a context-less event directly from a low-level method.

@xRookieFight xRookieFight left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some nits.

Also please remove your unrelated documentation formatting changes. And it would be good if you can add a small protected helper on Block (damageHeldItem(Durable $item, ?Player $player)) and the functions you've replaced to block classes can collapse to one argument.
Also. Every armor hit now fires 4 events, more with thorns I guess. I don't think you can do anything for this. But would be good to do some optimization here.

Comment thread src/item/Durable.php Outdated
Comment thread src/item/Durable.php Outdated
Comment thread src/item/Durable.php Outdated
Comment thread src/event/inventory/ItemDamageEvent.php Outdated
Comment thread src/item/Durable.php Outdated
Comment thread src/world/World.php Outdated
Comment thread src/player/Player.php
Comment thread src/entity/Living.php Outdated
Comment thread src/item/Durable.php Outdated
Comment thread src/event/inventory/ItemDamageEvent.php Outdated
@maxencelobry
maxencelobry marked this pull request as draft August 20, 2026 18:44
@maxencelobry
maxencelobry marked this pull request as ready for review August 20, 2026 19:03
@maxencelobry

Copy link
Copy Markdown
Contributor Author

that's good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants