Solved (Debugging): ArrayIndexOutOfBoundsException when teleporting

Discussion in 'Plugin Development' started by Jusi, Jan 9, 2013.

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

    Jusi

    As the title says, I sometimes (about oneout of five times)am getting an ArrayIndexOutOfBoundsException when attempting to teleport. Well first I do not have any idea what might bethe reason for this error occuring nor do I have a clue why anArrayIndexOutOfBoundsException could be happening here.


    First, here's the stack trace:

    2013-01-09 22:07:25 [SEVERE] Exception in thread "Thread-11"
    2013-01-09 22:07:25 [SEVERE] java.lang.ArrayIndexOutOfBoundsException: 286
    2013-01-09 22:07:25 [SEVERE] at java.util.LinkedList.toArray(Unknown Source)
    2013-01-09 22:07:25 [SEVERE] at java.util.ArrayList.<init>(Unknown Source)
    2013-01-09 22:07:25 [SEVERE] at net.minecraft.server.v1_4_6.PlayerChunkMap.b(PlayerChunkMap.java:122)
    2013-01-09 22:07:25 [SEVERE] at net.minecraft.server.v1_4_6.PlayerChunkMap.addPlayer(PlayerChunkMap.java:118)
    2013-01-09 22:07:25 [SEVERE] at net.minecraft.server.v1_4_6.PlayerList.moveToWorld(PlayerList.java:396)
    2013-01-09 22:07:25 [SEVERE] at org.bukkit.craftbukkit.v1_4_6.entity.CraftPlayer.teleport(CraftPlayer.java:392)
    2013-01-09 22:07:25 [SEVERE] at org.bukkit.craftbukkit.v1_4_6.entity.CraftEntity.teleport(CraftEntity.java:189)
    2013-01-09 22:07:25 [SEVERE] at de.JusiMinecraft.BurnTheFlagPVP.TimingThread.run(TimingThread.java:198)
    2013-01-09 22:07:25 [SEVERE] at java.lang.Thread.run(Unknown Source)


    And here are the important code lines:

    Code:
    //***************************************Teleport teams*****************************************************
                for(Player p : Bukkit.getOnlinePlayers())
                {
                if(Main.Teams.get(p.getName()) == "A")
                {
                    XPos = (Math.random() * 14 + 47);
                    ZPos = (Math.random() * 14 + 95);
                    Location l = new Location(plugin.getServer().getWorld(Main.NetherName), XPos, 85, ZPos);
                    p.teleport(l);
                    p.setBedSpawnLocation (l);
                }
                else
                {
                    r = (byte) (Math.random() * 2);
                    if(r == 0)
                    {
                        XPos = (Math.random() * 10 + 358);
                        Location l = new Location(plugin.getServer().getWorld(Main.WorldName), XPos, 63, 539.5);
                        p.teleport(l);                           //the error line
                        p.setBedSpawnLocation (l);
                    }
                    else
                    {
                        XPos = (Math.random() * 4 + 359.5);
                        Location l = new Location(plugin.getServer().getWorld(Main.WorldName), XPos, 64, 538.5);
                        p.teleport(l);
                        p.setBedSpawnLocation (l);
                    }
                }
                }
    Note that I have commented the error line!
     
  2. Offline

    CubixCoders

    Whats line 198?
     
  3. Offline

    evilmidget38

    Jusi First off, unrelated to your issue, you're comparing Strings with "==". you need to use .equals instead to compare the value of the string objects, rather than the objects. Additionally, it appears that you're doing this from another thread, rather than using Bukkit's scheduler. This is a very bad thing to do, and can crash the server, and generally break things.

    As for your issue, can you show us more code? The more we can see(preferably the entire class), the easier it is for us to help you.
     
  4. Offline

    Jusi

    Thanks for the response, I'm not a Java stylist or professional, I will try touse equals from now on, thanks for the advice!
    Posting the whole300 lines of the class wouldn't be practical.
    The only other things important for these lines might be the definitions:

    Code:
    int i = 0;
    byte r;
    double XPos;
    double ZPos;
    and

    Code:
    public static HashMap<String, String> Teams = new HashMap<String, String>(); 
    (out of the Main class)

    As you noticed I am using a seperate thread for this code. I do not know how to use bukkit scheduler so a seperate thread seemed to be the most efficient way of doing it.
     
  5. Offline

    fireblast709

    Jusi Don't use another thread when working directly with the API
     
  6. Offline

    Jusi

    Yea, alright, I understand the issue. Seems like I have to resetup thiswith the bukkit scheduler.
    A few questions:
    - How do I generally set up and start scheduled tasks? (commands, constructors etc.)
    - Is it possible to start synchronous tasks out of asynchronous tasks?(so that I can get a sense how to set up everything)
     
  7. Offline

    chasechocolate

    Use BukkitRunnable
     
  8. Offline

    fireblast709

    1. Either use the BukkitSchedulers methods (Bukkit.getScheduler().someMethod(stuff)) or use a BukkitRunnable.
    2. you can safely call sync tasks from within async tasks, as all scheduler methods are thread-safe
    Code:java
    1. new BukkitRunnable()
    2. {
    3. public void run()
    4. {
    5. // Optionally:
    6. if(shouldCancel)
    7. {
    8. cancel();
    9. }
    10. // Otherwise do stuff
    11. }
    12. }.runTask*(stuff)
    For the BukkitRunnable methods, check the javadocs
     
  9. Offline

    Jusi

    Yay thanks, very informative!
    I'll close this one now.
     
Thread Status:
Not open for further replies.

Share This Page