Bukkit's Yaml Configuration Tutorial

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

?

Is my first Guide good?

  1. yes

    69.5%
  2. no

    6.6%
  3. meh, needs improvement

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

    DomovoiButler

    hmmm...your using the latest bukkitDev api right?
     
  2. Offline

    Icelaunche

    I am getting this error
    Code:text
    1. 10:02:28 [SEVERE] Could not load 'plugins/ServerTime.jar' in folder 'plugins':
    2. java.lang.NoClassDefFoundError: org/bukkit/configuration/file/FileConfiguration
    3. at java.lang.Class.forName0(Native Method)
    4. at java.lang.Class.forName(Class.java:247)
    5. at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:168)
    6. at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:213)
    7. at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:136)
    8. at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:138)
    9. at org.bukkit.craftbukkit.CraftServer.reload(CraftServer.java:408)
    10. at org.bukkit.Bukkit.reload(Bukkit.java:173)
    11. at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:22)
    12. at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:163)
    13. at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:353)
    14. at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:349)
    15. at net.minecraft.server.MinecraftServer.b(MinecraftServer.java:499)
    16. at net.minecraft.server.MinecraftServer.h(MinecraftServer.java:478)
    17. at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:374)
    18. at net.minecraft.server.ThreadServerApplication.run(SourceFile:417)
    19. Caused by: java.lang.ClassNotFoundException: org.bukkit.configuration.file.FileConfiguration
    20. at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    21. at java.security.AccessController.doPrivileged(Native Method)
    22. at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    23. at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:36)
    24. at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:24)
    25. at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    26. at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    27. ... 16 more
    28.  
     
  3. Offline

    DomovoiButler

    you have the latest CB?
     
  4. Offline

    Icelaunche

    yup
     
  5. Offline

    DomovoiButler

    can i see your mainclass?
     
  6. Offline

    Icelaunche

    Im pretty much using your example one i got errors in console when i added the code bit I get the same one using the example

    Code:text
    1. package me.Icelaunche.ServerTime;
    2.  
    3. import java.io.File;
    4. import java.io.FileOutputStream;
    5. import java.io.IOException;
    6. import java.io.InputStream;
    7. import java.io.OutputStream;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10.  
    11. import org.bukkit.configuration.file.FileConfiguration;
    12. import org.bukkit.configuration.file.YamlConfiguration;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15.  
    16. public class ServerTime extends JavaPlugin{
    17.  
    18. File configFile; ////
    19. File groupsFile; // declare Files
    20. File usersFile; // (how many yamls are we dealing with?)
    21. File historyFile; ////
    22.  
    23. FileConfiguration config; ////
    24. FileConfiguration groups; // declare FileConfigurations (each File declared above
    25. FileConfiguration users; // has to have one FileConfiguration)
    26. FileConfiguration history; ////
    27.  
    28. @Override
    29. public void onDisable() {
    30. // in here, we need to save all our yamls if we have not yet,
    31. // this maybe also a critical part cause some methods you might have forgotten
    32. // to use the saveYamls(); on some of your methods that uses config.set or *.set(path,value)
    33. // so we will use saveYamls(); method here to auto save what we have done
    34. saveYamls();
    35. }
    36.  
    37. @Override
    38. public void onEnable() {
    39. // for error free purposes, you need to have all this on top of your onEnable first
    40. // and then do whatever you want to do with your plugin like
    41. // Events Registering and .gets and .sets just below all this
    42.  
    43. // first we have to initialize all Files and FileConfigurations
    44. configFile = new File(getDataFolder(), "config.yml"); ////
    45. groupsFile = new File(getDataFolder(), "groups.yml"); // getDataFolder() means the folder /plugins/<pluginName>/
    46. usersFile = new File(getDataFolder(), "users.yml"); // creates virtual Files( /plugins/MainClass/*.yml )
    47. historyFile = new File(getDataFolder(), "history.yml"); ////
    48.  
    49. // then we use firstRun(); method
    50. try {
    51. firstRun();
    52. } catch (Exception e) {
    53. e.printStackTrace();
    54. }
    55.  
    56. // and we declare the FileConfigurations using YamlConfigurations and
    57. // then we just use loadYamls(); method
    58. // this is the critical part, this is needed cause if we do not use this,
    59. // it will read from the yml located at your jar, not in /plugins/<pluginName>/*yml.
    60. config = new YamlConfiguration();
    61. groups = new YamlConfiguration();
    62. users = new YamlConfiguration();
    63. history = new YamlConfiguration();
    64. loadYamls();
    65.  
    66. /*
    67. * do whatever you wantyour plugin to do below this comment
    68. * like your listeners, Loggers and stuff
    69. */
    70. }
    71.  
    72. /*
    73. * in this firstRun(); method, we checked if each File that we initialized does not exists
    74. * if it does not exists, we load the yaml located at your jar file, then save it in
    75. * the File(/plugins/<pluginName>/*.yml)
    76. * only needed at onEnable()
    77. */
    78. private void firstRun() throws Exception {
    79. if(!configFile.exists()){ // checks if the yaml does not exists
    80. configFile.getParentFile().mkdirs(); // creates the /plugins/<pluginName>/ directory if not found
    81. copy(getResource("config.yml"), configFile); // copies the yaml from your jar to the folder /plugin/<pluginName>
    82. }
    83. if(!groupsFile.exists()){
    84. groupsFile.getParentFile().mkdirs();
    85. copy(getResource("groups.yml"), groupsFile);
    86. }
    87. if(!usersFile.exists()){
    88. usersFile.getParentFile().mkdirs();
    89. copy(getResource("users.yml"), usersFile);
    90. }
    91. if(!historyFile.exists()){
    92. historyFile.getParentFile().mkdirs();
    93. copy(getResource("history.yml"), historyFile);
    94. }
    95. }
    96.  
    97. /*
    98. * this copy(); method copies the specified file from your jar
    99. * to your /plugins/<pluginName>/ folder
    100. */
    101. private void copy(InputStream in, File file) {
    102. try {
    103. OutputStream out = new FileOutputStream(file);
    104. byte[] buf = new byte[1024];
    105. int len;
    106. while((len=in.read(buf))!=-1){
    107. out.write(buf,0,len);
    108. }
    109. out.close();
    110. in.close();
    111. } catch (Exception e) {
    112. e.printStackTrace();
    113. }
    114. }
    115.  
    116. /*
    117. * in here, each of the FileConfigurations loaded the contents of yamls
    118. * found at the /plugins/<pluginName>/*yml.
    119. * needed at onEnable() after using firstRun();
    120. * can be called anywhere if you need to reload the yamls.
    121. */
    122. public void loadYamls() {
    123. try {
    124. config.load(configFile); //loads the contents of the File to its FileConfiguration
    125. groups.load(groupsFile);
    126. users.load(usersFile);
    127. history.load(historyFile);
    128. } catch (Exception e) {
    129. e.printStackTrace();
    130. }
    131. }
    132. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    133. if(cmd.getName().equalsIgnoreCase("egg")){
    134. Player player = (Player) sender;
    135. player.throwEgg();
    136. return true;
    137. }
    138. return false;
    139.  
    140. /*
    141. * save all FileConfigurations to its corresponding File
    142. * optional at onDisable()
    143. * can be called anywhere if you have *.set(path,value) on your methods
    144. */
    145.  
    146. }
    147. public void saveYamls() {
    148. try {
    149. config.save(configFile); //saves the FileConfiguration to its File
    150. groups.save(groupsFile);
    151. users.save(usersFile);
    152. history.save(historyFile);
    153.  
    154. }catch (IOException e) {
    155. e.printStackTrace();
    156. }
    157. }
    158.  
    159. }
    160.  
    161.  
     
  7. Offline

    DomovoiButler

    this maybe a unrelated question...but can u screenshot your server folder?and whats in your RUN.bat?
     
  8. Offline

    Icelaunche

    right... I have a mac
     
  9. Offline

    DomovoiButler

    so...can u screenshot whats in your server folder? and whats in your RUN.bat?
     
  10. Offline

    Sagacious_Zed Bukkit Docs

    *nixes (unix and linux) can't run bat files.
     
  11. Offline

    DomovoiButler

    i mean .sh...xD...been awhile not using linux and unix
     
  12. Offline

    TACTICALboom

    Is there a saveconfig method that DOESENT destroy comments......
     
  13. Offline

    Windwaker

    The thing is you have to add comments to the parsers liking,

    Okay:
    Code:
    # a comment
    Not okay:
    Code:
    #a comment
     
  14. Offline

    DomovoiButler

    config.save(configFile)
    read this thread tutorial and its infos...it will not delete your comments or whatsoevers
     
  15. Offline

    TACTICALboom

    @DomovoiButler @WalkerCrouse Just so you guys know, lol, Im a longtime 5 year game developer, and experienced in many languages, now that i've cleared that up....this method ( as shown in the TUT) is fine for bukkit, but is unacceptable for software releases...of course it would be XML anyway....but that's beside the point. And now that i've said THAT, I would also like to say that this is a helpful tutorial for people new the language/API. I only commented what i did because i never read any Bukkit javadoc's. Props @DomovoiButler though, good job!
     
  16. Offline

    DomovoiButler

    wow, and I only know java programming. and im not even very good at it yet
    so wait, what were you asking again?i cant really understand it for a noob like me
     
  17. Offline

    TACTICALboom

    I'm not in attempt to be conceded or anything I just get annoyed when I make a generic comment and people infer that I am uneducated and incompetent(not that you were doing that). So i was just asking what exactly the:
    Code:
    Config.save(ConfigFile)
    method/class includes....is it in bukkits default Configuration class? or what?
     
  18. Offline

    DomovoiButler

  19. Offline

    TACTICALboom

  20. Offline

    Neodork

    The yml inside my plugin looks like:


    Code:
    Connection:
        Host: localhost
        Port: 3306
        Dbname: name
        User: root
        Password: 1234
    

    But somehow it generates like(after being saved):

    Code:
    Connection: Host: localhost Port: 3306 Dbname: name User: root Password: 1234

    Any suggestions?
     
  21. Offline

    TACTICALboom

    After each string add "\n"
    it will create a new line ex:
    Code:Java
    1.  
    2. System.out.println("People are people\n People are cool");
    3.  

    Would display
    Code:
    People are people
    People are cool
    
    in the console, or in your case, the yaml.
    //EDIT: forgot that in this tut, pre made configs are just copied not written with the config class. You must be doing something wrong...
     
  22. Offline

    Neodork

    PHP:
    public class StatLogs extends JavaPlugin {
     
    private 
    String author "Neodork";
    public 
    MySQL mysql;
    public 
    Logger log Logger.getLogger("Minecraft");
     
    public 
    String host;
    public 
    int port;
    public 
    String dbname;
    public 
    String user;
    public 
    int password;
    public 
    String cmdmsg "no";
     
     
     
    File configFile;
    FileConfiguration config;
    public 
    playerRegisterListener listener = new playerRegisterListener(this);
    public 
    sqlHandler handler = new sqlHandler(this);
     
    @
    Override
    public void onDisable() {
    PluginDescriptionFile pdffile this.getDescription();
    saveYamls();
    System.out.println("[StatLogs]Disabled");
    mysql.close();
    }
     
    @
    Override
    public void onEnable() {
    //config file
    runConfigFileMethod();
     
     
     
     
    //register events
    PluginDescriptionFile pdffile this.getDescription();
    PluginManager pm getServer().getPluginManager();
    pm.registerEvents(this.listenerthis);
     
     
    //SQL part
    handler.sqlConnection();
    handler.sqlDoesUserDbExists();
    handler.sqlDoesconfigDbExists();
    System.out.println("[StatLogs]By "+author+" Enabled");
     
     
    }
     
    private 
    void runConfigFileMethod() {
    //DO NOT TOUCH
    configFile = new File(getDataFolder(), "config.yml");
    try {
    firstRun();
    } catch (
    Exception e) {
    e.printStackTrace();
    this.getPluginLoader().disablePlugin(this);
    }
    config = new YamlConfiguration();
    loadYamls();
    this.host config.getString("Connection.Host");
    this.port config.getInt("Connection.Port");
    this.dbname config.getString("Connection.Dbname");
    this.user config.getString("Connection.User");
    this.password config.getInt("Connection.Password");
    if(
    cmdmsg.equalsIgnoreCase("yes")){
    System.out.println("[StatLogs]Host: "+host);
    System.out.println("[StatLogs]port: "+port);
    System.out.println("[StatLogs]dbname: "+dbname);
    System.out.println("[StatLogs]user: "+user);
    System.out.println("[StatLogs]password: "+password);
    }
     
    saveYamls();
     
    }
     
     
    public 
    void saveYamls() {
    try {
    config.save(configFile);
     
    } catch (
    IOException e) {
    e.printStackTrace();
    this.getPluginLoader().disablePlugin(this);
    }
    }
    public 
    void loadYamls() {
    try {
    config.load(configFile);
     
    } catch (
    Exception e) {
    e.printStackTrace();
    this.getPluginLoader().disablePlugin(this);
    }
    }
     
    private 
    void firstRun() throws Exception {
    if(!
    configFile.exists()){
    configFile.getParentFile().mkdirs();
    copy(getResource("config.yml"), configFile);
    }
    }
     
     
     
    private 
    void copy(InputStream inFile 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();
    this.getPluginLoader().disablePlugin(this);
    }
    }
     
     
     
     
    public 
    boolean onCommand(CommandSender senderCommand cmdString commandLabelString[] args) {
    Player player = (Playersender;
    if(
    commandLabel.equalsIgnoreCase("")){
    loadYamls();
     
    }else{
    if(
    commandLabel.equalsIgnoreCase("")){
    saveYamls();
    }else{
    //next commandLabel
    }
    }
    return 
    false;
     
    }
    //SQL part!!
     
     
     
     
    }


    This is my code, looks exactly like the one provided in the tutorial if you ask me...
     
  23. Offline

    TACTICALboom

    Okay so,as you should know, YAML is, in fact, a language. I don't know how much you know about parsers however, you should know that its parsed only when saved, so you don't have your text to the parser's liking.
    ////try 3 spaces between cat's.
     
  24. Offline

    BronzeByte

    You are using Windows' Notepad, get a decent text editor with decent (double or linux) linebreaks like notepad++

    Ontopic: thanks for this, never knew the class names, after reading some snippets I know what I needed :)
     
  25. Offline

    TopGear93

    so where do i add all my config items? My Configs will appear but will not add the items inside of it.

    Show Spoiler

    Code:
    package us.topgear93.occupy;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.util.logging.Logger;
    
    import net.milkbowl.vault.economy.Economy;
    import net.milkbowl.vault.permission.Permission;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Occupy extends JavaPlugin{
        File mining;
        File taxes;
        File bounties;
        File insurances;
        File pluginmain;
        FileConfiguration miningconfig;
        FileConfiguration taxesconfig;
        FileConfiguration insurancesconfig;
        FileConfiguration pluginmainconfig;
        FileConfiguration bountiesconfig;
        //MAIN PLUGIN CONFIG - THIS WILL HOLD ANYTHING THAT DOESNT RELATE TO THE OTHER CONFIGS.
        //- MORTGAGES
        //- SHOPS
        //- ETC
        public Logger log;
        public static Permission permission = null;
        public static Economy economy = null;
    
        public void onEnable(){
            mining = new File(getDataFolder(), "mining.yml");
            bounties = new File(getDataFolder(), "bounties.yml");
            insurances = new File(getDataFolder(), "insurances.yml");
            pluginmain = new File(getDataFolder(), "mainconfig.yml");
            taxes = new File(getDataFolder(), "taxes.yml");
            loadYamls();
            try {
                firstRun();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        private void firstRun() throws Exception {
            if(!mining.exists()){
                mining.getParentFile().mkdirs();
                copy(getResource("mining.yml"), mining);
            }
            if(!bounties.exists()){
                bounties.getParentFile().mkdirs();
                copy(getResource("bounties.yml"), bounties);
            }
            if(!insurances.exists()){
                insurances.getParentFile().mkdirs();
                copy(getResource("insurances.yml"), insurances);
            }
            if(!pluginmain.exists()){
                pluginmain.getParentFile().mkdirs();
                copy(getResource("mainconfig.yml"), pluginmain);
            }
            if(!taxes.exists()){
                taxes.getParentFile().mkdirs();
                copy(getResource("taxes.yml"), taxes);
            }
        }
        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();
            }
        }
        public void loadYamls() {
            try {
                miningconfig.load(mining);
                bountiesconfig.load(bounties);
                taxesconfig.load(taxes);
                insurancesconfig.load(insurances);
                pluginmainconfig.load(pluginmain);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        public void saveYamls() {
            try {
                miningconfig.save(mining);
                bountiesconfig.save(bounties);
                pluginmainconfig.save(pluginmain);
                taxesconfig.save(taxes);
                insurancesconfig.save(insurances);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //MAIN PLUGIN CONFIG - THIS WILL HOLD ANYTHING THAT DOESNT RELATE TO THE OTHER CONFIGS.
            //- MORTGAGES
            //- SHOPS
            //- ETC
            log = Logger.getLogger("Minecraft");
            setupPermissions();
            setupEconomy();
            getServer().getPluginManager().registerEvents(new TAX(this), this);
            getServer().getPluginManager().registerEvents(new Bounties(this), this);
            getServer().getPluginManager().registerEvents(new Mining(this), this);
        }
        public void onDisable() {
        }
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = null;
            if (sender instanceof Player) {
                player = (Player) sender;
                if(label.equalsIgnoreCase("opc")){
                    if(args.length == 1){
                        if(args[0].equalsIgnoreCase("reload")){
                            if(Occupy.permission.has(sender, "OccupyMining.config.reload") || player.isOp()){
                                this.reloadConfig();
                                Bukkit.reload();
                                player.sendMessage(ChatColor.GOLD + "[" + ChatColor.WHITE + miningconfig.getString("Occupy.title") + ChatColor.GOLD + "]-" + ChatColor.GRAY + "Configs reloaded!");
                                return true;
                            }else{
                                player.sendMessage(ChatColor.GOLD + "[" + ChatColor.WHITE + miningconfig.getString("Occupy.title") + ChatColor.GOLD + "]-" + ChatColor.GRAY + "No permissions to reload Config!");
                                return false;
                            }
                        }
                    }
                }
                else if(label.equalsIgnoreCase("opprice")){
                    if(args.length == 1){
                        if(args[0].equalsIgnoreCase("1")){
                            if(Occupy.permission.has(sender, "OccupyMining.pricelist") || player.isOp()){
                                player.sendMessage(ChatColor.GOLD + "[" + ChatColor.WHITE + miningconfig.getString("Occupy.title") + ChatColor.GOLD + "]-" + ChatColor.GRAY +"Stone-" + ChatColor.DARK_GREEN + " $" + ChatColor.WHITE + miningconfig.getInt("OccupyMining.blocks.stone"));
                                player.sendMessage(ChatColor.GOLD + "[" + ChatColor.WHITE + miningconfig.getString("Occupy.title") + ChatColor.GOLD + "]-" + ChatColor.GRAY +"Grass-" + ChatColor.DARK_GREEN + " $" + ChatColor.WHITE + miningconfig.getInt("OccupyMining.blocks.grass"));
                                player.sendMessage(ChatColor.GOLD + "[" + ChatColor.WHITE + miningconfig.getString("Occupy.title") + ChatColor.GOLD + "]-" + ChatColor.GRAY +"Dirt-" + ChatColor.DARK_GREEN + " $" + ChatColor.WHITE + miningconfig.getInt("OccupyMining.blocks.dirt"));
                                return true;
                            }
                        }
                        if(args[0].equalsIgnoreCase("2")){
                            player.sendMessage(ChatColor.GOLD + "[" + ChatColor.WHITE + miningconfig.getString("Occupy.title") + ChatColor.GOLD + "]-" + ChatColor.GRAY +"CobbleStone-" + ChatColor.DARK_GREEN + " $" + ChatColor.WHITE + miningconfig.getInt("OccupyMining.blocks.cobblestone"));
                            player.sendMessage(ChatColor.GOLD + "[" + ChatColor.WHITE + miningconfig.getString("Occupy.title") + ChatColor.GOLD + "]-" + ChatColor.GRAY +"WoodenPlanks-" + ChatColor.DARK_GREEN + " $" + ChatColor.WHITE + miningconfig.getInt("OccupyMining.blocks.woodenplanks"));
                            player.sendMessage(ChatColor.GOLD + "[" + ChatColor.WHITE + miningconfig.getString("Occupy.title") + ChatColor.GOLD + "]-" + ChatColor.GRAY +"Sand-");
                            return true;
                        }       
                        if(args[0].equalsIgnoreCase("3")){
                            player.sendMessage(ChatColor.GOLD + "[" + ChatColor.WHITE + miningconfig.getString("Occupy.title") + ChatColor.GOLD + "]-" + ChatColor.GRAY +"Gravel-" + ChatColor.DARK_GREEN + " $" + ChatColor.WHITE + miningconfig.getInt("OccupyMining.blocks.sand"));
                            player.sendMessage(ChatColor.GOLD + "[" + ChatColor.WHITE + miningconfig.getString("Occupy.title") + ChatColor.GOLD + "]-" + ChatColor.GRAY +"GoldOre-" + ChatColor.DARK_GREEN + " $" + ChatColor.WHITE + miningconfig.getInt("OccupyMining.blocks.gravel"));
                            player.sendMessage(ChatColor.GOLD + "[" + ChatColor.WHITE + miningconfig.getString("Occupy.title") + ChatColor.GOLD + "]-" + ChatColor.GRAY +"IronOre-" + ChatColor.DARK_GREEN + " $" + ChatColor.WHITE + miningconfig.getInt("OccupyMining.blocks.ironore"));
                            return true;
                        }
                        if(args[0].equalsIgnoreCase("4")){
                            player.sendMessage(ChatColor.GOLD + "[" + ChatColor.WHITE + miningconfig.getString("Occupy.title") + ChatColor.GOLD + "]-" + ChatColor.GRAY +"CoalOre-" + ChatColor.DARK_GREEN + " $" + ChatColor.WHITE + miningconfig.getInt("OccupyMining.blocks.coalore"));
                            player.sendMessage(ChatColor.GOLD + "[" + ChatColor.WHITE + miningconfig.getString("Occupy.title") + ChatColor.GOLD + "]-" + ChatColor.GRAY +"Wood-" + ChatColor.DARK_GREEN + " $" + ChatColor.WHITE + miningconfig.getInt("OccupyMining.blocks.wood"));
                            player.sendMessage(ChatColor.GOLD + "[" + ChatColor.WHITE + miningconfig.getString("Occupy.title") + ChatColor.GOLD + "]-" + ChatColor.GRAY +"Leaves-" + ChatColor.DARK_GREEN + " $" + ChatColor.WHITE + miningconfig.getInt("OccupyMining.blocks.leaves"));
                            return true;
                        }
                        if(args[0].equalsIgnoreCase("5")){
                            player.sendMessage(ChatColor.GOLD + "[" + ChatColor.WHITE + miningconfig.getString("Occupy.title") + ChatColor.GOLD + "]-" + ChatColor.GRAY +"LapisLazuliOre-" + ChatColor.DARK_GREEN + " $" + ChatColor.WHITE + miningconfig.getInt("OccupyMining.blocks.LapisLazuliOre"));
                            player.sendMessage(ChatColor.GOLD + "[" + ChatColor.WHITE + miningconfig.getString("Occupy.title") + ChatColor.GOLD + "]-" + ChatColor.GRAY +"SandStone-" + ChatColor.DARK_GREEN + " $" + ChatColor.WHITE + miningconfig.getInt("OccupyMining.blocks.sandstone"));
                            player.sendMessage(ChatColor.GOLD + "[" + ChatColor.WHITE + miningconfig.getString("Occupy.title") + ChatColor.GOLD + "]-" + ChatColor.GRAY +"MossyCobbleStone-" + ChatColor.DARK_GREEN + " $" + ChatColor.WHITE + miningconfig.getInt("OccupyMining.blocks.MossyCobblestone"));
                            return true;
                        }
                        if(args[0].equalsIgnoreCase("6")){
                            player.sendMessage(ChatColor.GOLD + "[" + ChatColor.WHITE + miningconfig.getString("Occupy.title") + ChatColor.GOLD + "]-" + ChatColor.GRAY +"Obsidian-" + ChatColor.DARK_GREEN + " $" + ChatColor.WHITE + miningconfig.getInt("OccupyMining.blocks.obsidian"));
                            player.sendMessage(ChatColor.GOLD + "[" + ChatColor.WHITE + miningconfig.getString("Occupy.title") + ChatColor.GOLD + "]-" + ChatColor.GRAY +"DiamondOre-" + ChatColor.DARK_GREEN + " $" + ChatColor.WHITE + miningconfig.getInt("OccupyMining.blocks.diamondore"));
                            player.sendMessage(ChatColor.GOLD + "[" + ChatColor.WHITE + miningconfig.getString("Occupy.title") + ChatColor.GOLD + "]-" + ChatColor.GRAY +"StoneBrick-" + ChatColor.DARK_GREEN + " $" + ChatColor.WHITE + miningconfig.getInt("OccupyMining.blocks.stonebrick"));
                            return true;
                        }
                        if(args[0].equalsIgnoreCase("7")){
                            player.sendMessage(ChatColor.GOLD + "[" + ChatColor.WHITE + miningconfig.getString("Occupy.title") + ChatColor.GOLD + "]-" + ChatColor.GRAY +"RedStoneOre-" + ChatColor.DARK_GREEN + " $" + ChatColor.WHITE + miningconfig.getInt("OccupyMining.blocks.RedstoneOre"));
                            player.sendMessage(ChatColor.GOLD + "[" + ChatColor.WHITE + miningconfig.getString("Occupy.title") + ChatColor.GOLD + "]-" + ChatColor.GRAY +"Clay-" + ChatColor.DARK_GREEN + " $" + ChatColor.WHITE + miningconfig.getInt("OccupyMining.blocks.clay"));
                            player.sendMessage(ChatColor.GOLD + "[" + ChatColor.WHITE + miningconfig.getString("Occupy.title") + ChatColor.GOLD + "]-" + ChatColor.GRAY +"NetherRack-" + ChatColor.DARK_GREEN + " $" + ChatColor.WHITE + miningconfig.getInt("OccupyMining.blocks.NetherRack"));
                            return true;
                        }
                        if(args[0].equalsIgnoreCase("8")){
                            player.sendMessage(ChatColor.GOLD + "[" + ChatColor.WHITE + miningconfig.getString("Occupy.title") + ChatColor.GOLD + "]-" + ChatColor.GRAY +"SoulSand-" + ChatColor.DARK_GREEN + " $" + ChatColor.WHITE + miningconfig.getInt("OccupyMining.blocks.soulsand"));
                            player.sendMessage(ChatColor.GOLD + "[" + ChatColor.WHITE + miningconfig.getString("Occupy.title") + ChatColor.GOLD + "]-" + ChatColor.GRAY +"More items to be added" + ChatColor.DARK_GREEN + " $" + ChatColor.WHITE + miningconfig.getInt("OccupyMining.blocks.stone"));
                            return true;
                        }else{
                            player.sendMessage(ChatColor.GOLD + "[" + ChatColor.WHITE + miningconfig.getString("Occupy.title") + ChatColor.GOLD + "]-" + ChatColor.GRAY + miningconfig.getString("Occupy.pricelist.noperms"));
                            return false;
                        }
                    }
                }
            }
            return false;
        }
        //NEED CONFIG FOR 
        //MINING
        //TAXES
        //BOUNTIES
        //INSURANCES
        //MAIN PLUGIN CONFIG - THIS WILL HOLD ANYTHING THAT DOESNT RELATE TO THE OTHER CONFIGS.
        //- MORTGAGES
        //- SHOPS
        //- ETC
        private boolean setupPermissions(){
            RegisteredServiceProvider<Permission> permissionProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.permission.Permission.class);
            if (permissionProvider != null) {
                permission = permissionProvider.getProvider();
            }
            return (permission != null);
        }
        private boolean setupEconomy(){
            RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
            if (economyProvider != null) {
                economy = economyProvider.getProvider();
            }
            return (economy != null);
        }
    }
    


    Items for Mining config:
    Code:
    miningconfig.options().header("Occupy Config");
            miningconfig.addDefault("Occupy.title", "Occupy");
            miningconfig.addDefault("Occupy.payedout", "You were payed");
            miningconfig.addDefault("Occupy.randompay", 6);
            miningconfig.addDefault("Occupy.pricelist.noperms", "No permissions to check Occupy price list!");
            miningconfig.addDefault("Occupy.payout.blocks.stone", 7);
            miningconfig.addDefault("Occupy.payout.blocks.grass", 7);
            miningconfig.addDefault("Occupy.payout.blocks.dirt", 7);
            miningconfig.addDefault("Occupy.payout.blocks.cobblestone", 7);
            miningconfig.addDefault("Occupy.payout.blocks.woodenplanks", 7);
            miningconfig.addDefault("Occupy.payout.blocks.sand", 7);
            miningconfig.addDefault("Occupy.payout.blocks.gravel", 7);
            miningconfig.addDefault("Occupy.payout.blocks.goldore", 7);
            miningconfig.addDefault("Occupy.payout.blocks.ironore", 7);
            miningconfig.addDefault("Occupy.payout.blocks.coalore", 7);
            miningconfig.addDefault("Occupy.payout.blocks.wood", 7);
            miningconfig.addDefault("Occupy.payout.blocks.leaves", 7);
            miningconfig.addDefault("Occupy.payout.blocks.LapisLazuliOre", 7);
            miningconfig.addDefault("Occupy.payout.blocks.sandstone", 7);
            miningconfig.addDefault("Occupy.payout.blocks.MossyCobblestone", 7);
            miningconfig.addDefault("Occupy.payout.blocks.obsidian", 7);
            miningconfig.addDefault("Occupy.payout.blocks.diamondore", 7);
            miningconfig.addDefault("Occupy.payout.blocks.RedstoneOre", 7);
            miningconfig.addDefault("Occupy.payout.blocks.clay", 7);
            miningconfig.addDefault("Occupy.payout.blocks.NetherRack", 7);
            miningconfig.addDefault("Occupy.payout.blocks.soulsand", 7);
            miningconfig.addDefault("Occupy.payout.blocks.stonebrick", 7);
            miningconfig.addDefault("Occupy.payout.message", true);
            miningconfig.options().copyDefaults(true);
    
     
  26. Offline

    Lolmewn

    What if I want to add comments to the config file? The Bukkit system tends to filter them out..
     
  27. Offline

    Don Redhorse

    yes.. if you do a saveConfig only the nodes will stay.. working as designed by Dinnerbone
     
  28. So thats my problem i want to can set my coords to a yml file from sethome
    this are my codes
    the problem is that i get an error and mostely of the time the coords don't save into my file
    please can someone help me

    main class
    PHP:
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.util.Arrays;
    import java.util.Properties;
    import java.util.logging.Logger;
     
    import listeners.Freeze;
    import me.net.dracinis.Commands.CommandBan;
    import me.net.dracinis.Commands.CommandClearInv;
    import me.net.dracinis.Commands.CommandCreative;
    import me.net.dracinis.Commands.CommandDeop;
    import me.net.dracinis.Commands.CommandFly;
    import me.net.dracinis.Commands.CommandFreeze;
    import me.net.dracinis.Commands.CommandHeal;
    import me.net.dracinis.Commands.CommandHelp;
    import me.net.dracinis.Commands.CommandKick;
    import me.net.dracinis.Commands.CommandKill;
    import me.net.dracinis.Commands.CommandOp;
    import me.net.dracinis.Commands.CommandRules;
    import me.net.dracinis.Commands.CommandSetHome;
    import me.net.dracinis.Commands.CommandSetSpawn;
    import me.net.dracinis.Commands.CommandSpawn;
    import me.net.dracinis.Commands.CommandSpawnAnimals;
    import me.net.dracinis.Commands.CommandSpawnMob;
    import me.net.dracinis.Commands.CommandStorm;
    import me.net.dracinis.Commands.CommandSun;
    import me.net.dracinis.Commands.CommandSurvival;
    import me.net.dracinis.Commands.CommandTeleport;
    import me.net.dracinis.Commands.CommandTime;
    import me.net.dracinis.Commands.CommandTphere;
    import me.net.dracinis.Commands.CommandTree;
    import me.net.dracinis.Commands.CommandUnban;
     
     
     
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.configuration.Configuration;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
     
    public class 
    dracinismain extends JavaPlugin {
        public final 
    Logger logger Logger.getLogger("minecraft");
        public static 
    dracinismain plugin;
        
    Configuration config;
        public static 
    String Rules;
        public static 
    boolean rulestoggle;
        public 
    File m_Folder;
        static 
    Properties prop = new Properties(); //creates a new properties file
        
    public static int currentLine 0;
        public static 
    int tid 0;
        public static 
    int running 1;
        public static 
    long interval 10;
     
        
    File configFile;
            
    File homesFile;
            
    File historyFile;
            
    FileConfiguration homes;
            
    FileConfiguration history;
     
       
       
     
        @
    Override
        
    public void onDisable() {
            
    PluginDescriptionFile pdfFile this.getDescription();
            
    this.logger.info(pdfFile.getName() + " Has been Disabled!");
        }
       
        
    String[] rules = { "Rule1""Rule2""Rule3""rule4""rule5""rule6" };
       
        @
    Override
        
    public void onEnable() {
           
           
     
           
     
            
    PluginDescriptionFile pdfFile this.getDescription();
            
    this.logger.info(pdfFile.getName() + " version " pdfFile.getVersion() + " is now enabled.");
            
    CommandExecutor CommandHeal = new CommandHeal(this);
            
    getCommand("heal").setExecutor(CommandHeal);
            
    CommandExecutor CommandHelp = new CommandHelp(this);
            
    getCommand("dr").setExecutor(CommandHelp);
            
    CommandExecutor CommandHelp1 = new CommandHelp(this);
            
    getCommand("help").setExecutor(CommandHelp1);
            
    CommandExecutor CommandTeleport = new CommandTeleport(this);
            
    getCommand("tp").setExecutor(CommandTeleport);
            
    CommandExecutor CommandTphere = new CommandTphere(this);
            
    getCommand("tphere").setExecutor(CommandTphere);
            
    CommandExecutor CommandRules = new CommandRules(this);
            
    getCommand("rules").setExecutor(CommandRules);
            
    CommandExecutor CommandTime = new CommandTime(this);
            
    getCommand("time").setExecutor(CommandTime);
            
    CommandExecutor CommandCreative = new CommandCreative(this);
            
    getCommand("c").setExecutor(CommandCreative);
            
    CommandExecutor CommandSurvival = new CommandSurvival(this);
            
    getCommand("s").setExecutor(CommandSurvival);
            
    CommandExecutor CommandSun = new CommandSun(this);
            
    getCommand("sun").setExecutor(CommandSun);
            
    CommandExecutor CommandStorm = new CommandStorm(this);
            
    getCommand("storm").setExecutor(CommandStorm);
            
    CommandExecutor CommandFly = new CommandFly(this);
            
    getCommand("fly").setExecutor(CommandFly);
            
    CommandExecutor CommandKick = new CommandKick(this);
            
    getCommand("kick").setExecutor(CommandKick);
            
    CommandExecutor CommandSetSpawn = new CommandSetSpawn(this);
            
    getCommand("setspawn").setExecutor(CommandSetSpawn);
            
    CommandExecutor CommandSpawn = new CommandSpawn(this);
            
    getCommand("spawn").setExecutor(CommandSpawn);
            
    CommandExecutor CommandBan = new CommandBan(this);
            
    getCommand("ban").setExecutor(CommandBan);
            
    CommandExecutor CommandUnban = new CommandUnban(this);
            
    getCommand("unban").setExecutor(CommandUnban);
            
    CommandExecutor CommandSpawnMob = new CommandSpawnMob(this);
            
    getCommand("spawnmob").setExecutor(CommandSpawnMob);
            
    CommandExecutor CommandClearInv = new CommandClearInv(this);
            
    getCommand("ci").setExecutor(CommandClearInv);
            
    CommandExecutor CommandTree = new CommandTree(this);
            
    getCommand("tree").setExecutor(CommandTree);
            
    CommandExecutor CommandKill = new CommandKill(this);
            
    getCommand("kill").setExecutor(CommandKill);
            
    CommandExecutor CommandSpawnAnimals = new CommandSpawnAnimals(this);
            
    getCommand("spawnanimal").setExecutor(CommandSpawnAnimals);
            
    CommandExecutor CommandOp = new CommandOp(this);
            
    getCommand("op").setExecutor(CommandOp);
            
    CommandExecutor CommandDeop = new CommandDeop(this);
            
    getCommand("deop").setExecutor(CommandDeop);
            
    CommandExecutor CommandFreeze = new CommandFreeze(this);
            
    getCommand("freeze").setExecutor(CommandFreeze);
            
    CommandExecutor CommandSetHome = new CommandSetHome(this);
            
    getCommand("sethome").setExecutor(CommandSetHome);
     
           
            
    //Normal config
           
            
    String[] rules = { "Rule1""Rule2""Rule3""rule4""rule5""rule6" };
           
           
            final 
    FileConfiguration config this.getConfig();
            
    config.options().header("this is the rules list you can change it you can have 6 rules!");
            
    config.addDefault("broadcastprefix""&5[attention]" );
            
    config.addDefault("Rules"Arrays.asList(rules));
            
    getConfig().options().copyDefaults(true);
            
    saveConfig();
           
           
           
            
    configFile = new File(getDataFolder(), "config.yml");  ////
            
    homesFile = new File(getDataFolder(), "homes.yml");  //  getDataFolder() means the folder /plugins/<pluginName>/
            
    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.
            
    homes = 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(!
    homesFile.exists()){
                
    homesFile.getParentFile().mkdirs();
                
    copy(getResource("homes.yml"), homesFile);
            }
            if(!
    historyFile.exists()){
                
    historyFile.getParentFile().mkdirs();
                
    copy(getResource("history.yml"), historyFile);
            }
        }
     
     
        private 
    void copy(InputStream inFile 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();
            }
        }
     
     
        public 
    void loadYamls() {
            try {
                
    homes.load(homesFile);
                
    history.load(historyFile);
            } catch (
    Exception e) {
                
    e.printStackTrace();
            }
        }
     
     
        public 
    void saveYamls() {
            try {
                
    homes.save(homesFile);
                
    history.save(historyFile);
            } catch (
    IOException e) {
                
    e.printStackTrace();
            }
           
        }
       
       
       
     
    }
       
       
       
     
           
     
    this is my commandexecutor where i have my command
    PHP:
    package me.net.dracinis.Commands;
     
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
     
    import me.net.dracinis.dracinismain;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
     
    public class 
    CommandSetHome implements CommandExecutor{
       
       
       
        public 
    CommandSetHome (dracinismain dracinismain) {
        }
       
        public 
    void onEnable() {
           
        }
        
    File homesFile;
        
    FileConfiguration homes;
       
        private static final 
    String PREFIX ChatColor.GREEN "[HOMECMD] " ChatColor.WHITE;
     
       
     
       
            public 
    boolean onCommand(CommandSender senderCommand commandString labelString[] args) {
                if (!(
    sender instanceof Player)) {
                    
    sender.sendMessage(PREFIX "Player only command");
                    return 
    true;
                }
                
    Player p = (Playersender;
                if (
    command.getName().equalsIgnoreCase("home") && args.length == && p.hasPermission("homecmd.go")) {
                    
    int x homes.getInt(p.getWorld().getName() + "." p.getName() + "." "X"p.getWorld().getSpawnLocation().getBlockX());
                    
    int y homes.getInt(p.getWorld().getName() + "." p.getName() + "." "Y"p.getWorld().getSpawnLocation().getBlockY());
                    
    int z homes.getInt(p.getWorld().getName() + "." p.getName() + "." "Z"p.getWorld().getSpawnLocation().getBlockZ());
                    
    p.teleport(new Location(p.getWorld(), xyz));
                    
    p.sendMessage(PREFIX "teleported to home");
                    return 
    true;
                }
                if (
    command.getName().equalsIgnoreCase("sethome") && args.length == && p.hasPermission("homecmd.set")) {
                    
    homes.set(p.getWorld().getName() + "." p.getName() + "." "X"p.getLocation().getBlockX());
                    
    homes.set(p.getWorld().getName() + "." p.getName() + "." "Y"p.getLocation().getBlockY());
                    
    homes.set(p.getWorld().getName() + "." p.getName() + "." "Z"p.getLocation().getBlockZ());
       
                    
    p.sendMessage(PREFIX "Set your home in this world to [" p.getLocation().getBlockX() + "|" p.getLocation().getBlockY() + "|" p.getLocation().getBlockZ() + "]");
                    return 
    true;
                }
                return 
    false;
            }
           
           
           
            private 
    void copy(InputStream inFile 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();
                }
            }
     
     
            public 
    void loadYamls() {
                try {
                    
    homes.load(homesFile);
                } catch (
    Exception e) {
                    
    e.printStackTrace();
                }
            }
     
       
            public 
    void saveYamls() {
                try {
                    
    homes.save(homesFile);
                } catch (
    IOException e) {
                    
    e.printStackTrace();
                }
               
            }
        }
    please help
     
  29. Offline

    Neodork

    You didn't stick to the guys tutorial, try reading it again.

    I say this because when I look through the code I find a lot of things like:
    Code:java
    1.  
    2. //Your missing something right ?
    3. File configFile;
    4. File homesFile;
    5. File historyFile;
    6. FileConfiguration homes;
    7. FileConfiguration history;
    8.  


    And:

    Code:java
    1. //I don't think your supposed to have 2 of those for 1 plugin ^^.
    2. //same for the onEnable methods <--- You have 2 of those :O?
    3. public void onEnable() {
    4.  
    5. }
    6. File homesFile;
    7. FileConfiguration homes;
    8.  
     
  30. Offline

    Alexis

    Hi, when i use getList, i have a result null
    Though my config.yml :
    PHP:
    Joueurs:
      
    pseudo:
        
    '260''on'
        '364'
    'on'
        '360'
    'on'
    And my getter :
    PHP:
        plugin.getConfig().options().copyDefaults(true);
                    
    plugin.saveConfig();
                }
                
    plugin.reloadConfig();
                List<
    StringlistFood plugin.getConfig().getStringList("Joueurs."+event.getEntity().getName());
    Where is the problem pliz ?
    Thanks in advance :)
     
Thread Status:
Not open for further replies.

Share This Page