Solved Saving Location to config.yml

Discussion in 'Plugin Development' started by CaLxCyMru, Dec 7, 2013.

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

    CaLxCyMru

    Hello there, I am saving a location to the config file; This part works.

    When I reload the server, the data in the key gets wiped and replaced with "{}" Does any one know why?

    Here is the code:
    Code:java
    1. //Setting the config
    2. Location loc = player.getLocation();
    3.  
    4. org.bukkit.World world = loc.getWorld();
    5. int x = loc.getBlockX();
    6. int y = loc.getBlockY();
    7. int z = loc.getBlockZ();
    8. float pitch = loc.getPitch();
    9. float yaw = loc.getYaw();
    10.  
    11. FileConfiguration config = plugin.getConfig();
    12.  
    13. String jl = "JOIN_LOCATION.";
    14. config.set(jl+"world", world);
    15. config.set(jl+"x", x);
    16. config.set(jl+"y", y);
    17. config.set(jl+"z", z);
    18. config.set(jl+"pitch", pitch);
    19. config.set(jl+"yaw", yaw);
    20. plugin.saveConfig();


    Code:java
    1. //Getting the config
    2. FileConfiguration config = plugin.getConfig();
    3. String jl = "JOIN_LOCATION.";
    4. org.bukkit.World world = (org.bukkit.World) config.get(jl+"world");
    5. int x = (int) config.get(jl+"x");
    6. int y = (int) config.get(jl+"y");
    7. int z = (int) config.get(jl+"z");
    8. float pitch = (float) config.get(jl+"pitch");
    9. float yaw = (float) config.get(jl+"yaw");
    10.  
    11. Location jLocation = new Location(world,x,y,z,yaw,pitch);
    12. player.teleport(jLocation);
     
  2. Offline

    BielSAA

    I'm writting the code , wait.
     
  3. change the world to a UUID

    config.set(jl+"world",world.getUUID())

    and

    Bukkit.getWorld(UUID.fromString((String)config.get(jl+"world")));
     
  4. Offline

    BielSAA

    getConfig().set("Loc.X", jLocation.getX());
    getConfig().set("Loc.Y", jLocation.getY());
    getConfig().set("Loc.Z", jLocation.getZ());

    And to load the location use:
    Location l = player.getLocation();
    l.setX(getConfig().getDouble("Loc.X"));
    l.setY(getConfig().getDouble("Loc.Y"));
    l.setZ(getConfig().getDouble("Loc.Z"));
    player.teleport(l);

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  5. you need the world...

    Some servers have multiple worlds, so it does not save the Location properly
     
  6. Offline

    CaLxCyMru


    I already have this in my plugin. When I reload my plugin, It then removes all the data from the JOIN_LOCATION key
    [​IMG]
    to this;
    [​IMG]
    This happens every time I reload/stop the server.


    Couldn't get that to work on my PlayerJoinEvent

    I used this code:

    Code:java
    1. FileConfiguration config = plugin.getConfig();
    2. String jl = "JOIN_LOCATION.";
    3. org.bukkit.World world = (org.bukkit.World)UUID.fromString((String) config.get(jl+"world");
    4. int x = (int) config.get(jl+"x");
    5. int y = (int) config.get(jl+"y");
    6. int z = (int) config.get(jl+"z");
    7. float pitch = (float) config.get(jl+"pitch");
    8. float yaw = (float) config.get(jl+"yaw");
    9.  
    10. Location jLocation = new Location(world,x,y,z,yaw,pitch);
    11. player.teleport(jLocation);
    12.  
    13. // I get a error on line 3


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  7. its Bukkit.getWorld(UUID.fromString((String) config.get(jl+"world")))

    read my post clearly -_-
     
  8. Offline

    CaLxCyMru

    Still gets removed after reloading/stopping the server.
     
  9. @CaLxCyMru
    Could you send me the whole file?
     
  10. Offline

    CaLxCyMru

    What do you need? onEnable()?
     
  11. Offline

    L33m4n123


    he just said he needs it all ;)
     
  12. Offline

    CaLxCyMru

    Yes, But I don't want to send my whole plugin to him :p
     
  13. Offline

    L33m4n123

    Well. it could be that somewhere deeper in your code is the issue. So he asks for it all. Because if you now send him the onEnable and there is no error he asks for the next part. and so on.

    However if you post all at once here we can look at it right now and then see where the issue is and can help you faster
     
  14. Offline

    CaLxCyMru

    I will post all of the methods the config is called.
    onEnable()
    Code:java
    1. @Override
    2. public void onEnable() {
    3.  
    4. getCommands();
    5.  
    6. eRegister();
    7.  
    8. getConfig().options().copyDefaults(true);
    9. saveConfig();
    10.  
    11.  
    12.  
    13. }


    Setting the JoinLocation command
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
    2. {
    3. if (!(sender instanceof Player)) {
    4.  
    5. sender.sendMessage(plugin.prefix + "You are not permitted to use this command in console!");
    6. return true;
    7. }
    8.  
    9. Player player = (Player)sender;
    10. if(cmd.getName().equalsIgnoreCase("joinlocation")){
    11.  
    12. if(!(player.hasPermission(new Permissions().joinlocation))) {
    13.  
    14. player.sendMessage(plugin.prefix + "The cow goes, NOO");
    15.  
    16. return true;
    17. }
    18.  
    19. Location loc = player.getLocation();
    20.  
    21. org.bukkit.World world = loc.getWorld();
    22. int x = loc.getBlockX();
    23. double y = loc.getBlockY();
    24. double z = loc.getBlockZ();
    25. float pitch = loc.getPitch();
    26. float yaw = loc.getYaw();
    27.  
    28. FileConfiguration config = plugin.getConfig();
    29.  
    30. String jl = "JOIN_LOCATION.";
    31. config.set(jl+"world", world.getUID());
    32. config.set(jl+"x", x);
    33. config.set(jl+"y", y);
    34. config.set(jl+"z", z);
    35. config.set(jl+"pitch", pitch);
    36. config.set(jl+"yaw", yaw);
    37. plugin.saveConfig();
    38.  
    39.  
    40. player.sendMessage(plugin.prefix + "You have set the join location to where you are standing");
    41.  
    42. return true;
    43.  
    44. }
    45.  
    46. return false;
    47.  
    48. }


    Player Join event that TP's Them to the location from the command.
    Code:java
    1. @EventHandler (priority = EventPriority.HIGHEST)
    2. public void onJoin(PlayerJoinEvent event) {
    3. Player player = event.getPlayer();
    4.  
    5. event.setJoinMessage(ChatColor.DARK_AQUA + player.getName() + ChatColor.GRAY + " Joined");
    6.  
    7. player.sendMessage(ChatColor.DARK_GRAY + "" + ChatColor.BOLD + " --------------------------------------------" );
    8. player.sendMessage(ChatColor.DARK_AQUA + "" + ChatColor.BOLD + " Viral Aftermath ");
    9. player.sendMessage(ChatColor.RED + " Will YOU Survive?.. ");
    10. player.sendMessage(ChatColor.DARK_GRAY + "" + ChatColor.BOLD + " --------------------------------------------" );
    11.  
    12. //Getting the config
    13. FileConfiguration config = plugin.getConfig();
    14. String jl = "JOIN_LOCATION.";
    15. org.bukkit.World world = Bukkit.getWorld(UUID.fromString((String)config.get(jl+"world")));
    16. double x = (double) config.get(jl+"x");
    17. double y = (double) config.get(jl+"y");
    18. double z = (double) config.get(jl+"z");
    19. float pitch = (float) config.get(jl+"pitch");
    20. float yaw = (float) config.get(jl+"yaw");
    21.  
    22. Location jLocation = new Location(world,x,y,z,yaw,pitch);
    23. player.teleport(jLocation);
    24. }
    25.  

    config.yml
    PHP:
    EU1_IPmc.hypixel.net // ingore this
    EU1_PORT25565 // ignore this
    JOIN_LOCATION:
        
    world:
        
    x:
        
    y:
        
    z:
        
    pitch:
        
    yaw:
     
  15. Offline

    CaLxCyMru

  16. Offline

    samosaara

    Yeah, yeah, blah, blah, send the whole code, this, that... God, it's simple. Your variables get wiped at reload/restart, and the player are already there, so the JoinEvent will not be triggered so, on the onEnable method you have to do a For loop thats RE-DO the JoinEvent for all players online.
    Code:java
    1. public void onEnable(){
    2. for (Player player : this.getServer().getOnlinePlayers()){
    3. FileConfiguration config = plugin.getConfig();
    4. String jl = "JOIN_LOCATION.";
    5. org.bukkit.World world = Bukkit.getWorld(UUID.fromString((String)config.get(jl+"world")));
    6. double x = (double) config.get(jl+"x");
    7. double y = (double) config.get(jl+"y");
    8. double z = (double) config.get(jl+"z");
    9.  
    10. float pitch = (float) config.get(jl+"pitch");
    11. float yaw = (float) config.get(jl+"yaw");
    12. Location jLocation = new Location(world,x,y,z,yaw,pitch);
    13. player.teleport(jLocation); }
    14. }

    And plus, there is no problem if there is no player online, the code is just skipped.
     
    CaLxCyMru likes this.
  17. Offline

    CaLxCyMru

    Thanks for the help man, I will try this out soon.

    I thought it was something to do with the onEnable(). No idea why they wanted to see all my code. So just posted the code I thought would be affected.

    samosaara I made a method in the main class, playerJoin() and put it in my onEnable() and also my PlayerJoinEvent. I still get the NPE after reloading.
    Code:
    Code:java
    1. public void playerJoin(){
    2.  
    3. for (Player player : this.getServer().getOnlinePlayers()){
    4. FileConfiguration config = plugin.getConfig();
    5. String jl = "JOIN_LOCATION.";
    6. org.bukkit.World world = Bukkit.getWorld(UUID.fromString((String)config.get(jl+"world")));
    7. double x = (double) config.get(jl+"x");
    8. double y = (double) config.get(jl+"y");
    9. double z = (double) config.get(jl+"z");
    10.  
    11. float pitch = (float) config.get(jl+"pitch");
    12. float yaw = (float) config.get(jl+"yaw");
    13. Location jLocation = new Location(world,x,y,z,yaw,pitch);
    14. player.teleport(jLocation); }
    15. }


    NPE:
    [​IMG]

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

    samosaara

    Give me the method of the line 197 is in (from your main class) so we can see whats giving the null pointer thing.
     
  19. Offline

    CaLxCyMru


    [​IMG]


    There you go. Here is the code in text format;

    Code:java
    1. public void playerJoin(){
    2.  
    3. for (Player player : this.getServer().getOnlinePlayers()){
    4. FileConfiguration config = plugin.getConfig();
    5. String jl = "JOIN_LOCATION.";
    6. org.bukkit.World world = Bukkit.getWorld(UUID.fromString((String)config.get(jl+"world")));
    7. double x = (double) config.get(jl+"x");
    8. double y = (double) config.get(jl+"y");
    9. double z = (double) config.get(jl+"z");
    10.  
    11. float pitch = (float) config.get(jl+"pitch");
    12. float yaw = (float) config.get(jl+"yaw");
    13. Location jLocation = new Location(world,x,y,z,yaw,pitch);
    14. player.teleport(jLocation); }
    15. }


    Here is a example of my config.yml

    Before I set the join location:

    PHP:
    EU1_IPmc.hypixel.net
    EU1_PORT
    25565
    JOIN_LOCATION
    :
      
    world:
      
    x:
      
    y:
      
    z:
      
    pitch:
      
    yaw:
    I set the join location:

    PHP:
    EU1_IPmc.hypixel.net
    EU1_PORT
    25565
    JOIN_LOCATION
    :
      
    world: !!java.util.UUID {}
      
    x1143
      y
    6.0
      z
    : -73.0
      pitch
    7.5000014
      yaw
    : -35.700005
    And this is after a restart/reload:

    PHP:
    EU1_IPmc.hypixel.net
    EU1_PORT
    25565
    JOIN_LOCATION
    : {}
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  20. Offline

    samosaara

    The plugin variable isn't initialized make sure to initialize it before using.
     
    CaLxCyMru likes this.
  21. Offline

    CaLxCyMru

    Of course! I will try this now!

    samosaara Its worked. But I now get this error while reloading:

    [​IMG]

    I am using this method:

    Code:java
    1. public void playerJoin(){
    2.  
    3. for (Player player : this.getServer().getOnlinePlayers()){
    4. FileConfiguration config = this.getConfig();
    5. String jl = "JOIN_LOCATION.";
    6. org.bukkit.World world = Bukkit.getWorld(UUID.fromString((String)config.get(jl+"world")));
    7. double x = (double) config.get(jl+"x");
    8. double y = (double) config.get(jl+"y");
    9. double z = (double) config.get(jl+"z");
    10.  
    11. float pitch = (float) config.get(jl+"pitch");
    12. float yaw = (float) config.get(jl+"yaw");
    13. Location jLocation = new Location(world,x,y,z,yaw,pitch);
    14. player.teleport(jLocation);
    15. }
    16. }


    And here is my onCommand() where I set the config.

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
    2. {
    3. if (!(sender instanceof Player)) {
    4.  
    5. sender.sendMessage(plugin.prefix + "You are not permitted to use this command in console!");
    6. return true;
    7. }
    8.  
    9. Player player = (Player)sender;
    10. if(cmd.getName().equalsIgnoreCase("joinlocation")){
    11.  
    12. if(!(player.hasPermission(new Permissions().joinlocation))) {
    13.  
    14. player.sendMessage(plugin.prefix + "The cow goes, NOO");
    15.  
    16. return true;
    17. }
    18.  
    19. Location loc = player.getLocation();
    20.  
    21. org.bukkit.World world = loc.getWorld();
    22. double x = loc.getBlockX();
    23. double y = loc.getBlockY();
    24. double z = loc.getBlockZ();
    25.  
    26. float pitch = loc.getPitch();
    27. float yaw = loc.getYaw();
    28.  
    29. FileConfiguration config = plugin.getConfig();
    30.  
    31. String jl = "JOIN_LOCATION.";
    32. config.set(jl+"world", world.getUID().toString());
    33. config.set(jl+"x", x);
    34. config.set(jl+"y", y);
    35. config.set(jl+"z", z);
    36. config.set(jl+"pitch", pitch);
    37. config.set(jl+"yaw", yaw);
    38. plugin.saveConfig();
    39.  
    40.  
    41. player.sendMessage(plugin.prefix + "You have set the join location to where you are standing");
    42.  
    43. return true;
    44.  
    45. }
    46.  
    47. return false;
    48.  
    49. }


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

    samosaara

    Hey, I think, if your lines still the same from this print:[​IMG]
    I'm a bit confused not sure. I think the yaw variable needs to be double. so converts to float (float) when u need it float.
    like:
    Code:java
    1. double yaw = config.getDouble(jl+"yaw");
    2. Location jLocation = new Location(world,x,y,z,(float)yaw,pitch);

    Tip: Instead of get the object with getConfig().get("path"); and converting. Get with the type already: getConfig().getTYPE("path"); Like I've done at line 1 of my code.
    EDIT: Grammar fix
     
    CaLxCyMru likes this.
  23. Offline

    CaLxCyMru

    Ah okay. I will try and use that method of getting info from a config. I will try the fix now :)

    Thanks for the help so far man!

    samosaara Thanks dude, This worked. I really do appreciate the help you have given me!

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

Share This Page