Converting a String into a Location

Discussion in 'Spigot Plugin Development' started by bman7842, May 8, 2015.

  1. I am making a plugin that needs to store and retrieve Location data from a config file. When i save the Location file, I save it to the config file using Location.toString() which returns something like this:
    Code (Text):
    Location{world=CraftWorld{name=world},x=-98.39808956505735,y=73.8109724700414,z=52.19371933940392,pitch=45.15001,yaw=-279.4501}
     
    Now, whenever the plugin is Enabled it needs to retrieve this location from the file and turn it into an actual location object. I have been attempting to accomplish this but unfortunately, no matter what I try I get an error.

    Here is the current method I have:
    Code (Text):
    public void loadData()
        {
            FileConfiguration config = getConfig();
       
            if (config.contains("tutorial_points"))
            {
                for (String key : config.getConfigurationSection("tutorial_points").getKeys(false))
                {
                    String[] arg = ((String)config.get("tutorial_points." + key + ".location")).split(",");
                    double[] parsed = new double[3];
                    for (int a = 0; a < 3; a++) {
                        parsed[a] = Double.parseDouble(arg[a].substring(3,arg[a].length()));
                    }

                    Location location = new Location (Bukkit.getWorld(arg[0]), parsed[0], parsed[1], parsed[2]);
                 
                    Utils.setTutorialPoint(location, (String)config.get("tutorial_points." + key + ".name"), Integer.parseInt(key), (String)config.get("tutorial_points." + key + ".description"));
                }
            }
        }
    Here is the output:
    Code (Text):
    [14:46:32 ERROR]: Error occurred while enabling EclipseIntro v0.1 (Is it up to d
    ate?)
    java.lang.NumberFormatException: For input string: "ation{world=CraftWorld{name=
    world}"
            at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source) ~[?:1.8
    .0_25]
            at sun.misc.FloatingDecimal.parseDouble(Unknown Source) ~[?:1.8.0_25]
            at java.lang.Double.parseDouble(Unknown Source) ~[?:1.8.0_25]
            at me.bman7842.Core.Main.loadData(Main.java:94) ~[?:?]
            at me.bman7842.Core.Main.onEnable(Main.java:35) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[s
    pigot.jar:git-Spigot-dbe012b-61ef214]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
    .java:335) [spigot.jar:git-Spigot-dbe012b-61ef214]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
    r.java:405) [spigot.jar:git-Spigot-dbe012b-61ef214]
            at org.bukkit.craftbukkit.v1_8_R2.CraftServer.loadPlugin(CraftServer.jav
    a:356) [spigot.jar:git-Spigot-dbe012b-61ef214]
            at org.bukkit.craftbukkit.v1_8_R2.CraftServer.enablePlugins(CraftServer.
    java:316) [spigot.jar:git-Spigot-dbe012b-61ef214]
            at net.minecraft.server.v1_8_R2.MinecraftServer.r(MinecraftServer.java:4
    16) [spigot.jar:git-Spigot-dbe012b-61ef214]
            at net.minecraft.server.v1_8_R2.MinecraftServer.k(MinecraftServer.java:3
    82) [spigot.jar:git-Spigot-dbe012b-61ef214]
            at net.minecraft.server.v1_8_R2.MinecraftServer.a(MinecraftServer.java:3
    37) [spigot.jar:git-Spigot-dbe012b-61ef214]
            at net.minecraft.server.v1_8_R2.DedicatedServer.init(DedicatedServer.jav
    a:257) [spigot.jar:git-Spigot-dbe012b-61ef214]
            at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java
    :522) [spigot.jar:git-Spigot-dbe012b-61ef214]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_25]
    Line 94 of the main class is this:
    Code (Text):
    parsed[a] = Double.parseDouble(arg[a].substring(3,arg[a].length()));
    Any help would be great, thanks!
     
  2. JamesJ

    Supporter

    What?

    Just use something like this:

    Code (Text):

    tutorial_points:
        1:
            x:
            y:
            z:

    etc
     
     
    • Agree Agree x 4
  3. No, I need the entire location value saved, not just X Y Z
     
  4. JamesJ

    Supporter

    "etc"

    Then make more paths for yaw, pitch, world.
     
    • Agree Agree x 2
  5. Now that is just extremely over complicating things, I know there is a way to do it I'm just not sure how.
     
  6. JamesJ

    Supporter

    So you're asking for people's help, but when someone suggests the best way to do it, you shut them down? I really should stop helping people..
     
    • Agree Agree x 1
    • Funny Funny x 1
    • Friendly Friendly x 1
  7. I'm not shutting you down, I'm simply saying that your suggestion is not what I was looking for. Please stop continuing to reply to my thread if that is all you have to offer,
    Thank you.
     
  8. JamesJ

    Supporter

    Please, do find another way. If you can, I will be delighted to know.
    If there was another way, other plugins would use it, which they do not. Stop being lazy, and use the only method there is.
     
  9. Code (Text):
    location: X:Y:Z:YAW:PITCH
    location: 5:70:97:0:0
    (I don't know how YAW/PITCH is numbered)

    PHP:
    String loc = getConfig().getString("location");
    String[] args = loc.split(":");

    Location loc = new Location(args[0], args[1], args[2], args[3], args[4];
    ?
     
  10. Inkzzz

    Resource Staff

    Save and get locations like this:

    Code (Text):
    public void setLobby(Location loc) {
            locMang.getData().set("Lobby.World", loc.getWorld().getName());
            locMang.getData().set("Lobby.X", loc.getX());
            locMang.getData().set("Lobby.Y", loc.getY());
            locMang.getData().set("Lobby.Z", loc.getZ());
            locMang.saveData();
            return;
        }

        public Location getLobby() {
            World w = Bukkit.getWorld(locMang.getData().getString("Lobby.World"));
            double x = locMang.getData().getDouble("Lobby.X");
            double y = locMang.getData().getDouble("Lobby.Y");
            double z = locMang.getData().getDouble("Lobby.Z");
            return new Location(w, x, y, z);
        }
    P.S: locMang.detData() is a custom yml file.
     
    • Agree Agree x 2
  11. Please don't store locations as one string.

    @JamesJ solution is the accepted solution people should be using when storing a Location. Full example:

    Code (YAML):
    sample:
      locations
    :
      - x
    : 0
        y
    : 0
        z
    : 0
        pitch
    : 0
        yaw
    : 0
        world
    : world
      - x
    : 0
        y
    : 0
        z
    : 0
        pitch
    : 0
        yaw
    : 0
        world
    : triforce
     
     
    • Agree Agree x 3
    • Informative Informative x 1
  12. Yeah, the well known stringToLoc and locToString method ;)

    You want this?
    Location: world , x , y , z , yaw , pitch

    Create the 2 methods:
    locToString:
    • Set your world , x , y , z etc next to each other with a , between each other.
    • So you need to save it like this: world , x , y , z
    stringToLoc:
    • Get your config and create a stringarray and split it by a ','
    • Use your array[0] to get your world and array[1] to get your x make it with parseDouble(array[1])
    • If you are done you can create a new loc because you have the x y z and world and you could tp the player to the loc.
     
  13. I'm not being lazy, and you are continuing to reply to this thread in such a way to agitate me, lets stop with this nonsense now. Your point is not wrong, it is just not what I am looking for.
     
  14. Okay, it appears this must be the best way. I'll try it
    Thanks guys!
     
  15. JamesJ

    Supporter

    Well you're saying that the easiest method is overcomplicating things?
    You can either get a Float by using the config (my suggested method), or you can get String, then use Arrays to split the string into sections, then parse them, and all that, those are the two methods. To me (and clearly others), that is the easiest way.
     
  16. Okay, I'll try this. Thanks!
     
  17. You could use java's serialization api, it allows you to save entire objects.
     
  18. My apologizes, for my aspect of use it would be simpler to just convert it back. Obviously you have all proved this method to be improper or unaccepted. I was unaware there was no way of receiving it the way I wanted to so I did not think the way you did it was the only way. The way you acted made me think less of you and apologize for this but please do not treat me like trash because of a simple mistake.
     
  19. This explains alot, thank you!
     
  20. Sarcasm alert?