0

I am trying to teleport a user to their own realm using the following code:

@EventHandler
public static void onPortalTravel(PlayerPortalEvent event) throws Exception {
    if(event.getCause() == PlayerPortalEvent.TeleportCause.END_PORTAL) {
        int x = event.getPlayer().getLocation().getBlockX();
        int y = event.getPlayer().getLocation().getBlockY();
        int z = event.getPlayer().getLocation().getBlockZ();

        String[] data = getPageData("http://example.com/game.php?type=getRealm&location="+x+":"+y+":"+z ).split(":"); // THIS RETURNS <username>:<oldblockid>

        String realm = data[0];
        int oldID = Integer.parseInt(data[1].trim());

        Bukkit.getServer().getWorld("world").getBlockAt(x, y, z).setTypeId(oldID);
 *err*  event.getPlayer().teleport(new Location(Bukkit.getWorld("realms/" + realm), 1, 65, 16.5));
    }

}

and the error is:

Caused by: java.lang.NullPointerException
  at org.bukkit.craftbukkit.v1_6_R2.entity.CraftPlayer.teleport(CraftPlayer.java:395)
  at org.bukkit.craftbukkit.v1_6_R2_entity.CraftEntity.teleport(CraftEntity.java:199)
  at com.mysite.plugin.Start.onPortalTravel(Start.java:202)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  at java.lang.reflect.Method.invoke(Unknown Source)
  at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
     ... 26 more
MRVDOG
  • 1,717
  • 2
  • 13
  • 20
  • 2
    Do you have the Bukkit source to see what's going on on line 395 of CraftPlayer? – Jon Skeet Aug 13 '13 at 14:47
  • yea I do, I will take a look – MRVDOG Aug 13 '13 at 14:57
  • the line at CraftPlayer.java:395 = WorldServer toWorld = ((CraftWorld) to.getWorld()).getHandle(); *** the line at CraftEntity.java:199 = return teleport(location, TeleportCause.PLUGIN); – MRVDOG Aug 13 '13 at 15:01
  • Have you checked that `Bukkit.getWorld` in your own code hasn't returned null? – Jon Skeet Aug 13 '13 at 15:53
  • yep it's returning null :/ – MRVDOG Aug 13 '13 at 16:07
  • Okay, so that's what you need to focus on. Check whether `realm` is actually what you expect, for example. – Jon Skeet Aug 13 '13 at 16:11
  • System.out.println(Bukkit.getWorld("realms/" + realm) + ", " + realm); returns "null, MRVDOG", so realm is returning what it's supposed to, and world "realms/MRVDOG" exists, so I don't know what else to do – MRVDOG Aug 13 '13 at 16:19
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/35372/discussion-between-mrvdog-and-jon-skeet) – MRVDOG Aug 13 '13 at 16:29
  • 1
    The dudes here won't know as much as the people at http://forums.bukkit.org/forums/plugin-development.5/ Try asking there. – Tubular May 21 '14 at 03:01

1 Answers1

3

It seems like this world is not loaded yet. You have to load it first.

This snippets checks if the world is null. If it is null, it loads(if the world already exists) or creates(if the world doesn't exist yet) the world.

@EventHandler
public static void onPortalTravel(PlayerPortalEvent event) throws Exception {
    if (event.getCause() == PlayerPortalEvent.TeleportCause.END_PORTAL) {
        int x = event.getPlayer().getLocation().getBlockX();
        int y = event.getPlayer().getLocation().getBlockY();
        int z = event.getPlayer().getLocation().getBlockZ();

        String[] data = getPageData("http://example.com/game.php?type=getRealm&location="+x+":"+y+":"+z).split(":"); // THIS RETURNS <username>:<oldblockid>
        String realm = data[0];
        int oldID = Integer.parseInt(data[1].trim());

        Bukkit.getServer().getWorld("world").getBlockAt(x, y, z).setTypeId(oldID);
        World world = Bukkit.getWorld("realms/" + realm);
        if(world == null){
            //Loads a world with the name given in the constructor
            WorldCreator wc = new WorldCreator("realms/" + realm);
            world = Bukkit.createWorld(wc);

        }
        event.getPlayer().teleport(new Location(world, 1, 65,16.5));
    }

}
Miromed
  • 105
  • 10