[urgent][easy][unsolved][noob][please help] String to location bug!

Discussion in 'Plugin Development' started by griffy100, Oct 2, 2013.

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

    griffy100

    I am trying to convert string to locations. I have the string
    Code:java
    1. "166!62!-634"

    and i do this:
    Code:java
    1. String locationString = "166!63!-634"
    2. String[] data = locationString.split("!");
    3. int x = Integer.parseInt(data[0]);
    4. int y = Integer.parseInt(data[1]);
    5. int z = Integer.parseInt(data[2]);
    6. World w = Bukkit.getServer().getWorlds().get(0);
    7. Location loc = new Location(w,x,y,z);


    But this does not work! I have tried stack tracing and it only gets to
    Code:java
    1. int x = Integer.parseInt(data[0]);
    before it stops.

    I printed out the value of
    Code:java
    1. data[1] data[2] ect...
    and they seem to be empty!

    This is the final part in my mini-game that needs to be released tomorrow!
    Thanks, griffy100!

    Please help me!

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

    Plo124

    griffy100
    You need to put a semi-colon ( ; ) at the end of this line
    Code:java
    1. String locationString = "166!63!-634"
     
  3. Offline

    griffy100

    Obviosly. The code isn't giving any errors so its not syntax. The error pops up when parsing strings to ints
     
  4. nvm, you did this already, but I couldn't see it.
     
  5. Offline

    kreashenz

    Aren't locations serializable? My friend gave me this code
    Whole class (open)
    Code:java
    1. import java.io.Serializable;
    2. import org.bukkit.Location;
    3. import org.bukkit.Server;
    4. import org.bukkit.World;
    5. import org.bukkit.plugin.Plugin;
    6.  
    7. public class SerializableLocation
    8. implements Serializable
    9. {
    10. private static final long serialVersionUID = -3081339483209881873L;
    11. private String world;
    12. private double x;
    13. private double y;
    14. private double z;
    15. private transient Plugin plugin;
    16.  
    17. public SerializableLocation(Plugin plugin, Location loc)
    18. {
    19. this.world = loc.getWorld().getName();
    20. this.x = loc.getX();
    21. this.y = loc.getY();
    22. this.z = loc.getZ();
    23. this.plugin = plugin;
    24. }
    25.  
    26. public Location toLocation() {
    27. if (this.plugin == null) {
    28. throw new IllegalStateException("The transient field plugin is not initialized");
    29. }
    30. return new Location(this.plugin.getServer().getWorld(this.world), this.x, this.y, this.z);
    31. }
    32.  
    33. public void reInitializePlugin(Plugin plugin) {
    34. this.plugin = plugin;
    35. }
    36.  
    37. public boolean compare(Location loc) {
    38. return (this.x == loc.getX()) && (this.y == loc.getY()) && (this.z == loc.getZ()) && (this.world.equalsIgnoreCase(loc.getWorld().getName()));
    39. }
    40.  
    41. public String toString()
    42. {
    43. return "world=" + this.world + ", x=" + this.x + ", y=" + this.y + ", z=" + this.z;
    44. }
    45.  
    46. public String getWorld() {
    47. return this.world;
    48. }
    49.  
    50. public double getX() {
    51. return this.x;
    52. }
    53.  
    54. public double getY() {
    55. return this.y;
    56. }
    57.  
    58. public double getZ() {
    59. return this.z;
    60. }
    61. }


    Just use
    Code:java
    1. SerializableLocation sLoc = new SerializableLocation(<Main class>, location);

    And
    Code:java
    1. sLoc.toString();

    And that'll print out something like,
    Code:
    world=world, x=365, y=64, z=464
    Pretty simple, I guess.
     
  6. Offline

    MastaC

    Copy and pasted your code into my own plugin. Besides the semicolon that you said you put in, there are no issues with it. Could you paste the stack trace?
     
  7. Offline

    Plo124

    If you would like to try, you can try declaring each split part as a string...
    Code:java
    1. String locationString = "166!63!-634";
    2. String[] data = locationString.split("!");
    3. String xP = data[0];
    4. String yP = data[1];
    5. String zP = data[2];
    6. int x = Integer.parseInt(xP);
    7. int y = Integer.parseInt(yP);
    8. int z = Integer.parseInt(zP);
    9. World w = Bukkit.getServer().getWorlds().get(0);
    10. Location loc = new Location(w,x,y,z);
     
  8. Offline

    sgavster

    [why][all][the][tags]? We know you need help, you don't need 50 tags :/
     
    Chinwe and spoljo666 like this.
  9. Offline

    1Rogue

    You're passing .split() the char '!'. If I remember correctly .split() accepts a regex pattern, so try escaping the value.

    Code:java
    1. String[] data = locationString.split("\!");
     
  10. Offline

    griffy100

    Sorry!!!

    Nope, doesn't work!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
Thread Status:
Not open for further replies.

Share This Page