New bukkit broke my teleport plugin :< But hell I don't know where :)

Discussion in 'Plugin Development' started by zajacmp3, Aug 6, 2012.

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

    zajacmp3

    Hello,

    I just runned a few tests. New dev build broke my teleport plugin.
    I get this error
    Code:
    09:10:33 [SEVERE] java.lang.NullPointerException
    09:10:33 [SEVERE]       at org.bukkit.craftbukkit.entity.CraftPlayer.teleport(CraftPlayer.java:355)
    09:10:33 [SEVERE]       at org.bukkit.craftbukkit.entity.CraftEntity.teleport(CraftEntity.java:162)
    09:10:33 [SEVERE]       at com.zajacmp3.survivalgame.SurvivalGame.preparePlayerPositions(SurvivalGame.java:168)
    09:10:33 [SEVERE]       at com.zajacmp3.survivalgame.SurvivalGame.startBattle(SurvivalGame.java:68)
    09:10:33 [SEVERE]       at com.zajacmp3.survivalgame.SurvivalGame$1.run(SurvivalGame.java:53)
    09:10:33 [SEVERE]       at org.bukkit.craftbukkit.scheduler.CraftWorker.run(CraftWorker.java:34)
    09:10:33 [SEVERE]       at java.lang.Thread.run(Thread.java:679)
    
    Where this is referring to this part of my code:
    Code:
        private void preparePlayerPositions(Player[] players) {
             World world = this.getServer().getWorld("Lobby");
             FileConfiguration config = this.getConfig();
             for(int temp = 0; temp<players.length;temp++){
                 Location location = players[temp].getLocation();
                 location.setWorld(world);
                 location.setX(config.getDouble("MapConfiguration.1mapPositions."+(temp+1)+".X"));
                 location.setY(config.getDouble("MapConfiguration.1mapPositions."+(temp+1)+".Y"));
                 location.setZ(config.getDouble("MapConfiguration.1mapPositions."+(temp+1)+".Z"));
                 players[temp].teleport(location);
             }
            
        }

    And every little thing would be just fine but I runned a test on a completely new plugin that I just wrote to test if just something with location is going wrong.

    Code:
    package com.zajacmp3.plugin;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerLoginEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Plugin extends JavaPlugin implements Listener{
        public void onDisable(){
            
            
        }
        public void onEnable(){
            this.getServer().getPluginManager().registerEvents(new Listener(){
                @EventHandler(priority = EventPriority.NORMAL)
                public void getLocationOnPlayerSpawn(final PlayerLoginEvent event){
                    Location location = event.getPlayer().getLocation();
                    Player player = event.getPlayer();
                    System.out.println("TEST");
                    scheduleRespawn(player, location);
                }
            }, this);
        }
        protected void scheduleRespawn(final Player player, final Location location) {
            System.out.println("TEST2");
    
            this.getServer().getScheduler().scheduleAsyncDelayedTask(this, new Runnable(){
    
                @Override
                public void run() {
                    System.out.println("TEST3");
                    
                    player.teleport(location);
                    System.out.println("TEST3");
                }
                
            }, 200);
        }
    
    }
    
    And frankly this is working.

    So main question. I need a fresh eyes what is wrong with my first code. I will tell you that it was working on 1.2.5 and I did not change a thing to it since then. There is a way now to make if work again cause the seconds code is working.
     
  2. CraftPlayer at line 355 is: WorldServer toWorld = ((CraftWorld)to.getWorld()).getHandle();

    So most likele the world you provided is null :) that may be because it doesn't exists or not loaded... check it against null before looping.
     
    zajacmp3 likes this.
  3. Offline

    zajacmp3

    You have got to be kidding me...

    I really don't know why and how but I made the check you told me. Nothing more, and code started working...

    Code:
        private void preparePlayerPositions(Player[] players) {
            World world = this.getServer().getWorld("Lobby");
            FileConfiguration config = this.getConfig();
            if(world!=null){
                for(int temp = 0; temp<players.length;temp++){
                    Location location = players[temp].getLocation();
                    location.setWorld(world);
                    location.setX(config.getDouble("MapConfiguration.1mapPositions."+(temp+1)+".X"));
                    location.setY(config.getDouble("MapConfiguration.1mapPositions."+(temp+1)+".Y"));
                    location.setZ(config.getDouble("MapConfiguration.1mapPositions."+(temp+1)+".Z"));
                    players[temp].teleport(location);
                }
            }
            else System.out.println("Test");
           
        }

    @EDIT:

    Okay, it is not working so awesome as I though.

    It is working with the world that I am currently on. But when I change the name of the world to get me a other that I am on world it is not working :)

    I thing I get it... My server is for reason I cannot explain load any world other than the one specified in server.properties.

    Any idea why?
    I checked it by listing a list of worlds.

    Code:
        private void preparePlayerPositions(Player[] players) {
            List<World> list = this.getServer().getWorlds();
            for(int temp = 0; temp<=list.size();temp++){
                list.get(temp).getName();
            }
    [...]
    and the error
    Code:
    09:58:44 [SEVERE] java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
    09:58:44 [SEVERE]       at java.util.ArrayList.rangeCheck(ArrayList.java:571)
    09:58:44 [SEVERE]       at java.util.ArrayList.get(ArrayList.java:349)
    09:58:44 [SEVERE]       at com.zajacmp3.survivalgame.SurvivalGame.preparePlayerPositions(SurvivalGame.java:162)
    09:58:44 [SEVERE]       at com.zajacmp3.survivalgame.SurvivalGame.startBattle(SurvivalGame.java:68)
    09:58:44 [SEVERE]       at com.zajacmp3.survivalgame.SurvivalGame$1.run(SurvivalGame.java:53)
    09:58:44 [SEVERE]       at org.bukkit.craftbukkit.scheduler.CraftWorker.run(CraftWorker.java:34)
    09:58:44 [SEVERE]       at java.lang.Thread.run(Thread.java:679)
    
    Where the most important part is Index: 1, Size: 1

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  4. Index is 1 less than the size because it starts from 0... so if size is 1, the max index is 0.
    Code:
    for(int i = 0; i < list.size(); i++)
    But you can just use:
    Code:
    System.out.print("loaded worlds:");
    for(World world : getServer().getWorlds())
    {
        System.out.print("  - " + world.getName());
    }
    Also, you only have one world loaded, so it doesn't load all but the one specified in server.properties, it only loads that one.
     
  5. Offline

    zajacmp3

    I checked from 0 and it did not give me any results. And I somehow remembered that Result Type is starting from 1 so I also checked from 1.

    But better question. Why does it not load other worlds? I now have there like 5 or 6 different worlds.
    As I recall on 1.2.5 it loaded it all so I could just teleport there.
     
  6. Result Type ? What's that got to do with array indexes ?
    In most programming languages, array indexes start from 0... and the same thing applies to List since it's an 'extension' of the primitive array.

    I dunno, you may've had a plugin that loads the worlds, because the server doesn't just load all worlds.
    You can trigger a manual world load tough... getServer().loadWorld() I belive, it's easy to find :)
     
Thread Status:
Not open for further replies.

Share This Page