Skip to content

Commit 9792062

Browse files
committed
PlayerSwapWithEquipmentSlotEvent
1 parent 76e0dd1 commit 9792062

File tree

4 files changed

+94
-48
lines changed

4 files changed

+94
-48
lines changed

paper-api/src/main/java/io/papermc/paper/event/player/PlayerSwapWithEquipmentSlotEvent.java

Lines changed: 9 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,70 +6,31 @@
66
import org.bukkit.event.player.PlayerEvent;
77
import org.bukkit.inventory.EquipmentSlot;
88
import org.bukkit.inventory.ItemStack;
9-
import org.jetbrains.annotations.ApiStatus;
10-
import org.jspecify.annotations.NullMarked;
119

1210
/**
1311
* Triggered when a {@link Player} swaps an item with an equipment slot.
1412
*/
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-
}
13+
public interface PlayerSwapWithEquipmentSlotEvent extends PlayerEvent, Cancellable {
3514

3615
/**
3716
* {@return the item in one of the hand slots}
3817
*/
39-
public ItemStack getItemInHand() {
40-
return this.itemInHand.clone();
41-
}
18+
ItemStack getItemInHand();
4219

4320
/**
4421
* {@return the slot to swap into}
4522
*/
46-
public EquipmentSlot getSlot() {
47-
return this.slot;
48-
}
23+
EquipmentSlot getSlot();
4924

5025
/**
5126
* {@return the item to swap}
5227
*/
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-
}
28+
ItemStack getItemToSwap();
7129

72-
public static HandlerList getHandlerList() {
73-
return HANDLERS;
30+
static HandlerList getHandlerList() {
31+
final class Holder {
32+
private static final HandlerList HANDLER_LIST = new HandlerList();
33+
}
34+
return Holder.HANDLER_LIST;
7435
}
7536
}

paper-server/patches/features/0035-Events-to-interfaces.patch

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,6 +1814,30 @@ index 8e9db734d19b2082437eaa424f33df2b3321c2cf..1ece773f4943760af95f609b8a99f8bb
18141814
if (!shieldDisableEvent.callEvent()) return;
18151815
player.getCooldowns().addCooldown(stack, shieldDisableEvent.getCooldown());
18161816
// Paper end
1817+
diff --git a/net/minecraft/world/item/equipment/Equippable.java b/net/minecraft/world/item/equipment/Equippable.java
1818+
index fe24aff29f04027449abd68b9d0630c2b106877f..5617091793e8d68f5e388e27d37c33b33819f86c 100644
1819+
--- a/net/minecraft/world/item/equipment/Equippable.java
1820+
+++ b/net/minecraft/world/item/equipment/Equippable.java
1821+
@@ -130,7 +130,7 @@ public record Equippable(
1822+
if ((!EnchantmentHelper.has(itemBySlot, EnchantmentEffectComponents.PREVENT_ARMOR_CHANGE) || player.isCreative())
1823+
&& !ItemStack.isSameItemSameComponents(stack, itemBySlot)) {
1824+
// Paper start
1825+
- final io.papermc.paper.event.player.PlayerSwapWithEquipmentSlotEvent event = new io.papermc.paper.event.player.PlayerSwapWithEquipmentSlotEvent(
1826+
+ final io.papermc.paper.event.player.PlayerSwapWithEquipmentSlotEvent event = new io.papermc.paper.event.player.PaperPlayerSwapWithEquipmentSlotEvent(
1827+
(org.bukkit.entity.Player) player.getBukkitEntity(),
1828+
org.bukkit.craftbukkit.inventory.CraftItemStack.asBukkitCopy(stack),
1829+
org.bukkit.craftbukkit.CraftEquipmentSlot.getSlot(this.slot),
1830+
@@ -138,7 +138,9 @@ public record Equippable(
1831+
);
1832+
event.callEvent();
1833+
if (event.isCancelled()) {
1834+
- ((org.bukkit.entity.Player) player.getBukkitEntity()).updateInventory();
1835+
+ player.containerMenu.forceHeldSlot(net.minecraft.world.InteractionHand.MAIN_HAND);
1836+
+ player.containerMenu.forceHeldSlot(net.minecraft.world.InteractionHand.OFF_HAND);
1837+
+ player.containerMenu.forceSlot(player.getInventory(), this.slot.getIndex(net.minecraft.world.entity.player.Inventory.INVENTORY_SIZE));
1838+
return InteractionResult.FAIL;
1839+
}
1840+
// Paper end
18171841
diff --git a/net/minecraft/world/level/Level.java b/net/minecraft/world/level/Level.java
18181842
index 579bbba4e823d4d0318e58759ca732b7c8e4d865..38325b7f799f4636f01efcfd248b1736cf86484f 100644
18191843
--- a/net/minecraft/world/level/Level.java
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package io.papermc.paper.event.player;
2+
3+
import org.bukkit.craftbukkit.event.player.CraftPlayerEvent;
4+
import org.bukkit.entity.Player;
5+
import org.bukkit.event.HandlerList;
6+
import org.bukkit.inventory.EquipmentSlot;
7+
import org.bukkit.inventory.ItemStack;
8+
import org.jetbrains.annotations.ApiStatus;
9+
10+
public class PaperPlayerSwapWithEquipmentSlotEvent extends CraftPlayerEvent implements PlayerSwapWithEquipmentSlotEvent {
11+
12+
private final ItemStack itemInHand;
13+
private final EquipmentSlot slot;
14+
private final ItemStack itemToSwap;
15+
16+
private boolean cancelled;
17+
18+
@ApiStatus.Internal
19+
public PaperPlayerSwapWithEquipmentSlotEvent(
20+
final Player player,
21+
final ItemStack itemInHand,
22+
final EquipmentSlot slot,
23+
final ItemStack itemToSwap
24+
) {
25+
super(player);
26+
this.itemInHand = itemInHand;
27+
this.slot = slot;
28+
this.itemToSwap = itemToSwap;
29+
}
30+
31+
@Override
32+
public ItemStack getItemInHand() {
33+
return this.itemInHand.clone();
34+
}
35+
36+
@Override
37+
public EquipmentSlot getSlot() {
38+
return this.slot;
39+
}
40+
41+
@Override
42+
public ItemStack getItemToSwap() {
43+
return this.itemToSwap.clone();
44+
}
45+
46+
@Override
47+
public boolean isCancelled() {
48+
return this.cancelled;
49+
}
50+
51+
@Override
52+
public void setCancelled(final boolean cancel) {
53+
this.cancelled = cancel;
54+
}
55+
56+
@Override
57+
public HandlerList getHandlers() {
58+
return PlayerSwapWithEquipmentSlotEvent.getHandlerList();
59+
}
60+
}

paper-server/src/main/java/io/papermc/paper/plugin/entrypoint/classloader/bytecode/ClassToInterfaceRules.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ private static Set<Class<?>> classes() {
9494
PlayerPickBlockEvent.class,
9595
PlayerPickEntityEvent.class,
9696
PlayerStopUsingItemEvent.class,
97+
PlayerSwapWithEquipmentSlotEvent.class,
9798
PlayerTrackEntityEvent.class,
9899
PlayerUntrackEntityEvent.class,
99100
PrePlayerAttackEntityEvent.class,

0 commit comments

Comments
 (0)