Skip to content

Add damaged slot to PlayerItemDamageEvent#2153

Closed
MTM123 wants to merge 2 commits into
PaperMC:masterfrom
MTM123:damaged-item-slot
Closed

Add damaged slot to PlayerItemDamageEvent#2153
MTM123 wants to merge 2 commits into
PaperMC:masterfrom
MTM123:damaged-item-slot

Conversation

@MTM123
Copy link
Copy Markdown

@MTM123 MTM123 commented Jun 9, 2019

Continuation of #1834
(Unfortunately had to create a new PR because couldn't reopen the original PR)

+ } else if (entityplayer.getItemInOffHand() == this) {
+ slot = 104; //Shield slot: (last armor slot + 1)
+ } else {
+ int w = 100; //First armor slot. Based on https://bukkit.org/threads/inventory-slots.431469/
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.

I'm curious why you chose that outdated image to set your slot numbers instead of the API specification? I'm not sure where slot numbers 100-104 are going to come in handy since they cannot be used with setItem(slot), etc.

@MTM123
Copy link
Copy Markdown
Author

MTM123 commented Oct 24, 2019

I initially used the slots you recommended I believe but there was a debate about future proofing and there was no decision on this and the final decision was up to project leads. I guess I could correct to it to slots you mentioned and it would make more sense to do so.

@Proximyst Proximyst added status: rebase required type: feature Request for a new Feature. labels Aug 24, 2020
private static final HandlerList handlers = new HandlerList();
private final ItemStack item;
private int damage;
+ private final int slot; //Paper - add damaged slot
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.

Suggested change
+ private final int slot; //Paper - add damaged slot
+ private final int slot; // Paper - add damaged slot

super(player);
this.item = what;
this.damage = damage;
+ this.slot = -1;
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.

Delegate to a new constructor instead

this.damage = damage;
}

+ //Paper start
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.

Suggested change
+ //Paper start
+ // Paper start

if (entityplayer != null) {
- PlayerItemDamageEvent event = new PlayerItemDamageEvent(entityplayer.getBukkitEntity(), CraftItemStack.asCraftMirror(this), i);
+
+ //Paper start - getting slot that was damaged
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.

Suggested change
+ //Paper start - getting slot that was damaged
+ // Paper start - getting slot that was damaged

- PlayerItemDamageEvent event = new PlayerItemDamageEvent(entityplayer.getBukkitEntity(), CraftItemStack.asCraftMirror(this), i);
+
+ //Paper start - getting slot that was damaged
+ int slot = -1;
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.

Should have a look at how e.g. book modification handling is done for getting the slot...

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I'm not sure what you are thinking of exactly

@MTM123 MTM123 changed the base branch from ver/1.14 to master November 30, 2020 18:37
@MTM123 MTM123 requested a review from a team as a code owner November 30, 2020 18:37
Copy link
Copy Markdown
Contributor

@Trigary Trigary left a comment

Choose a reason for hiding this comment

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

I am not sure whether using an integer slot ID is a good idea. Why not just use the EquipmentSlot enum? Sure, it would limit the event, but it would provide a much more useful API. In what kind of use case is the slot ID useful? The enum would be a lot more useful in my opinion.

super(player);
this.item = what;
this.damage = damage;
+ this.slot = -1;
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.

// Paper is missing

+ //Paper start
+ /**
+ * Gets the slot where the item took durability damage
+ *
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.

Maybe add an example or how this slot can be used: how to get the ItemStack at this slot from a Player instance. (But that's a stupid use case since the ItemStack is directly exposed in this method.)

@MTM123
Copy link
Copy Markdown
Author

MTM123 commented Nov 30, 2020

I think using EquipmentSlot would make more sense indeed because you can already get ItemStack from the event so I guess I will change that then.

EDIT: Though this limits plugins that might call this event to slots defined by EquipmentSlot

@Machine-Maker
Copy link
Copy Markdown
Member

I am not sure whether using an integer slot ID is a good idea. Why not just use the EquipmentSlot enum? Sure, it would limit the event, but it would provide a much more useful API. In what kind of use case is the slot ID useful? The enum would be a lot more useful in my opinion.

I suppose its possible in the future for itemstacks to be "damaged" even if they are not in one of the slots covered by the EquipmentSlot enum. That is already possible via nms, just calling the isDamaged method on the nms ItemStack.

@Trigary
Copy link
Copy Markdown
Contributor

Trigary commented Nov 30, 2020

Exposing both the slot ID and the equipmentslot enum is still an option. Maybe calculate the slot id in the constructor based on the enum value (if the constructor with the enum value was used).

@stale
Copy link
Copy Markdown

stale Bot commented Jan 29, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@MTM123
Copy link
Copy Markdown
Author

MTM123 commented Feb 2, 2021

Talking about this in discord there were few changes suggested by @Trigary and @yannicklamprecht:

  1. Expose raw slot to PlayerItemDamageEvent and create a new event EquipmentDamageEvent that extends PlayerItemDamageEvent that would expose just the EquipmentSlot. This would give ability to somewhat futureproof the event also exposing the raw slot to PlayerItemDamageEvent would allow plugin authors to call the event on items in other slots other than the ones defined by EquipmentSlot.
  2. Stick to just PlayerItemDamageEvent and add both raw slot and EquipmentSlot making EquipmentSlot nullable giving plugin authors the same ability as stated in first solution.

@stale stale Bot removed the resolution: stale label Feb 2, 2021
@kennytv
Copy link
Copy Markdown
Member

kennytv commented Apr 24, 2021

The second should be future-proof enough (and less confusing imo) for plugin devs to already check for the nullable equipment slot, depending on what they need.

@MTM123 MTM123 closed this Jun 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants