Solved player.teleport() in a scheduleAsyncRepeatingTask gives me an nullpointer exception.

Discussion in 'Plugin Development' started by ninja2003, Jan 1, 2015.

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

    ninja2003

    I actually have 2 problems one is
    Code:
    public void timer()
        {
            final String world = Config.getConfig().getString("permareas.FallenBridge.sworld");
            final double x = Config.getConfig().getDouble("permareas.FallenBridge.sx");
            final double y = Config.getConfig().getDouble("permareas.FallenBridge.sy");
            final double z = Config.getConfig().getDouble("permareas.FallenBridge.sz");
            final int yaw = Config.getConfig().getInt("permareas.FallenBridge.syaw");
            final int pitch = Config.getConfig().getInt("permareas.FallenBridge.spitch");
            final Location location = new Location(Bukkit.getWorld(world), x, y, z);
            this.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable()
            {
                public void run()
                {
                    for(Player player : Bukkit.getOnlinePlayers())
                    {
                        if (player.getWorld().equals(location.getWorld()) && (Config.getConfig().getDouble("permareas.FallenBridge.x1") > (player.getLocation().getX())) || (player.getLocation().getX() > (Config.getConfig().getDouble("permareas.FallenBridge.x2"))) && ((Config.getConfig().getDouble("permareas.FallenBridge.y1") > player.getLocation().getY()) || (player.getLocation().getY() > Config.getConfig().getDouble("permareas.FallenBridge.y2"))) && (Config.getConfig().getDouble("permareas.FallenBridge.z1") > player.getLocation().getZ()) || (player.getLocation().getZ() > Config.getConfig().getDouble("permareas.FallenBridge.z2"))) {
                            player.getLocation().setYaw(yaw);
                            player.getLocation().setPitch(pitch);
                            player.teleport(location); <--- line 47
                        }
                        }
                    }
            }
            , 20, 20);
    }
    the if(player.getWorld().equals(location.getWorld()) thing doesn't work cause it just skips it. The second problem is player.teleport(location); is the crash maker and i've already checked if the world, x, y and z is null and they aren't, So perhaps the scheduleAsyncRepeatingTask is causing this problem
    Code:
    [17:21:57] [Craft Scheduler Thread - 11/ERROR]: Could not pass event PlayerTeleportEvent to WorldGuard v5.9
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[craftbukkit.jar:git-Spigot-1642]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.jar:git-Spigot-1642]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:514) [craftbukkit.jar:git-Spigot-1642]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:499) [craftbukkit.jar:git-Spigot-1642]
        at org.bukkit.craftbukkit.v1_7_R4.entity.CraftPlayer.teleport(CraftPlayer.java:484) [craftbukkit.jar:git-Spigot-1642]
        at org.bukkit.craftbukkit.v1_7_R4.entity.CraftEntity.teleport(CraftEntity.java:200) [craftbukkit.jar:git-Spigot-1642]
        at me.ninja.seetzcraft.Main$1.run(Main.java:47) [SeetzCraft.jar:?]
        at org.bukkit.craftbukkit.v1_7_R4.scheduler.CraftTask.run(CraftTask.java:71) [craftbukkit.jar:git-Spigot-1642]
        at org.bukkit.craftbukkit.v1_7_R4.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:53) [craftbukkit.jar:git-Spigot-1642]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:1.7.0_65]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:1.7.0_65]
        at java.lang.Thread.run(Unknown Source) [?:1.7.0_65]
    Caused by: java.lang.NullPointerException
        at com.sk89q.worldguard.protection.GlobalRegionManager.hasBypass(GlobalRegionManager.java:269) ~[?:?]
        at com.sk89q.worldguard.bukkit.WorldGuardPlayerListener.checkMove(WorldGuardPlayerListener.java:139) ~[?:?]
        at com.sk89q.worldguard.bukkit.WorldGuardPlayerListener.onPlayerTeleport(WorldGuardPlayerListener.java:1411) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_65]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_65]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_65]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_65]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:298) ~[craftbukkit.jar:git-Spigot-1642]
    Also Happy New Year!
     
    Last edited: Jan 2, 2015
  2. @ninja2003 You're trying to access the BukkitAPI in an asynchronous thread (don't do that!)
     
    moo3oo3oo3 likes this.
  3. Offline

    ninja2003

    But is there any way to have some other kind of 1 second loop that you can use player.teleport in?
     
    Last edited: Jan 1, 2015
  4. Offline

    SuchSwegMuchWow

    @ninja2003

    Are you trying to just teleport the player once or every second or two?
     
  5. Offline

    xTrollxDudex

    moo3oo3oo3 likes this.
  6. Offline

    567legodude

    @ninja2003 Dont schedule the task Asynchronously.
    change
    scheduleAsyncRepeatingTask
    to
    scheduleSyncRepeatingTask
     
  7. Offline

    ninja2003

    It still gives me the same error.

    Yes with the if statement i had in my code

    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than double posting.>
     
    Last edited by a moderator: Jan 3, 2015
  8. @ninja2003
    final Location location = new Location(Bukkit.getWorld(world), x, y, z);
    isn`t best
    final Location location = new Location(Bukkit.getWorld(world), x, y, z, yaw, pitch);
    ?

    Be sure the mentioned config exists
     
  9. Offline

    ninja2003

    I checked if the config doubles, string and the ints were null and they weren't. Now this is my current code perhaps someone can find the problem please?
    Code:
    public void timer()
        {
            final String world = Config.getConfig().getString("permareas.FallenBridge.sworld");
            final double x = Config.getConfig().getDouble("permareas.FallenBridge.sx");
            final double y = Config.getConfig().getDouble("permareas.FallenBridge.sy");
            final double z = Config.getConfig().getDouble("permareas.FallenBridge.sz");
            final int yaw = Config.getConfig().getInt("permareas.FallenBridge.syaw");
            final int pitch = Config.getConfig().getInt("permareas.FallenBridge.spitch");
            final Location location = new Location(Bukkit.getWorld(world), x, y, z, yaw, pitch);
            this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable()
            {
                public void run()
                {
                    for(Player player : Bukkit.getOnlinePlayers())
                    {
                        if (player.getWorld().equals(location.getWorld()) && (Config.getConfig().getDouble("permareas.FallenBridge.x1") > (player.getLocation().getX())) || (player.getLocation().getX() > (Config.getConfig().getDouble("permareas.FallenBridge.x2"))) && ((Config.getConfig().getDouble("permareas.FallenBridge.y1") > player.getLocation().getY()) || (player.getLocation().getY() > Config.getConfig().getDouble("permareas.FallenBridge.y2"))) && (Config.getConfig().getDouble("permareas.FallenBridge.z1") > player.getLocation().getZ()) || (player.getLocation().getZ() > Config.getConfig().getDouble("permareas.FallenBridge.z2"))) {
                            player.teleport(location);
                        }
                        }
                    }
            }
            , 20, 20);
        }
    
    Could i use the playermove event to do the same thing or does it update too fast so the server will crash?

    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than double posting.>
     
    Last edited by a moderator: Jan 3, 2015
  10. Offline

    mythbusterma

    @ninja2003

    Judging by the error you're causing, I would say that it's because you're passing a Location with a null "world" member. Make sure the world isn't null before passing it to Player#teleport()
     
  11. Offline

    ninja2003

    It's fully working when i do System.out.println(world); i get the expected string that i have in the config.yml
     
  12. @ninja2003
    Case sensitive?
    U`re trying to teleport a player to a place when he enters to an area or to forbid entering an area?
     
  13. Offline

    ninja2003

    i'm trying to just teleport the player as soon as he enters the area.
     
  14. Offline

    ninja2003

    I got it solved but i can't figure out how to make the onPlayerMove not update when you look around. it would make the server alot faster.
     
    Last edited: Jan 3, 2015
  15. Offline

    Ivan

    compare the vectors of the from and to location. I think it was something along the lines of getFrom().toVector() == getTo().toVector()
     
Thread Status:
Not open for further replies.

Share This Page