[UTIL] Easy Custom Config creation

Discussion in 'Resources' started by Datdenkikniet, Oct 26, 2013.

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

    valon750

  2. valon750 its actually pretty hard :s
    actuall, nvm, its not that hard :)
     
  3. Offline

    valon750

    Datdenkikniet

    Don't say that!

    It's crucial I find out how to create a file inside of a folder, inside of the dataFolder, incredibly crucial D:
     
  4. valon750 I am on it, the reloadCustomConfig() and getCustomConfig() are now done :)
    just saveDefaultConfig() and saveCustomConfig() have to be done!
    SHOO its not that easy :(
     
  5. Offline

    valon750

    Datdenkikniet

    Excellent, awesome job!

    Don't forget about creating files inside of another folder, it at all possible would be shweeeeeet!
     
  6. valon750 it is possible, but I don't think I will be able to finish it tonigh (its 21:40)
     
  7. Offline

    valon750

    Datdenkikniet

    Hmm, I have a solution!

    Travel to a location 6 hours behind, perfect!
     
  8. valon750 good idea :) . But, too bad I didn't get to finish it :( but I just thought of something: try to change the name of the config (previously it was "players") to this: "yourdirectory/players" so:
    Code:
    CustomConfig config = new CustomConfig("yourdirectory/config");
    ow and, could you stop tahgging me, because I am watching this thread :)
     
  9. Offline

    valon750

    Hmm, so seems that having it set up like that doesn't bring up any NullPointerExceptions, which is one thing.​
    However, it doesn't actually DO anything... no file is created, even after using saveCustomConfig.​
     
  10. valon750 yeah, i have to fix that.... Use reloadCustomConfig on it, that does work :s
     
  11. Offline

    valon750

    Hmm, well, aaactually, it doesn't work, in the sense of it doesn't do as it's asked.

    I attempts to work, yet brings up a NullPointerException on this line...

    Code:
    config.file = new File(plugin.getDataFolder(), config.name + ".yml");
    this bit of code being in your method.

    Err..

    Anyone alive?

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

    AniKaBa

    Code:java
    1. import org.bukkit.configuration.file.FileConfiguration;
    2. import org.bukkit.configuration.file.YamlConfiguration;
    3. import org.bukkit.entity.Player;
    4. import org.bukkit.event.EventHandler;
    5. import org.bukkit.event.Listener;
    6. import org.bukkit.event.player.PlayerJoinEvent;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. import java.io.File;
    10. import java.io.IOException;
    11. import java.io.InputStream;
    12. import java.util.logging.Level;
    13.  
    14.  
    15. public class Attribute extends JavaPlugin implements Listener {
    16.  
    17. CustomConfig attribute = new CustomConfig ("attribute");
    18. CustomConfig config = new CustomConfig ("config");
    19. CustomConfig healthy = new CustomConfig ("healthy");
    20. CustomConfig race = new CustomConfig ("race");
    21.  
    22. public void onEnable(){
    23.  
    24. saveDefaultCustomConfig(attribute);
    25. saveDefaultCustomConfig(config);
    26. saveDefaultCustomConfig(healthy);
    27. saveDefaultCustomConfig(race);
    28.  
    29. getServer().getPluginManager().registerEvents(this, this);
    30.  
    31. }
    32.  
    33. public void onDisable(){
    34. }
    35.  
    36. public class CustomConfig{
    37. private String name;
    38. private File file;
    39. private FileConfiguration fileConfig;
    40. public CustomConfig(String name){
    41. this.name = name;
    42. }
    43. }
    44.  
    45. public FileConfiguration getCustomConfig(CustomConfig config) {
    46. if (config.fileConfig == null){
    47. reloadCustomConfig(config);
    48. }
    49. return config.fileConfig;
    50. }
    51.  
    52. public void reloadCustomConfig(CustomConfig config) {
    53. if (config.fileConfig == null) {
    54. config.file = new File(getDataFolder(), config.name + ".yml");
    55. }
    56. config.fileConfig = YamlConfiguration.loadConfiguration(config.file);
    57.  
    58. InputStream defConfigStream = this.getResource(config.name + ".yml");
    59. if (defConfigStream != null) {
    60. YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
    61. config.fileConfig.setDefaults(defConfig);
    62. }
    63. }
    64.  
    65. public void saveCustomConfig(CustomConfig config) {
    66. if (config.fileConfig == null || config.file == null) {
    67. return;
    68. }
    69. try {
    70. getCustomConfig(config).save(config.file);
    71. } catch (IOException ex) {
    72. getLogger().log(Level.SEVERE, "Could not save config to " + config.file, ex);
    73. }
    74. }
    75.  
    76. public void saveDefaultCustomConfig(CustomConfig config) {
    77. if (config.file == null) {
    78. config.file = new File(getDataFolder(), config.name + ".yml");
    79. }
    80. if (!config.file.exists()) {
    81. this.saveResource(config.name + ".yml", false);
    82. }
    83. }
    84.  
    85. @EventHandler
    86. public void PlayerJoinConfig(PlayerJoinEvent pje){
    87. Player player = pje.getPlayer();
    88. player.sendMessage("Welcome!!");
    89. String playerName = player.getName();
    90. CustomConfig players = new CustomConfig(playerName);
    91. reloadCustomConfig(players);
    92. }
    93.  
    94. }


    These are my main program, although incidents have occurred, but the players did not create the file, I do not know where the error occurred?

    @valon750 you can try it.

    Code:java
    1. public class PlayerConfig{
    2. private String name;
    3. private File file;
    4. private FileConfiguration fileConfig;
    5.  
    6. public PlayerConfig(String name){
    7. this.name = "plugins\\Attribute\\PlayersData\\" + name;
    8. }
    9. }
    10.  
    11. public FileConfiguration getPlayerConfig(PlayerConfig config) {
    12. if (config.fileConfig == null){
    13. reloadPlayerConfig(config);
    14. }
    15. return config.fileConfig;
    16. }
    17.  
    18. public void reloadPlayerConfig(PlayerConfig config) {
    19. if (config.fileConfig == null) {
    20. config.file = new File(config.name + ".yml");
    21. }
    22. config.fileConfig = YamlConfiguration.loadConfiguration(config.file);
    23.  
    24. InputStream defConfigStream = this.getResource(config.name + ".yml");
    25. if (defConfigStream != null) {
    26. YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
    27. config.fileConfig.setDefaults(defConfig);
    28. }
    29. }
    30.  
    31. public void savePlayerConfig(PlayerConfig config) {
    32. if (config.fileConfig == null || config.file == null) {
    33. return;
    34. }
    35. try {
    36. getPlayerConfig(config).save(config.file);
    37. } catch (IOException ex) {
    38. getLogger().log(Level.SEVERE, "Could not save config to " + config.file, ex);
    39. }
    40. }
    41.  
    42. public void saveDefaultPlayerConfig(PlayerConfig config) {
    43. if (config.file == null) {
    44. config.file = new File(config.name + ".yml");
    45. }
    46. if (!config.file.exists()) {
    47. this.saveResource("playersdata.yml", false);
    48. File file = new File("plugins\\Attribute\\playersdata.yml");
    49. file.renameTo(new File(config.name + ".yml"));
    50. }
    51. }
    52.  
    53. @EventHandler
    54. public void PlayerJoinConfig(PlayerJoinEvent pje) throws IOException {
    55. PlayerConfig players = new PlayerConfig(pje.getPlayer().getName());
    56. saveDefaultPlayerConfig(players);
    57. }


    OK I GOT IT!!
     
  13. Offline

    patey

    I apologize if this is too old to be posting in-

    I'm trying to use this in a plugin for the recommended bukkit 1.7.10 build (if that matters), and placing it in a seperate class but my plugin is laid out differently so either I've done something wrong or I just can't figure out how to initialize the config in the onEnable.

    I had to remove this from this class:
    Code:java
    1. MainClassName plugin;
    2. public CustomConfigClass(MainClassName instance) {
    3. plugin = instance;
    4. }


    because my main class already defines the instance for listeners/commands in different classes like this:
    (in main class)
    Code:java
    1. private static DwarfBorder instance = null;
    2.  
    3. public void onEnable() {
    4. instance = this;
    5. registerEvents(this,new playerEvents());
    6. initconfig();
    7. initCustom();
    8. getLogger().info("DwarfBorder Initialized");
    9. }
    10.  
    11. public static DwarfBorder getInstance() {
    12. return instance;
    13. }


    I then replaced plugin in the class in this tutorial with DwarfBorder.getInstance().getDataFolder() for example as I do in my other classes when referring to the plugin.

    my main class:

    Code:java
    1. package com.ntcreations.DwarfBorder;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.configuration.file.FileConfiguration;
    8. import org.bukkit.configuration.file.YamlConfiguration;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class DwarfBorder extends JavaPlugin{
    13. private static DwarfBorder instance = null;
    14.  
    15. public void onEnable() {
    16. instance = this;
    17. registerEvents(this,new playerEvents());
    18. initconfig();
    19. getLogger().info("DwarfBorder Initialized");
    20. }
    21.  
    22. public static DwarfBorder getInstance() {
    23. return instance;
    24. }
    25.  
    26. public void onDisable() {
    27. getLogger().info("DwarfBorder Unloaded");
    28. }
    29.  
    30. private void initconfig(){
    31. FileConfiguration config = getConfig();
    32.  
    33. config.addDefault("Borders.Border1.Name", "border1");
    34. config.addDefault("Borders.Border1.Radius", 20);
    35. config.addDefault("Borders.Border1.Center.X", 0);
    36. config.addDefault("Borders.Border1.Center.Z", 0);
    37. config.addDefault("Borders.Border1.BarrierMessage", "You can't go there!");
    38.  
    39. config.options().copyDefaults(true);
    40. saveConfig();
    41. }
    42.  
    43.  
    44. public static void registerEvents(org.bukkit.plugin.Plugin plugin, Listener... listeners) {
    45. for (Listener listener : listeners) {
    46. Bukkit.getServer().getPluginManager().registerEvents(listener, plugin);
    47. }
    48. }
    49.  
    50. }


    The class from this tutorial:

    Code:java
    1. package com.ntcreations.DwarfBorder;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5. import java.io.InputStream;
    6. import java.util.logging.Level;
    7.  
    8. import org.bukkit.configuration.file.FileConfiguration;
    9. import org.bukkit.configuration.file.YamlConfiguration;
    10.  
    11. public class customConfig{
    12.  
    13. public class CustomConfig{
    14. private String name;
    15. private File file;
    16. private FileConfiguration fileConfig;
    17. public CustomConfig(String name){
    18. this.name = name;
    19. }
    20. }
    21.  
    22. public FileConfiguration getCustomConfig(CustomConfig config) {
    23. if (config.fileConfig == null){
    24. reloadCustomConfig(config);
    25. }
    26. return config.fileConfig;
    27. }
    28.  
    29. public void reloadCustomConfig(CustomConfig config) {
    30.  
    31. if (config.fileConfig == null) {
    32. config.file = new File(DwarfBorder.getInstance().getDataFolder(), config.name + ".yml");
    33. }
    34. config.fileConfig = YamlConfiguration.loadConfiguration(config.file);
    35.  
    36. InputStream defConfigStream = DwarfBorder.getInstance().getResource(config.name + ".yml");
    37. if (defConfigStream != null) {
    38. @SuppressWarnings("deprecation")
    39. YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
    40. config.fileConfig.setDefaults(defConfig);
    41. }
    42. }
    43.  
    44. public void saveCustomConfig(CustomConfig config) {
    45. if (config.fileConfig == null || config.file == null) {
    46. return;
    47. }
    48. try {
    49. getCustomConfig(config).save(config.file);
    50. } catch (IOException ex) {
    51. DwarfBorder.getInstance().getLogger().log(Level.SEVERE, "Could not save config to " + config.file, ex);
    52. }
    53. }
    54.  
    55. public void saveDefaultConfig(CustomConfig config) {
    56. if (config.file == null) {
    57. config.file = new File(DwarfBorder.getInstance().getDataFolder(), config.name + ".yml");
    58. }
    59. if (!config.file.exists()) {
    60. DwarfBorder.getInstance().saveResource(config.name + ".yml", false);
    61. }
    62. }
    63. }
    64.  
     
  14. patey gah, let me update this old crap (I've added and removed some things, it should work better, see if it does for you, too)
     
Thread Status:
Not open for further replies.

Share This Page