PaperMC/patches/server/0854-Add-PlayerPickItemEvent.patch

71 lines
3.8 KiB
Diff
Raw Permalink Normal View History

2024-12-03 20:34:55 +01:00
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
2024-12-05 10:27:55 +01:00
index a2c404e4cd808a3a676ae3808852e4105bdc9af4..b5a960f9d1b169a6a39e3ab289e4abf161c3ac2c 100644
2024-12-03 20:34:55 +01:00
--- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
+++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
2024-12-05 10:27:55 +01:00
@@ -994,15 +994,25 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl
2024-12-03 20:34:55 +01:00
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;
+ }
2024-12-05 10:27:55 +01:00
+ i = event.getSourceSlot();
2024-12-03 20:34:55 +01:00
if (i != -1) {
2024-12-05 10:27:55 +01:00
- if (Inventory.isHotbarSlot(i)) {
2024-12-03 20:34:55 +01:00
- playerinventory.selected = i;
2024-12-05 10:27:55 +01:00
+ if (Inventory.isHotbarSlot(i) && Inventory.isHotbarSlot(event.getTargetSlot())) {
+ playerinventory.selected = event.getTargetSlot();
2024-12-03 20:34:55 +01:00
} else {
- playerinventory.pickSlot(i);
2024-12-05 10:27:55 +01:00
+ playerinventory.pickSlot(i, event.getTargetSlot());
2024-12-03 20:34:55 +01:00
}
} else if (this.player.hasInfiniteMaterials()) {
- playerinventory.addAndPickItem(stack);
2024-12-05 10:27:55 +01:00
+ playerinventory.addAndPickItem(stack, event.getTargetSlot());
+ // Paper end - Add PlayerPickItemEvent
2024-12-03 20:34:55 +01:00
}
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));