简体   繁体   中英

Creating and Setting ItemStacks in an Anvil Inventory (Bukkit/Spigot API)

How do I set an ItemStack in an anvil inventory? I need the method to be supported for versions 1.8.8-1.14.4. I can create the inventory, but can't set items. This is what I have:

void open(Player player) {
        Inventory createKitGUI = Bukkit.createInventory(null, InventoryType.ANVIL);

        createKitGUI.setItem(1, new ItemStack(XMaterial.PAPER.parseMaterial()));

        player.openInventory(createKitGUI);
    }

I've tried setting in slots 0 and 1 but it doesn't work. 0 and 1 are the first 2 slots in the anvil inventory.

WHY YOU CANNOT DO THIS WITHOUT NMS

After further research I've found out this:

  • You cannot cast CraftInventoryCustom to AnvilInventory because:
    1. AnvilInventory is a child class of CraftInventory.
    2. CraftInventoryCustom is a child class of CraftInventory.
    3. You cannot cast CraftInventoryCustom to AnvilInventory without casting to CraftInventory first.
    4. According to the main spigot developer on this post he states that you cannot cast an CraftInventoryCustom to CraftInventory.

So your only chance is to implement an NMS solution.

ANVIL GUI

If you don't want to dive into NMS development then you can use the following API called AnvilGUI by WesJD. Here you have the documentation of this API.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM