Call method from other class

Discussion in 'Plugin Development' started by ElCreeperHD, Mar 13, 2016.

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

    ElCreeperHD

    Hello, I am trying to get the config of my plugin from other method, I made that in the same plugin, but it is not working on one class. Here is the code of Main and the class not working. getLanguage() at class Config is getting NullPointerException at the String "language", so I think Config class is not getting the plugin correctly, because I can get values from config from other classes using the same constructor.
    Main:

    Code:
    package mentioner;
    
    import java.io.IOException;
    import java.util.logging.Logger;
    
    
    
    import org.bukkit.Bukkit;
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    public class Main extends JavaPlugin {
      
        CommandListener listener = new CommandListener(this);
        public Logger log = Bukkit.getLogger();
        public ChatEventListener eventos = new ChatEventListener(this);
      
        public void onEnable(){
            getServer().getPluginManager().registerEvents(eventos,this);
            saveDefaultConfig();
            getCommand("mentioner").setExecutor(listener);
            //shut up ElCreeperHD!! Write a function for the language checking!! :P
            if (getConfig().getString("language").equalsIgnoreCase("en")){
            log.info("Mentioner V 0.3-STABLE is starting!");
            }  
            else if (getConfig().getString("language").equalsIgnoreCase("es")){
            log.info("Mentioner V 0.3-ESTABLE se esta iniciando!");
          
            }else if(getConfig().getString("language").equalsIgnoreCase("custom")){
                log.info(getConfig().getString("mentionerenable"));
              
            }
            try {
                Metrics metrics = new Metrics(this);
                metrics.start();
            } catch (IOException e) {
                log.warning("Metrics failed to start for Mentioner. Nothing to worry.");
            }
        }
        public void onDisable(){
            if (getConfig().getString("language").equalsIgnoreCase("en")){
            log.info("Mentioner V 0.3-STABLE was disabled!");
            }else if (getConfig().getString("language").equalsIgnoreCase("es")){
            log.info("Mentioner V 0.3-ESTABLE ha sido desactivado!");  
            }
            else if(getConfig().getString("language").equalsIgnoreCase("custom")){
            log.info(getConfig().getString("mentionerdisable"));
          
        }
          
          
        }  
      
      
    }
    Config:


    Code:
    package mentioner;
    
    public class Config {
    
        private Main plugin;
    
        public Config(Main plugin)
        {
          this.plugin = plugin;
        }
      
      
        public Config()    {
          
        }
      
        public String getLanguage(){
        String language = plugin.getConfig().getString("language");
        plugin.log.severe("DEBUG" + language);
        if(language.equalsIgnoreCase("en") || language.equalsIgnoreCase("es") || language.equalsIgnoreCase("custom") ){  
      
      
      
            return language;
        }//end if
        else{  
        return "en";
        }  
      
      
        }
      
        public void setValue(String path, String args){
        plugin.getConfig().set(path, args);
        plugin.reloadConfig();
        }
      
      
    }
     
  2. Offline

    Scorpionvssub

    so wait to be clear, you are creating plugin B but wanna get the configurations from config A?
     
  3. Offline

    ElCreeperHD

    No, I want to get config (plugin A)from Config class (plugin A) , so I created methods at Config class who get config values.
     
  4. Offline

    Scorpionvssub

    to get anything from the default config you can just do getconfig.getstring(<path to string>); for int just replace string with int and for null check just get(<path>); not complicated same really for custom configs


    When outside the main class you call instance to the main class example via plugin.getconfig im sure java docs are on those not sure just make the new classget the main class as this.plugin = plugin where plugin = <main class>
     
  5. Offline

    Mrs. bwfctower

Thread Status:
Not open for further replies.

Share This Page