IndexOutOfBoundsException problem

Discussion in 'Plugin Development' started by bennie3211, Dec 25, 2014.

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

    bennie3211

    Hey guys, I've a problem with my plugin that I'm trying to finish. I'm using player metadata to store a Long value that I need later on. Why? I'm trying to detect if a player is pressing the shift twice. This all works but after a couple of minutes (I dunno how long precisely) it gives me an IndexOutOfBoundsException. And yes I know what that means but I can't figure out why it is happening :S

    The error:
    http://pastebin.com/YC92gvk8

    The code:
    Code:
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onSneakToggle(PlayerToggleSneakEvent event) {
           
            Player pl = event.getPlayer();
           
            if (!event.isSneaking())
                return;
           
            if (!pl.isOnGround())
                return;
           
            if (!jumperHandler.hasJumper(pl))
                return;
           
            if (pl.isFlying() || pl.getGameMode() == GameMode.CREATIVE)
                return;
           
            if (!pl.hasMetadata(jumperTime)) {
                pl.setMetadata(jumperTime, new FixedMetadataValue(plugin, System.currentTimeMillis()));
                return;
            } else {
                           
                if (getTimeDifference(pl) < 220) {
                   
                    Bukkit.getServer().getScheduler().scheduleAsyncDelayedTask(plugin,
                        new Runnable() {
                            @Override
                            public void run() {
                                if (pl != null)
                                    jumperHandler.jump(pl);
                            }
                    }, 2L);
                }
                pl.setMetadata(jumperTime, new FixedMetadataValue(plugin, System.currentTimeMillis()));
            }
        }
       
        public Long getTime(Player pl) {
            if (!pl.hasMetadata(jumperTime))
                return 0L;
            return pl.getMetadata(jumperTime).get(0).asLong();
        }
       
        public Long getTimeDifference(Player pl) {
            if (!pl.hasMetadata(jumperTime)) {
                return 0L;
            }
            return (System.currentTimeMillis() - getTime(pl));
        }
    The line "@SuppressWarnings("deprecation")" is line no 81.

    Can someone tell me why this is happening? Thnx :)
     
  2. Offline

    mythbusterma

    @bennie3211

    Why do you think modifying the Player's metadata asynchronously is okay? Why in the world would anyone think that's okay? Don't do that.

    It's also quite simple, you're trying to access a member of a List that doesn't exist.
     
    bennie3211 likes this.
Thread Status:
Not open for further replies.

Share This Page