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
This commit is contained in:
Miles 2025-03-23 17:46:10 -05:00 committed by GitHub
parent bb3b7e6979
commit 058455e4ca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 22 additions and 1 deletions

View File

@ -491,6 +491,7 @@ public net.minecraft.world.inventory.AnvilMenu repairItemCountCost
public net.minecraft.world.inventory.BrewingStandMenu brewingStandData public net.minecraft.world.inventory.BrewingStandMenu brewingStandData
public net.minecraft.world.inventory.CraftingMenu access public net.minecraft.world.inventory.CraftingMenu access
public net.minecraft.world.inventory.DispenserMenu dispenser public net.minecraft.world.inventory.DispenserMenu dispenser
public net.minecraft.world.inventory.HorseInventoryMenu horse
public net.minecraft.world.inventory.HorseInventoryMenu SLOT_BODY_ARMOR public net.minecraft.world.inventory.HorseInventoryMenu SLOT_BODY_ARMOR
public net.minecraft.world.inventory.MerchantContainer selectionHint public net.minecraft.world.inventory.MerchantContainer selectionHint
public net.minecraft.world.inventory.Slot slot public net.minecraft.world.inventory.Slot slot

View File

@ -242,6 +242,11 @@ public interface InventoryView {
@NotNull @NotNull
public InventoryType.SlotType getSlotType(int slot); public InventoryType.SlotType getSlotType(int slot);
/**
* Opens the inventory view.
*/
void open();
/** /**
* Closes the inventory view. * Closes the inventory view.
*/ */

View File

@ -11,6 +11,7 @@ import java.util.function.Consumer;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.game.ClientboundHorseScreenOpenPacket;
import net.minecraft.network.protocol.game.ClientboundOpenScreenPacket; import net.minecraft.network.protocol.game.ClientboundOpenScreenPacket;
import net.minecraft.network.protocol.game.ServerboundContainerClosePacket; import net.minecraft.network.protocol.game.ServerboundContainerClosePacket;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
@ -24,6 +25,8 @@ import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.projectile.FireworkRocketEntity; import net.minecraft.world.entity.projectile.FireworkRocketEntity;
import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.HorseInventoryMenu;
import net.minecraft.world.inventory.InventoryMenu;
import net.minecraft.world.inventory.MenuType; import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.inventory.MerchantMenu; import net.minecraft.world.inventory.MerchantMenu;
import net.minecraft.world.item.ItemCooldowns; import net.minecraft.world.item.ItemCooldowns;
@ -455,6 +458,7 @@ public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity {
AbstractContainerMenu container; AbstractContainerMenu container;
if (inventory instanceof CraftInventoryView) { if (inventory instanceof CraftInventoryView) {
container = ((CraftInventoryView) inventory).getHandle(); container = ((CraftInventoryView) inventory).getHandle();
Preconditions.checkArgument(!(container instanceof InventoryMenu), "Can not open player's InventoryView");
} else { } else {
container = new CraftContainer(inventory, this.getHandle(), player.nextContainerCounter()); container = new CraftContainer(inventory, this.getHandle(), player.nextContainerCounter());
} }
@ -481,7 +485,13 @@ public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity {
if (adventure$title == null) adventure$title = net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacySection().deserialize(inventory.getTitle()); // Paper 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 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 //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 if (!player.isImmobile()) {
if (container instanceof HorseInventoryMenu horse) {
player.connection.send(new ClientboundHorseScreenOpenPacket(horse.containerId, horse.horse.getInventoryColumns(), horse.horse.getId()));
} else {
player.connection.send(new ClientboundOpenScreenPacket(container.containerId, windowType, io.papermc.paper.adventure.PaperAdventure.asVanilla(adventure$title)));
}
}
player.containerMenu = container; player.containerMenu = container;
player.initMenu(container); player.initMenu(container);
} }

View File

@ -208,6 +208,11 @@ public abstract class CraftAbstractInventoryView implements InventoryView {
return type; return type;
} }
@Override
public void open() {
getPlayer().openInventory(this);
}
@Override @Override
public void close() { public void close() {
this.getPlayer().closeInventory(); this.getPlayer().closeInventory();