Solved Get config from another class.

Discussion in 'Spigot Plugin Development' started by epe07, Jun 29, 2016.

  1. Hi!

    I tried to do it with old tutorials but none of them worked.

    What i am doing wrong?

    Code (Text):
    package eu.epe07.onnenpyörä;

    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin {
     
        VariableManager vam = new VariableManager(this);
        public void onLoad(){
            this.getLogger().info("Hello");
        }
     
    }
     
    Code (Text):
    package eu.epe07.onnenpyörä;

    import org.bukkit.block.Block;



    public class VariableManager {
     
        Main plugin;
     
        public VariableManager(Main instance) {
            plugin = instance;
        }

    }
     
    Eclipse does not find anything when im trying to write plugin.(something)
    so it must me wrong.
     
  2. Commands(Main) class:


    Code (Text):
    public class Commands extends JavaPlugin implements Listener {
     
        File cF;
        FileConfiguration c;
        static Plugin plugin;

        static Plugin getPlugin() {
            return plugin;
        }

        void createConfigYML() {
            cF = new File(getDataFolder() + "/config.yml");
            if (!cF.exists()) {
                cF.getParentFile().mkdirs();
                copy(getResource("config.yml"), cF);
            }
            c = new YamlConfiguration();
            try {
                c.load(cF);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

      static void copy(InputStream in, File file) {
         try {
           OutputStream out = new FileOutputStream(file);
           byte[] buf = new byte[1024];
           int len;
           while ((len = in.read(buf)) > 0) {
             out.write(buf, 0, len);
           }
           out.close();
           in.close();
         } catch (Exception e) {
           e.printStackTrace();
         }
       }

        public void onEnable() {
            plugin = this;
            createConfigYML();
        }

    Getting it in other class:

    Code (Text):
        @EventHandler
        void toggleSneak(PlayerToggleSneakEvent e) {
            if (Commands.c.getString(DoSomethingOnSneak) != null && Commands.c.getBoolean(DoSomethingOnSneak)) {
                //Do something
           
    You need Commands.getPlugin() if you want to start scheduler in other class.
     
    • Informative Informative x 1
  3. I know it might be a dumb question, but are you importing the right Main class. There could be multiple Main classes and you could be importing the wrong one.
     
  4. try:

    Code (Text):
    public Main plugin;

    public VariableManager(Main plugin){
         this.plugin = plugin;
    }

    // Then use:

    plugin.getConfig(your stuff here)

    // To get configuration stuff
     
     
    • Informative Informative x 1
  5. thx
     
    • Like Like x 1
  6. Really happy I could help you !
     
  7. Just out of curiosity, how does what you suggested differ from what he already had in the start?
     
  8. I understand your curiosity. The only thing he didn't understand, was how to use his configuration. I just created a little new part as the same like had it, and showed him how to use the configuration in the external class.
     
  9. But he said when he tries to type plugin.(something), nothing shows up. However, when he uses what you said, which seems identical to what he already had, it works all of the sudden. That's my confusion.
     
  10. Oh like that, will I don't really know why it didn't work. Wrong import maybe? Whatever, it is fixed now so let's stop hijacking his thread :)

    EDIT: He just forgot to import the main class, as he didn't define 'Main plugin;' with an import to his main class.