Pass FileConfiguration to other class

Discussion in 'Plugin Development' started by Julian1Reinhardt, Mar 9, 2014.

Thread Status:
Not open for further replies.
  1. I have a custom Config File
    Code:
    File confFile= new File(this.getDataFolder(), "custom.yml");
    @Override
    public void onEnable(){
         if(!confFile.exists()){
                try {
                     confFile.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
           }
    }
    public FileConfiguration conf = YamlConfiguration.loadConfiguration(confFile);
    
    now I want to check if this file contains a playername in my listener class
    Code:
    public static main plugin;
    @EventHandler
        public void  onPlayerJoin(PlayerJoinEvent event){
            Player p = event.getPlayer();
            if (!plugin.conf.contains(p.getName())) {
                plugin.conf.set(p.getName(), 0);
            }
        }
    But everytime I try it it has a NullPointerException at line 14 in my listener (That's the line with the if statement)
    It probably can't pass the FileConfiguration.
     
  2. Offline

    GameplayJDK

    You need to give the plugin to the constructor of your listener when you register the it as listener:

    Code:
    ...
    // The plugin variable of the listener
    public <NAME OF YOUR PLUGINS MAIN CLASS HERE> myPlugin;
    // The constuctor
    public <NAME OF YOUR LISTENER CLASS HERE>(<NAME OF YOUR PLUGINS MAIN CLASS HERE> thePlugin) {
    this.myPlugin = thePlugin;
    }
    ...
     
    Then you can access the plugin like this:
     
    ...
    this.myPlugin.<DO WHAT YOU WANT>;
    ...
    
     
Thread Status:
Not open for further replies.

Share This Page