getConfig()

Discussion in 'Plugin Development' started by Brent Fagersten, Mar 13, 2013.

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

    Brent Fagersten

    RESOLVED
    Hi, im new to bukkit programming and a beginner in java, so please excuse the noob question :p
    How can i use getConfig() outside of the main class?
    i've tried looking it up but i only found confusing answers.
    basically what im trying to do is getConfig().getString("toggle") for example, in another class.
    Any help would be greatly appreciated.

    pastebin code
    http://pastebin.com/qThnfr2c
    http://pastebin.com/ATKuKCDy
     
  2. Offline

    MrOAriO

    You can use this.getConfig() .... or MainClassName.getConfig()
     
  3. Offline

    Brent Fagersten

    mainclassname.getConfig() gets me this error Cannot make a static reference to the non-static method getConfig() from the type JavaPlugin
    and this.getConfig() wants me to make a method?
     
    jflory7 likes this.
  4. Offline

    Lolmewn

    Pass your Main class object to the constructor of the other class, store it in a variable and use variable.getConfig() to call it.
     
  5. Offline

    Brent Fagersten

    im sorry i cant figure that out.. could i have an example? i tried google but its not making sense to me.. again sorry.. im trying to learn as much as i can so i can stop bugging the forums :p
     
  6. Offline

    Lolmewn

    Main class:
    onEnable(){
    YourOtherClass class = new YourOtherClass(this);
    }

    YourOtherClass:
    private Main instance;
    public YourOtherClass(Main instance){
    this.instance = instance;
    }

    public void yourEvent(SomeEvent event){
    instance.getConfig().whatever();
    }
     
  7. Offline

    Brent Fagersten

    Lolmewn
    Ok, this isnt working for me :/
    this is the class wobBlockListener
    Code:
    package com.wobhosting.wobplugin;
    @SuppressWarnings("unused")
     
    public class wobBlockListener implements Listener {
        public static WobPlugin plugin;
        public static Material[] blacklist = {Material.TNT};
     
       
        public wobBlockListener() {
       
        }
       
     
        @EventHandler
        public void onBlockPlace(BlockPlaceEvent event){
            Material block = event.getBlock().getType();
            Player player = event.getPlayer();
        //    config().options().
            //this.getConfig().options().copyDefaults(true);
        if(plugin.getConfig().getString("BlackList")=="true"){
        if(player.isOp()){
           
        }else
            for(Material blocked : blacklist){
                if(blocked == block){
                    if(player.isOp()){}
                    else{
                        event.getBlock().setType(Material.AIR);
                        player.chat("I just placed " + ChatColor.DARK_RED + blocked);
                    }
                }
            }
            }
            else{
               
            }
        }
       
       
       
    }
    
    and this is the main class, WobPlugin
    Code:
    public class WobPlugin extends JavaPlugin {
        public final Logger logger = Logger.getLogger("Minecraft");
        public static WobPlugin plugin;
        public final wobBlockListener bl = new wobBlockListener();
       
        public final wobPlayerListener pl = new wobPlayerListener();
    //declaring config
        FileConfiguration config;
       
       
       
        @Override
        public void onDisable() {
            PluginDescriptionFile pdffile = this.getDescription();
            this.logger.info(pdffile.getName() + " Version " + pdffile.getVersion()
                    + " has been disabled :(");
        }
     
        @Override
        public void onEnable() {
           
            PluginDescriptionFile pdffile = this.getDescription();
            this.logger.info(pdffile.getName() + " Version " + pdffile.getVersion()
                    + " has been enabled");
            //register events
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(bl, this);
            pm.registerEvents(pl, this);
           
            //register commands
            getCommand("roll").setExecutor(new wobRoll());
            //getCommand("restart").setExecutor(new wobRestart());
            //getCommand("wob roll tell").setExecutor(new wobRoll());
     
            //setting config defaults
           
            setupConfig();
            //get config
            //getConfig().options().copyDefaults(true);
            //saveConfig();
            //saveDefaultConfig()?
           
     
           
        }
        public void setupConfig() {
     
            config = this.getConfig();
     
            // fill in these addDefaults with your config values
            config.addDefault("MOTD" , "need a minecraft host? go to www.wobhosting.com");
            config.addDefault("motdToggle", "on");
            config.addDefault("BlackList", "off");
          //  config.addDefault();
            //config.addDefault();
     
            config.options().copyDefaults(true);
            saveConfig();
        }
    
    Theres more at the end but i dont think you need that in this case.
    trying what you said didnt work it gives me errors. However when i do plugin.getConfig() it removes the errors but then in console it freaks out saying theres an error on that line. "Caused by: java.lang.NullPointerException
    at com.wobhosting.wobplugin.wobBlockListener.onBlockPlace(wobBlockListener.java:30)
    "
    sorry for the extensive questions. But anyones help will help me greatly and teach me more and hopefully one day i can be good enough to help the beginners on this forum and contribute instead of just asking noob questions.
     
  8. Offline

    AmShaegar

    Code:
        public wobBlockListener(Plugin p) {
        plugin = p;
        }
    Code:
    wobBlockListener bl = new wobBlockListener(this);
     
  9. Offline

    Lolmewn

    Brent Fagersten Ah, the WoBPlugin. I reviewed that this morning, I think :)
    In your main class, replace
    Code:
    public final wobBlockListener bl = new wobBlockListener();
    with
    Code:
    public wobBlockListener bl;
    In your onEnable, do
    Code:
    bl = new wobBlockListener(this);
    In your wobBlockListener class, change the constructor (or let it auto-generate by your IDE), and save the variable to your 'plugin' variable.
     
  10. Offline

    Brent Fagersten

    Lolmewn
    ok, so i dont really understand what you mean by change the constructor and save the variable to your 'plugin' variable.

    and thanks for accepting my plugin :D

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

    Lolmewn

    Code:
    public wobBlockListener(WobPlugin main){
    this.plugin = main;
    }
     
  12. Offline

    gomeow

    Brent Fagersten
    Look up 'java constructor' as this really isn't too hard
     
    FlorianBK likes this.
  13. Offline

    Brent Fagersten

    Lolmewn
    THANK YOU! that works. thank you for putting up with me :p much appreciated!
     
Thread Status:
Not open for further replies.

Share This Page