Using getConfig to teleport a player

Discussion in 'Plugin Development' started by MineCrypt, Nov 24, 2012.

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

    MineCrypt

    I am having troubles teleporting a player using a getConfig so they can right now the world and coordinates, right now I am focusing on the world so my code is:
    Code:
        String CTFWorld = plugin.getConfig().getString("CTFWorld");
       
        World world = plugin.getServer().getWorld(CTFWorld);
        Location loc = new Location(world, 668, 4, 113);
    and my config.yml is

    # Default Config
    CTFWorld: TEST

    Any help would be appreciated.

    ~MineCrypt

    Hate to bump, but I need an answer

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  2. Offline

    Sagacious_Zed Bukkit Docs

    You should state the nature of your problem.
     
  3. Offline

    Whats for Today

    Just going to say I finished typing this out completely and my computer crashed >.> moving on...
    Im assuming this is for teleporting to a defined "warp". If this is the case I can help; first of all we are going to need to set the location, this is possible through command or config. (In this case I use a command.)
    Code:java
    1. if(commandLabel.equalsIgnoreCase("setwarp")){
    2. Player player = (Player) sender;
    3. String warp = args[0];
    4. getConfig().set(warp + ".x", player.getLocation().getBlockX());
    5. getConfig().set(warp + ".y", player.getLocation().getBlockY());
    6. getConfig().set(warp + ".z", player.getLocation().getBlockZ());
    7. getConfig().set(warp + ".yaw", player.getLocation().getYaw());
    8. getConfig().set(warp + ".pitch", player.getLocation().getPitch());
    9. saveConfig();
    10. player.sendMessage("The warp" + ChatColor.AQUA + warp + ChatColor.WHITE);//not needed but nice to make sure everything worked!

    Then you just have to get the "warp's" location information and teleport the player(s):
    Code:java
    1. int warpX = getConfig().getInt("WarpName" + ".x"), warpY = getConfig().getInt("WarpName" + ".y"), warpZ= getConfig().getInt("WarpName" + ".z"), warpYaw=getConfig().getInt("WarpName" + ".yaw"), warpPitch=getConfig().getInt("WarpName" + ".pitch");//replace WarpName with whatever you used
    2. .teleport(new Location(.getWorld(), warpX, warpY, warpZ, warpYaw, warpPitch));

    I have not tested this but hopefully it should work, if it doesn't just tell me. If you are confused about something, ask! :)
     
    Bavestry likes this.
  4. Offline

    MineCrypt

    Thanks alot I am testing that now I will tell you how it goes

    Thanks that part works, but the problem is that I want them to get teleported to another world which they can write the world name in the config, is that part possible?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  5. Offline

    boardinggamer

    You need to make sure the world is loaded in the server. if the world is loaded use this.
    Code:java
    1. World world = getServer().getWorld("worldname");
    2. Location loc = world.getSpawnLocation(); //or get use getBlockAt(x,y,z).getLocation();
    3. player.teleport(loc);
     
  6. Offline

    MineCrypt

    Okay so the last part of that code is allowing the config to change the .getWorld("worldname"); to the name of the world
     
  7. Offline

    boardinggamer

    you would replace "worldname" with getConfig().getString(warp + ".world");

    if the command if /warp [warpname]
    "warp" would need to be args[0] of the command.

    and just make the code given to already this (added the world part)
    Code:java
    1. if(commandLabel.equalsIgnoreCase("setwarp")){
    2. Player player = (Player) sender;
    3. String warp = args[0];
    4. String worldname = player.getWorld().getName();
    5. getConfig().set(warp + ".world", worldname);
    6. getConfig().set(warp + ".x", player.getLocation().getBlockX());
    7. getConfig().set(warp + ".y", player.getLocation().getBlockY());
    8. getConfig().set(warp + ".z", player.getLocation().getBlockZ());
    9. getConfig().set(warp + ".yaw", player.getLocation().getYaw());
    10. getConfig().set(warp + ".pitch", player.getLocation().getPitch());
    11. saveConfig();
    12. player.sendMessage("The warp" + ChatColor.AQUA + warp + ChatColor.WHITE);//not needed but nice to make sure everything worked!

    this will make the player spawn to the location in the world selected
     
  8. Offline

    MineCrypt

    Okay i dont know if that will work or not but here is essentially what i need to work:
    Code:
        World world = Bukkit.getWorld(plugin.getConfig().getString("Test"));
        Location loc = new Location(world, 668, 4, 113);
    and the config would be
    Test: worldname

    But that does not work for some reason I get a error with NullPointException

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  9. Offline

    boardinggamer

    make sure the world exists. the world has to be loaded in the server
     
  10. Offline

    MineCrypt

    It is loaded
     
  11. Offline

    Tirelessly

    and my config.yml is

    # Default Config
    CTFWorld: TEST

    Try making it:
    CTFWorld: 'TEST'
     
  12. Offline

    MineCrypt

    Still not working... I really need this fixed so im going to upload the classes to pastebin and hopefully someone can help me.

    Main Class:
    http://pastebin.com/u3pdyZDc
    Command Executor Class:
    http://pastebin.com/feGxrDRm
    config.yml:
    http://pastebin.com/kf8hBN0V

    My world name is TEST and it is my only world so it is loaded. I need this world support for multiverse because who i am making it for has multiple worlds.
     
  13. Offline

    boardinggamer

    first off it is recommended to NOT use bukkit in your code which I see you have in a few places.
    second, do this instead of having the config.yml thing you have
    Code:
    getConfig().addDefault("CTFWorld", "TEST");
    getConfig().options().copyDefaults(true);
    saveConfig();
    and lastly use this for location
    Code:
    Location loc = this.plugin.getServer().getWorld(plugin.getConfig().getString("CTFWorld")).getBlockAt(668, 4, 113).getLocation();
     
  14. Offline

    MineCrypt

    That still doesnt work :/ Im still getting null pointer exception, Ugh why does this not work.
     
  15. Offline

    Sagacious_Zed Bukkit Docs

    Instead of chaining method calls together split them up and assign the intermediate steps to local variables. And figure out which part is returning null.
     
  16. Offline

    MineCrypt

    When doing tests earlier of why it would not work it was in :
    Code:
    World world = plugin.getServer().getWorld(CTFWorld);
    So something in the new code that is like this is the problem.
     
  17. Offline

    boardinggamer

    Chances are its because of where the code is located. try adding that code for world and location just above the code using it instead of above all the methods.
    also post the error you are getting.
     
  18. Offline

    MineCrypt

    Thats the error, everything I change i still get the same error:
    Code:
    16:36:10 [SEVERE] Error occurred while enabling CaptureTheFlag v0.1 (Is it up to
    date?)
    java.lang.NullPointerException
            at com.hotmail.MineCrypt.CTF.CTFArenaJoin.<init>(CTFArenaJoin.java:62)
            at com.hotmail.MineCrypt.CTF.CTFMain.onEnable(CTFMain.java:13)
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
    .java:374)
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
    r.java:381)
            at org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftServer.java:270)
            at org.bukkit.craftbukkit.CraftServer.enablePlugins(CraftServer.java:252
    )
            at net.minecraft.server.MinecraftServer.j(MinecraftServer.java:320)
            at net.minecraft.server.MinecraftServer.e(MinecraftServer.java:299)
            at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:258)
            at net.minecraft.server.DedicatedServer.init(DedicatedServer.java:147)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:398)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:856)
    Anyone know why that is caused?

    Bump, hopefully someone can fix this

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  19. Offline

    ZeusAllMighty11 Retired Staff

    Something on line 62 is null
     
  20. Offline

    MineCrypt

    So it is this line:
    World world = this.plugin.getServer().getWorld(plugin.getConfig().getString("CTFWorld"));
    Now does anyone know how to correct that and fix it so it works?

    The broken part of it is: getWorld(plugin.getConfig().getString("CTFWorld"))

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  21. Offline

    the_merciless

    You have a world on your server called "TEST" ?
     
  22. Offline

    MineCrypt

  23. Offline

    streetskater

    Just a random guess cause im kinda noobish but maybe this:
    World world = Bukkit.getWorld(getConfig().getString("CTFWorld"));
     
  24. Offline

    MineCrypt

    Nope :/
     
  25. Offline

    the_merciless

    Your error is on line 62.

    If (team2.contains(player.getName())){
     
  26. Offline

    MineCrypt

    That line is fine, it worked before and it works now. I know the problem is
    Code:
    getWorld(plugin.getConfig().getString("CTFWorld"))
    So I need a person to rephrase that so it does work.
     
  27. Offline

    MineCrypt

    Bump. I guess.
     
  28. Offline

    chasechocolate

    Try checking if the world = null
     
  29. Offline

    Barinade

    Check what the config has for CTFWorld and make sure that's the actual name of the world
     
  30. Offline

    MineCrypt

    World is not null, and that is the actual name of the world
     
Thread Status:
Not open for further replies.

Share This Page