Add CartographyItemEvent and get/setResult for CartographyInventory (#10396)

This commit is contained in:
Janet Blackquill 2024-04-20 14:10:35 -04:00 committed by GitHub
parent a033033b7a
commit c5f68ff9be
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 121 additions and 2 deletions

View File

@ -3,6 +3,7 @@ From: Nassim Jahnke <nassim@njahnke.dev>
Date: Sat, 25 Apr 2020 23:31:28 +0200
Subject: [PATCH] Add item slot convenience methods
Co-authored-by: Janet Blackquill <uhhadd@gmail.com>
diff --git a/src/main/java/org/bukkit/inventory/AnvilInventory.java b/src/main/java/org/bukkit/inventory/AnvilInventory.java
index 52519cd877017704b53d36088d4d4c28f8f27397..c60be4fd24c7fdf65251dd6169e5e1ac3b588d95 100644
@ -73,6 +74,37 @@ index 52519cd877017704b53d36088d4d4c28f8f27397..c60be4fd24c7fdf65251dd6169e5e1ac
+ }
+ // Paper end
}
diff --git a/src/main/java/org/bukkit/inventory/CartographyInventory.java b/src/main/java/org/bukkit/inventory/CartographyInventory.java
index 29c9b2682b92433f468d434d25d3c2495b5ac91b..d040ecea3a086711acbf5a852def090ba6c51fae 100644
--- a/src/main/java/org/bukkit/inventory/CartographyInventory.java
+++ b/src/main/java/org/bukkit/inventory/CartographyInventory.java
@@ -3,4 +3,25 @@ package org.bukkit.inventory;
/**
* Interface to the inventory of a Cartography table.
*/
-public interface CartographyInventory extends Inventory { }
+public interface CartographyInventory extends Inventory {
+ // Paper begin - add getResult/setResult to CartographyInventory
+ /**
+ * Check what item is in the result slot of this smithing table.
+ *
+ * @return the result item
+ */
+ @org.jetbrains.annotations.Nullable
+ default ItemStack getResult() {
+ return this.getItem(2); // net.minecraft.world.inventory.CartographyTableMenu.RESULT_SLOT
+ }
+
+ /**
+ * Set the item in the result slot of the smithing table
+ *
+ * @param newResult the new result item
+ */
+ default void setResult(final @org.jetbrains.annotations.Nullable ItemStack newResult) {
+ this.setItem(2, newResult); // net.minecraft.world.inventory.CartographyTableMenu.RESULT_SLOT
+ }
+ // Paper end - add getResult/setResult to CartographyInventory
+}
diff --git a/src/main/java/org/bukkit/inventory/GrindstoneInventory.java b/src/main/java/org/bukkit/inventory/GrindstoneInventory.java
index 9048892c8768c6b4d6cea03da73339f13bfbe82e..1c750108f55a0a31ad23433b333e0ea486a63ff2 100644
--- a/src/main/java/org/bukkit/inventory/GrindstoneInventory.java

View File

@ -0,0 +1,44 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Janet Blackquill <uhhadd@gmail.com>
Date: Sun, 7 Apr 2024 16:51:04 -0400
Subject: [PATCH] Add CartographyItemEvent
Similar to SmithItemEvent, but for cartography tables.
diff --git a/src/main/java/io/papermc/paper/event/player/CartographyItemEvent.java b/src/main/java/io/papermc/paper/event/player/CartographyItemEvent.java
new file mode 100644
index 0000000000000000000000000000000000000000..659b620696e5cc0784ed707c70876e4348897c7f
--- /dev/null
+++ b/src/main/java/io/papermc/paper/event/player/CartographyItemEvent.java
@@ -0,0 +1,31 @@
+package io.papermc.paper.event.player;
+
+import org.bukkit.inventory.InventoryView;
+import org.bukkit.inventory.CartographyInventory;
+import org.bukkit.event.inventory.ClickType;
+import org.bukkit.event.inventory.InventoryType;
+import org.bukkit.event.inventory.InventoryAction;
+import org.bukkit.event.inventory.InventoryClickEvent;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.ApiStatus;
+
+/**
+ * Called when the recipe of an Item is completed inside a cartography table.
+ */
+public class CartographyItemEvent extends InventoryClickEvent {
+ @ApiStatus.Internal
+ public CartographyItemEvent(@NotNull InventoryView view, @NotNull InventoryType.SlotType type, int slot, @NotNull ClickType click, @NotNull InventoryAction action) {
+ super(view, type, slot, click, action);
+ }
+
+ @ApiStatus.Internal
+ public CartographyItemEvent(@NotNull InventoryView view, @NotNull InventoryType.SlotType type, int slot, @NotNull ClickType click, @NotNull InventoryAction action, int key) {
+ super(view, type, slot, click, action, key);
+ }
+
+ @NotNull
+ @Override
+ public CartographyInventory getInventory() {
+ return (CartographyInventory) super.getInventory();
+ }
+}

