Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions patches/api/0501-Fix-and-enhance-openInventory-InventoryView.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: masmc05 <masmc05@gmail.com>
Date: Tue, 1 Oct 2024 00:11:13 +0300
Subject: [PATCH] Fix and enhance openInventory(InventoryView)


diff --git a/src/main/java/org/bukkit/entity/HumanEntity.java b/src/main/java/org/bukkit/entity/HumanEntity.java
index 488604ba1a516b477693877c74712e4a45624a8b..e4f5a728043adbd1bd3bdf9a04b0aed0a1534f0c 100644
--- a/src/main/java/org/bukkit/entity/HumanEntity.java
+++ b/src/main/java/org/bukkit/entity/HumanEntity.java
@@ -154,6 +154,26 @@ public interface HumanEntity extends LivingEntity, AnimalTamer, InventoryHolder
*/
public void openInventory(@NotNull InventoryView inventory);

+ // Paper start - fix and enhance openInventory(InventoryView)
+ /**
+ * Opens an inventory window to the specified inventory view.
+ * <p>
+ * The player associated with the InventoryView must be the same as this
+ * instance of HumanEntity.
+ * <p>
+ * The player of the InventoryView can be checked using
+ * {@link InventoryView#getPlayer()}.
+ * <p>
+ * <b>NOTE:</b> Horse inventories are a special case where setting the override will
+ * have no effect. Horse inventory titles are set by the horse display name.
+ *
+ * @param inventory The view to open
+ * @param titleOverride The title which will override the view and inventory title, this doesn't change
+ * the title returned by {@link InventoryView#title()}, hence "override".
+ */
+ void openInventory(@NotNull InventoryView inventory, @NotNull net.kyori.adventure.text.Component titleOverride);
+ // Paper end - fix and enhance openInventory(InventoryView)
+
/**
* Starts a trade between the player and the villager.
*
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: masmc05 <masmc05@gmail.com>
Date: Mon, 30 Sep 2024 23:51:49 +0300
Subject: [PATCH] Fix and enhance openInventory(InventoryView)

== AT ==
public net.minecraft.world.inventory.HorseInventoryMenu horse

diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java
index e345cdbfab44a0f5da80d738798dbb4424b7ab5c..67d39b86406ca735df55c40bf73dd1685b2ecaac 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java
@@ -436,6 +436,18 @@ public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity {

@Override
public void openInventory(InventoryView inventory) {
+ // Paper start - fix and enhance openInventory(InventoryView)
+ this.openInventory0(inventory, null);
+ }
+
+ @Override
+ public void openInventory(InventoryView inventory, net.kyori.adventure.text.Component defaultOverride) {
+ Preconditions.checkNotNull(defaultOverride, "defaultOverride cannot be null");
+ this.openInventory0(inventory, defaultOverride);
+ }
+
+ private void openInventory0(InventoryView inventory, net.kyori.adventure.text.Component defaultOverride) {
+ // Paper end - fix and enhance openInventory(InventoryView)
Preconditions.checkArgument(this.equals(inventory.getPlayer()), "InventoryView must belong to the opening player");
if (!(this.getHandle() instanceof ServerPlayer)) return; // TODO: NPC support?
if (((ServerPlayer) this.getHandle()).connection == null) return;
@@ -461,14 +473,24 @@ public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity {
}

// Now open the window
- MenuType<?> windowType = CraftContainer.getNotchInventoryType(inventory.getTopInventory());
+ // MenuType<?> windowType = CraftContainer.getNotchInventoryType(inventory.getTopInventory()); // Paper - move down - fix and enhance openInventory(InventoryView)

//String title = inventory.getTitle(); // Paper - comment
- net.kyori.adventure.text.Component adventure$title = inventory.title(); // Paper
+ net.kyori.adventure.text.Component adventure$title = defaultOverride == null ? inventory.title() : defaultOverride; // Paper // Paper - fix and enhance openInventory(InventoryView)
if (adventure$title == null) adventure$title = net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacySection().deserialize(inventory.getTitle()); // Paper
if (result.getFirst() != null) adventure$title = result.getFirst(); // Paper - Add titleOverride to InventoryOpenEvent
//player.connection.send(new ClientboundOpenScreenPacket(container.containerId, windowType, CraftChatMessage.fromString(title)[0])); // Paper - comment
- if (!player.isImmobile()) player.connection.send(new ClientboundOpenScreenPacket(container.containerId, windowType, io.papermc.paper.adventure.PaperAdventure.asVanilla(adventure$title))); // Paper - Prevent opening inventories when frozen
+ // Paper start - fix and enhance openInventory(InventoryView)
+ if (!player.isImmobile()) { // Paper - Prevent opening inventories when frozen
+ if (container instanceof net.minecraft.world.inventory.HorseInventoryMenu menu) {
+ player.connection.send(new net.minecraft.network.protocol.game.ClientboundHorseScreenOpenPacket(menu.containerId, menu.horse.getInventoryColumns(), menu.horse.getId()));
+ } else {
+ MenuType<?> windowType = CraftContainer.getNotchInventoryType(inventory.getTopInventory());
+ player.connection.send(new ClientboundOpenScreenPacket(container.containerId, windowType, io.papermc.paper.adventure.PaperAdventure.asVanilla(adventure$title)));
+ }
+ }
+ container.resumeRemoteUpdates();
+ // Paper end - fix and enhance openInventory(InventoryView)
player.containerMenu = container;
player.initMenu(container);
}