Add a method on InventoryView to get the MenuType (#12193)

Since there is a new (better) way to create views for players using MenuType, it would be nice to also be able to get it back from InventoryView after creating.
This commit is contained in:
Glicz 2025-05-01 03:43:51 +02:00 committed by GitHub
parent 0e9b94d533
commit 835b955913
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 25 additions and 0 deletions

View File

@ -512,6 +512,7 @@ public net.minecraft.world.flag.FeatureFlagRegistry names
public net.minecraft.world.food.FoodData exhaustionLevel public net.minecraft.world.food.FoodData exhaustionLevel
public net.minecraft.world.food.FoodData foodLevel public net.minecraft.world.food.FoodData foodLevel
public net.minecraft.world.food.FoodData saturationLevel public net.minecraft.world.food.FoodData saturationLevel
public net.minecraft.world.inventory.AbstractContainerMenu menuType
public net.minecraft.world.inventory.AbstractContainerMenu quickcraftSlots public net.minecraft.world.inventory.AbstractContainerMenu quickcraftSlots
public net.minecraft.world.inventory.AbstractContainerMenu quickcraftStatus public net.minecraft.world.inventory.AbstractContainerMenu quickcraftStatus
public net.minecraft.world.inventory.AbstractContainerMenu quickcraftType public net.minecraft.world.inventory.AbstractContainerMenu quickcraftType

View File

@ -2,6 +2,7 @@ package org.bukkit.inventory;
import org.bukkit.entity.HumanEntity; import org.bukkit.entity.HumanEntity;
import org.bukkit.event.inventory.InventoryType; import org.bukkit.event.inventory.InventoryType;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -321,4 +322,16 @@ public interface InventoryView {
*/ */
@Deprecated(since = "1.21.1") // Paper @Deprecated(since = "1.21.1") // Paper
public void setTitle(@NotNull String title); public void setTitle(@NotNull String title);
/**
* Gets the menu type of the inventory view if applicable.
* <p>
* Some inventory types do not support a menu type. In such cases, this method
* returns null. This typically applies to inventories belonging to entities
* like players or animals (e.g., a horse).
*
* @return the menu type of the inventory view or null if not applicable
*/
@ApiStatus.Experimental
@Nullable MenuType getMenuType();
} }

View File

@ -95,6 +95,11 @@ public class CraftContainer extends AbstractContainerMenu {
this.title = title; this.title = title;
} }
@Override
public MenuType getMenuType() {
return CraftMenuType.minecraftToBukkit(getNotchInventoryType(inventory));
}
}, player, id); }, player, id);
} }

View File

@ -94,6 +94,12 @@ public class CraftInventoryView<T extends AbstractContainerMenu, I extends Inven
this.title = title; this.title = title;
} }
@Override
public org.bukkit.inventory.MenuType getMenuType() {
MenuType<?> menuType = container.menuType;
return menuType != null ? CraftMenuType.minecraftToBukkit(menuType) : null;
}
public boolean isInTop(int rawSlot) { public boolean isInTop(int rawSlot) {
return rawSlot < this.viewing.getSize(); return rawSlot < this.viewing.getSize();
} }