Custom Config File Where I Can Store Locations

Discussion in 'Plugin Development' started by Niknea, Apr 14, 2014.

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

    Niknea

    Hey guys, so I got a few quick questions,

    1. How can I create a new custom config file? Ex. created a new config file named "Location.yml" that will be located in the plugin folder of that certain plugin.

    2. How can I store locations into that file?

    3. How can I retrieve a location from the file and then teleport the player to the location I retrieved?

    Thanks!
     
  2. Offline

    rfsantos1996

    I won't help you with customConfig because i'm lazy.

    2- Configuration is stored as Strings normally, its easier. So, in order to transform a Location to a String, normally i use the format: world_name;x;y;z or world_name;x;y;z;yaw;pitch

    Code:java
    1. String locationString = location.getWorld().getName()+";"+location.getX()+";"+location.getY()+";"+location.getZ() // put here aditional pitch and yaw if you want to save eye location
    2.  
    3. //OBS: pitch and yaw are floats


    Save a double (location.getX() is a double, can be like 243.324124314134) require a lot of space.. Normally I use something to make it a 2 digit number after coma ("243.31")

    3- Make the reverse.
    Code:java
    1. String[] loc = LocationString.split(";");
    2. if(loc.lenght < 5) {
    3. Location location = new Location(Bukkit.getWorld(loc[0]), Double.parseDouble(loc[1]), Double.parseDouble(loc[2]), Double.parseDouble(loc[3]));
    4. } else {
    5. Location location = new Location(Bukkit.getWorld(loc[0]), Double.parseDouble(loc[1]), Double.parseDouble(loc[2]), Double.parseDouble(loc[3]), Float.parseFloat(loc[4]), Float.parseFloat(loc[5]));
    6. }
     
    Niknea likes this.
  3. Offline

    Niknea

    rfsantos1996 Alright I'll try that thanks, can anyone help me with the multiple configs?
     
  4. Offline

    Asgernohns

    Niknea make a Google Search on custom config and go to the bukkit wiki. There you Will find help to make your own custom config.
    Now for saving/loading you Will need to convert the location to a string.
    Like this for saving:
    for (Entry<String, Location> map: hashmap.entrySet())
    {
    Location loc = map.getValue();
    String saveLoc = loc.getWorld().getName()+":"+loc.getX()+":"+loc.getY()+":"+loc.getZ();
    customConfig.set(map.getKey(), saveLoc)
    }

    And for loading:
    for (String locs : customConfig.getKeys(true))
    {
    String[] parts = customConfig.getValue().split(":");
    If(parts.length == 4)
    {
    World w = Bukkit.getServer().getWorld(parts[0]);
    double x = Double.parseDouble(parts[1]);
    double y = Double.parseDouble(parts[2]);
    double z = Double.parseDouble(parts[3]);
    Location loc = new Location(w, x, y, z);

    hashmap.put(locs, loc);
    }
    }

    This code is untested!!!

    EDIT: Too slow :'(
     
    Niknea likes this.
  5. Offline

    coasterman10

    Here are some snippets to help with loading/saving custom configs:
    Code:java
    1. // Loading the config
    2. File configFile = new File(getDataFolder(), "foo.yml");
    3. FileConfiguration config = YamlConfiguration.loadConfiguration(configFile);
    4.  
    5. // Saving the config
    6. try {
    7. config.save(configFile);
    8. } catch (IOException e) {
    9. // TODO: Handle the exception
    10. }
     
    Niknea likes this.
  6. Offline

    Niknea

    Asgernohns coasterman10 Alright it worked! Here is my code
    PHP:
      private FileConfiguration customConfig null;
        private 
    File customConfigFile null;
        public 
    void reloadCustomConfig(){
            if (
    customConfigFile == null) {
                
    customConfigFile = new File(getDataFolder(), "location.yml");
            }
            
    customConfig YamlConfiguration.loadConfiguration(customConfigFile);
     
            
    // Look for defaults in the jar
            
    InputStream defConfigStream this.getResource("location.yml");
            if (
    defConfigStream != null) {
                
    YamlConfiguration defConfig YamlConfiguration.loadConfiguration(defConfigStream);
                
    customConfig.setDefaults(defConfig);
            }
        }
     
        public 
    FileConfiguration getCustomConfig() {
            if (
    customConfig == null) {
                
    reloadCustomConfig();
            }
            return 
    customConfig;
        }
        public 
    void saveCustomConfig() {
            if (
    customConfig == null || customConfigFile == null) {
                return;
            }
            try {
                
    getCustomConfig().save(customConfigFile);
            } catch (
    IOException ex) {
                
    getLogger().log(Level.SEVERE"Could not save config to " customConfigFileex);
            }
        }
     
        @
    EventHandler
        
    public void onJoin123(PlayerJoinEvent e){
            if(
    getCustomConfig().get(e.getPlayer().getName()) == null){
                
    getCustomConfig().set(e.getPlayer().getName(), "");
                
    saveCustomConfig();
                
    e.getPlayer().sendMessage(ChatColor.GOLD "Added to Database!");
            }
            else{
                
    e.getPlayer().sendMessage(ChatColor.GREEN "Found in Database!");
            }
        }
    Please keep in mind this wont be the real code, I know its messy :p.

    Now I got a quick question, say I want to add stuff at the start of the config loading, ex. adding a "Location:" in the location.yml when the plugin starts, how do I add that? Would it be just like doing it to a config.yml? Would I have to do anything else? Thanks.
     
  7. Offline

    Asgernohns

    Niknea No it Will be the same, so Like: customConfig.set("hi", "Hello");
    customConfig.getString("hi");
     
  8. Offline

    coasterman10

    Niknea getConfig() returns a FileConfiguration, so a custom config works identically to getConfig() except you put customConfig instead of getConfig().
     
  9. Offline

    Niknea

    coasterman10 Asgernohns Not sure you understand what I'm saying, in the default config ( config.yml ) in your IDE you can add stuff that will be added to the config right when it loads, how can I do that with a custom file config? Would it be the same as the default config?

    Seemed to run into a problem, on reload, the custon file resets, any ways to fix it? Thanks

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

    Asgernohns

    Niknea Yes you can. Just make a new file called: location.yml :)

    Niknea Did you save the file on shutdown?

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

    Niknea

    Asgernohns I save it every time I edited something, CODE : http://pastie.org/9081185, pretty much when I reload the server, and do the /esetup command, it says "no overrides made", even though I set it before, but if I do it again without reloading, it will say a override was made. Any ideas?
     
  12. Offline

    1Rogue

    Use a ConfigurationSerializable class for location loading with a lazy-initialized world:

    Code:java
    1. /*
    2. * Copyright (C) 2014 CodeLanx , All Rights Reserved
    3. *
    4. * This work is licensed under a Creative Commons
    5. * Attribution-NonCommercial-NoDerivs 3.0 Unported License.
    6. *
    7. * This program is protected software: You are free to distrubute your
    8. * own use of this software under the terms of the Creative Commons BY-NC-ND
    9. * license as published by Creative Commons in the year 2014 or as published
    10. * by a later date. You may not provide the source files or provide a means
    11. * of running the software outside of those licensed to use it.
    12. *
    13. * This program is distributed in the hope that it will be useful,
    14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
    15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    16. *
    17. * You should have received a copy of the Creative Commons BY-NC-ND license
    18. * along with this program. If not, see <[url]https://creativecommons.org/licenses/>[/url].
    19. */
    20. package com.codelanx.codelanxlib.serialize;
    21.  
    22. import java.util.HashMap;
    23. import java.util.Map;
    24. import java.util.UUID;
    25. import org.bukkit.Bukkit;
    26. import org.bukkit.World;
    27. import org.bukkit.block.Block;
    28. import org.bukkit.configuration.serialization.ConfigurationSerializable;
    29. import org.bukkit.configuration.serialization.SerializableAs;
    30. import org.bukkit.util.Vector;
    31.  
    32. /**
    33. *
    34. * @since 1.0.0
    35. * @author 1Rogue
    36. * @version 1.0.0
    37. */
    38. @SerializableAs("Location")
    39. public class SLocation implements ConfigurationSerializable {
    40.  
    41. private final Vector loc;
    42. private final UUID uuid;
    43. private World world;
    44.  
    45. public SLocation(Block block) {
    46. this.loc = block.getLocation().toVector();
    47. this.uuid = block.getWorld().getUID();
    48. }
    49.  
    50. public SLocation(Map<String, Object> config) {
    51. this.loc = (Vector) config.get("location");
    52. this.uuid = UUID.fromString((String) config.get("world"));
    53. }
    54.  
    55. @Override
    56. public Map<String, Object> serialize() {
    57. Map<String, Object> back = new HashMap<>();
    58. back.put("location", this.loc);
    59. back.put("world", this.uuid.toString());
    60. return back;
    61. }
    62.  
    63. public SLocation valueOf(Map<String, Object> config) {
    64. return new SLocation(config);
    65. }
    66.  
    67. public SLocation deserialize(Map<String, Object> config) {
    68. return new SLocation(config);
    69. }
    70.  
    71. public World getWorld() {
    72. if (this.world == null) {
    73. this.world = Bukkit.getWorld(this.uuid);
    74. }
    75. return this.world;
    76. }
    77.  
    78. public Vector getVector() {
    79. return this.loc;
    80. }
    81.  
    82. }


    This becomes easier to manage:

    Code:java
    1. SLocation loc = (SLocation) config.get("path-to-slocation");
    2. config.put("path-to-slocation", loc);
    3.  
    4. Location bloc = loc.getVector().toLocation(loc.getWorld());
     
Thread Status:
Not open for further replies.

Share This Page