[Tutorial] Multiple Yaml Configs and Player Files

Discussion in 'Resources' started by clmcd42, Apr 27, 2014.

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

    clmcd42

    This tutorial is to teach you how to make a custom YAML configuration file. You can use this to make player files, multiple configs, and easily set things to your configs.

    1. Make a new class and title it Config.
    Create Class (open)
    Code:java
    1. public class Config {
    2.  
    3. }

    2. Make the constructors.
    Make Constructor (open)
    Code:java
    1. public class Config {
    2. //This stuff will be initialized in your constructor
    3. private final Plugin PLUGIN;
    4. private final String FILENAME;
    5. private final File FOLDER;
    6. private FileConfiguration config;
    7. private File configFile;
    8.  
    9. //This first constructor is for putting the config file directly into the getDataFolder().
    10. public Config(String filename, MainPluginClass instance) {
    11. if(!filename.endsWith(".yml")) {
    12. filename += ".yml"; //Check whether the filename already has the extension, and if it doesn't, add it.
    13. }
    14. this.FILENAME = filename;
    15. this.PLUGIN = instance; //You can also remove the instance from your constructor and use Bukkit.getPluginManager().getPlugin("<pluginname>");
    16. this.FOLDER = this.PLUGIN.getDataFolder();
    17. this.config = null; //this.config and this.configFile are set to null here, but they will be properly initialized in the reload() method.
    18. this.configFile = null;
    19. reload(); //We will define this method in a minute, hold your horses.
    20. }
    21.  
    22. //The second constructor is for putting the config file into a specified folder.
    23. public Config(File folder, String filename, MainPluginClass instance) {
    24. if(!filename.endsWith(".yml")) {
    25. filename += ".yml"; //Check whether the filename already has the extension, and if it doesn't, add it.
    26. }
    27. this.FILENAME = filename;
    28. this.PLUGIN = instance; //You can also remove the instance from your constructor and use Bukkit.getPluginManager().getPlugin("<pluginname>");
    29. this.FOLDER = folder;
    30. this.config = null; //this.config and this.configFile are set to null here, but they will be properly initialized in the reload() method.
    31. this.configFile = null;
    32. reload(); //We will define this method in a minute, hold your horses.
    33. }
    34. }

    3. Create reload() and getConfig() and saveDefaultConfig() methods.
    Create Necessary Methods (open)
    Code:java
    1. public class Config {
    2. private final Plugin PLUGIN;
    3. private final String FILENAME;
    4. private final File FOLDER;
    5. private FileConfiguration config;
    6. private File configFile;
    7.  
    8. public Config(String filename, MainPluginClass instance) {
    9. if(!filename.endsWith(".yml")) {
    10. filename += ".yml";
    11. }
    12. this.FILENAME = filename;
    13. this.PLUGIN = instance;
    14. this.FOLDER = this.PLUGIN.getDataFolder();
    15. this.config = null;
    16. this.configFile = null;
    17. reload();
    18. }
    19.  
    20. public Config(File folder, String filename, MainPluginClass instance) {
    21. if(!filename.endsWith(".yml")) {
    22. filename += ".yml";
    23. }
    24. this.FILENAME = filename;
    25. this.PLUGIN = instance;
    26. this.FOLDER = folder;
    27. this.config = null;
    28. this.configFile = null;
    29. reload();
    30. }
    31.  
    32. public FileConfiguration getConfig() {
    33. if (config == null) {
    34. reload();
    35. }
    36. return config; //Return the config so that the player can save, set, and get from the config.
    37. }
    38.  
    39. public void reload() {
    40. if(!this.FOLDER.exists()) { //Checks if folder exists
    41. try {
    42. if(this.FOLDER.mkdir()) { //Attempts to make a folder if the folder does not exist.
    43. this.PLUGIN.getLogger().log(Level.INFO, "Folder " + this.FOLDER.getName() + " created.");
    44. } else {
    45. this.PLUGIN.getLogger().log(Level.WARNING, "Unable to create folder " + this.FOLDER.getName() + ".");
    46. }
    47. } catch(Exception e) {
    48.  
    49. }
    50. }
    51. configFile = new File(this.FOLDER, this.FILENAME); //Makes the file in the folder
    52. if(!configFile.exists()) { //Creates the file if it doesn't exist
    53. try {
    54. configFile.createNewFile();
    55. } catch(IOException e) {
    56.  
    57. }
    58. }
    59. config = YamlConfiguration.loadConfiguration(configFile); //Loads the file through YAML's system
    60. }
    61.  
    62. public void saveDefaultConfig() {
    63. if (configFile == null) {
    64. configFile = new File(plugin.getDataFolder(), this.FILENAME);
    65. }
    66.  
    67. if (!configFile.exists()) {
    68. plugin.saveResource(this.FILENAME, false); //Gets resource from jar (from the same place as plugin.yml)
    69. }
    70. }
    71. }


    4. Create convenience methods.
    Create Convenience Methods (open)
    Code:java
    1. public class Config {
    2. private final Plugin PLUGIN;
    3. private final String FILENAME;
    4. private final File FOLDER;
    5. private FileConfiguration config;
    6. private File configFile;
    7.  
    8. public Config(String filename, MainPluginClass instance) {
    9. if(!filename.endsWith(".yml")) {
    10. filename += ".yml";
    11. }
    12. this.FILENAME = filename;
    13. this.PLUGIN = instance;
    14. this.FOLDER = this.PLUGIN.getDataFolder();
    15. this.config = null;
    16. this.configFile = null;
    17. reload();
    18. }
    19.  
    20. public Config(File folder, String filename, MainPluginClass instance) {
    21. if(!filename.endsWith(".yml")) {
    22. filename += ".yml";
    23. }
    24. this.FILENAME = filename;
    25. this.PLUGIN = instance;
    26. this.FOLDER = folder;
    27. this.config = null;
    28. this.configFile = null;
    29. reload();
    30. }
    31.  
    32. public FileConfiguration getConfig() {
    33. if(config == null) {
    34. reload();
    35. }
    36. return config;
    37. }
    38.  
    39. public void reload() {
    40. if(!this.FOLDER.exists()) {
    41. try {
    42. if(this.FOLDER.mkdir()) {
    43. this.PLUGIN.getLogger().log(Level.INFO, "Folder " + this.FOLDER.getName() + " created.");
    44. } else {
    45. this.PLUGIN.getLogger().log(Level.WARNING, "Unable to create folder " + this.FOLDER.getName() + ".");
    46. }
    47. } catch(Exception e) {
    48.  
    49. }
    50. }
    51. configFile = new File(this.FOLDER, this.FILENAME);
    52. if(!configFile.exists()) {
    53. try {
    54. configFile.createNewFile();
    55. } catch(IOException e) {
    56.  
    57. }
    58. }
    59. config = YamlConfiguration.loadConfiguration(configFile);
    60. }
    61.  
    62. public void saveDefaultConfig() {
    63. if (configFile == null) {
    64. configFile = new File(this.PLUGIN.getDataFolder(), this.FILENAME);
    65. }
    66.  
    67. if (!configFile.exists()) {
    68. this.PLUGIN.saveResource(this.FILENAME, false);
    69. }
    70. }
    71.  
    72. public void save() { //This method makes it so the you don't have to do config.getConfig().save(configFile); every time you want to save.
    73. if (config == null || configFile == null) {
    74. return;
    75. }
    76. try {
    77. getConfig().save(configFile);
    78. } catch (IOException ex) {
    79. this.PLUGIN.getLogger().log(Level.WARNING, "Could not save config to " + configFile.getName(), ex);
    80. }
    81. }
    82.  
    83. public void set(String path, Object o) { //Shortens the path to set something.
    84. getConfig().set(path, o);
    85. //You could also add a save() here, but I decided not to, just so it only had to save once per time I set things.
    86. }
    87.  
    88. public void setLocation(String path, Location l) { //I found myself putting locations in configs a lot, so I made a generic location setting tool.
    89. getConfig().set(path + ".w", l.getWorld().getName());
    90. getConfig().set(path + ".x", l.getX());
    91. getConfig().set(path + ".y", l.getY());
    92. getConfig().set(path + ".z", l.getZ());
    93. getConfig().set(path + ".yaw", l.getYaw());
    94. getConfig().set(path + ".pitch", l.getPitch());
    95. save();
    96. }
    97.  
    98. public Location getLocation(String path) { //You can get locations that you set with the above method by using this one.
    99. Location l = new Location(Bukkit.getWorld(getConfig().getString(path + ".w")), getConfig().getDouble(path + ".x"), getConfig().getDouble(path + ".y"), getConfig().getDouble(path + ".z"), Float.parseFloat("" + getConfig().getDouble(path + ".yaw")), Float.parseFloat("" + getConfig().getDouble(path + ".pitch")));
    100. return l;
    101. }
    102. }


    Here's the final Config class:
    Final Config class (open)

    Code:java
    1. public class Config {
    2. private final Plugin PLUGIN;
    3. private final String FILENAME;
    4. private final File FOLDER;
    5. private FileConfiguration config;
    6. private File configFile;
    7.  
    8. public Config(String filename, MainPluginClass instance) {
    9. if(!filename.endsWith(".yml")) {
    10. filename += ".yml";
    11. }
    12. this.FILENAME = filename;
    13. this.PLUGIN = instance;
    14. this.FOLDER = this.PLUGIN.getDataFolder();
    15. this.config = null;
    16. this.configFile = null;
    17. reload();
    18. }
    19.  
    20. public Config(File folder, String filename, MainPluginClass instance) {
    21. if(!filename.endsWith(".yml")) {
    22. filename += ".yml";
    23. }
    24. this.FILENAME = filename;
    25. this.PLUGIN = instance;
    26. this.FOLDER = folder;
    27. this.config = null;
    28. this.configFile = null;
    29. reload();
    30. }
    31.  
    32. public FileConfiguration getConfig() {
    33. if(config == null) {
    34. reload();
    35. }
    36. return config;
    37. }
    38.  
    39. public void reload() {
    40. if(!this.FOLDER.exists()) {
    41. try {
    42. if(this.FOLDER.mkdir()) {
    43. this.PLUGIN.getLogger().log(Level.INFO, "Folder " + this.FOLDER.getName() + " created.");
    44. } else {
    45. this.PLUGIN.getLogger().log(Level.WARNING, "Unable to create folder " + this.FOLDER.getName() + ".");
    46. }
    47. } catch(Exception e) {
    48.  
    49. }
    50. }
    51. configFile = new File(this.FOLDER, this.FILENAME);
    52. if(!configFile.exists()) {
    53. try {
    54. configFile.createNewFile();
    55. } catch(IOException e) {
    56.  
    57. }
    58. }
    59. config = YamlConfiguration.loadConfiguration(configFile);
    60. }
    61.  
    62. public void saveDefaultConfig() {
    63. if (configFile == null) {
    64. configFile = new File(this.PLUGIN.getDataFolder(), this.FILENAME);
    65. }
    66.  
    67. if (!configFile.exists()) {
    68. this.PLUGIN.saveResource(this.FILENAME, false);
    69. }
    70. }
    71.  
    72. public void save() {
    73. if (config == null || configFile == null) {
    74. return;
    75. }
    76. try {
    77. getConfig().save(configFile);
    78. } catch (IOException ex) {
    79. this.PLUGIN.getLogger().log(Level.WARNING, "Could not save config to " + configFile.getName(), ex);
    80. }
    81. }
    82.  
    83. public void set(String path, Object o) {
    84. getConfig().set(path, o);
    85. }
    86.  
    87. public void setLocation(String path, Location l) {
    88. getConfig().set(path + ".w", l.getWorld().getName());
    89. getConfig().set(path + ".x", l.getX());
    90. getConfig().set(path + ".y", l.getY());
    91. getConfig().set(path + ".z", l.getZ());
    92. getConfig().set(path + ".yaw", l.getYaw());
    93. getConfig().set(path + ".pitch", l.getPitch());
    94. save();
    95. }
    96.  
    97. public Location getLocation(String path) {
    98. Location l = new Location(Bukkit.getWorld(getConfig().getString(path + ".w")), getConfig().getDouble(path + ".x"), getConfig().getDouble(path + ".y"), getConfig().getDouble(path + ".z"), Float.parseFloat("" + getConfig().getDouble(path + ".yaw")), Float.parseFloat("" + getConfig().getDouble(path + ".pitch")));
    99. return l;
    100. }
    101. }

    Make sure that you have replaced MainPluginClass with the class that extends JavaPlugin in your project.

    Last, but not least, the how to use the Config class:
    Usage (open)

    Code:java
    1. Config c = new Config(player.getUniqueId(), this); //Using "this" only works if it's in the main class, otherwise pass an instance of the main class into the class you're using and use the variable you set it to.
    2. c.set("are-cookies-good", true);
    3. c.set("multiple.lines.possible", true);
    4. c.save();
    5. boolean cookies = c.getConfig().getBoolean().get("are-cookies-good");



    IMPORTANT: If you are making player files, make sure to use player.getUniqueId().toString() as the title, not player.getName(). This is because in Minecraft 1.8, players will be able to change their names, but UUIDs will not change.
     
    luigieai likes this.
  2. Offline

    luigieai

  3. Offline

    jusjus112

    clmcd42
    Nice tutorial :D
    But now i have an problem when im using this tut!
    When im do:> in my Shop.class
    Code:java
    1. Config c = new Config(e.getPlayer().getName(), plugin);

    And i have my setup in the Main.class
    When i get the boolean from this config, it doesnt work.
    The setup works, but not the getBoolean.
    Here is my getBollean in the interActEvent:
    Code:java
    1. boolean cookies = c.getConfig().getBoolean("Guns.uzi");
    2. if (cookies == false) {
    3. e.getPlayer().sendMessage(ChatColor.RED + "Uzi bought!");
    4. c.getConfig().set("Guns.uzi", true);
    5. }else {
    6. e.getPlayer().sendMessage(ChatColor.RED + "Already an uzi in you minigame list!");
    7. }
    8.  

    Onlu when im rightclicking it works, but it dont set it to true!
     
  4. Offline

    Aqua

    jusjus112
    Call the c.save() method after setting the value.
     
    clmcd42 likes this.
  5. Offline

    jusjus112

    Aqua
    What i said, is the set method is working, but the getBoolean not!
     
  6. Offline

    Aqua

    jusjus112
    Sorry, didn't read it correct.
     
  7. Offline

    clmcd42

    jusjus112 First of all, use UUID's. I forgot to say that in my tutorial, but i'll edit it in. Player name changing will be supported in the future, so player files will get screwed up. Second, I'm not entirely sure, but I think that if you call getBoolean before you have actually set the variable, then it will return null. Try setting it to false and then using getBoolean, or you could make your own getBoolean method in the config class that checks if it's returns the boolean unless it's null, and then return false if it's null. Example:
    Code:java
    1. public boolean getBoolean() {
    2. if(getConfig().getBoolean("Guns.uzi") == null) {
    3. return false;
    4. } else {
    5. return getConfig().getBoolean("Guns.uzi");
    6. }
    7. }

    I think that's the problem. Tell me if it doesn't work
     
  8. Offline

    jusjus112

    clmcd42
    I have tried you method, but when im using it it says "The operator == is undefined for the argument type(s) boolean, null". But one more thing, you have with the getUniqueId not .toString.
     
  9. Offline

    clmcd42

    jusjus112 Oh yeah, I forgot that booleans were primitive data types and couldn't be null. I just threw that code together really fast. I agree with Aqua, the error is because you are not saving. I don't think you could know that the set method was working and the get method wasn't, because the set method only executes if the get method is working. Just try adding a c.save() after your set method.
     
Thread Status:
Not open for further replies.

Share This Page