Solved Convert String to Location?

Discussion in 'Plugin Development' started by Alias_Me, Jan 19, 2019.

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

    Alias_Me

    Quick question, how would I convert a Location, that I saved as a String in a File and used a BufferedReader to read, back into a Location so that a player can be teleported to that Location?
     
  2. Offline

    timtower Administrator Administrator Moderator

    How did you save it?
     
  3. Offline

    Alias_Me

    @timtower
    Short answer: As a String inside a .yml

    Long answer:
    When a sign is placed on top of a specific construction, the signs Location gets saved into a seperate .yml file for every one of those constructions.
    Heres the Method where the Location gets saved:
    Code:Java
    1.  
    2. public void signPlaced(SignChangeEvent event) throws IOException {
    3. Block signblock = (Block)event.getBlock();
    4. Sign sign = (Sign) signblock.getState();
    5. String text = (String) event.getLine(0);
    6. Player user = (Player) event.getPlayer();
    7.  
    8. if(isGuidepost(sign)) {
    9. File waypoint = new File("plugins/Guideposts/" + text + ".yml");
    10. //Checks if waypoint file already exists
    11. if (!waypoint.exists()) {
    12. waypoint.createNewFile();
    13. writeTeleportLocation(waypoint, sign);
    14. BufferedWriter writer = new BufferedWriter(new FileWriter("plugins/Guideposts/" + text + ".yml"));
    15. writer.write(signblock.getLocation().add(-2,0,0).toString());
    16. writer.close();
    17. //TO-DO: Reload Plugin
    18. user.sendMessage(ChatColor.YELLOW + "Guidepost " + ChatColor.RED + text + ChatColor.YELLOW + " has been build and is now available for fast travel.");
    19. } else {
    20. user.sendMessage(ChatColor.RED + "A guidepost with this name already exists!");
    21. signblock.setType(AIR);
    22. user.getInventory().addItem(new ItemStack(SIGN));
    23. }
    24. }
    25. }
    26.  


    And heres the full class:
    https://pastebin.com/c8kinAVm
     
  4. Offline

    timtower Administrator Administrator Moderator

    @Alias_Me Why not handle it as yml and save it with x, y, z, world?
     
  5. Offline

    Alias_Me

    @timtower Don`t know, im pretty new to this :rolleyes: What benefit would that have? Is it not possible to convert a String to a Location?
     
  6. Offline

    timtower Administrator Administrator Moderator

    Makes parsing easy. And you can, but then you need to store it in a way that is parsable as well.
     
  7. Offline

    Alias_Me

    @timtower Okay so writing the world into a file works normally, but for some reason
    Code:Java
    1.  
    2. writer.write(signblock.getX());
    3. //and
    4. writer.write(signblock.getX().toString());
    5.  

    dont work
     
  8. Offline

    timtower Administrator Administrator Moderator

  9. Offline

    Alias_Me

    @timtower the complete output of
    Code:Java
    1. writer.write(signblock.getWorld().toString());
    2. writer.write(signblock.getX());
    3. writer.write(signblock.getY());
    4. writer.write(signblock.getZ());
    5. writer.close();


    is:
    CraftWorld{name=world}?F?
     
  10. Offline

    timtower Administrator Administrator Moderator

    @Alias_Me Why not turn the location into a single string first? And world.getName
     
  11. Offline

    Alias_Me

    @timtower what do you mean "turn the location into a single string"? like .signblock.getLocation().toString()?
    Cause thats what I did here.
     
  12. Offline

    timtower Administrator Administrator Moderator

    @Alias_Me Serializing it yourself.
    String something = x,y,z,world
     
  13. Offline

    Alias_Me

    @timtower Oh okay, that makes sense.
    So what now? :p
     
  14. Offline

    timtower Administrator Administrator Moderator

    You write it to the file.
    For reading you load it, split on the comma, parse the rest.
     
  15. Offline

    Alias_Me

Thread Status:
Not open for further replies.

Share This Page