1.15.2 Placing Items in Anvil Inventory

Discussion in 'Spigot Plugin Development' started by Skifty, Feb 9, 2020.

  1. I want to create an item editor to for example change the name, give it a custom weapon type, etc.
    Tried using an anvil for it, but I have a small problem:
    When I open the anvil Inventory with
    Code (Text):
    ItemStack paper = new ItemStack(Material.PAPER);
              Inventory anvil = Bukkit.getServer().createInventory(p, InventoryType.ANVIL, "Editor");
              anvil.setItem(0, paper);
    It doesn't put an Item in it.
    Tried setting the Item in the InventoryOpenEvent as well
    Code (Text):
        private void onInvOpen(InventoryOpenEvent e) {
            if (e.getInventory().getType().equals(InventoryType.ANVIL)) {
                ItemStack paper = new ItemStack(Material.PAPER);
                Inventory inv = e.getInventory();
                inv.setItem(0, paper);
    This works (kind of). When I open an anvilInventory by rightclicking on one, it puts the item in. If I open it with the code above, it doesn't put the item in.
  2. Try inv.addItem instead
  3. Same result
    Rightclicking an anvil works and adds the item, opening the inv per plugin doesn't add it.
  4. Okay. I did some research and found out that this is not possible without the use of packets. (As stated here and here)

    The code I've tried at first throws a ClassCastException:

    Code (Java):
            Player p = (Player) commandSender;

            Inventory inv = Bukkit.createInventory(p, InventoryType.ANVIL, "Test");
            AnvilInventory anvilInventory = (AnvilInventory) inv;

            anvilInventory.setItem(0, new ItemStack(Material.PAPER));

    This is because AnvilInventory can not be casted to CraftInventoryCustom.

    You should try to look into packets if you want to do this.
    • Informative Informative x 1
  5. Ah well, guess that has to wait for a while then, never worked with nms.
    Thanks a lot
  6. Seems like you don't have it imported correctly
  7. Well, here's my repositories and dependencies:
    Code (Text):
        <!-- This adds the Spigot Maven repository to the build -->
        <!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->

  8. Then put the error in the API theard, the creator will probably help you a lot more