Use Variable From Config In Listener Class?

Discussion in 'Plugin Development' started by rylinaux, Nov 20, 2011.

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

    rylinaux

    Hello,

    I'm having trouble using a boolean set in my plugin's config in a listener class. In my main class, I have this:
    Code:
    config = getConfig();
    this.getConfig().set("Block-Creeper-Explosion", true);
    boolean creeperPower = this.getConfig().getBoolean("Block-Creeper-Explosion", true);
    
    My EntityListener looks something like this:
    Code:
    
    public CreeperBlock plugin;
    public CreeperBlockEntityListener (CreeperBlock instance) {
        plugin = instance;
    }
    public void onCreeperPower(CreeperPower event) {
        if (creeperPower is equal to true) {
            event.setCancelled(true)
        } else {
            blah blah
        }
    
    I need to know how to check the value of the creeperPower boolean in this class, any insight would be appreciated.

    Thanks!
     
  2. Offline

    Sagacious_Zed Bukkit Docs

    invoke the getConfig method of your plugin.
     
  3. Offline

    bleachisback

    You can't since it isn't a field variable.
     
  4. Offline

    rylinaux

    Would you care to elaborate? An example maybe?
     
  5. Offline

    bleachisback

    you did this:

    Code:
    public Cunstructor(Parameter param)
    {
       Variable var=getVar();
    }
    
    You need this:


    Code:
    public Variable var=new Variable();
    
    public Cunstructor(Parameter param)
    {
      var=getVar();
    }
    
     
  6. Offline

    Sagacious_Zed Bukkit Docs

    Of course methods are not field variables.

    @ryanclancy000
    in our listener i see you have your plugin as a field so under your else clause
    Code:
    FileConfiguration plugin config = plugin.getConfig();
    // off you go do stuff
    
     
  7. Offline

    bleachisback

    You just said the exact same thing I did >.>
     
  8. Offline

    Sagacious_Zed Bukkit Docs

    I mentioned nothing about the constructor, err cunstrutor as you call it.

    Actually the step assigning it to a local variable is optional.
     
  9. Offline

    bleachisback

    I was assuming that he had put it in a constructor which I realise is stupid because it uses onEnable()

    I was pointing out that the variable declaration has to be outside of a method/constructor
     
  10. Offline

    Sagacious_Zed Bukkit Docs

    Actually it is bad to assign the return to a field level class as getConfig can return different objects, so you should only declare it at a local level, inside a method.
     
  11. Offline

    bleachisback

    But he wouldn't be able to reach it outside of the class, which is what he wanted
     
  12. Offline

    Sagacious_Zed Bukkit Docs

    You can always reach the FileConfiguraiton object if you have a reference to his plugin's main class, which he does. getConfig is a public method you know...
     
  13. Offline

    bleachisback

    I believe he wanted to load the boolean "creeperPower" once in his main class and then access it later.
     
Thread Status:
Not open for further replies.

Share This Page