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);
              p.openInventory(anvil);
     
    It doesn't put an Item in it.
    Tried setting the Item in the InventoryOpenEvent as well
    Code (Text):
    @EventHandler
        private void onInvOpen(InventoryOpenEvent e) {
            if (e.getInventory().getType().equals(InventoryType.ANVIL)) {
                System.out.println("yes");
                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));

            p.openInventory(anvilInventory);
    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):
        <repositories>
        <!-- This adds the Spigot Maven repository to the build -->
        <repository>
            <id>spigot-repo</id>
            <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
        </repository>
            <repository>
                <id>jitpack.io</id>
                <url>https://jitpack.io</url>
            </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>org.spigotmc</groupId>
            <artifactId>spigot-api</artifactId>
            <version>1.15.2-R0.1-SNAPSHOT</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1</version>
        </dependency>
    <dependency>
        <groupId>com.github.WesJD.AnvilGUI</groupId>
        <artifactId>anvilgui</artifactId>
        <version>8853507add</version>
    </dependency>

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