[Solved] All config value reverts?

Discussion in 'Plugin Development' started by russjr08, May 15, 2012.

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

    russjr08

    So as of request by TnT (I know, I'm not tagging him, don't want to increase his probably massive list of Alerts) I am adding a verbose config option to my plugin. However to keep code clean I want to create those methods in a separate class file. The problem is that I don't know how I can use stuff from ConfigFileOptions(The name of the new class) into the player listener when my constructor wants to be passed a plugin type (Which cannot be passed from a listener). How can I over come this?

    Edit: Please see my bottom post, thank you :D
     
  2. public final MyPlayerListener pl = new MyPlayerListener(this);
    public final ConfigFileOptions configOptions = new ConfigFileOptions(this);
    So you're creating a new instance of MyPlayerListener first, then a new instance of ConfigFileOptions. That means at the time the constructor of MyPlayerListener is called ConfigFileOptions is null, but:
    boolean configOptions = plugin.configOptions.isVerboseEnabled();
    This is why you get an NPE.
    to avoid this turn them around:
    public final ConfigFileOptions configOptions = new ConfigFileOptions(this);
    public final MyPlayerListener pl = new MyPlayerListener(this);
    But then you'll most likely get a error because you're calling plugin.getConfig(); before onEnable(), to fix this remove the two lines and change this (in onEnable()):
    pm.registerEvents(this.pl, this);
    to this:
    ConfigFileOptions configOptions = new ConfigFileOptions(this);
    pm.registerEvents(new MyPlayerListener(this), this);
     
  3. Offline

    russjr08

    I didn't even think about the order! Oops.. however, it still doesn't like my changes :/

    PHP:
    package me.russjr08.plugins;
     
    import java.util.Arrays;
    import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
     
    public class 
    IFail extends JavaPlugin{
        public final 
    Logger logger Logger.getLogger("Minecraft");
        public static 
    IFail plugin;
        public 
    IFail instance;
        
    ConfigFileOptions configOptions = new ConfigFileOptions(this);
     
           
           
     
       
            @
    Override
            
    public void onEnable(){
            
    PluginDescriptionFile pdfFile this.getDescription();
     
            if(
    configOptions.isVerboseEnabled() == true){
               
           
            
    this.logger.info(pdfFile.getName() + " Has Been Enabled!"); //Display in the console that the plugin was enabled
            
    }
            
    String[] startingKeywords = {"fail""fial""f-a-i-l""f.a.i.l.""phail" };
           
            
    PluginManager pm getServer().getPluginManager();
            
    pm.registerEvents(new MyPlayerListener(this), this);
            
    this.instance this;
            if(
    configOptions.isVerboseEnabled() == true){
                
    this.logger.info("Adding Default Configuration settings!");
            }
            
    getConfig().addDefault("User-Control.Added-Words"Arrays.asList(startingKeywords));
            
    getConfig().set("Configuration.kickMessage""For Failing!");
            
    getConfig().set("Configuration.chatMessage""I shouldn't say the word for doing something incorrectly!");
            
    getConfig().set("Configuration.loginMessage""iFail is running on this server!");
            
    getConfig().set("Configuration.permMessage""Ehh... you were close.. lucky you have permission to use that..");
            
    getConfig().set("Configuration.verboseMessages"false);
            
    getConfig().options().copyDefaults(true);
           
            
    saveConfig();
            if(
    configOptions.isVerboseEnabled() == true){
               
           
            
    boolean isEnabled getConfig().getBoolean("Enabled");
            
    this.logger.info("Warning: You have iFail set to " isEnabled);
            }
           
            }
           
           
            @
    Override
            
    public void onDisable(){
            
    PluginDescriptionFile pdfFile this.getDescription();
            if(
    configOptions.isVerboseEnabled() == true){
            
    this.logger.info(pdfFile.getName() + " Version " pdfFile.getVersion() +  " Plugin has been disabled!"); //Display in the console that the plugin was disabled
            
    }
            }
     
            public 
    boolean onCommand(CommandSender senderCommand cmdString commandLabelString[] args){ //Setup a new command
                
    Player player = (Playersender//Allows you to use the Player
                
    if(commandLabel.equalsIgnoreCase("sendme")){
                    
    player.sendMessage(ChatColor.DARK_RED "Sent!"); //Sends the message
                
    }
            return 
    false;
            }
           
           
           
     
    }
    PHP:
    package me.russjr08.plugins;
     
    import org.bukkit.configuration.file.FileConfiguration;
     
    public class 
    ConfigFileOptions {
     
        private 
    IFail plugin;
       
       
     
        public 
    ConfigFileOptions(IFail plugin) {
            
    this.plugin plugin;
        }
       
        public 
    FileConfiguration config plugin.getConfig();
        
    //This method will accept TnT's request at a option for verbose messages.
        
    public boolean isVerboseEnabled(){
           
            
    /*if(!config.getBoolean("Configuration.verboseMessages") == true){
                return false;
            }*/
           
            
    return true;
        }
     
    }
    Code:
    2012-05-16 18:05:42 [SEVERE] Could not load 'plugins\iFail.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.NullPointerException
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:148)
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:305)
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:230)
        at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:207)
        at org.bukkit.craftbukkit.CraftServer.<init>(CraftServer.java:183)
        at net.minecraft.server.ServerConfigurationManager.<init>(ServerConfigurationManager.java:53)
        at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:156)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:422)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused by: java.lang.NullPointerException
        at me.russjr08.plugins.ConfigFileOptions.<init>(ConfigFileOptions.java:15)
        at me.russjr08.plugins.IFail.<init>(IFail.java:19)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:144)
        ... 8 more
    Edit 2: I've solved most of the problem, just need to figure out why the config is reverted on just that one option (verbose messages)

    https://github.com/russjr08/NewBukkit/tree/master/iFail/src/me/russjr08/plugins

    Actually, just figured out... all of the configs are reverting. :/

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  4. Offline

    Sagacious_Zed Bukkit Docs

    Youre using the set method, which will set the key to the value every time onEnable is called.
     
  5. Offline

    russjr08

    Well now I feel like an idiot. I'm suppose to use addDefaults -_-

    Thanks for pointing this out :)
     
Thread Status:
Not open for further replies.

Share This Page