Skip to content

Commit 3f6b2e9

Browse files
committed
feat: PlayerSwapWithEquipmentSlotEvent
1 parent 3f5728e commit 3f6b2e9

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package io.papermc.paper.event.player;
2+
3+
import org.bukkit.entity.Player;
4+
import org.bukkit.event.Cancellable;
5+
import org.bukkit.event.HandlerList;
6+
import org.bukkit.event.player.PlayerEvent;
7+
import org.bukkit.inventory.ItemStack;
8+
import org.jetbrains.annotations.ApiStatus;
9+
import org.jspecify.annotations.NullMarked;
10+
11+
/**
12+
* Triggered when a {@link Player} swaps an item with an equipment slot.
13+
*/
14+
@NullMarked
15+
public class PlayerSwapWithEquipmentSlotEvent extends PlayerEvent implements Cancellable {
16+
private static final HandlerList HANDLERS = new HandlerList();
17+
private final ItemStack itemInHand;
18+
private final ItemStack itemToSwap;
19+
private boolean cancelled;
20+
21+
@ApiStatus.Internal
22+
public PlayerSwapWithEquipmentSlotEvent(
23+
final Player player,
24+
final ItemStack itemInHand,
25+
final ItemStack itemToSwap
26+
) {
27+
super(player);
28+
this.itemInHand = itemInHand;
29+
this.itemToSwap = itemToSwap;
30+
}
31+
32+
/**
33+
* Gets the item in hand.
34+
*
35+
* @return item in hand
36+
*/
37+
public ItemStack getItemInHand() {
38+
return this.itemInHand;
39+
}
40+
41+
/**
42+
* Gets the item to swap.
43+
*
44+
* @return item to swap
45+
*/
46+
public ItemStack getItemToSwap() {
47+
return this.itemToSwap;
48+
}
49+
50+
@Override
51+
public boolean isCancelled() {
52+
return this.cancelled;
53+
}
54+
55+
@Override
56+
public void setCancelled(final boolean cancelled) {
57+
this.cancelled = cancelled;
58+
}
59+
60+
@Override
61+
public HandlerList getHandlers() {
62+
return HANDLERS;
63+
}
64+
65+
public static HandlerList getHandlerList() {
66+
return HANDLERS;
67+
}
68+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--- a/net/minecraft/world/item/equipment/Equippable.java
2+
+++ b/net/minecraft/world/item/equipment/Equippable.java
3+
@@ -129,6 +_,17 @@
4+
ItemStack itemBySlot = player.getItemBySlot(this.slot);
5+
if ((!EnchantmentHelper.has(itemBySlot, EnchantmentEffectComponents.PREVENT_ARMOR_CHANGE) || player.isCreative())
6+
&& !ItemStack.isSameItemSameComponents(stack, itemBySlot)) {
7+
+ // Paper start
8+
+ final io.papermc.paper.event.player.PlayerSwapWithEquipmentSlotEvent event = new io.papermc.paper.event.player.PlayerSwapWithEquipmentSlotEvent(
9+
+ (org.bukkit.entity.Player) player.getBukkitEntity(),
10+
+ org.bukkit.craftbukkit.inventory.CraftItemStack.asBukkitCopy(stack),
11+
+ org.bukkit.craftbukkit.inventory.CraftItemStack.asBukkitCopy(itemBySlot)
12+
+ );
13+
+ event.callEvent();
14+
+ if (event.isCancelled()) {
15+
+ return InteractionResult.FAIL;
16+
+ }
17+
+ // Paper end
18+
if (!player.level().isClientSide()) {
19+
player.awardStat(Stats.ITEM_USED.get(stack.getItem()));
20+
}

0 commit comments

Comments
 (0)