[Tutorial] Create A Configuration File with YAML!

Discussion in 'Resources' started by Jayjay110, May 4, 2011.

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

    Dark_Balor

    @captainawesome7 : only to generate the config file with the "default" options in it when we first launch, or when we add a new options without overriding the one made by the server admin.

    I prefer doing like that then add the config.yml in my jar, copy it in the folder, then when I add a new option, say in the threads : be careful new option added, you have to add it in your config file if you want to configure it.
     
  2. Offline

    Jayjay110

    The main reason is, if you want to have a neat set of multiple config files, multi world etc, it makes it set out neater I guess, rather than having the same 100 config entries 3 times in a file, you can have it in 3 files, then if say you want the same config for all worlds, ctrl+a -> copy+paste and your done, rather than, select -> scroll -> copy+paste...

    I thought people should have been able to figure that out for themselves though
     
  3. Offline

    captainawesome7

    Yeah except all of the methods in your Config.java are useless, you can do all of that with Bukkit's Default Configuration. And I have no idea what your talking about when you say multiple files, your config class is hardcoded to be for one specific file (config.yml). Also, you should use plugin.getDataFolder() instead of making a new folder at plugins/MyPlugin.
     
  4. Offline

    Jayjay110

    What are you talking about? Its not hardcoded, anyone can just change the name of the file and have mmultiple classes for each config, besides when I did this I A) didnt know there was a bukkit configuration and B) didnt know it handled multiple files, and I only typed plugins/MyPlugin so that people could see you can make them in different directories.
     
  5. Offline

    captainawesome7

    But, you used org.bukkit.util.config.Configuration, so how could you not have known about it?
    And as far as loading more than one file why wouldn't you just use something like this:
    Code:
        public Configuration loadConfig(String path) {
            File file = new File(path);
            Configuration config = new Configuration(file);
            config.load();
            return config;
        }
     
  6. Offline

    Jayjay110

    I made this tutorial the first week I was coding bukkit, never done java b4 and I looked through the forums but no tuts came up soo... I just looked around, put a few posts together and viola, I figured it out so i thought im top shit and wrote it up
     
  7. Offline

    Alexis

    Hello, i followed your tuto, and i tryed two times but i have a bug when i launch my plugin with bukkit.
    in console i have :
    PHP:
    23:15:06 [GRAVECould not load 'plugins\SessionTime.jar' in folder 'plugins':
    java.lang.reflect.InvocationTargetException
            at sun
    .reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

            
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

            
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Sou
    rce
    )
            
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
            
    at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava
    :173)
            
    at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.
    java:213)
            
    at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager
    .java:136)
            
    at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:143)
            
    at org.bukkit.craftbukkit.CraftServer.<init>(CraftServer.java:110)
            
    at net.minecraft.server.ServerConfigurationManager.<init>(ServerConfigur
    ationManager
    .java:51)
            
    at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:133)
            
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:337)
            
    at net.minecraft.server.ThreadServerApplication.run(SourceFile:422)
    Caused byjava.lang.NullPointerException
            at me
    .slayug.SessionTime.Config.<init>(Config.java:18)
            
    at me.slayug.SessionTime.SessionTime.<init>(SessionTime.java:28)
            ... 
    13 more
    i have well put
    PHP:
    Config config = new Config(this);
        public 
    String enabledstartup "Enabled On Startup";
        public 
    boolean enabled;
    in the main class and
    PHP:
    config.configCheck();
    in onEnable(); and create the class Config with the changes.


    Sorry for my english ;)
    Thank you in advance !
     
  8. Offline

    4am

    java.lang.reflect.InvocationTargetException means there is some kind of reflection bug in your code. Could you pastebin all your code?
     
  9. Offline

    Alexis

    thanks for answer.
    i put only main class & Config.java because when without Config.java all is right.
    So my main class :
    PHP:
    package me.slayug.SessionTime;
     
    import java.util.logging.Logger;

    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.event.Event;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class 
    SessionTime extends JavaPlugin {

        private final 
    SessionTimePlayerListener playerListener = new SessionTimePlayerListener(this);

        protected 
    int timeLogin;
        protected 
    int dayLogin;
        protected 
    int dayQuit;
        protected 
    int timeQuit;
        protected 
    int timeSession;

        
    Config config = new Config(this);
        public 
    String enabledstartup "Enabled On Startup";
        public 
    boolean enabled;

        
    Logger log Logger.getLogger("Minecraft");

        public 
    void onEnable(){
            
    PluginManager pm this.getServer().getPluginManager();
            
    log.info("[SessionTime] enabled.");
            
    pm.registerEvent(Event.Type.PLAYER_LOGIN, (ListenerplayerListenerEvent.Priority.Normalthis);
            
    pm.registerEvent(Event.Type.PLAYER_QUIT, (ListenerplayerListenerEvent.Priority.Normalthis);
     
            
    getCommand("stime").setExecutor(new CommandExecutor() {

                public 
    boolean onCommand(CommandSender senderCommand cmdString labelString[] args) {
                    if(
    label.equalsIgnoreCase("stime")){

                        
    int dayQuit SessionTimePlayerListener.getDay();
                        
    int timeQuit SessionTimePlayerListener.getTime();
                        
    int timeSession SessionTimePlayerListener.getCountTimeSession(timeLogintimeQuitdayLogindayQuit);
                        
    sender.sendMessage("Tu es sur le serveur depuis "+ChatColor.RED+SessionTimePlayerListener.getTimeConverted(timeSession));
                        return 
    true;
                    }
                    return 
    false;
                }
            });

            
    config.configCheck();

        }
       
        public 
    void onDisable(){
            
    log.info("[SessionTime] disabled.");
        }
      
    }
    Config.java
    PHP:
    package me.slayug.SessionTime;

    import java.io.File;
    import java.util.List;

    import org.bukkit.util.config.Configuration;

    public class 
    Config {
        public static 
    SessionTime plugin;
        public 
    Config(SessionTime instance) {
            
    plugin instance;
        }

           public 
    String directory "plugins" File.separator +plugin.getDescription().getName();
           
    File file = new File(directory File.separator "logs.yml");
     
        public 
    void configCheck(){
            new 
    File(directory).mkdir();
     
            if(!
    file.exists()){
                try {
                    
    file.createNewFile();
                    
    addDefaults();

                } catch (
    Exception ex) {
                    
    ex.printStackTrace();
                }
            } else {

                
    loadkeys();
            }
        }
        private 
    void write(String rootObject x){
            
    Configuration config load();
            
    config.setProperty(rootx);
            
    config.save();
        }
        private 
    Boolean readBoolean(String root){
            
    Configuration config load();
            return 
    config.getBoolean(roottrue);
        }

        private 
    Double readDouble(String root){
            
    Configuration config load();
            return 
    config.getDouble(root0);
        }
        private List<
    StringreadStringList(String root){
            
    Configuration config load();
            return 
    config.getKeys(root);
        }
        private 
    String readString(String root){
            
    Configuration config load();
            return 
    config.getString(root);
        }
        private 
    Configuration load(){

            try {
                
    Configuration config = new Configuration(file);
                
    config.load();
                return 
    config;

            } catch (
    Exception e) {
                
    e.printStackTrace();
            }
            return 
    null;
        }
        private 
    void addDefaults(){
            
    plugin.log.info("Generating Config File...");
        
    write(plugin.enabledstartuptrue);
         
    loadkeys();
        }
        private 
    void loadkeys(){
            
    plugin.log.info("Loading Config File...");
            
    plugin.enabled readBoolean(plugin.enabledstartup);

            }
    }
     
  10. Offline

    Alexis

Thread Status:
Not open for further replies.

Share This Page