String to Location!?

Discussion in 'Plugin Development' started by JeykoExample, Sep 26, 2014.

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

    JeykoExample

    Hey Guys,

    i wanted to ask you, if there is a way to change String to Location?

    I have this ...

    PHP:
                            String x this.plugin.getConfig().getString("meeting.location.x");
                            
    String y this.plugin.getConfig().getString("meeting.location.y");
                            
    String z this.plugin.getConfig().getString("meeting.location.z");
                            
    String world this.plugin.getConfig().getString("meeting.location.world");
    and i want to teleport a Player to this Location but it says its a String not a Location...

    Thanks a LOT!
     
  2. Offline

    Unica

    JeykoExample

    Ofcourse ;)

    • You can retrieve an integer from a string using
      Code:java
      1. Integer.parseInt(String);
    • You would rather save an integer to the config using
      Code:java
      1. getConfig().set("Path", x); //x = integer (no quotes)
      then u can just use
      Code:java
      1. getConfig().getInt("path");
    • You could make this lot's more compact by converting the entire location to a string, then retrieve every needed object from that entire string. Using
      Code:java
      1. private String location2String(Location loc){
      2. return loc.getWorld().getName()+","+loc.getX()+","+loc.getY()+","+loc.getZ();
      3. }
      Then u can
      Code:java
      1. private Location string2Loc(String string){
      2. String[] s = string.split(",");
      3. return new Location(Bukkit.getWorld(s[0]), Integer.parseInt(s[1]), Integer.parseInt(s[2]), Integer.parseInt(s[3]));
      4. }
    EDIT:
    With your current code you can do,
    Code:java
    1. Location loc = new Location(Bukkit.getWorld(world), Integer.parseInt(x), Integer.parseInt(y), Integer.parseInt(z));


    EDIT2:
    Changed syntax to java :p
     
    JeykoExample likes this.
  3. Offline

    JeykoExample

    Unica

    Thanks a LOT! :)

    Unica

    and if I want to change the Default in the Config can I do this or what can I do?

    PHP:
    this.plugin.getConfig().set("meeting.location.x"p.getLocation().getX());
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  4. Offline

    Unica

    JeykoExample

    I would rather do
    Code:java
    1. p.getLocation().getBlockX();

    because getX() doesn't exactly return an integer. getBlockX does ;)

    Don't forget to use saveConfig() after setting something, else it won't work.

    EDIT
    Also, put
    Code:java
    1. getConfig().options().copyDefaults(true);
    in the onEnable method! (Main class) ;)
     
    JeykoExample likes this.
  5. Offline

    JeykoExample

    Unica

    Thanks again, you are really helpful!!!
     
  6. Offline

    Unica

  7. Offline

    JeykoExample

    Unica

    I need help again... I got this...
    PHP:
            this.getConfig().addDefault("meeting.players", new String[] {
                
    "JeykoPlays",
                
    "YanikPvP",
                
    "Yanik2301",
                
    "DaviGamezPvP",
                
    "Nikislolxd",
            });
       
    and what do i have to type in to remove a Playername from the List and what do i have to type in when i want to add a player to the list?
     
  8. Offline

    Unica

    JeykoExample
    I would work with StringLists ;)

    Code:java
    1. private void saveToList(String path_to_list, Object what_to_add){
    2. List<String> list = getConfig().getStringList(path_to_list);
    3. list.add(what_to_add);
    4. getConfig.set(path_to_list, list);
    5. saveConfig();
    6. }


    With the method above, u can use
    Code:java
    1. saveToList("meeting.players", "a Name");


    For removing, simply,
    Code:java
    1. private void removeFromList(String path_to_list, Object what_to_remove){
    2. List<String> list = getConfig().getStringList(path_to_list);
    3. if(list.contains(what_to_remove){
    4. list.remove(what_to_remove);
    5. }
    6. getConfig().set(path_to_list), list); //re set the list ;)
    7. saveConfig();
    8. }


    Now if you want to remove "Jack" from the list, u can use
    Code:java
    1. removeFromList("meeting.players", "Jack");
     
  9. Offline

    JeykoExample

    Unica

    I don't know how this works with the private void method thingy can u do it without? so in the 3rd path_to_list ?

    wow im dumb Unica now i know how it works :) THANKS!!!! :rolleyes:

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

Share This Page