Skip to content

Commit bd74bf6

Browse files
kashikeSirYwell
andauthored
feat: PlayerSwapWithEquipmentSlotEvent (#13687)
Co-authored-by: Hannes Greule <SirYwell@users.noreply.github.com>
1 parent 3f5728e commit bd74bf6

File tree

2 files changed

+97
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)