aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/server/0855-Add-PlayerPickItemEvent.patch
blob: bdd51c4e2495b7d34466f18880fb0ac8530a687b (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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 5674852a8be42c4a411858ad3b55dbfa0e5254c6..9230afb0c75133a51e99545b160333ace6036075 100644
--- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
+++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
@@ -994,15 +994,24 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl
         if (stack.isItemEnabled(this.player.level().enabledFeatures())) {
             Inventory playerinventory = this.player.getInventory();
             int i = playerinventory.findSlotMatchingItem(stack);
+            // Paper start - Add PlayerPickItemEvent
+            final int sourceSlot = i;
+            final int targetSlot = Inventory.isHotbarSlot(sourceSlot) ? sourceSlot : playerinventory.getSuitableHotbarSlot();
+            final Player bukkitPlayer = this.player.getBukkitEntity();
+            final io.papermc.paper.event.player.PlayerPickItemEvent event = new io.papermc.paper.event.player.PlayerPickItemEvent(bukkitPlayer, targetSlot, sourceSlot);
+            if (!event.callEvent()) {
+                return;
+            }
+            // Paper end - Add PlayerPickItemEvent
 
             if (i != -1) {
                 if (Inventory.isHotbarSlot(i)) {
-                    playerinventory.selected = i;
+                    playerinventory.selected = event.getTargetSlot(); // Paper - Add target slot
                 } else {
-                    playerinventory.pickSlot(i);
+                    playerinventory.pickSlot(i, event.getTargetSlot()); // Paper - Add target slot
                 }
             } else if (this.player.hasInfiniteMaterials()) {
-                playerinventory.addAndPickItem(stack);
+                playerinventory.addAndPickItem(stack, event.getTargetSlot()); // Paper - Add target slot
             }
 
             this.player.connection.send(new ClientboundSetHeldSlotPacket(playerinventory.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 320b6829b8c31bba3c528babe2dd065bdf29581f..39e536d5a9a6fb2cb7654bba8828bdb2c49425dd 100644
--- a/src/main/java/net/minecraft/world/entity/player/Inventory.java
+++ b/src/main/java/net/minecraft/world/entity/player/Inventory.java
@@ -145,8 +145,10 @@ public class Inventory implements Container, Nameable {
         return -1;
     }
 
-    public void addAndPickItem(ItemStack stack) {
-        this.selected = this.getSuitableHotbarSlot();
+    // Paper start - Add PlayerPickItemEvent
+    public void addAndPickItem(ItemStack stack, final int targetSlot) {
+        this.selected = targetSlot;
+        // Paper end - Add PlayerPickItemEvent
         if (!((ItemStack) this.items.get(this.selected)).isEmpty()) {
             int i = this.getFreeSlot();
 
@@ -158,8 +160,10 @@ public class Inventory implements Container, Nameable {
         this.items.set(this.selected, stack);
     }
 
-    public void pickSlot(int slot) {
-        this.selected = this.getSuitableHotbarSlot();
+    // Paper start - Add PlayerPickItemEvent
+    public void pickSlot(int slot, final 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));