aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/server/0895-Add-PlayerPickItemEvent.patch
blob: 005f1da60dd9f0fed004b2b149cba43a707cc124 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: RodneyMKay <36546810+RodneyMKay@users.noreply.github.com>
Date: Wed, 8 Sep 2021 21:34:01 +0200
Subject: [PATCH] Add PlayerPickItemEvent


diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
index 0206521fff9c148e95cfb031b05511041af39746..ff8593b3835be310affc39cde964fabfcc105094 100644
--- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
+++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
@@ -932,8 +932,17 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl
             this.disconnect("Invalid hotbar selection (Hacking?)", org.bukkit.event.player.PlayerKickEvent.Cause.ILLEGAL_ACTION); // Paper - kick event cause
             return;
         }
-        this.player.getInventory().pickSlot(packet.getSlot()); // Paper - Diff above if changed
         // Paper end - validate pick item position
+        // Paper start - Add PlayerPickItemEvent
+        Player bukkitPlayer = this.player.getBukkitEntity();
+        int targetSlot = this.player.getInventory().getSuitableHotbarSlot();
+        int sourceSlot = packet.getSlot();
+
+        io.papermc.paper.event.player.PlayerPickItemEvent event = new io.papermc.paper.event.player.PlayerPickItemEvent(bukkitPlayer, targetSlot, sourceSlot);
+        if (!event.callEvent()) return;
+
+        this.player.getInventory().pickSlot(event.getSourceSlot(), event.getTargetSlot());
+        // Paper end - Add PlayerPickItemEvent
         this.player.connection.send(new ClientboundContainerSetSlotPacket(-2, 0, this.player.getInventory().selected, this.player.getInventory().getItem(this.player.getInventory().selected)));
         this.player.connection.send(new ClientboundContainerSetSlotPacket(-2, 0, packet.getSlot(), this.player.getInventory().getItem(packet.getSlot())));
         this.player.connection.send(new ClientboundSetCarriedItemPacket(this.player.getInventory().selected));
diff --git a/src/main/java/net/minecraft/world/entity/player/Inventory.java b/src/main/java/net/minecraft/world/entity/player/Inventory.java
index 2c20441cce1e07239a65aadfb85004fcdf3672c8..1e77482a98869e464c1f0a873cff8febf7924c8c 100644
--- a/src/main/java/net/minecraft/world/entity/player/Inventory.java
+++ b/src/main/java/net/minecraft/world/entity/player/Inventory.java
@@ -171,7 +171,13 @@ public class Inventory implements Container, Nameable {
     }
 
     public void pickSlot(int slot) {
-        this.selected = this.getSuitableHotbarSlot();
+        // Paper start - Add PlayerPickItemEvent
+        pickSlot(slot, this.getSuitableHotbarSlot());
+    }
+
+    public void pickSlot(int slot, int targetSlot) {
+        this.selected = targetSlot;
+        // Paper end - Add PlayerPickItemEvent
         ItemStack itemstack = (ItemStack) this.items.get(this.selected);
 
         this.items.set(this.selected, (ItemStack) this.items.get(slot));