Solved Teleporting a player to a custom world

Discussion in 'Plugin Development' started by tangochicken, Jul 26, 2013.

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

    tangochicken

    Hello once again!

    I recently started the development of a small mini-game, but before I got into the proper development of the plugin, I decided to test out ways of achieving my various goals. Everything that I have tested so far, has worked almost flawlessly but when it came to teleporting a player to a custom lobby world, I hit a pretty big wall.When I run this code:
    Code:java
    1. ((Player) sender).teleport(LobbySpawn);
    2. //LobbySpawn is a location defined above OnEnable

    It returns a null pointer exception with the error
    Show Spoiler


    org.bukkit.command.CommandException: Unhandled exception executing command 'isot
    ope' in plugin Isotope MiniGame v0.1
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    9)
    at org.bukkit.craftbukkit.v1_6_R2.CraftServer.dispatchCommand(CraftServe
    r.java:523)
    at net.minecraft.server.v1_6_R2.PlayerConnection.handleCommand(PlayerCon
    nection.java:962)
    at net.minecraft.server.v1_6_R2.PlayerConnection.chat(PlayerConnection.j
    ava:880)
    at net.minecraft.server.v1_6_R2.PlayerConnection.a(PlayerConnection.java
    :837)
    at net.minecraft.server.v1_6_R2.Packet3Chat.handle(SourceFile:49)
    at net.minecraft.server.v1_6_R2.NetworkManager.b(NetworkManager.java:296
    )
    at net.minecraft.server.v1_6_R2.PlayerConnection.e(PlayerConnection.java
    :116)
    at net.minecraft.server.v1_6_R2.ServerConnection.b(SourceFile:37)
    at net.minecraft.server.v1_6_R2.DedicatedServerConnection.b(SourceFile:3
    0)
    at net.minecraft.server.v1_6_R2.MinecraftServer.t(MinecraftServer.java:5
    90)
    at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:2
    26)
    at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:4
    86)
    at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java
    :419)
    at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:5
    82)
    Caused by: java.lang.NullPointerException
    at org.bukkit.craftbukkit.v1_6_R2.entity.CraftPlayer.teleport(CraftPlaye
    r.java:388)
    at org.bukkit.craftbukkit.v1_6_R2.entity.CraftEntity.teleport(CraftEntit
    y.java:198)
    at epvp.tangochicken.isotope.Main.onCommand(Main.java:81)
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    ... 15 more


    Main.java being my base class and line 81 being the above snippet.

    I have checked if the world "Isotope" is null. If it isn't, I set the world Isotope to be
    Code:java
    1. Isotope = Bukkit.getServer().getWorld("Isotope");

    and if it is null to use
    Code:java
    1. Bukkit.getServer().createWorld(new WorldCreator("Isotope"));
    2. Isotope = Bukkit.getServer().getWorld("Isotope");


    Any help here would be greatly appreciated and I am willing to give more information if it is required. Thanks in advance!
     
  2. Offline

    xTrollxDudex

    tangochicken
    You can't get fields from another method

    At the top of the class you would declare that location null, and give it a value in the on enable.
    PHP:
    public class Example extends JavaPlugin{
    Location loc null;

    @
    Override
    public void onEnable(){
    World world Bukkit.getWorld("Isotope");
    this.loc = new Location(world000);
    }

    //teleport command
    player.teleport(this.loc);
     
  3. Offline

    tangochicken

  4. Offline

    Alex5657

    If its in a method, its not a field. Its a local variable
    If you have something like this:
    Code:
    public void onEnable(){
       Location location = new Location(someworld,10,10,10);
    }
    
    You can't access it from another method. You need to do this:
    Code:
    private Location loc;
    public void onEnable(){
      loc = new Location(someworld,10,10,10);
    }
    
     
  5. Offline

    Samthelord1

    Coding isn't as easy as some people think, you can't just teleport a player to a world. You have to get the new world and set a location in that new world.
     
  6. Offline

    tangochicken

    xTrollxDudex

    I changed my code according to what you said and it certainly helped progress! Although, now when I use the command which teleports, in the console it will show progress of loading the world but as soon as it does, there is a null pointer exception, what is causing it now?

    The error
    Show Spoiler

    20:30:21 [INFO] tangochicken issued server command: /isotope join
    20:30:21 [INFO] Preparing start region for level 3 (Seed: 4217387841049932187)
    20:30:22 [INFO] Preparing spawn area for Isotope, 20%
    20:30:23 [INFO] Preparing spawn area for Isotope, 52%
    20:30:24 [INFO] Preparing spawn area for Isotope, 89%
    20:30:24 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'isot
    ope' in plugin Isotope MiniGame v0.1
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    9)
    at org.bukkit.craftbukkit.v1_6_R2.CraftServer.dispatchCommand(CraftServe
    r.java:523)
    at net.minecraft.server.v1_6_R2.PlayerConnection.handleCommand(PlayerCon
    nection.java:962)
    at net.minecraft.server.v1_6_R2.PlayerConnection.chat(PlayerConnection.j
    ava:880)
    at net.minecraft.server.v1_6_R2.PlayerConnection.a(PlayerConnection.java
    :837)
    at net.minecraft.server.v1_6_R2.Packet3Chat.handle(SourceFile:49)
    at net.minecraft.server.v1_6_R2.NetworkManager.b(NetworkManager.java:296
    )
    at net.minecraft.server.v1_6_R2.PlayerConnection.e(PlayerConnection.java
    :116)
    at net.minecraft.server.v1_6_R2.ServerConnection.b(SourceFile:37)
    at net.minecraft.server.v1_6_R2.DedicatedServerConnection.b(SourceFile:3
    0)
    at net.minecraft.server.v1_6_R2.MinecraftServer.t(MinecraftServer.java:5
    90)
    at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:2
    26)
    at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:4
    86)
    at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java
    :419)
    at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:5
    82)
    Caused by: java.lang.NullPointerException
    at org.bukkit.craftbukkit.v1_6_R2.entity.CraftPlayer.teleport(CraftPlaye
    r.java:388)
    at org.bukkit.craftbukkit.v1_6_R2.entity.CraftEntity.teleport(CraftEntit
    y.java:198)
    at epvp.tangochicken.isotope.Main.onCommand(Main.java:82)
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    ... 15 more
    >
     
  7. Offline

    xTrollxDudex

    tangochicken
    Delay the teleport, the world isn't done loading yet.
     
  8. Offline

    Samthelord1

  9. Offline

    tangochicken

    I delayed the teleport (although from the console, it seems as if the world is loaded) but I get a NPE at
    Code:java
    1. ((Player) sender).teleport(LobbySpawn);

    And I have done checks to guarantee that the sender is a player, so that is not the cause of the NPE.
     
  10. Offline

    xTrollxDudex

  11. Offline

    tangochicken

    Even though it was not solved in this thread, I have found the answer and I am therefore marking this a solved.
    This is where I found the answer (Bukkit forums post).
     
Thread Status:
Not open for further replies.

Share This Page