Solved ArrayIndexOutOfBoundsException: -1

Discussion in 'Plugin Development' started by euro1988, Jul 10, 2013.

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

    euro1988

    Code:java
    1. public void BrisiQuartz(PlayerInteractEvent e){
    2. int amountToRemove = 1;
    3. Player p = e.getPlayer();
    4. if(e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK))
    5. if(checkForAmount(new ArrayList<ItemStack>(p.getInventory().all(Material.QUARTZ).values()), 40)){
    6. }
    7. while(amountToRemove > 0) {
    8. int first = p.getInventory().first(Material.QUARTZ);
    9. ItemStack stack = p.getInventory().getItem(first);
    10. if(stack.getAmount() > amountToRemove){
    11. stack.setAmount(stack.getAmount() - amountToRemove);
    12. amountToRemove = 0;
    13. } else {
    14. amountToRemove -= stack.getAmount();
    15. p.getInventory().remove(stack);
    16. }
    17. }
    18. }

    Code:text
    1. Could not pass event PlayerInteractEvent to Brogan v2.1
    2. org.bukkit.event.EventException
    3. at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
    4. at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    5. at org.bukkit.plugin.TimedRegisteredListener.callEvent(TimedRegisteredListener.java:30)
    6. at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:478)
    7. at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:463)
    8. at org.bukkit.craftbukkit.v1_6_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:208)
    9. at net.minecraft.server.v1_6_R1.PlayerInteractManager.interact(PlayerInteractManager.java:374)
    10. at net.minecraft.server.v1_6_R1.PlayerConnection.a(PlayerConnection.java:649)
    11. at net.minecraft.server.v1_6_R1.Packet15Place.handle(SourceFile:58)
    12. at net.minecraft.server.v1_6_R1.NetworkManager.b(NetworkManager.java:293)
    13. at net.minecraft.server.v1_6_R1.PlayerConnection.d(PlayerConnection.java:118)
    14. at net.minecraft.server.v1_6_R1.ServerConnection.b(SourceFile:37)
    15. at net.minecraft.server.v1_6_R1.DedicatedServerConnection.b(SourceFile:30)
    16. at net.minecraft.server.v1_6_R1.MinecraftServer.t(MinecraftServer.java:592)
    17. at net.minecraft.server.v1_6_R1.DedicatedServer.t(DedicatedServer.java:239)
    18. at net.minecraft.server.v1_6_R1.MinecraftServer.s(MinecraftServer.java:481)
    19. at net.minecraft.server.v1_6_R1.MinecraftServer.run(MinecraftServer.java:413)
    20. at net.minecraft.server.v1_6_R1.ThreadServerApplication.run(SourceFile:582)
    21.  
    22. Caused by: java.lang.ArrayIndexOutOfBoundsException


    Code:java
    1. private boolean checkForAmount(ArrayList<? extends ItemStack> items, int amount){
    2. for(ItemStack i : items)
    3. amount -= i.getAmount();
    4. if(amount <= 0)
    5. return true;
    6. return false;


    Why do I get it and how to fix it?

    Ugh, anyone?

    Didn't manage to fix it...
    The code executes fine and works tho.

    I somehow need to get rid of
    1. ItemStack stack = p.getInventory().getItem(first);
    because it gives me this error.

    Any ideas?

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

    Sessional

  3. Offline

    euro1988

    Sessional

    Yeah, it does return -1. How do I break it, with return?
     
  4. Offline

    Sessional

    euro1988
    It's in a loop so break; or return; would work, or even continue - but if you need continue chances are you'll get stuck in an infinite loop if you aren't careful.
     
  5. Offline

    euro1988

    Sessional
    Uh, is there any other safer way to remove the ItemStack one by one then?
     
  6. Offline

    Sessional

    euro1988
    You can get by with a break or a return statement if you want, if you want some help formulating a safe loop, go ahead and explain exactly what your loop is attempting to do and I'll help ya out.
     
  7. Offline

    euro1988

    Sessional
    It checks if player who right clicks with nether quartz has any of the item left, then if he has, it removes one nether quartz with each right click. When there's none , it should stop removing.
     
  8. Offline

    Sessional

    euro1988
    So placing the block also removes an extra block from the inventory?

    Code:
    ItemStack[] items = player.getInventory().getContents();
     
    for (ItemStack i : items)
    {
      if (i.getType() == Material.NetherQuartz)
      {
        if (i.getAmount() > 1)
        {
            i.setamount(i.getAmount() - 1);
            break;
        }
      }
    }
    With this code you will loop through ALL the slots, and if there is a netherquartz item stack with an amount > 1 it'll remove it. This will have flaws, because if it's in the inventory and they aren't placing it it won't remove a stack higher then one, but that's easy enough to fix by checking the stack in the players hand first. As a note: these events are called BEFORE the block is actually placed - you may not want to remove the block that is getting placed, it might cause issues.
     
    euro1988 likes this.
  9. Offline

    euro1988

    Sessional

    Works flawlessly, thanks a lot.

    Fixed
    Code:java
    1. @EventHandler
    2. public void deletequartz(PlayerInteractEvent e) {
    3. Player player = e.getPlayer();
    4. if (player.getItemInHand().getType().equals(Material.QUARTZ)) {
    5. if(e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
    6. ItemStack[] items = player.getInventory().getContents();
    7.  
    8. for (ItemStack i : items)
    9. {
    10. if (i.getType() == Material.QUARTZ)
    11. {
    12. if (i.getAmount() > 1)
    13. {
    14. i.setAmount(i.getAmount() - 1);
    15. } else {
    16. e.getPlayer().getInventory().setItemInHand(new ItemStack(Material.AIR, 0));
    17. }
    18. break;
    19. }
    20. }
    21. }
    22. }
    23. }
    24.  


    Figured it out, posted code if anyone needs it :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
Thread Status:
Not open for further replies.

Share This Page