[UTIL] Easy Custom Config creation

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

Thread Status:
Not open for further replies.
  1. I wrote a little class that lets you create, save and reload custom YAML files really easy. First, you have to add two classes to your Project (find them a little lower).
    Then, to create one, add this field:
    Code:java
    1. CustomConfig customConfig;

    and in your OnEnable, you initialize it by using
    Code:java
    1. customConfig = new CustomConfig("yourConfigNameHere");

    saving and reloading requires a ConfigHandler instance, created the same way as the Config, but using the ConfigHandler name.
    After that, you can save, reload, save from default and get the CustomConfig's FileConfiguration using the following methods:
    Code:java
    1. <ConfigHandler>.saveCustomConfig(<CustomConfig>);
    2. <ConfigHandler>.reloadCustomConfig(<CustomConfig>);
    3. <ConfigHandler>.saveDefaultConfig(<CustomConfig>);
    4. <ConfigHandler>.getCustomConfig(<CustomConfig>);


    here is the code:
    the code (open)

    Code:java
    1. package ;
    2.  
    3. import java.io.File;
    4.  
    5. import org.bukkit.configuration.file.FileConfiguration;
    6.  
    7. public class Config{
    8. public String name;
    9. public File file;
    10. public FileConfiguration fileConfig;
    11. public Config(String name){
    12. this.name = name;
    13. }
    14. }
    15.  
    16.  

    Code:java
    1. package ;
    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.  
    12. public class ConfigHandler{
    13. MainClass plugin;
    14. public CustomConfig(MainClass instance){
    15. plugin = instance;
    16. }
    17. public FileConfiguration getCustomConfig(Config config) {
    18. if (config.fileConfig == null){
    19. reloadCustomConfig(config);
    20. }
    21. return config.fileConfig;
    22. }
    23. public void reloadCustomConfig(Config config) {
    24. if (config.fileConfig == null) {
    25. config.file = new File(plugin.getDataFolder(), config.name + ".yml");
    26. }
    27. config.fileConfig = YamlConfiguration.loadConfiguration(config.file);
    28.  
    29. InputStream defConfigStream = plugin.getResource(config.name + ".yml");
    30. if (defConfigStream != null) {
    31. @SuppressWarnings("deprecation")
    32. YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
    33. config.fileConfig.setDefaults(defConfig);
    34. }
    35. }
    36. public void saveCustomConfig(Config config) {
    37. if (config.fileConfig == null || config.file == null) {
    38. return;
    39. }
    40. try {
    41. getCustomConfig(config).save(config.file);
    42. } catch (IOException ex) {
    43. plugin.getLogger().log(Level.SEVERE, "Could not save config to " + config.file, ex);
    44. }
    45. }
    46. public void saveDefaultConfig(Config config) {
    47. if (config.file == null) {
    48. config.file = new File(plugin.getDataFolder(), config.name + ".yml");
    49. }
    50. if (!config.file.exists()) {
    51. plugin.saveResource(config.name + ".yml", false);
    52. }
    53. }
    54. }



    I hope its useful! If you've got problems, ask them in the comments! (Or PM me)

    Datdenkikniet
     
  2. Offline

    Sweatyyyy

    Datdenkikniet likes this.
  3. Offline

    monkeymanboy

    I am trying to make it so it generates a config for the player when they log in this is giving me errors
    Code:java
    1. @EventHandler
    2. public void Config(PlayerJoinEvent event){
    3. CustomConfig players = new CustomConfig(event.getPlayer().getName());
    4. players.reloadCustomConfig(players);
    5. }
     

  4. change it to:
    Code:java
    1. @EventHandler
    2. public void Config(PlayerJoinEvent event){
    3. CustomConfig players = new CustomConfig(event.getPlayer().getName());
    4. reloadCustomConfig(players);
    5. }

    and If you are doing this from a seperate class:
    Code:java
    1. Main plugin;
    2. public ToggleExecutor(Main instance) {
    3. plugin = instance;
    4. }
    5. @EventHandler
    6. public void Config(PlayerJoinEvent event){
    7. CustomConfig players = new CustomConfig(event.getPlayer().getName());
    8. plugin.reloadCustomConfig(players);
    9. }
    10.  

    where you should replace Main with the name of the class you pasted my code in.
     
  5. Offline

    sgavster

    Datdenkikniet I'm getting
    The method getResource(String) is undefined for the type CustomConfig
    and
    The method saveResource(String, boolean) is undefined for the type CustomConfig
    How do I fix? :O
     
  6. sgavster
    saveResource and getResource? What do you want to accomplish by using that method?
     
  7. Offline

    sgavster

  8. sgavster what do you want to do with the methods getResource() and saveResource()?
     
  9. Offline

    sgavster

    Datdenkikniet ... T_T
    Code:java
    1. public class CustomConfig{
    2.  
    3. public static QuadularMC plugin;
    4. public CustomConfig(QuadularMC instance)
    5. {
    6. plugin = instance;
    7. }
    8.  
    9. private String name;
    10. private File file;
    11. private FileConfiguration fileConfig;
    12. public CustomConfig(String name){
    13. this.name = name;
    14. }
    15. public FileConfiguration getCustomConfig(CustomConfig config) {
    16. if (config.fileConfig == null){
    17. reloadCustomConfig(config);
    18. }
    19. return config.fileConfig;
    20. }
    21. public void reloadCustomConfig(CustomConfig config) {
    22. if (config.fileConfig == null) {
    23. config.file = new File(plugin.getDataFolder(), config.name + ".yml");
    24. }
    25. config.fileConfig = YamlConfiguration.loadConfiguration(config.file);
    26.  
    27. InputStream defConfigStream =[SIZE=7][COLOR=#ff9900] this.getResource[/COLOR][/SIZE](config.name + ".yml");
    28. if (defConfigStream != null) {
    29. YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
    30. config.fileConfig.setDefaults(defConfig);
    31. }
    32. }
    33. public void saveCustomConfig(CustomConfig config) {
    34. if (config.fileConfig == null || config.file == null) {
    35. return;
    36. }
    37. try {
    38. getCustomConfig(config).save(config.file);
    39. } catch (IOException ex) {
    40. plugin.getLogger().log(Level.SEVERE, "Could not save config to " + config.file, ex);
    41. }
    42. }
    43. public void saveDefaultConfig(CustomConfig config) {
    44. if (config.file == null) {
    45. config.file = new File(plugin.getDataFolder(), config.name + ".yml");
    46. }
    47. if (!config.file.exists()) {
    48. [SIZE=7][COLOR=#ff9900] this.saveResource[/COLOR][/SIZE](config.name + ".yml", false);
    49. }
    50. }
    51. }

    this.saveResource
    this.getResource
     
  10. sgavster what version of java are you using?
     
  11. Offline

    sgavster

    Datdenkikniet the recommended Java installed (Version 7 Update 45).
     
  12. sgavster you should change the this in front of both of the methods to plugin
     
  13. Offline

    sgavster

    Datdenkikniet ok, that fixed it, when it makes the file does it check if it exists?
     
  14. sgavster making the file? Do you mean saving it? Of course it does!
     
  15. Offline

    sgavster

    Datdenkikniet How do I do it in a different class? I can't seem to access it through my main class.
     
  16. sgavster You should put my class in your main one :/
     
  17. Offline

    monkeymanboy

  18. Offline

    sgavster

    Datdenkikniet Why would I do that? I have 1k lines of code in my main class. Guess I'll just make my own files B:
     
  19. sgavster rewrite the name to something else (someting like CusomtConfigClass) and then post my class inside of that, I think that would fix it.
     
  20. valon750 I added the seperate class support :)
     
  21. Offline

    WiggPerson

    Nice, i will probably use this. :)
     
  22. Offline

    valon750

    Datdenkikniet

    Why thank you good sir :)

    Datdenkikniet

    Oh! Quick question...

    I'm wanting to set up a file, that's inside of a folder, inside of the plugins main folder... where as I only see how to make a single file here.. am I missing something?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  23. valon750 you can only create a single file, indeed. I will try to find a way to do this :) Another question for you: are you able to acces the methods of my util in your main class? Because I can't get it to work :/
     
  24. Offline

    valon750

    Datdenkikniet

    Sadly not, I pasted what I needed in to my main class, yet couldn't access the methods in a separate one :(

    I had to past everything I needed in to the same class as what was attempting to access it, that was the only way I could find. Of course now I'm facing the nullpointerexception when trying to set up a file in a place that apparently doesn't exist :p
     
  25. Offline

    DexArio

    Datdenkikniet

    Hi :)
    Thank you very much for your util, it's very useful and it's a big amount of time gained.
    The only problem that I encounter is that (I think) there is no way to break a line (like using a \n but it doesn't work). I've searched solutions on the internet but the only solution that I have found is to use a PrintWriter or things like this.
    Is there a way to break a line or to do a list without modifying the util's code ?

    DexArio
     
  26. DexArio breake a line in what? The name? Because this plugin relies on the YAML thing that's implemented in bukkit.
     
  27. Offline

    DexArio

    For example :
    If I add a location to the path Locations I would do :
    Code:java
    1. getCustomConfig(config).set("Locations", location);


    But the problem is that if I add an other location after the first one it would replace the first location for example:

    If I add a first location the yml would look like:

    Code:
    Locations: Location{world=CraftWorld{name=world},x=-69.40960378217355,y=64.0,z=161.3009534617979,pitch=65.72586,yaw=3.0343482}
    (I have put a random location)

    If I add a second location it would look like:

    Code:
    Locations: Location{world=CraftWorld{name=world},x=5461.40960378217355,y=41.0,z=-555.3009534617979,pitch=65.72586,yaw=3.0343482}
    (I have put an other random location)

    The problem is that the second location overwrites the first instead of just adding it on a new line or adding it after the first location.

    The thing is that I don't want the locations to overwrite the previous location for this, the solution would be to return line.
     
  28. Offline

    valon750

    Datdenkikniet

    See, what I'm hoping for is to have something like...

    Code:
    Player player = event.getPlayer();
    String playername = player.getName();
    CustomConfig players = new CustomConfig(plugin.getDataFolder() + "UserData", playername);
    Which would create a file named after the players username, in a folder named "UserData"
     
  29. DexArio It would be really hard for me to do.
    valon750 I will try to add that, just a little change in the constructor, well, little :s
     
Thread Status:
Not open for further replies.

Share This Page