Solved Exception, hard to solve

Discussion in 'Plugin Development' started by sebcio98, Aug 10, 2014.

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

    sebcio98

    Hello.
    I have a probem with my plugin. Everything seems to be fine, but the server is throwing an exception.
    Code:java
    1. public class PlayerInteractListener implements Listener
    2. {
    3. @EventHandler
    4. public void onPlayerInteract(PlayerInteractEvent e)
    5. {
    6. Player p = e.getPlayer();
    7. String nick = p.getName();
    8.  
    9. if (GameDB.players.contains(nick))
    10. {
    11. else if (GameDB.getArenaMode() == GameDB.Modes.INGAME)
    12. {
    13. if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK)
    14. {
    15. if (e.getItem().getType() != null)
    16. {
    17. if (e.getItem().getType() == Material.BLAZE_POWDER)
    18. {
    19. int i;
    20. ItemStack item = p.getItemInHand();
    21.  
    22. for (i = 0; GameDB.BlazePowderShedID.contains(i); i++);
    23.  
    24. GameDB.BlazePowderShedID.add(i);
    25. GameDB.BlazePowderShedTime.add(0);
    26. GameDB.BlazePowderShedLoc.add(p.getLocation());
    27. TheHerobrine.TaskIdBlazePowderShedule.add(Bukkit.getScheduler().scheduleSyncRepeatingTask(TheHerobrine.getPlugin, new BlazePowderShedule(i), 0, 5L));
    28.  
    29. item.setAmount(item.getAmount() - 1);
    30. p.setItemInHand(item);
    31.  
    32. e.setCancelled(true);
    33. }
    34.  
    35. if (e.isCancelled())
    36. {
    37. e.setUseInteractedBlock(Result.DENY);
    38. e.setUseItemInHand(Result.DENY);
    39. }
    40. }
    41. }
    42. }
    43. }
    44. }
    45. }

    Code:java
    1. public class BlazePowderShedule implements Runnable
    2. {
    3. private static int signalID;
    4. private static int signalIndex;
    5.  
    6. public BlazePowderShedule(int id)
    7. {
    8. signalID = id;
    9. signalIndex = GameDB.BlazePowderShedID.indexOf(signalID);
    10. }
    11.  
    12. @Override
    13. public void run()
    14. {
    15. signalIndex = GameDB.BlazePowderShedID.indexOf(signalID);
    16. int ShedTime = GameDB.BlazePowderShedTime.get(signalIndex) + 1;
    17.  
    18. GameDB.BlazePowderShedTime.set(signalIndex, ShedTime);
    19.  
    20. GameDB.BlazePowderShedLoc.get(signalIndex).getWorld().playEffect(GameDB.BlazePowderShedLoc.get(signalIndex), Effect.ENDER_SIGNAL, 0);
    21. GameDB.BlazePowderShedLoc.get(signalIndex).getWorld().playEffect(GameDB.BlazePowderShedLoc.get(signalIndex), Effect.ENDER_SIGNAL, 0);
    22.  
    23. for (String nick : GameDB.players)
    24. {
    25. if (GameDB.hb != nick)
    26. {
    27. Player p = Bukkit.getPlayer(nick);
    28.  
    29. if (GameDB.BlazePowderShedLoc.get(signalIndex).distance(p.getLocation()) < 2)
    30. {
    31. p.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 1, 0));
    32. }
    33. }
    34. }
    35.  
    36. if (GameDB.BlazePowderShedTime.get(signalIndex) == 40)
    37. {
    38. GameDB.BlazePowderShedTime.remove(signalIndex);
    39. GameDB.BlazePowderShedLoc.remove(signalIndex);
    40. Bukkit.getScheduler().cancelTask(TheHerobrine.TaskIdBlazePowderShedule.get(signalIndex));
    41. TheHerobrine.TaskIdBlazePowderShedule.remove(signalIndex);
    42. }
    43. }
    44. }

    The error is java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
    at pl.com.sebus.mc.shedule.BlazePowderShedule.run(BlazePowderShedule.java:27) ~[?:?]
    Line 27: int ShedTime = GameDB.BlazePowderShedTime.get(signalIndex) + 1;

    This problem happens when I use Blaze Powder second time, after waiting 10 or more seconds. (the shedule task is ran for 10 seconds)
    When I'm using blaze Powder 1st tie, it works fine (except the Regeneration effect), but for the second time it starts spamming in console the error 4 times per second and has no effect at all.

    I have everything initiated properly in TheHerobrine (main) and GameDB.
    The BlazePowderShed.. variables aren't used anywhere else.

    If you need any more information, just ask.
    (And sorry, if the answer is obvious, but this is really a problem for me)

    Any help would be appreciated.
     
  2. Offline

    iBecameALoaf

    remove the + 1 on line 27. In java arrays, everything starts at 0, not 1.
     
  3. Offline

    sebcio98

    I know that. This line is supposed to change the value to one higher. The index of the array (which goes from 0) is the signalIndex variable.
     
  4. Offline

    Gamecube762

    sebcio98 The array only has 1 object, which is 0. If you try to access an object that the array doesn't have, it will throw that error.
    a[0] // object 1 - your array has
    a[1] // object 2 - your array doesn't have and is trying to access.
    a[2] // object 3 - your array doesn't have
    a[3] // object 4 - your array doesn't have
     
  5. Offline

    sebcio98

    Gamecube762 Yeah, I'm not dumb, I know that. Just need to fix this. I want to increase one List's element with index signalIndex by 1. signalIndex is supposed to be 0 at this point.
     
  6. Offline

    sebcio98

    I fixed it. Just forgot to put "GameDB.BlazePowderShedID.remove(signalIndex);" in BlazePowderShedule (in line 38 of the code I pasted above). Such a simple thing and so much trouble to fix this.

    Thanks for good intentions!

    Thread closed.
     
Thread Status:
Not open for further replies.

Share This Page