Solved Inventory Click Event - java.lang.IndexOutOfBoundsException Index 45, Size 45

Discussion in 'Plugin Development' started by DiamGamingWTF, Feb 11, 2014.

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

    DiamGamingWTF

    I am developing a plugin and i have come across this error

    Code:
    [210150] [User Authenticator #1INFO] UUID of player DiamGamingWTF is 158f33a137d745d186bfed7f82a716b1
    [210150] [Server threadINFO] DiamGamingWTF[81.109.136.9960069] logged in with entity id 147 at ([SkyBlock] 450.62803597546264, 100.0, 756.4905773063408)
    [210152] [Server threadWARN] [WorldEdit] No compatible nms block class found.
    [210154] [Server threadWARN] Failed to handle packet for 81.109.136.9960069
    java.lang.IndexOutOfBoundsException Index 45, Size 45
    at java.util.ArrayList.rangeCheck(Unknown Source) ~[1.7.0_40]
    at java.util.ArrayList.get(Unknown Source) ~[1.7.0_40]
    at net.minecraft.server.v1_7_R1.Container.getSlot(Container.java104) ~[craftbukkit.jargit-Bukkit-1.7.2-R0.2-50-gdce2b35-b3019jnks]
    at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java1399) ~[craftbukkit.jargit-Bukkit-1.7.2-R0.2-50-gdce2b35-b3019jnks]
    at net.minecraft.server.v1_7_R1.PacketPlayInWindowClick.a(SourceFile32) ~[craftbukkit.jargit-Bukkit-1.7.2-R0.2-50-gdce2b35-b3019jnks]
    at net.minecraft.server.v1_7_R1.PacketPlayInWindowClick.handle(SourceFile10) ~[craftbukkit.jargit-Bukkit-1.7.2-R0.2-50-gdce2b35-b3019jnks]
    at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java146) ~[craftbukkit.jargit-Bukkit-1.7.2-R0.2-50-gdce2b35-b3019jnks]
    at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile134) [craftbukkit.jargit-Bukkit-1.7.2-R0.2-50-gdce2b35-b3019jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java655) [craftbukkit.jargit-Bukkit-1.7.2-R0.2-50-gdce2b35-b3019jnks]
    at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java250) [craftbukkit.jargit-Bukkit-1.7.2-R0.2-50-gdce2b35-b3019jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java545) [craftbukkit.jargit-Bukkit-1.7.2-R0.2-50-gdce2b35-b3019jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java457) [craftbukkit.jargit-Bukkit-1.7.2-R0.2-50-gdce2b35-b3019jnks]
    at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile617) [craftbukkit.jargit-Bukkit-1.7.2-R0.2-50-gdce2b35-b3019jnks]
    [210154] [Server threadINFO] DiamGamingWTF lost connection Internal server error
    [210154] [Server threadINFO] DiamGamingWTF left the game.
    
    All that i know about this is that it is caused by RIGHT clicking the item i have forbidden to be moved within a different inventory (crafting bench and chests) The code i have currently is supposed to cancel the inventory click event on that item and close the inventory, it works fine in the main player inventory but as i said, it breaks when right clicking the item used in the code in a different type of inventory.

    My InventoryClickEvent part of the file:

    Code:java
    1. @EventHandler
    2. public void onInventoryClick(InventoryClickEvent e) {
    3. ItemStack BlankSlot = new ItemStack(Material.INK_SACK);
    4. ItemMeta BS = BlankSlot.getItemMeta();
    5. BlankSlot.setDurability((short) 8);
    6. BS.setDisplayName(ChatColor.DARK_RED + "-=" + ChatColor.GOLD + " Race Item Slot" + ChatColor.DARK_RED + " =-");
    7. BS.setLore(Arrays.asList(ChatColor.GRAY + "This slot will be used when you have", ChatColor.GRAY + "a certain race that requires it"));
    8. BlankSlot.setItemMeta(BS);
    9. BlankSlot.addUnsafeEnchantment(Enchantment.ARROW_INFINITE, 10);
    10.  
    11. Player p = (Player) e.getWhoClicked();
    12. if (e.getCurrentItem().equals(BlankSlot)) {
    13. if (e.isLeftClick()) {
    14. e.setResult(Result.DENY);
    15. e.setCancelled(true);
    16. p.closeInventory();
    17. p.sendMessage(ServerPrefix + ChatColor.RED + "You cannot edit slot 9!");
    18. return;
    19. }
    20. if (e.isRightClick()) {
    21. e.setResult(Result.DENY);
    22. e.setCancelled(true);
    23. p.closeInventory();
    24. p.sendMessage(ServerPrefix + ChatColor.RED + "You cannot edit slot 9!");
    25. return;
    26. }
    27. if (e.isShiftClick()) {
    28. e.setResult(Result.DENY);
    29. e.setCancelled(true);
    30. p.closeInventory();
    31. p.sendMessage(ServerPrefix + ChatColor.RED + "You cannot edit slot 9!");
    32. return;
    33. }
    34. e.setResult(Result.DENY);
    35. e.setCancelled(true);
    36. p.closeInventory();
    37. p.sendMessage(ServerPrefix + ChatColor.RED + "You cannot edit slot 9!");
    38. return;
    39. }
    40. }


    I do not know how to fix this, any help will be much appreciated, thank you.

    PS. The only plugins i am using is my custom made one, vault, worldedit & world guard
    PPS. The e.isRightClick and left click & shift click was me trying to fix this but it has not worked, so you can ignore that part of the code if you want.

    I figured it out:

    Code:java
    1. @EventHandler
    2. public void onInventoryClick(final InventoryClickEvent e) {
    3. ItemStack BlankSlot = new ItemStack(Material.INK_SACK);
    4. ItemMeta BS = BlankSlot.getItemMeta();
    5. BlankSlot.setDurability((short) 8);
    6. BS.setDisplayName(ChatColor.DARK_RED + "-=" + ChatColor.GOLD + " Race Item Slot" + ChatColor.DARK_RED + " =-");
    7. BS.setLore(Arrays.asList(ChatColor.GRAY + "This slot will be used when you have", ChatColor.GRAY + "a certain race that requires it"));
    8. BlankSlot.setItemMeta(BS);
    9. BlankSlot.addUnsafeEnchantment(Enchantment.ARROW_INFINITE, 10);
    10.  
    11. final Player p = (Player) e.getWhoClicked();
    12. if (e.getCurrentItem().equals(BlankSlot) && e.getSlot() == 8) {
    13. e.setResult(Result.DENY);
    14. e.setCancelled(true);
    15. Bukkit.getScheduler().scheduleSyncDelayedTask(Bukkit.getPluginManager().getPlugin("SurvivalServer"), new Runnable() {
    16. public void run() {
    17. p.closeInventory();
    18. p.sendMessage(ServerPrefix + ChatColor.RED + "You cannot edit slot 9!");
    19. }
    20. }, 1L);
    21. return;
    22. }
    23. }



    This is a way i came up with to stop the error and make the event work

    essentially its calling the close inventory a tick later so it does not cause the error.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  2. Offline

    Cryptite

    DiamGamingWTF:
    I just ran into this issue myself but I solved it a little differently without needing tasks. You're right that it's a timing issue because a right-click is the inventory action, PICKUP_HALF. Presumably the inventory is destroyed before minecraft can figure out what stack you're trying to split.

    Anyway, I just opted to remove PICKUP_HALF from the Inventory Actions that I'd check. Basically removing that ensures that only Left-Clicking gets you where you want. This works in my scenario anyway, but I don't need right-clicks happening.

    Either way, figured I'd share my findings as well, knowledge is power!
     
  3. Offline

    N00BHUN73R

    Cryptite
    This is a month old I wouldn't post on it anymore :p
     
  4. Offline

    der71

    @Cryptite
    I have the same problem now with my plugin. I just started making plugins so could you show how you removed PICKEDUP_HALF from the InvertoryActions that you check for?
     
  5. Offline

    Cryptite

    der71
    It's not so much about removing the actions to check for as much as it is just checking that it wasn't PICKEDUP_HALF.

    If you only need left clicks, just check
    Code:java
    1. if (!e.getAction().equals(InventoryAction.PICKUP_HALF)) {
    2. //This wasn't a right-click event
    3. }
     
  6. Offline

    Musician101

    DiamGamingWTF Was having a similar issue but it had to do with detecting which raw slot was clicked. Your fix helped me out.
    Code:java
    1. @EventHandler
    2. public void onInventoryClick(InventoryClickEvent event)
    3. {
    4. final Player player = (Player) event.getWhoClicked();
    5. int slot = event.getRawSlot();
    6. Inventory top = event.getView().getTopInventory();
    7.  
    8. if (slot < 27 && slot > -1)
    9. {
    10.  
    11. //Do something
    12. }
    13. else
    14. {
    15.  
    16. event.setCancelled(true);
    17.  
    18. event.setResult(Result.DENY);
    19.  
    20. Bukkit.getScheduler().scheduleSyncDelayedTask(npc, new Runnable()
    21.  
    22. {
    23.  
    24. public void run()
    25.  
    26. {
    27.  
    28. player.closeInventory();
    29.  
    30. player.sendMessage(ChatColor.GOLD + "Invalid inventory! Click on the shop's inventory.");
    31.  
    32. }
    33.  
    34. }, 1L);
    35. }
    36. }
    37.  
     
Thread Status:
Not open for further replies.

Share This Page