[SOLVED] Convert string to location then tp the player to the location

Discussion in 'Plugin Development' started by davejavu, Apr 3, 2012.

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

    davejavu

    I'm outputting locations into a HashMap - Im outputting the world name, and x, y, z.
    How would I convert the string back to a location?
    Note, I've put a "," between each thing, so it saves the locations as: "world,0,0,0" or whatever.
     
  2. Offline

    comp8nerd4u2

    See String.Split(",");
     
  3. Offline

    Double0negative

    Code:
      String [] arg = str.split(",");
        Double [] parsed = new Double[3];
            for(int a = 0;a<2;a++){
                  parsed[a] = Double.parseDouble(arg[a+1]);
            }
     
      Location location =  new Location  (arg[0],parsed[1],parsed[2],parsed[3]);
     
    
     
  4. Offline

    Njol

    Fixed some errors:
    Code:java
    1. String[] arg = str.split(",");
    2. double[] parsed = new double[3];
    3. for (int a = 0; a < 3; a++) {
    4. parsed[a] = Double.parseDouble(arg[a+1]);
    5. }
    6.  
    7. Location location = new Location (Bukkit.getWorld(arg[0]), parsed[0], parsed[1], parsed[2]);

    Also you might want to check if the string is correctly formatted and if the world still exists, but that's up to you.
     
  5. Offline

    davejavu

    Njol it gives me formatting errors on the .split line
     
  6. Offline

    Njol

    Replace str with whatever contains the location string, e.g. config.getString("some.node").
     
  7. Offline

    davejavu

    It's loading the string from a hashmap.
    I made it into a method btw

    Njol
    Code (open)
    Code:java
    1. Location parseLoc(String str){
    2. String[] arg = str.split(",");
    3. double[] parsed = new double[3];
    4. for (int a = 0; a < 3; a++) {
    5. parsed[a] = Double.parseDouble(arg[a+1]);
    6. }
    7.  
    8. Location location = new Location (plugin.getServer().getWorld(arg[0]), parsed[0], parsed[1], parsed[2]);
    9. return new Location (plugin.getServer().getWorld(arg[0]), parsed[0], parsed[1], parsed[2]);
    10.  
    11. }
    [/code]


    Error (open)
    Code:
    2012-04-04 10:45:10 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'home' in plugin RandomStuff v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:166)
        at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:473)
        at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:821)
        at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:781)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:764)
        at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:33)
        at net.minecraft.server.NetworkManager.b(NetworkManager.java:229)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:113)
        at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:78)
        at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:554)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:452)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:490)
    Caused by: java.lang.NullPointerException
        at me.davejavu.RandomStuff.RandomStuffCommandExecutor.parseLoc(RandomStuffCommandExecutor.java:2603)
        at me.davejavu.RandomStuff.RandomStuffCommandExecutor.onCommand(RandomStuffCommandExecutor.java:2343)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
        ... 12 more


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

    spywhere

    I currently use this but my format didn't have a world. So, I just rewrite this code...
    Code:
    public Location str2loc(String str){
     
        String str2loc[]=str.split("\\:");
     
        Location loc = new Location(getServer().getWorld(str2loc[0]),0,0,0);
     
        loc.setX(Double.parseDouble(str2loc[1]));
     
        loc.setY(Double.parseDouble(str2loc[2]));
     
        loc.setZ(Double.parseDouble(str2loc[3]));
     
        return loc;
     
    }
     
     
     
    public String loc2str(Location loc){
     
        return loc.getWorld().getName()+":"+loc.getBlockX()+":"+loc.getBlockY()+":"+loc.getBlockZ();
     
    }
     
  9. Offline

    davejavu

    spywhere Okay, all that works, then when I go to teleport the player to the location, I get something returning "null"

    Still unsolved, help

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

    CorrieKay

    this may or may not help, and i havnt read the thread yet, so bear with me if i say something someone else has already said...

    However, this is how my warping plugin handles grabbing warps from a configuration:
    https://github.com/CorrieKay/PonyWarp3/blob/master/src/me/Corrie/PonyWarp3/Utils/Teleport.java#L30
    Code:
    public void prepareLoc(){
    try {
    toThis = new Location(defaultWorld, 0, 0, 0, 0, 0);
    toThis.setWorld(instance.getServer().getWorld(path.get(0)));
    toThis.setX(getDub(path.get(1)));
    toThis.setY(getDub(path.get(2)));
    toThis.setZ(getDub(path.get(3)));
    toThis.setYaw(getFloat(path.get(4)));
    toThis.setPitch(getFloat(path.get(5)));
    } catch (Exception e) {
    e.printStackTrace();
    instance.log.severe("There was an error preparing the teleport location. is the correct warp path: warps."+path+"?");
    }
    }
    path is a string, this is the path you will find a List<String> at, that contains your coordinates. 0 is world, 1-3 is xyz, 4 is your yaw, and 5 is your pitch.
    i will warn you, as i do to everyone else that i show this code, that it is not a public plugin. This code is highly proprietary to my server, so it wont work if you copypaste it.
     
  11. Offline

    davejavu

    I notice it won't work, as you've used custom methods. Also, would I just do player.teleport(location) after that? Thanks though!
     
  12. Offline

    spywhere

    Please note that I use "World:X:Y:Z" not "World,X,Y,Z"... but if it still doesn't work may be I need to try it again...
     
  13. Offline

    CorrieKay

    oh, the getDub and getFloat are custom methods to parse for doubles and floats from a string, since im storing the warps in a yml stringlist.
    this is what it looks like in config:
    Code:
    warps:
      examplewarp:
      - world_nether
      - '-29.715873468090365'
      - '19.0'
      - '15.48884604176535'
      - '144.75143'
      - '22.200087'
     
  14. Offline

    davejavu

    spywhere that was the first thing I changed. It parses it to a location, but then cant tp the player there.
     
  15. Offline

    spywhere

    ok, i'll try to make plugin test for this... take some minutes...
     
  16. Offline

    davejavu

    spywhere okay, tag me when youre done.
     
  17. Offline

    spywhere

    Code:java
    1. package me.spywhere.LocationTest;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.Location;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.plugin.PluginDescriptionFile;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class LocationTest extends JavaPlugin{
    13. private Logger log=Logger.getLogger("Minecraft");
    14. private PluginDescriptionFile pdf=null;
    15.  
    16. String testLocation="";
    17.  
    18. public Location str2loc(String str){
    19. String str2loc[]=str.split("\\:");
    20. Location loc = new Location(getServer().getWorld(str2loc[0]),0,0,0);
    21. loc.setX(Double.parseDouble(str2loc[1]));
    22. loc.setY(Double.parseDouble(str2loc[2]));
    23. loc.setZ(Double.parseDouble(str2loc[3]));
    24. return loc;
    25. }
    26.  
    27. public String loc2str(Location loc){
    28. return loc.getWorld().getName()+":"+loc.getBlockX()+":"+loc.getBlockY()+":"+loc.getBlockZ();
    29. }
    30.  
    31. public void onEnable() {
    32. pdf=this.getDescription();
    33. log.info("["+pdf.getName()+"] v"+pdf.getVersion()+" successfully enabled.");
    34. }
    35.  
    36. public void onDisable() {
    37. log.info("["+pdf.getName()+"] v"+pdf.getVersion()+" successfully disabled.");
    38. }
    39.  
    40. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    41. if(sender instanceof Player){
    42. if(args.length==1){
    43. String[] data = args[0].split(",");
    44. if(getServer().getWorlds().contains(getServer().getWorld(data[0]))){
    45. if(data.length==4){
    46. ((Player)sender).teleport(new Location(getServer().getWorld(data[0]),Double.parseDouble(data[1]),Double.parseDouble(data[2]),Double.parseDouble(data[3])));
    47. return true;
    48. }else{
    49. sender.sendMessage("Data is invalid.");
    50. sender.sendMessage("Format: [World],[X],[Y],[Z]");
    51. return true;
    52. }
    53. }else{
    54. sender.sendMessage("World not found!");
    55. return true;
    56. }
    57. }
    58. }
    59. return false;
    60. }
    61. }


    Here, it's working for me. Also, checking your plugin that the world you teleport is exist on your server.
     
  18. Offline

    Napkin222

    I believe I have your answer. Change the String into an array and then use the parse int solution.

    Code:
    String[] locationvariables = HashMap info
    int xpos = Integer.parseInt(locationvariables[1]);
    int ypos = Integer.parseInt(locationvariables[2]);
    int zpos = Integer.parseInt(locationvariables[3]);
    Location toTP;
    toTP.setX(xpos);
    toTP.setY(ypos);
    toTP.setZ(zpos);
    
     
  19. Offline

    ItsHarry

    Why would you use a string for this? You can just put the Location in the hashmap.
    Unless you want to save it, in that case the easiest solution would be to make your own serializable Location class.
     
Thread Status:
Not open for further replies.

Share This Page