[Util] Plugin File Management Made Easy

Discussion in 'Resources' started by Hollasch, Sep 30, 2013.

?

Will this help you?

  1. Yes..

    4 vote(s)
    80.0%
  2. No..

    1 vote(s)
    20.0%
Thread Status:
Not open for further replies.
  1. Offline

    Hollasch

    Hello everyone!

    I recently created a lightweight class that allows you to have as many files as you please!

    Let's say you wanted to create a file that managed every sign location for sign updating and what not... Instead of creating the file manually and creating reload / save / get methods for it, you could use the FileManager.

    To create a file manager, you must do this...

    Code:java
    1. FileManager manager = new FileManager(somePlugin);


    Once you have created the file manager, you would want to store the file, when you store the file, you can then reference it and modify it. To store the file, you must do this...

    Code:java
    1. String myFileReference = "signs";
    2. String myFileName = "SignsFile";
    3. manager.addConfiguration(myFileReference, myFileName);


    This snippet of code will create a new file called SignsFile.yml if it does not already exist, if it does already exist, it can then be used as a reference. To reference our file, we must use the reference string "signs". FileManager uses a class called BukkitFile, this allows us to call saveFile(), reloadFile(), or getFile(). To get the BukkitFile for the SignsFile.yml, you must do this...

    Code:java
    1. BukkitFile bkFile = manager.getBukkitFile("signs");
    2.  
    3. //We can now call any of the BukkitFile methods for
    4. //the signs file!
    5.  
    6. bkFile.getFile().set("sign.x", 1);
    7. bkFile.saveFile();
    8. bkFile.reloadFile();


    You may also check to see if the file is contained, to do so, you must do this...

    Code:java
    1. if (manager.isConfigurationFile("signs"))
    2. {
    3. //this is true since "signs" is a valid
    4. //reference to a file!
    5. }
    6.  
    7. if (manager.isConfigurationFile("blah"))
    8. {
    9. //this is false since we never added a
    10. //configuration with a reference of
    11. //"blah"
    12. }


    That is about all there is to say about my FileManager! The source code is located below

    Code:java
    1. import java.io.File;
    2. import java.util.HashMap;
    3.  
    4. import org.bukkit.configuration.file.FileConfiguration;
    5. import org.bukkit.configuration.file.YamlConfiguration;
    6. import org.bukkit.plugin.Plugin;
    7.  
    8. public class FileManager
    9. {
    10. private Plugin plugin;
    11. private HashMap<String, BukkitFile> files = new HashMap<String, BukkitFile>();
    12.  
    13. public FileManager(Plugin instance)
    14. {
    15. plugin = instance;
    16. }
    17.  
    18. public void addConfiguration(String reference, String fileName)
    19. {
    20. if (!(files.containsKey(reference)))
    21. {
    22. files.put(reference, new BukkitFile(plugin, fileName));
    23. }
    24. }
    25.  
    26. public BukkitFile getBukkitFile(String reference)
    27. {
    28. if (files.containsKey(reference))
    29. {
    30. return files.get(reference);
    31. }
    32. return null;
    33. }
    34.  
    35. public boolean isConfigurationFile(String reference)
    36. {
    37. return files.containsKey(reference);
    38. }
    39.  
    40. public class BukkitFile
    41. {
    42. private FileConfiguration conf;
    43. private File f;
    44.  
    45. public BukkitFile(Plugin plugin, String name)
    46. {
    47. f = new File(plugin.getDataFolder(), name+".yml");
    48. try
    49. {
    50. if (!f.exists()) f.createNewFile();
    51. }
    52. catch (Exception ex)
    53. {
    54. ex.printStackTrace();
    55. }
    56.  
    57. reloadFile();
    58. }
    59.  
    60. public FileConfiguration getFile()
    61. {
    62. return conf;
    63. }
    64.  
    65. public void saveFile()
    66. {
    67. try { conf.save(f); } catch (Exception ex) { ex.printStackTrace(); }
    68. }
    69.  
    70. public void reloadFile()
    71. {
    72. conf = YamlConfiguration.loadConfiguration(f);
    73. saveFile();
    74. }
    75. }
    76. }
    77.  
     
    Goblom likes this.
  2. Offline

    viper_monster

    Hollasch, I have used the example you gave us and every time I reload the server that file gets reset, a fix for it? :3 I'm too lazy to fix it by myself :p

    This is what I used:
    Code:java
    1. SimpleFileManager mng = new SimpleFileManager(this);
    2.  
    3. String myFileReference = "signs";
    4. String myFileName = "SignsFile";
    5. mng.addConfiguration(myFileReference, myFileName);
    6.  
    7. BukkitFile bkFile = mng.getBukkitFile("signs");
    8.  
    9. bkFile.getFile().set("sign.x", 1);
    10. bkFile.saveFile();
    11. bkFile.reloadFile();
     
  3. Offline

    Hollasch

    Is your file embedded into the plugin? If so, that's the problem.. Otherwise it works fine for me
     
  4. Offline

    viper_monster

    Hollasch, thats not the problem, the problem I that every time I reload or restart the server that file gets overridden with the default values (
    PHP:
    sign:
      
    x1
    )
     
  5. Offline

    Hollasch

    Cause in your code you're doing bkFile.getFile().set("sign.x", 1); //Lol
     
  6. Offline

    monkeymanboy

    I made it so when the player joins there is a config made with there name made (For storing player information) The way I'm retrieving the information and setting it is like this
    Code:java
    1.  
    2. BukkitFile config = mng.getBukkitFile(player.getName());
    3. if(config.getFile().getBoolean("achievements." + player.getName() + ".first")) return;
    4. config.getFile().set("achievements." + player.getName() + ".first", true);

    But I get an error on the middle line whenever it gets triggered
     
  7. Offline

    Ultimate_n00b

    So if it's false you set it to true? But if it's true you do nothing? Why not just set it to true?
     
  8. Offline

    monkeymanboy

    Ultimate_n00b Because I have a system of achievements(I should mention this works all in one config before I switched to this) where they would get the achievement and then this checks if they already have it and such there is code after that also
     
  9. Offline

    Ultimate_n00b

    But.. it doesn't matter if you do the check or not. Either way, you set it to true.
     
  10. Offline

    monkeymanboy

    Ultimate_n00b No, the thing is one that is not the problem and 2 that there is code after that I only want to be run if they don't have it
     
  11. Offline

    Ultimate_n00b

    Check if it's true:​
    True: Don't do anything​
    Ends being true​
    False: Set it to true​
    Ends being true​
    @monkeymanboy the check doesn't matter, it's still true.​
     
  12. Offline

    monkeymanboy

    Ultimate_n00b
    Except I am doing it on PlayerJoinEvent or ShearEvent and stuff like that and I don't want it to announce in the chat that they have earned the achievement every time they do those things because I also have it so that it announces that they earned it and if I remove it like you are suggesting then it will keep saying they earned the achievement
     
  13. Offline

    Ultimate_n00b

    I'm just going to stop arguing now, it's like arguing with a little kid.
     
  14. Offline

    monkeymanboy

    Ultimate_n00b sorry not trying to continue this or anything just trying to explain myself since I probably worded it horribly
    Code:java
    1. @EventHandler
    2. public void WitherAchievement(EntityDeathEvent event){
    3. if(event.getEntity().getKiller() instanceof Player && event.getEntity() instanceof Wither){
    4. Player player = event.getEntity().getKiller();
    5. BukkitFile config = mng.getBukkitFile(player.getName());
    6. if(player.getWorld().getName().startsWith("world") || player.getWorld().getName().equals("MineCloudHard") || player.getWorld().getName().startsWith("PVP"))
    7. if(!config.getFile().getBoolean("achievements.wither")){
    8. config.getFile().set("achievements.wither", true);
    9. Bukkit.broadcastMessage("§6[§2§lMinecloud§6] §b§l" + player.getName() + " has just earned the achievment No rest for the wicked");
    10. config.saveFile();
    11. FireworkEffectPlayer fplayer = new FireworkEffectPlayer();
    12. try {
    13. fplayer.playFirework(player.getWorld(), player.getLocation(), FireworkEffect.builder().with(Type.BURST).withColor(Color.LIME).build());
    14. e.printStackTrace();
    15. } catch (Exception e) {
    16. e.printStackTrace();
    17. }
    18. }
    19. }
    20. }

    That is one of my achievements the thing is if I remove the check and just make it set it to true every time then
    Code:java
    1. Bukkit.broadcastMessage("§6[§2§lMinecloud§6] §b§l" + player.getName() + " has just earned the achievment No rest for the wicked");

    Would fire every time they kill a wither
     
  15. Offline

    Ultimate_n00b

    And yet none of that contains the code I was talking about.
    Code:
    BukkitFile config = mng.getBukkitFile(player.getName());
            if(config.getFile().getBoolean("achievements." + player.getName() + ".first")) return;
            config.getFile().set("achievements." + player.getName() + ".first", true);
     
  16. Offline

    monkeymanboy

    Ultimate_n00b Just done a little different so
    Code:java
    1. if(config.getFile().getBoolean("achievements." + player.getName() + ".first")) return;

    is this
    Code:java
    1. if(!config.getFile().getBoolean("achievements.wither")){

    and it is a different achievement
     
  17. Offline

    monkeymanboy

    Hollasch I still need help with it not working
     
  18. Offline

    monkeymanboy

    Hollasch Any idea what's wrong with it?
     
  19. Offline

    Hollasch

    What's the error message?
     
  20. Offline

    monkeymanboy

    Hollasch I'll get the full error message later since I can't at the moment but if you look above I say what line that it says it's on
     
Thread Status:
Not open for further replies.

Share This Page