Bukkit's Yaml Configuration Tutorial

Discussion in 'Resources' started by DomovoiButler, Oct 24, 2011.

?

Is my first Guide good?

yes 97 vote(s) 69.3%
no 11 vote(s) 7.9%
meh, needs improvement 32 vote(s) 22.9%
  1. Offline

    DomovoiButler

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Introduction
    I made this guide because I have not seen any better tutorial on the new config system of bukkit yet. Some people might have problems on the new config system, i'm having some problems too, but I also cannot make a guide as the apidocs is not fixed. But recently i have found out that maven can make javadocs so i made one. And then i studied how the new configuration works.
    xD, Anyway, this guide does not consists of the defaults method or whatever you call it. This guide is more advance or something like that. This guide will be using the InputStreams. Another reason i made this because i have multiple yaml configurations with different names, not just one.

    NOTE: you are required to create or include the config.yml or any yamls(if you have multiple configs) in your jar when u export!

    Imports
    you need to import these
    Code:
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    i know that its a bit too much, but i have to post the needed imports for these to work

    Declarations
    declare some main variables(do not initialize)
    this will go just below your class declaration(not on onEnable or onDisable)

    Code:
    public class PluginName extends JavaPlugin{
    
        File configFile;
        File groupsFile;
        File historyFile;
        FileConfiguration config;
        FileConfiguration groups;
        FileConfiguration history;
    
        public void onEnable(){
    
        }
        public void onDisable(){
    
        }
    }
    Initializing
    first we need to initialize the Files

    Code:
    public void onEnable(){
        configFile = new File(getDataFolder(), "config.yml");
        groupsFile = new File(getDataFolder(), "groups.yml");
        historyFile = new File(getDataFolder(), "history.yml");
    }
    in here, we made a new virtual file in the /plugins/PluginName/(config.yml/groups.yml/history.yml)
    yeah...i said virtual cause its not a real file yet, it will be only real if there is already a yml in the folder(maybe because the plugin has already run last time?)

    then we copy the 'defaults' from your jar to the initialized file using the 'InputStream getResource'

    Code:
    public void onEnable(){
        try {
            firstRun();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void firstRun() throws Exception {
        if(!configFile.exists()){
            configFile.getParentFile().mkdirs();
            copy(getResource("config.yml"), configFile);
        }
        if(!groupsFile.exists()){
            groupsFile.getParentFile().mkdirs();
            copy(getResource("groups.yml"), groupsFile);
        }
        if(!usersFile.exists()){
            usersFile.getParentFile().mkdirs();
            copy(getResource("users.yml"), usersFile);
        }
        if(!historyFile.exists()){
            historyFile.getParentFile().mkdirs();
            copy(getResource("history.yml"), historyFile);
        }
    }
    private void copy(InputStream in, File file) {
        try {
            OutputStream out = new FileOutputStream(file);
            byte[] buf = new byte[1024];
            int len;
            while((len=in.read(buf))>0){
                out.write(buf,0,len);
            }
            out.close();
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    to those who dont understand what I did here, let me explain
    I call the method firstRun. In this method, the plugin will check if the yamls exists in the folder /plugins/<pluginName>/. If they do not exists, the folder /plugins/<pluginName>/ will be created and then call the copy() method. This method will copy the contents of the yaml found in jar to the /plugin/<pluginName>/*.yml . This method will also copy the comments (####), so feel free to add lots of comments on your yaml.

    then we intialize the FileConfiguration using new YamlConfiguration(); and the load it
    Code:
    public void onEnable() {
        config = new YamlConfiguration();
        groups = new YamlConfiguration();
        users = new YamlConfiguration();
        history = new YamlConfiguration();
        loadYamls();
    }
    Loading and Saving

    Code:
    public void saveYamls() {
        try {
            config.save(configFile);
            groups.save(groupsFile);
            history.save(historyFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void loadYamls() {
        try {
            config.load(configFile);
            groups.load(groupsFile);
            history.load(historyFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    in here, you make a method called loadYamls and saveYamls and
    use .save and .load to all your FileConfigurations to its Corresponding File
    and call loadYamls(); on onEnable or wherever to load all your yaml
    and call saveYamls(); on onDisable or wherever to save all your yaml

    Getting and Setting Infos
    this is the easiest part, GETTING INFOS.
    just use config.getBoolean(String path, Boolean def); , groups.getString (String path, String def): , history.getList(String path, List def); or config.set(String path, Object value).
    its all self explanatory, i cant and will not explain every gets and sets unless asked.
    go here to check how to use gets and sets

    Example MainClass.java
    Code:
    package ex.ample.mainclass;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class MainClass extends JavaPlugin{
    
        File configFile;            ////
        File groupsFile;            //  declare Files
        File usersFile;             //      (how many yamls are we dealing with?)
        File historyFile;           ////
    
        FileConfiguration config;   ////
        FileConfiguration groups;   //  declare FileConfigurations (each File declared above
        FileConfiguration users;    //      has to have one FileConfiguration)
        FileConfiguration history;  ////
    
        @Override
        public void onDisable() {
            // in here, we need to save all our yamls if we have not yet,
            //  this maybe also a critical part cause some methods you might have forgotten
            //  to use the saveYamls(); on some of your methods that uses config.set or *.set(path,value)
            //  so we will use saveYamls(); method here to auto save what we have done
            saveYamls();
        }
    
        @Override
        public void onEnable() {
            // for error free purposes, you need to have all this on top of your onEnable first
            //   and then do whatever you want to do with your plugin like
            //   Events Registering and .gets and .sets just below all this
    
            // first we have to initialize all Files and FileConfigurations
            configFile = new File(getDataFolder(), "config.yml");   ////
            groupsFile = new File(getDataFolder(), "groups.yml");   //  getDataFolder() means the folder /plugins/<pluginName>/
            usersFile = new File(getDataFolder(), "users.yml");     //  creates virtual Files( /plugins/MainClass/*.yml )
            historyFile = new File(getDataFolder(), "history.yml"); ////
    
            // then we use firstRun(); method
            try {
                firstRun();
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            // and we declare the FileConfigurations using YamlConfigurations and
            // then we just use loadYamls(); method
            // this is the critical part, this is needed cause if we do not use this,
            // it will read from the yml located at your jar, not in /plugins/<pluginName>/*yml.
            config = new YamlConfiguration();
            groups = new YamlConfiguration();
            users = new YamlConfiguration();
            history = new YamlConfiguration();
            loadYamls();
    
            /*
             * do whatever you wantyour plugin to do below this comment
             *     like your listeners, Loggers and stuff
             */
        }
    
        /*
         * in this firstRun(); method, we checked if each File that we initialized does not exists
         *  if it does not exists, we load the yaml located at your jar file, then save it in
         *  the File(/plugins/<pluginName>/*.yml)
         * only needed at onEnable()
         */
        private void firstRun() throws Exception {
            if(!configFile.exists()){                        // checks if the yaml does not exists
                configFile.getParentFile().mkdirs();         // creates the /plugins/<pluginName>/ directory if not found
                copy(getResource("config.yml"), configFile); // copies the yaml from your jar to the folder /plugin/<pluginName>
            }
            if(!groupsFile.exists()){
                groupsFile.getParentFile().mkdirs();
                copy(getResource("groups.yml"), groupsFile);
            }
            if(!usersFile.exists()){
                usersFile.getParentFile().mkdirs();
                copy(getResource("users.yml"), usersFile);
            }
            if(!historyFile.exists()){
                historyFile.getParentFile().mkdirs();
                copy(getResource("history.yml"), historyFile);
            }
        }
    
        /*
         * this copy(); method copies the specified file from your jar
         *     to your /plugins/<pluginName>/ folder
         */
        private void copy(InputStream in, File file) {
            try {
                OutputStream out = new FileOutputStream(file);
                byte[] buf = new byte[1024];
                int len;
                while((len=in.read(buf))>0){
                    out.write(buf,0,len);
                }
                out.close();
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /*
         * in here, each of the FileConfigurations loaded the contents of yamls
         *  found at the /plugins/<pluginName>/*yml.
         * needed at onEnable() after using firstRun();
         * can be called anywhere if you need to reload the yamls.
         */
        public void loadYamls() {
            try {
                config.load(configFile); //loads the contents of the File to its FileConfiguration
                groups.load(groupsFile);
                users.load(usersFile);
                history.load(historyFile);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /*
         * save all FileConfigurations to its corresponding File
         * optional at onDisable()
         * can be called anywhere if you have *.set(path,value) on your methods
         */
        public void saveYamls() {
            try {
                config.save(configFile); //saves the FileConfiguration to its File
                groups.save(groupsFile);
                users.save(usersFile);
                history.save(historyFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    }

    This post has been edited 7 times. It was last edited by DomovoiButler Nov 3, 2011.
  2. Offline

    Windwaker

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Rats, beat me to it.
  3. Offline

    DomovoiButler

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    wth happened...i just edited some stuff...wait a sec
    EDIT: done...phew

    This post has been edited 1 time. It was last edited by DomovoiButler Oct 24, 2011.
  4. Offline

    DomovoiButler

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME

    This post has been edited 8 times. It was last edited by DomovoiButler Oct 28, 2011.
    McMhz and nicholasntp like this.
  5. Offline

    Sagacious_Zed Bukkit Docs

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    @DomovoiButler
    You really should not be throwing a generic exception.

    Additional drawbacks to this. You do not load default values, if keys are missing from an existing yaml. This could be a problem for upgrading for instance.
  6. Offline

    DomovoiButler

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    @Sagacious_Zed
    i loaded it, but not using it...it just to copy all the yml from the jar to the file on your /plugins/PluginName if its not been copied yet...the real load is the loadYamls

    EDIT: and also...i dont use defaults

    This post has been edited 2 times. It was last edited by DomovoiButler Oct 24, 2011.
  7. Offline

    Windwaker

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    But, but why? Not using them is a bit counter intuitive don't you think?
  8. Offline

    DomovoiButler

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    i just done like 'em
  9. Offline

    Sagacious_Zed Bukkit Docs

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    @DomovoiButler
    If you have a copy of the yaml in your jar, then it is the default yaml, thus you are writing out the default. Thus you are using defaults, without actually loading them as such.

    EDIT: also why ignore JavaPlugin's getConfig method. It saves the headache of going through all these setup and teardown steps, at least for config.yml

    This post has been edited 3 times. It was last edited by Sagacious_Zed Oct 24, 2011.
  10. Offline

    DomovoiButler

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    @Sagacious_Zed
    what i meant by not using the defaults is like copyDefaults and stuff
    and also this guide is dealing with multiple configs...if i use copyDefaults it would take a lot of effort and also time...and another thing, copyDefaults is for config.yml only(i think, thats what i understand in the apidocs)
    it would be easier if you just type all the necessay keys and values and also #####comments on a yml...and not do addDefaults and stuff
  11. Offline

    Windwaker

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
  12. Offline

    Sagacious_Zed Bukkit Docs

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    @DomovoiButler
    No, I am not advocating the use of a whole slew of calls to addDefaults. That would be a really bad idea. Rather a single call to setDefaults, much like how Bukkit does getConfig. Although it is much more verbose, an example method can be found in the following wiki article. http://wiki.bukkit.org/Introduction_to_the_New_Configuration. With the setup found on that page, it allows you to use getConfig and getYourConfig consistently.
  13. Offline

    DomovoiButler

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    @Sagacious_Zed i already went in there, and the advanced topic isnt there while ago, is that part of wiki yours?or did someone just added it? and also...the advance topic doesnt make any sense to me..and by the coding of advance topic...it seems that the coder of the advance topic rushing to code

    This post has been edited 1 time. It was last edited by DomovoiButler Oct 24, 2011.
  14. Offline

    Sagacious_Zed Bukkit Docs

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    @DomovoiButler
    Yes, I agree that wording needs some more work. The reason why I put of writing it, is that I could not really think of a good way of explaining it.
  15. Offline

    DomovoiButler

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    i dont recommend that too, i use the IOException and the other exception i cant remember it right...but thats not the main thing about this config...developers know what they should do about it...this is just an example
  16. Offline

    Sagacious_Zed Bukkit Docs

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    @DomovoiButler
    Except, that people are just gonna copy pasta verbatim, and it is going to cause all sorts of mess when it doesn't work for them.
  17. Offline

    DomovoiButler

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    @Segacious_Zed
    lol...all sorts of mess?really...dude its just the Exception is the problem...and really?its not much of a problem? and what kind of devs doesn't know how to deal with exceptions?they are better off reading java tutorials first and then study bukkit api, then start creating plugins
    and thanks for making this thread alive by replying to this...xD
  18. Offline

    Sagacious_Zed Bukkit Docs

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    @DomovoniButler
    The kind of people who know enough java just to copy it into the right section. And trust me, it happens quite frequently.
  19. Offline

    nicholasntp

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    I've always needed to know about configs. Thanks!
  20. Offline

    thehutch

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    @DomovoiButler Ok well I followed this tutorial but where do I put my resources folder because I get NPE's because my YAML's cant being detected. Please help your my only hope :p

    This post has been edited 1 time. It was last edited by thehutch Oct 25, 2011.
  21. Offline

    DomovoiButler

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    @thehutch
    do you mean the yamls? think of the yamls like your plugin.yml and put them in your jar.
    also u need to use loadYamls(); from my toturial.. and then use whatevs.get or .set

    abit unrelated question:by your use of 'resources folder', are you using maven?

    This post has been edited 1 time. It was last edited by DomovoiButler Oct 25, 2011.
  22. Offline

    thehutch

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Ok so I basically just put them in the same place as my plugin.yml? ok I followed your tutorial so everything works so far but the file generation ill report later and no I dont use maven
  23. Offline

    thehutch

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    oh and here is my error so you know what it is:
    its a NPE on the if(!friendsFile.exists()) {}
    Code:
        private void firstRun() throws Exception {
    
            if (!friendsFile.exists()) {
                friends.load(this.getResource("friends.yml"));
                friends.save(friendsFile);
            }
            if (!mailBoxFile.exists()) {
                mailBox.load(this.getResource("mailbox.yml"));
                mailBox.save(mailBoxFile);
            }
            if (!configFile.exists()) {
                config.load(this.getResource("config.yml"));
                config.save(configFile);
            }
        }

    This post has been edited 1 time. It was last edited by thehutch Oct 25, 2011.
  24. Offline

    DomovoiButler

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    it should work...hmmm, maybe it will work(dont be discourage by this)...i checked my maven generated apidoc several times and i know it will work great...reason that i have not tested this yet cause im currently making another tutorial. cant say what is it yet, but hope i finish it in a week or so
  25. Offline

    DomovoiButler

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    ah....that error?i think i know what error is that...NullPointer right? another thing why i need to test this tutorial...but you dont have to worry about that error, its because the server doesnt have a yml in the /plugins/PluginName/ yet...so when u run the server the second time and the yml is already in the folder...it will not be sending error...and if you really dont like this... just use
    if(configFile == null){} instead of if(!configFile.exists){}

    This post has been edited 1 time. It was last edited by DomovoiButler Oct 25, 2011.
  26. Offline

    thehutch

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    hmm well here is my mainclass dw about the mess alot of the methods are in :p
    Code:brainfuck
    1. package org.xdevs.FriendsList;
    2. import java.io.File;
    3. import java.util.HashSet;
    4. import java.util.List;
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.OfflinePlayer;
    8. import org.bukkit.World;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.configuration.file.FileConfiguration;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.Event;
    13. import org.bukkit.plugin.Plugin;
    14. import org.bukkit.plugin.PluginManager;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16. import org.xdevs.FriendsList.commands.AddFriendCommands;
    17. import org.xdevs.FriendsList.commands.MailBoxCommands;
    18. import com.gmail.nossr50.mcMMO;
    19. import com.herocraftonline.dev.heroes.Heroes;
    20. public class FriendsList extends JavaPlugin {
    21.  
    22. /*
    23. * Variables
    24. */
    25. public File configFile;
    26. public File friendsFile;
    27. public File mailBoxFile;
    28. public File ignoreListFile;
    29. protected FileConfiguration config;
    30. protected FileConfiguration friends;
    31. protected FileConfiguration mailBox;
    32. protected FileConfiguration ignoreList;
    33.  
    34.  
    35. @Override
    36. public void onDisable() {
    37. System.out.println("[FriendsList] Disabled version: " + getDescription().getVersion());
    38. }
    39. @Override
    40. public void onEnable() {
    41.  
    42. try {
    43. firstRun();
    44. } catch (Exception e) {
    45. System.out.println("Exception :O error error error");
    46. e.printStackTrace();
    47. }
    48.  
    49. setupMcmmo();
    50. setupHeroes();
    51. configFile = new File(this.getDataFolder(), "config.yml");
    52. friendsFile = new File(this.getDataFolder(), "friends.yml");
    53. mailBoxFile = new File(this.getDataFolder(), "mailbox.yml");
    54. ignoreListFile = new File(this.getDataFolder(), "ignoreList.yml");
    55. getCommand("xFriends").setExecutor(new AddFriendCommands(this));
    56. getCommand("mailbox").setExecutor(new MailBoxCommands(this));
    57. PluginManager pm = getServer().getPluginManager();
    58. pm.registerEvent(Event.Type.PLAYER_INTERACT_ENTITY, new Plistener(this) , Event.Priority.Normal, this);
    59. pm.registerEvent(Event.Type.PLAYER_LOGIN, new Plistener(this) , Event.Priority.Normal, this);
    60. pm.registerEvent(Event.Type.PLAYER_JOIN, new Plistener(this), Event.Priority.Normal, this);
    61. System.out.println("[FriendsList] Enabled version: " + getDescription().getVersion());
    62. System.out.println("[FriendsList] created by" + getDescription().getAuthors());
    63. }
    64. /*
    65. * Generates the files
    66. */
    67. private void firstRun() throws Exception {
    68.  
    69. if (!configFile.exists()) {
    70. config.load(this.getResource("config.yml"));
    71. config.save(configFile);
    72. }
    73. if (!friendsFile.exists()) {
    74. friends.load(this.getResource("friends.yml"));
    75. friends.save(friendsFile);
    76. }
    77. if (!mailBoxFile.exists()) {
    78. mailBox.load(this.getResource("mailbox.yml"));
    79. mailBox.save(mailBoxFile);
    80. }
    81. if (!ignoreListFile.exists()) {
    82. ignoreList.load(this.getResource("ignoreList.yml"));
    83. ignoreList.save(ignoreListFile);
    84. }
    85. }
    86.  
    87. public void saveYamls() {
    88. try {
    89. friends.save(configFile);
    90. mailBox.save(mailBoxFile);
    91. config.save(configFile);
    92. ignoreList.save(ignoreListFile);
    93. } catch (Exception ex){
    94. ex.printStackTrace();
    95. }
    96. }
    97.  
    98. public void loadYamls() {
    99. try {
    100. config.load(configFile);
    101. friends.load(configFile);
    102. mailBox.load(mailBoxFile);
    103. ignoreList.load(ignoreListFile);
    104. } catch (Exception ex){
    105. ex.printStackTrace();
    106. }
    107. }
    108. @SuppressWarnings("unchecked")
    109. public void writeMessageToMailBox(String msg, String player) {
    110. mailBox.getList("Mail." + player).add(msg);
    111. }
    112.  
    113. public void readFromMailBox(String player) {
    114.  
    115.  
    116. }
    117.  
    118. @SuppressWarnings("unchecked")
    119. public List<String> getMailBox(String mailowner) {
    120. return mailBox.getList("mail." + mailowner);
    121. }
    122.  
    123.  
    124.  
    125. /*
    126. * Gets every player that has ever joined
    127. */
    128. public HashSet<OfflinePlayer> getAllExistingPlayers() {
    129.  
    130. List<World> worlds = Bukkit.getServer().getWorlds();
    131. HashSet<OfflinePlayer> allPlayers = new HashSet<OfflinePlayer>();
    132.  
    133. for (World w : worlds) {
    134. File f = new File(w.getName() + File.separator + "players" + File.separator);
    135.  
    136. for (File playerFile : f.listFiles()) {
    137. allPlayers.add(Bukkit.getServer().getOfflinePlayer(playerFile.getName().substring(0, playerFile.getName().length() - 4)));
    138. }
    139. }
    140. return allPlayers;
    141. }
    142.  
    143. /**
    144. * Creates friends.txt
    145.  
    146. public void loadFile() throws IOException {
    147.  
    148. File file = new File(this.getDataFolder() + File.separator + "Friends.txt");
    149.  
    150. if (!file.exists()) {
    151.  
    152. try {
    153. file.createNewFile();
    154. } catch (IOException ex) {
    155. System.out.println("[FriendsList] Error creating Friends.txt");
    156. ex.printStackTrace();
    157. }
    158. BufferedWriter bw = new BufferedWriter(new FileWriter(file));
    159. bw.write("stuff");
    160. bw.close();
    161. }
    162.  
    163. }
    164.  
    165. /**
    166. * Help Message
    167. **/
    168. public void helpMessage(CommandSender cs) {
    169.  
    170. cs.sendMessage(ChatColor.GOLD + "========= xFriends =========");
    171. cs.sendMessage(ChatColor.GREEN + "");
    172. cs.sendMessage(ChatColor.GREEN + "");
    173. cs.sendMessage(ChatColor.GREEN + "");
    174. cs.sendMessage(ChatColor.GREEN + "");
    175. cs.sendMessage(ChatColor.GREEN + "");
    176. cs.sendMessage(ChatColor.GOLD + "");
    177.  
    178. }
    179. // converts a string into a player
    180. public Player stringToPlayer(String player) {
    181. return this.getServer().getPlayer(player);
    182. }
    183.  
    184.  
    185.  
    186.  
    187.  
    188. /**
    189. * mcMMO Setup
    190. **/
    191. public mcMMO mcmmo = null;
    192. public boolean setupMcmmo() {
    193.  
    194. Plugin p = this.getServer().getPluginManager().getPlugin("mcMMO");
    195.  
    196. if (p !=null) {
    197. mcmmo = (mcMMO)p;
    198. System.out.println("[FriendsList] mcMMO has been detected!");
    199. // if plugin "mcMMO" does exist then cast the mcmmo plugins over to mcmmo
    200. }
    201. return false;
    202. }
    203. public mcMMO getMcmmo() {
    204. return mcmmo;
    205. // returns mcmmo plugin
    206. }
    207.  
    208.  
    209. /*
    210. * Heroes setup
    211. */
    212. public Heroes heroes = null;
    213. public boolean setupHeroes() {
    214. Plugin p = this.getServer().getPluginManager().getPlugin("Heroes");
    215. if (p != null) {
    216. heroes = (Heroes)p;
    217. System.out.println("[FriendsList] Heroes has been detected!");
    218. // if plugin "Heroes" does exist then cast the heroes plugin over to heroes
    219. }
    220. return false;
    221. }
    222. public Heroes getHeroes() {
    223. return heroes;
    224. // returns heroes plugin
    225. }
    226.  
    227. @SuppressWarnings("unchecked")
    228. public void sendMessageToAllFriends(Player p, String msg) {
    229.  
    230. p = this.getServer().getPlayer(p.toString());
    231. List<String> f = getConfig().getList("Friends." + p.toString());
    232.  
    233. p.sendMessage("A message has been sent to your friends");
    234.  
    235. for (String player : f) {
    236. Player pl = this.stringToPlayer(player);
    237.  
    238. if (pl.isOnline()) {
    239. pl.sendMessage(msg);
    240. } else {
    241. p.sendMessage(ChatColor.BLUE + "The following players are offline and cannot recieve your message");
    242.  
    243. for (int x=0 ; x<= f.size() ; x++) {
    244. p.sendMessage(pl.toString() + ChatColor.GREEN + ", ");
    245. }
    246. }
    247. }
    248. }
    249. }
  27. Offline

    thehutch

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    hmm the error has moved now thanks but to this line now:
    Code:
    config.load(this.getResource("config.yml"));
  28. Offline

    DomovoiButler

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    u didnt really follow the tutorial really good...the arranging is not right...let me edit an example later cause im gonna go eat(i mean add an example later)... the error is because u run the firstRun(); before initializing the yamlFiles.
    u need to initialize the
    1. configFile = new File(this.getDataFolder(), "config.yml");
    2. friendsFile = new File(this.getDataFolder(), "friends.yml");
    3. mailBoxFile = new File(this.getDataFolder(), "mailbox.yml");
    4. ignoreListFile = new File(this.getDataFolder(), "ignoreList.yml");
    then use the first run after the initializing

    This post has been edited 1 time. It was last edited by DomovoiButler Oct 25, 2011.
  29. Offline

    thehutch

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Slap me please just do it :p facepalm :(
    <=== is stupid :(
  30. Offline

    thehutch

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    hmm well there are no more errors but the files are not generating now :( here is the updated version:
    Code:php
    1. public
    2. class FriendsList extends JavaPlugin {
    3. /*
    4. * Variables
    5. */
    6.  
    7. public File configFile;
    8. public File friendsFile;
    9. public File mailBoxFile;
    10. public File ignoreListFile;
    11. protected FileConfiguration config;
    12. protected FileConfiguration friends;
    13. protected FileConfiguration mailBox;
    14. protected FileConfiguration ignoreList;
    15. @Override
    16. public void onDisable() {
    17. out.println("[FriendsList] Disabled version: " + getDescription().getVersion());
    18. }
    19.  
    20. @Override
    21. public void onEnable() {
    22. configFile = new File(this.getDataFolder() + File.separator + "config.yml");
    23. friendsFile = new File(this.getDataFolder() + File.separator + "friends.yml");
    24. mailBoxFile = new File(this.getDataFolder() + File.separator + "mailbox.yml");
    25. ignoreListFile = new File(this.getDataFolder() + File.separator + "ignoreList.yml");
    26. try {
    27. firstRun();
    28. }
    29. catch (Exception e) {
    30. out.println("Exception :O error error error");
    31. e.printStackTrace();
    32. }
    33.  
    34. setupMcmmo();
    35. setupHeroes();
    36. getCommand(
    37. "xFriends").setExecutor(new AddFriendCommands(this));
    38. getCommand(
    39. "mailbox").setExecutor(new MailBoxCommands(this));
    40. PluginManager pm = getServer().getPluginManager();
    41. pm.registerEvent(Event.Type.
    42. PLAYER_INTERACT_ENTITY, new Plistener(this) , Event.Priority.Normal, this);
    43. pm.registerEvent(Event.Type.
    44. PLAYER_LOGIN, new Plistener(this) , Event.Priority.Normal, this);
    45. pm.registerEvent(Event.Type.
    46. PLAYER_JOIN, new Plistener(this), Event.Priority.Normal, this);
    47. out.println("[FriendsList] Enabled version: " + getDescription().getVersion());
    48. out.println("[FriendsList] created by " + getDescription().getAuthors());
    49. }
    50.  
    51. /*
    52. * Generates the files
    53. */
    54.  
    55. private void firstRun() throws Exception {
    56. if (configFile == null) {
    57. config.load(getResource("config.yml"));
    58. config.save(configFile);
    59. }
    60.  
    61. if (friendsFile == null) {
    62. friends.load(getResource("friends.yml"));
    63. friends.save(friendsFile);
    64. }
    65.  
    66. if (mailBoxFile == null) {
    67. mailBox.load(getResource("mailbox.yml"));
    68. mailBox.save(mailBoxFile);
    69. }
    70.  
    71. if (ignoreListFile == null) {
    72. ignoreList.load(getResource("ignoreList.yml"));
    73. ignoreList.save(ignoreListFile);
    74. }
    75. }
    76.  
    77. public void saveYamls() {
    78. try {
    79. friends.save(configFile);
    80. mailBox.save(mailBoxFile);
    81. config.save(configFile);
    82. ignoreList.save(ignoreListFile);
    83. }
    84. catch (Exception ex){
    85. ex.printStackTrace();
    86. }
    87. }
    88.  
    89. public void loadYamls() {
    90. try {
    91. config.load(configFile);
    92. friends.load(configFile);
    93. mailBox.load(mailBoxFile);
    94. ignoreList.load(ignoreListFile);
    95. }
    96. catch (Exception ex){
    97. ex.printStackTrace();
    98. }
    99. }
    100.  

    This post has been edited 7 times. It was last edited by thehutch Oct 25, 2011.

Share This Page