From f4817c90130aae971830820e1b518567f22b723c Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Thu, 5 Dec 2024 10:27:55 +0100 Subject: [PATCH] Finish PlayerPickItemEvent --- patches/api/Add-PlayerPickItemEvent.patch | 14 +++++++++----- patches/server/Add-PlayerPickItemEvent.patch | 12 +++++++----- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/patches/api/Add-PlayerPickItemEvent.patch b/patches/api/Add-PlayerPickItemEvent.patch index 893aad1821..f516720cec 100644 --- a/patches/api/Add-PlayerPickItemEvent.patch +++ b/patches/api/Add-PlayerPickItemEvent.patch @@ -67,20 +67,24 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + + /** + * Returns the slot in which the item that will be put into the players hotbar is located. ++ *

++ * Returns {@code -1} if the item is not in the player's inventory and should be spawned in if in creative mode. + * -+ * @return player inventory slot (0-35 inclusive) ++ * @return player inventory slot (0-35 inclusive, or {@code -1} not taken from the inventory) + */ -+ public @Range(from = 0, to = 35) int getSourceSlot() { ++ public @Range(from = -1, to = 35) int getSourceSlot() { + return this.sourceSlot; + } + + /** + * Change the source slot from which the item that will be put in the players hotbar will be taken. ++ *

++ * If set to {@code -1} and the player is in creative mode, the item will be spawned in. + * -+ * @param sourceSlot player inventory slot (0-35 inclusive) ++ * @param sourceSlot player inventory slot (0-35 inclusive, or {@code -1} not taken from the inventory) + */ -+ public void setSourceSlot(final @Range(from = 0, to = 35) int sourceSlot) { -+ Preconditions.checkArgument(sourceSlot >= 0 && sourceSlot <= 35, "Source slot must be in range of the player's inventory slot"); ++ public void setSourceSlot(final @Range(from = -1, to = 35) int sourceSlot) { ++ Preconditions.checkArgument(sourceSlot >= -1 && sourceSlot <= 35, "Source slot must be in range of the player's inventory slot"); + this.sourceSlot = sourceSlot; + } + diff --git a/patches/server/Add-PlayerPickItemEvent.patch b/patches/server/Add-PlayerPickItemEvent.patch index c13e60ced6..5aaa2ea84b 100644 --- a/patches/server/Add-PlayerPickItemEvent.patch +++ b/patches/server/Add-PlayerPickItemEvent.patch @@ -20,19 +20,21 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + if (!event.callEvent()) { + return; + } -+ // Paper end - Add PlayerPickItemEvent ++ i = event.getSourceSlot(); if (i != -1) { - if (Inventory.isHotbarSlot(i)) { +- if (Inventory.isHotbarSlot(i)) { - playerinventory.selected = i; -+ playerinventory.selected = event.getTargetSlot(); // Paper - Add target slot ++ if (Inventory.isHotbarSlot(i) && Inventory.isHotbarSlot(event.getTargetSlot())) { ++ playerinventory.selected = event.getTargetSlot(); } else { - playerinventory.pickSlot(i); -+ playerinventory.pickSlot(i, event.getTargetSlot()); // Paper - Add target slot ++ playerinventory.pickSlot(i, event.getTargetSlot()); } } else if (this.player.hasInfiniteMaterials()) { - playerinventory.addAndPickItem(stack); -+ playerinventory.addAndPickItem(stack, event.getTargetSlot()); // Paper - Add target slot ++ playerinventory.addAndPickItem(stack, event.getTargetSlot()); ++ // Paper end - Add PlayerPickItemEvent } this.player.connection.send(new ClientboundSetHeldSlotPacket(playerinventory.selected));