View File

@ -21,10 +21,10 @@ index 1b090f1e79b996e52097afc49c1cec85936653e6..bf3c5efa0d58c58a5b0b6b73880aaf03
// TODO rebase into util patch
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
index 4aa6084fb3540f7811f13e09b5d11e2e0cf48220..d5dd3c0d50ad34fe339f7aaaba0652ec69441e44 100644
index 3fbc0312ed291a3878c26c005bfc79f417c695e4..44f4665db613c558078df5bb49106e4ca5679dfe 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
@@ -3390,6 +3390,35 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
@@ -3392,6 +3392,35 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
}
// Paper end

View File

@ -0,0 +1,43 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Janet Blackquill <uhhadd@gmail.com>
Date: Sun, 7 Apr 2024 16:52:42 -0400
Subject: [PATCH] Add CartographyItemEvent
diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
index 2ad17823bf442ce0455227b64e5d3bb10d0ee2c1..fe2ef36ab5dc4b933abf24dbfd0e811c53239cf0 100644
--- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
+++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
@@ -3101,6 +3101,19 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl
}
}
+ // Paper start - cartography item event
+ if (packet.getSlotNum() == net.minecraft.world.inventory.CartographyTableMenu.RESULT_SLOT && top instanceof org.bukkit.inventory.CartographyInventory cartographyInventory) {
+ org.bukkit.inventory.ItemStack result = cartographyInventory.getResult();
+ if (result != null && !result.isEmpty()) {
+ if (click == ClickType.NUMBER_KEY) {
+ event = new io.papermc.paper.event.player.CartographyItemEvent(inventory, type, packet.getSlotNum(), click, action, packet.getButtonNum());
+ } else {
+ event = new io.papermc.paper.event.player.CartographyItemEvent(inventory, type, packet.getSlotNum(), click, action);
+ }
+ }
+ }
+ // Paper end - cartography item event
+
event.setCancelled(cancelled);
AbstractContainerMenu oldContainer = this.player.containerMenu; // SPIGOT-1224
this.cserver.getPluginManager().callEvent(event);
diff --git a/src/main/java/net/minecraft/world/inventory/CartographyTableMenu.java b/src/main/java/net/minecraft/world/inventory/CartographyTableMenu.java
index 45bf1c95d86bdfc709c5f1a1fbefb18e1cc51f4c..5ae3fd3ec22b4c3aa6bbd0cbda770f39dcab24a1 100644
--- a/src/main/java/net/minecraft/world/inventory/CartographyTableMenu.java
+++ b/src/main/java/net/minecraft/world/inventory/CartographyTableMenu.java
@@ -69,7 +69,7 @@ public class CartographyTableMenu extends AbstractContainerMenu {
this.resultContainer = new ResultContainer(this.createBlockHolder(context)) { // Paper - Add missing InventoryHolders
@Override
public void setChanged() {
- CartographyTableMenu.this.slotsChanged(this);
+ // CartographyTableMenu.this.slotsChanged(this); // Paper - Add CatographyItemEvent - do not recompute results if the result slot changes - allows to set the result slot via api
super.setChanged();
}