Need help with getConfig

Discussion in 'Plugin Development' started by Blender4Me, Aug 19, 2014.

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

    Blender4Me

    I created the method "loadConfig" in the main-class and I also want to use the getConfig in a other class. But I dont know how :C (And sorry for my bad english, but I hope you can understand what I want ^^)
    Code:
    //Main Class
     
    //package
    package de.blender4me.plugin17x;
     
    //imports
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
     
    import commands.BacktpCMD;
    import commands.CountdownCMD;
    import commands.HealCMD;
     
    public class main extends JavaPlugin implements Listener {
     
        @Override
        public void onEnable() {
            System.out.println("Zeugs by Blender4You");
                    //config laden
            loadConfig();
         
                    //commands aktivieren
            this.getCommand("heal").setExecutor(new HealCMD(this));
            this.getCommand("backtp").setExecutor(new BacktpCMD(this));
            this.getCommand("countdown").setExecutor(new CountdownCMD(this));     
        }
            //config laden
        public void loadConfig() {
            getConfig().options().copyDefaults(true);
            saveConfig();
         
            String msg = getConfig().getString("Config.message"); 
        }
    }
     
     
     
    //>>Here I want to use the config<<
     
    package commands;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
     
    import de.blender4me.plugin17x.main;
     
    public class HealConfigCMD implements CommandExecutor {
     
        public main plugin;
     
        public HealConfigCMD(main plugin) {
            this.plugin = plugin;
        }
     
        public boolean onCommand(CommandSender sender, Command cmd,String label, String[] args) {
         
            Player p = (Player)sender;
            if(!p.hasPermission("blender4me.heal")) {
                p.sendMessage("&6Nope! :3");
             
            } else {
         
                if(args.length == 0) {
                                    String msg = getConfig().getString("Config.message");
                    p.sendMessage(msg); 
                    }
            }
            return false;
        }
    }
     
  2. Offline

    Tecno_Wizard

    Blender4Me, Your english was nearly perfect by the way.
    In the alternate class, use plugin.methodName() (Not actually using method name)
    In your case, plugin.loadConfig()
    If i missed something just ask
     
  3. Offline

    Blender4Me

    @Tecno_Wizard Thank you :D But how can i use the method?/Where I have to put the" plugin.loadConfig()" I tried it but Eclipse marked it red :/

    I used 3 Years Python and Java is new for me. I just know the Basics

    "This message is awaiting moderator approval, and is invisible to normal visitors." ._.
     
  4. Offline

    RenditionsRule

    Blender4Me
    Actually all you have to do is copy/paste the contents of "loadConfig()" into your "onEnable()" function. This has always worked for me and is the way that most developers do it.
     
  5. Offline

    Blender4Me

    RenditionsRule In the "onEnable()" function of the main-class? Will it work in the other classes? Or can I use the onEnable function in every class? :/
     
  6. Offline

    RenditionsRule

    Blender4Me As long as you put it in the "onEnable()" of your main class, it should load your pre-created config from your plugin project.

    You are able to call getConfig() from any class as long as it is loaded into the Data Folder (which is what the onEnable() thing does)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  7. Offline

    ProMCKingz

Thread Status:
Not open for further replies.

Share This Page