Opening an anvil

Discussion in 'Plugin Development' started by mcdorli, Dec 29, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    mcdorli

    Hi, I need to use an anvil to retrive the input the player types in. I got the opening working, but there are some problems with it. I can't place item in it with code, and if someone click on them it throws an "ArrayOutOfBoundException: size 0", then I tried to cast the anvil to a anvilInventory, but it didn't work (InventoryCustom can't be cast to AnvilInventory). My question: Is there a way, to open an anvil, and manage it? Thanks in advance.
     
  2. Offline

    Zombie_Striker

    Can you post your code? Can you post what line this error is occurring on?
     
  3. Offline

    mcdorli

    Code:
    Inventory anvilInventory = Bukkit.createInventory(null, InventoryType.ANVIL, "Request a ban");
    thisheadMeta.setLore(lores);
    anvilInventory.setItem(0, playerHead);
    player.openInventory(anvilInventory);
    
    The anvil opens just fine, but when I click on a slot, I get a nullpointerException, and where the line should be, it only says ???.java line ???
     
    Last edited: Dec 29, 2015
  4. Offline

    Zombie_Striker

    That means you have an InventoryClickEvent. What you posted looks like it's from something that opens the inventory.

    Can you post the whole InventoryClickEvent? And exactly what line is causing the error.
     
  5. Offline

    mug561

  6. Offline

    mcdorli

    Code:
    Player player = (Player) e.getWhoClicked();
    
            if (e.getCurrentItem() == null
                    || !e.getCurrentItem().hasItemMeta()
                    || !e.getCurrentItem().getItemMeta().hasDisplayName())
                return;
    
            if (e.getInventory().getName().equalsIgnoreCase("List")) {
                e.setCancelled(true);
                List<String> lores = new ArrayList<>();
                switch (ChatColor.stripColor(e.getCurrentItem().getItemMeta().getDisplayName())) {
                    case "Players":
                        Inventory inv = Bukkit.createInventory(null, 54, "Players (page 1)");
                        inv.setItem(53, next);
                        e.getWhoClicked().openInventory(inv);
                        break;
                    case "Messages":
                        Inventory messages = Bukkit.createInventory(null, 54, "Messages");
                        if (commands.getPending().get(player.getName()) != null) {
                            for (String name : commands.getPending().get(player.getName())) {
                                headMeta.setDisplayName(name);
                                headMeta.setOwner(name);
    
                                lores.add(ChatColor.BLUE + "To remove" + ChatColor.YELLOW + name + ChatColor.BLUE + "'s message, click on this head!");
                                headMeta.setLore(lores);
                                playerHead.setItemMeta(headMeta);
                                messages.addItem(playerHead);
                            }
                        }
                        player.openInventory(messages);
                        break;
                    case "Ban request":
                        Inventory anvilInventory = Bukkit.createInventory(null, InventoryType.ANVIL, "Ban request");
                        lores.add("§1§2§3§4"); //For distinction, do not remove this
                        headMeta.setLore(lores);
                        headMeta.setDisplayName(ChatColor.BLUE + "Enter the name of the corresponding player and click on the output, to request him as banned.");
                        playerHead.setItemMeta(headMeta);
                        anvilInventory.setItem(0, playerHead);
                        player.openInventory(anvilInventory);
                        break;
                }
                return;
            }
            if (e.getInventory().getName().contains("Your requests")) {
                e.setCancelled(true);
                if (e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.DARK_GREEN + "Next page")) {
                    String name = e.getInventory().getName();
                    name = name.replace("Your request (page ", "");
                    name = name.replace(")", "");
                    int page;
                    try {
                        page = Integer.parseInt(name);
                    } catch (NumberFormatException ex) {
                        return;
                    }
                    page++;
                    if (Math.floor(Bukkit.getOnlinePlayers().size() / 53) >= page) {
                        Inventory requests = Bukkit.createInventory(null, 54, "Your requests(page " + page + ")");
                        requests.setItem(53, next);
                        e.getWhoClicked().openInventory(requests);
                    }
                }
                return;
            }
            if (e.getInventory().getType() == InventoryType.ANVIL
                    && e.getInventory().getName().equalsIgnoreCase("Ban request")) {
                e.setCancelled(true);
                if (e.getRawSlot() == 2) {
                    System.out.println(e.getCurrentItem().getItemMeta().getDisplayName());
                }
            }
            if (e.getInventory().getName().equalsIgnoreCase("Ban request")) {
                e.setCancelled(true);
            }
            if (e.getInventory().getName().equalsIgnoreCase("Your ban requests")) {
                e.setCancelled(true);
                return;
            }
    
        }
    
    Code:
    Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
        at java.util.ArrayList.rangeCheck(Unknown Source) ~[?:1.8.0_66]
        at java.util.ArrayList.get(Unknown Source) ~[?:1.8.0_66]
        at net.minecraft.server.v1_8_R3.Container.getSlot(Container.java:109) ~[spigot.jar:git-Spigot-76236cb-c373061]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1463) ~[spigot.jar:git-Spigot-76236cb-c373061]
        at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:31) ~[spigot.jar:git-Spigot-76236cb-c373061]
        at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:9) ~[spigot.jar:git-Spigot-76236cb-c373061]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) ~[spigot.jar:git-Spigot-76236cb-c373061]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) ~[?:1.8.0_66]
        at java.util.concurrent.FutureTask.run(Unknown Source) ~[?:1.8.0_66]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) ~[spigot.jar:git-Spigot-76236cb-c373061]
        ... 5 more
    
     
    Last edited: Dec 29, 2015
  7. Offline

    Zombie_Striker

    @mug561
    This is what you use to create the inventory. The name of the inventory is "Request A Ban".
    You do not have anything that checks for if the title is "Request A Ban"
     
  8. Offline

    mcdorli

    I probably renamed something. Again, that doesn't explain the arrayOutOfBoundsException
     
  9. Offline

    teej107

    @mcdorli Have you looked into Bukkit's conversation API to get input?
     
  10. Offline

    mcdorli

    I know you can do it with that, but the guy wants this.
     
  11. Offline

    teej107

    Have you asked him? Not trying not help you but I'm just trying to see if you can go the more simple way.
     
Thread Status:
Not open for further replies.

Share This Page