Config connect to different class?

Discussion in 'Plugin Development' started by BetaNyan, Jun 8, 2014.

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

    BetaNyan

    How can I make it so I can connect to my config in a different class (Not my main) ?
     
  2. Offline

    es359

    Do you mean access the getConfig() in a separate class? BetaNyan
     
  3. Offline

    itzrobotix

    Make an instance of the main class s0mething like;
    Code:java
    1. private Plugin plugin;
    2. public *MainClass*(Plugin instance){
    3. plugin = instance;
    4. }

    I think you should then be able to get the config.
     
  4. Offline

    BetaNyan

    This is my main class.
    What do I put in my other class?
    Code:
    package me.BetaNyan.Listeners;
     
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Main extends JavaPlugin implements Listener {
       
        private Plugin plugin;
            public Main(Plugin instance){
            plugin = instance;
            }
       
        public void onEnable() {
            getLogger().info("Listeners Test Enabled");
            PluginManager pm = getServer().getPluginManager();
            Bukkit.getPluginManager().registerEvents(this, this);
            pm.registerEvents(new Chicken(), this);   
            this.saveDefaultConfig();
           
        }
     
       
        public void onDisable() {
            getLogger().info("Listeners Test Disabled");
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(sender instanceof Player) {
            Player player = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("placeenable")) {
                player.sendMessage("Block Placing Messages have been enabled.");
            }
    }
            return false;
      }
       
    }
    
     
  5. Offline

    Kilovice

    BetaNyan
    When I need quick and dirty access to a config I use MAINCLASSHERE.getPlugin().getConfig()
    Not the best way, but it works
     
  6. Offline

    BetaNyan

    Yeah, I have 2 classes. Main and Chicken. (dont ask)
    Anyways, I want to do something with the configs, so how can I connect it to it?
     
  7. Offline

    Kilovice

    BetaNyan
    From the chicken class, use Main.getPlugin().getConfig()
    this will allow you to access the config and write to it as needed.

    EDIT: You said configs, you have two?
     
  8. BetaNyan
    Make a "getter" for your main class. For example:

    Code:java
    1. public class Main extends JavaPlugin {
    2. private static Main plugin;
    3.  
    4. public static Main getInstance(){
    5. return plugin;
    6. }


    This will allow you to access anything that is in your main class.
     
  9. Offline

    1Rogue

    Don't use static, pass your instances:

    Code:java
    1. public class Main {
    2.  
    3. private SomeExample some;
    4.  
    5. public void onEnable() {
    6. this.some = new SomeExample(this);
    7. this.some.doSomething();
    8. }
    9.  
    10. }

    Code:java
    1. public class SomeExample {
    2.  
    3. private final Main plugin;
    4.  
    5. public SomeExample(Main plugin) {
    6. this.plugin = plugin;
    7. }
    8.  
    9. public void doSomething() {
    10. this.plugin.getConfig();//Do what you want
    11. }
    12.  
    13. }
     
    L33m4n123 likes this.
  10. Offline

    itzrobotix

    I knew I wasn't far off :3
     
Thread Status:
Not open for further replies.

Share This Page