2023-08-26 21:28:42 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
Date: Fri, 4 Mar 2022 12:45:03 -0800
Subject: [PATCH] Add titleOverride to InventoryOpenEvent
diff --git a/src/main/java/net/minecraft/server/level/ServerPlayer.java b/src/main/java/net/minecraft/server/level/ServerPlayer.java
2024-05-29 22:58:57 +02:00
index 88ea8ed1b81ece6315bc8dcde04b5fa1bb6a7d04..9179434d490bc8546ba3a35434998b9fc916e6b2 100644
2023-08-26 21:28:42 +02:00
--- a/src/main/java/net/minecraft/server/level/ServerPlayer.java
+++ b/src/main/java/net/minecraft/server/level/ServerPlayer.java
2024-05-26 23:30:45 +02:00
@@ -1618,12 +1618,17 @@ public class ServerPlayer extends Player {
2023-08-26 21:28:42 +02:00
this.nextContainerCounter();
AbstractContainerMenu container = factory.createMenu(this.containerCounter, this.getInventory(), this);
2024-01-14 16:31:39 +01:00
+ Component title = null; // Paper - Add titleOverride to InventoryOpenEvent
2023-08-26 21:28:42 +02:00
// CraftBukkit start - Inventory open hook
if (container != null) {
container.setTitle(factory.getDisplayName());
boolean cancelled = false;
- container = CraftEventFactory.callInventoryOpenEvent(this, container, cancelled);
2024-01-14 16:31:39 +01:00
+ // Paper start - Add titleOverride to InventoryOpenEvent
2023-08-26 21:28:42 +02:00
+ final com.mojang.datafixers.util.Pair<net.kyori.adventure.text.Component, AbstractContainerMenu> result = CraftEventFactory.callInventoryOpenEventWithTitle(this, container, cancelled);
+ container = result.getSecond();
+ title = PaperAdventure.asVanilla(result.getFirst());
2024-01-14 16:31:39 +01:00
+ // Paper end - Add titleOverride to InventoryOpenEvent
2023-08-26 21:28:42 +02:00
if (container == null && !cancelled) { // Let pre-cancelled events fall through
// SPIGOT-5263 - close chest if cancelled
if (factory instanceof Container) {
2024-05-26 23:30:45 +02:00
@@ -1645,7 +1650,7 @@ public class ServerPlayer extends Player {
2023-08-26 21:28:42 +02:00
} else {
// CraftBukkit start
this.containerMenu = container;
2024-01-21 12:11:43 +01:00
- if (!this.isImmobile()) this.connection.send(new ClientboundOpenScreenPacket(container.containerId, container.getType(), container.getTitle())); // Paper - Prevent opening inventories when frozen
2024-01-14 16:31:39 +01:00
+ if (!this.isImmobile()) this.connection.send(new ClientboundOpenScreenPacket(container.containerId, container.getType(), Objects.requireNonNullElseGet(title, container::getTitle))); // Paper - Add titleOverride to InventoryOpenEvent
2023-08-26 21:28:42 +02:00
// CraftBukkit end
this.initMenu(container);
return OptionalInt.of(this.containerCounter);
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java
2024-04-25 00:36:49 +02:00
index 3d36d79a4e7f16f6face3465cdf54656984f3ebc..41f3cdec7deabf34358b8087df77169f85a5b919 100644
2023-08-26 21:28:42 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java
2024-04-25 00:36:49 +02:00
@@ -358,12 +358,16 @@ public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity {
2023-08-26 21:28:42 +02:00
Preconditions.checkArgument(windowType != null, "Unknown windowType");
AbstractContainerMenu container = new CraftContainer(inventory, player, player.nextContainerCounter());
- container = CraftEventFactory.callInventoryOpenEvent(player, container);
2024-01-14 16:31:39 +01:00
+ // Paper start - Add titleOverride to InventoryOpenEvent
2023-08-26 21:28:42 +02:00
+ final com.mojang.datafixers.util.Pair<net.kyori.adventure.text.Component, AbstractContainerMenu> result = CraftEventFactory.callInventoryOpenEventWithTitle(player, container);
+ container = result.getSecond();
2024-01-14 16:31:39 +01:00
+ // Paper end - Add titleOverride to InventoryOpenEvent
2023-08-26 21:28:42 +02:00
if (container == null) return;
//String title = container.getBukkitView().getTitle(); // Paper - comment
net.kyori.adventure.text.Component adventure$title = container.getBukkitView().title(); // Paper
if (adventure$title == null) adventure$title = net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacySection().deserialize(container.getBukkitView().getTitle()); // Paper
2024-01-14 16:31:39 +01:00
+ if (result.getFirst() != null) adventure$title = result.getFirst(); // Paper - Add titleOverride to InventoryOpenEvent
2023-08-26 21:28:42 +02:00
//player.connection.send(new ClientboundOpenScreenPacket(container.containerId, windowType, CraftChatMessage.fromString(title)[0])); // Paper - comment
2024-01-21 12:11:43 +01:00
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
2024-04-25 00:36:49 +02:00
@@ -439,7 +443,10 @@ public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity {
2023-08-26 21:28:42 +02:00
}
// Trigger an INVENTORY_OPEN event
- container = CraftEventFactory.callInventoryOpenEvent(player, container);
2024-01-14 16:31:39 +01:00
+ // Paper start - Add titleOverride to InventoryOpenEvent
2023-08-26 21:28:42 +02:00
+ final com.mojang.datafixers.util.Pair<net.kyori.adventure.text.Component, AbstractContainerMenu> result = CraftEventFactory.callInventoryOpenEventWithTitle(player, container);
+ container = result.getSecond();
2024-01-14 16:31:39 +01:00
+ // Paper end - Add titleOverride to InventoryOpenEvent
2023-08-26 21:28:42 +02:00
if (container == null) {
return;
}
2024-04-25 00:36:49 +02:00
@@ -450,6 +457,7 @@ public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity {
2023-08-26 21:28:42 +02:00
//String title = inventory.getTitle(); // Paper - comment
net.kyori.adventure.text.Component adventure$title = inventory.title(); // Paper
if (adventure$title == null) adventure$title = net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacySection().deserialize(inventory.getTitle()); // Paper
2024-01-21 19:37:09 +01:00
+ if (result.getFirst() != null) adventure$title = result.getFirst(); // Paper - Add titleOverride to InventoryOpenEvent
2023-08-26 21:28:42 +02:00
//player.connection.send(new ClientboundOpenScreenPacket(container.containerId, windowType, CraftChatMessage.fromString(title)[0])); // Paper - comment
2024-01-21 12:11:43 +01:00
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
2023-08-26 21:28:42 +02:00
player.containerMenu = container;
diff --git a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
2024-06-13 17:45:43 +02:00
index cdb5fd2642e7c5325f11633de8d58a5a8cdc2c77..64d4246b9d13e6fe8b638c4a6b664f87fc0e66a0 100644
2023-08-26 21:28:42 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
+++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
2024-05-11 23:48:37 +02:00
@@ -1381,10 +1381,21 @@ public class CraftEventFactory {
2023-08-26 21:28:42 +02:00
}
public static AbstractContainerMenu callInventoryOpenEvent(ServerPlayer player, AbstractContainerMenu container) {
- return CraftEventFactory.callInventoryOpenEvent(player, container, false);
2024-01-14 16:31:39 +01:00
+ // Paper start - Add titleOverride to InventoryOpenEvent
2023-08-26 21:28:42 +02:00
+ return callInventoryOpenEventWithTitle(player, container).getSecond();
+ }
+ public static com.mojang.datafixers.util.Pair<net.kyori.adventure.text.@org.jetbrains.annotations.Nullable Component, @org.jetbrains.annotations.Nullable AbstractContainerMenu> callInventoryOpenEventWithTitle(ServerPlayer player, AbstractContainerMenu container) {
+ return CraftEventFactory.callInventoryOpenEventWithTitle(player, container, false);
2024-01-14 16:31:39 +01:00
+ // Paper end - Add titleOverride to InventoryOpenEvent
2023-08-26 21:28:42 +02:00
}
+ @Deprecated @io.papermc.paper.annotation.DoNotUse // Paper - use method that acknowledges title overrides
public static AbstractContainerMenu callInventoryOpenEvent(ServerPlayer player, AbstractContainerMenu container, boolean cancelled) {
2024-01-14 16:31:39 +01:00
+ // Paper start - Add titleOverride to InventoryOpenEvent
2023-08-26 21:28:42 +02:00
+ return callInventoryOpenEventWithTitle(player, container, cancelled).getSecond();
+ }
+ public static com.mojang.datafixers.util.Pair<net.kyori.adventure.text.@org.jetbrains.annotations.Nullable Component, @org.jetbrains.annotations.Nullable AbstractContainerMenu> callInventoryOpenEventWithTitle(ServerPlayer player, AbstractContainerMenu container, boolean cancelled) {
2024-01-14 16:31:39 +01:00
+ // Paper end - Add titleOverride to InventoryOpenEvent
2023-08-26 21:28:42 +02:00
if (player.containerMenu != player.inventoryMenu) { // fire INVENTORY_CLOSE if one already open
2024-01-21 19:37:09 +01:00
player.connection.handleContainerClose(new ServerboundContainerClosePacket(player.containerMenu.containerId), InventoryCloseEvent.Reason.OPEN_NEW); // Paper - Inventory close reason
2023-08-26 21:28:42 +02:00
}
2024-05-11 23:48:37 +02:00
@@ -1399,10 +1410,10 @@ public class CraftEventFactory {
2023-08-26 21:28:42 +02:00
if (event.isCancelled()) {
container.transferTo(player.containerMenu, craftPlayer);
- return null;
2024-01-14 16:31:39 +01:00
+ return com.mojang.datafixers.util.Pair.of(null, null); // Paper - Add titleOverride to InventoryOpenEvent
2023-08-26 21:28:42 +02:00
}
- return container;
2024-01-14 16:31:39 +01:00
+ return com.mojang.datafixers.util.Pair.of(event.titleOverride(), container); // Paper - Add titleOverride to InventoryOpenEvent
2023-08-26 21:28:42 +02:00
}
public static ItemStack callPreCraftEvent(Container matrix, Container resultInventory, ItemStack result, InventoryView lastCraftView, boolean isRepair) {