Skip to content

Commit 058455e

Browse files
authored
InventoryView QOL open method (#12282)
* Add QOL open method to InventoryView * Check to ensure the opening isn't a InventoryMenu, allow HorseMenus * Fix instanceof against API instaed of AbstractContainerMenu * [ci skip] Remove suggested comment
1 parent bb3b7e6 commit 058455e

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

build-data/paper.at

+1
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,7 @@ public net.minecraft.world.inventory.AnvilMenu repairItemCountCost
491491
public net.minecraft.world.inventory.BrewingStandMenu brewingStandData
492492
public net.minecraft.world.inventory.CraftingMenu access
493493
public net.minecraft.world.inventory.DispenserMenu dispenser
494+
public net.minecraft.world.inventory.HorseInventoryMenu horse
494495
public net.minecraft.world.inventory.HorseInventoryMenu SLOT_BODY_ARMOR
495496
public net.minecraft.world.inventory.MerchantContainer selectionHint
496497
public net.minecraft.world.inventory.Slot slot

paper-api/src/main/java/org/bukkit/inventory/InventoryView.java

+5
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,11 @@ public int getId() {
242242
@NotNull
243243
public InventoryType.SlotType getSlotType(int slot);
244244

245+
/**
246+
* Opens the inventory view.
247+
*/
248+
void open();
249+
245250
/**
246251
* Closes the inventory view.
247252
*/

paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import net.minecraft.core.BlockPos;
1212
import net.minecraft.nbt.CompoundTag;
1313
import net.minecraft.network.chat.Component;
14+
import net.minecraft.network.protocol.game.ClientboundHorseScreenOpenPacket;
1415
import net.minecraft.network.protocol.game.ClientboundOpenScreenPacket;
1516
import net.minecraft.network.protocol.game.ServerboundContainerClosePacket;
1617
import net.minecraft.resources.ResourceLocation;
@@ -24,6 +25,8 @@
2425
import net.minecraft.world.entity.player.Player;
2526
import net.minecraft.world.entity.projectile.FireworkRocketEntity;
2627
import net.minecraft.world.inventory.AbstractContainerMenu;
28+
import net.minecraft.world.inventory.HorseInventoryMenu;
29+
import net.minecraft.world.inventory.InventoryMenu;
2730
import net.minecraft.world.inventory.MenuType;
2831
import net.minecraft.world.inventory.MerchantMenu;
2932
import net.minecraft.world.item.ItemCooldowns;
@@ -455,6 +458,7 @@ public void openInventory(InventoryView inventory) {
455458
AbstractContainerMenu container;
456459
if (inventory instanceof CraftInventoryView) {
457460
container = ((CraftInventoryView) inventory).getHandle();
461+
Preconditions.checkArgument(!(container instanceof InventoryMenu), "Can not open player's InventoryView");
458462
} else {
459463
container = new CraftContainer(inventory, this.getHandle(), player.nextContainerCounter());
460464
}
@@ -481,7 +485,13 @@ public void openInventory(InventoryView inventory) {
481485
if (adventure$title == null) adventure$title = net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacySection().deserialize(inventory.getTitle()); // Paper
482486
if (result.getFirst() != null) adventure$title = result.getFirst(); // Paper - Add titleOverride to InventoryOpenEvent
483487
//player.connection.send(new ClientboundOpenScreenPacket(container.containerId, windowType, CraftChatMessage.fromString(title)[0])); // Paper - comment
484-
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
488+
if (!player.isImmobile()) {
489+
if (container instanceof HorseInventoryMenu horse) {
490+
player.connection.send(new ClientboundHorseScreenOpenPacket(horse.containerId, horse.horse.getInventoryColumns(), horse.horse.getId()));
491+
} else {
492+
player.connection.send(new ClientboundOpenScreenPacket(container.containerId, windowType, io.papermc.paper.adventure.PaperAdventure.asVanilla(adventure$title)));
493+
}
494+
}
485495
player.containerMenu = container;
486496
player.initMenu(container);
487497
}

paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftAbstractInventoryView.java

+5
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,11 @@ public InventoryType.SlotType getSlotType(final int slot) {
208208
return type;
209209
}
210210

211+
@Override
212+
public void open() {
213+
getPlayer().openInventory(this);
214+
}
215+
211216
@Override
212217
public void close() {
213218
this.getPlayer().closeInventory();

0 commit comments

Comments
 (0)