[SPIGOT-7259] Making Bukkit.createInventory returning matching classes (interfaces) Created: 07/Feb/23  Updated: 25/Dec/24

Status: Open
Project: Spigot
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Minor
Reporter: GotiM Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: inventory

Version: -
Guidelines Read: Yes

 Description   

Currently, Bukkit.createInventory(...) returns a CraftInventoryCustom. I think it would be better for it to return a class matching chosen InventoryType, Just like world.spawnEntity does.
For example:
ANVIL=> AnvilInventory
CRAFTING => CraftingInventory
ENCHANTING => EnchantingInventory

This would give us access to more fuctions, like getRenameText in AnvilInventory, which we can only get by using CraftAnvilInventory.



 Comments   
Comment by Mihad Khondker [ 03/Oct/23 ]

I plan on revisiting this, taking @GotiM's idea. It seems that the "special" inventories you mentioned have a lot of NMS that could be made into effective methods. I've taken a look at the EnchantingInventory currently and there are a lot of potential attributes that could be provided, such as the costs for each enchant. the enchantment hint, and the level hint. There's a lot of potential with this suggestion, and I hope I can create a PR soon for this.

Comment by GotiM [ 24/Feb/23 ]

Hi, I'm back .

Casting is ok, just it would be great if we didn't have to use NMS to get the anvil text input when creating manually an Anvil-based inventory.

Anvil, Enchanting, Beacon and Merchant are the only "special" inventories having non-standard buttons/inputs, like text or enchantment selector

Comment by FreeSoccerHDX [ 13/Feb/23 ]

I think what he wants is that the Methods #createInventory(holder, InventoryType (, title)) directly return the ####Inventory based on the type 
without the need for casting... for example:

AnvilInventory anvilInv = Bukkit.createInventory(null, InventoryType.ANVIL, "abcdefg");
and not AnvilInventory anvilInv = (AnvilInventory) Bukkit.createInventory(null, InventoryType.ANVIL, "abcdefg");

Comment by md_5 [ 13/Feb/23 ]

Some already do, mostly tile entities

 

        converterMap.put(InventoryType.CHEST, DEFAULT_CONVERTER);
        converterMap.put(InventoryType.DISPENSER, new CraftTileInventoryConverter.Dispenser());
        converterMap.put(InventoryType.DROPPER, new CraftTileInventoryConverter.Dropper());
        converterMap.put(InventoryType.FURNACE, new CraftTileInventoryConverter.Furnace());
        converterMap.put(InventoryType.WORKBENCH, DEFAULT_CONVERTER);
        converterMap.put(InventoryType.ENCHANTING, DEFAULT_CONVERTER);
        converterMap.put(InventoryType.BREWING, new CraftTileInventoryConverter.BrewingStand());
        converterMap.put(InventoryType.PLAYER, DEFAULT_CONVERTER);
        converterMap.put(InventoryType.MERCHANT, DEFAULT_CONVERTER);
        converterMap.put(InventoryType.ENDER_CHEST, DEFAULT_CONVERTER);
        converterMap.put(InventoryType.ANVIL, DEFAULT_CONVERTER);
        converterMap.put(InventoryType.SMITHING, DEFAULT_CONVERTER);
        converterMap.put(InventoryType.BEACON, DEFAULT_CONVERTER);
        converterMap.put(InventoryType.HOPPER, new CraftTileInventoryConverter.Hopper());
        converterMap.put(InventoryType.SHULKER_BOX, DEFAULT_CONVERTER);
        converterMap.put(InventoryType.BARREL, DEFAULT_CONVERTER);
        converterMap.put(InventoryType.BLAST_FURNACE, new CraftTileInventoryConverter.BlastFurnace());
        converterMap.put(InventoryType.LECTERN, new CraftTileInventoryConverter.Lectern());
        converterMap.put(InventoryType.SMOKER, new CraftTileInventoryConverter.Smoker());
        converterMap.put(InventoryType.LOOM, DEFAULT_CONVERTER);
        converterMap.put(InventoryType.CARTOGRAPHY, DEFAULT_CONVERTER);
        converterMap.put(InventoryType.GRINDSTONE, DEFAULT_CONVERTER);
        converterMap.put(InventoryType.STONECUTTER, DEFAULT_CONVERTER); 
Generated at Fri Mar 14 09:44:36 UTC 2025 using Jira 10.3.3#10030003-sha1:d220e3fefc8dfc6d47f522d3b9a20c1455e12b7b.