Solved Config connect

Discussion in 'Plugin Development' started by NoSpanMan, Apr 5, 2015.

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

    NoSpanMan

    How can i connect my config to my secondary class?
    First class:
    Code:
    public void onEnable() {
         getConfig().options().copyDefaults(true);
         saveConfig();
         Commands();
       }
    
    Secondary:
    Code:
    private First plugin;
       
       public Secondary(First first) {
         this.plugin = plugin;
         
       }
    
     
  2. Offline

    NoSpanMan

  3. Offline

    NoSpanMan

    getConfig().set("fsfd", p.getName()); getconfig here stays red. @bwfcwalshy
     
  4. @NoSpanMan As I said in my first post you need to do plugin.getConfig() if it's from another class and that class has passed the main instance.
     
  5. you need to pass your Main class through a constructor to your other class (Where you want to access the config).
    Example:
    In the other class:
    Code:Java
    1. public class MyOtherClass {
    2. private MainClass plugin;
    3.  
    4. public MyOtherClass(MainClass plugin) {
    5. this.plugin = plugin;
    6.  
    7. //Do stuff like:
    8. plugin.getConfig().set("some.random.key", "._.");
    9. }
    10. }

    In your main class:
    Code:Java
    1. new MyOtherClass(this);
     
  6. Offline

    NoSpanMan

  7. @NoSpanMan In your second class you have passed instance. Now you need to get the config from the main class. Your code initializes it as plugin so you need to do plugin. then you need to type what you want in this case config. So it is then plugin.getConfig() and there you go.
     
  8. Offline

    NoSpanMan

    Secondary:
    Code:
      private Main plugin;
      
       public Secondary(Main main) {
         this.plugin = plugin;
         plugin.getConfig();
       }
    
    I have this now

    @bwfcwalshy

    @bwfcwalshy @FisheyLP
    Main:
    Code:
      public static First plugin;
      
       public void onEnable() {
         getConfig().options().copyDefaults(true);
         saveConfig();
         Commands();
         new Secondary(this);
       }
    
    Secondary:
    Code:
      private First plugin;
       
       public Secondary(First First) {
         this.plugin = plugin;
         plugin.getConfig();
       }
    
    It still stays red

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  9. the constructor is:
    public Secondary(Main main)
    so you need to do:
    Code:
    this.plugin = main;
     
  10. Offline

    NoSpanMan

    I stil stays red:
    Code:
      private Minigame plugin;
       
       public ArenaCreator(Minigame minigame) {
         this.plugin = minigame;
         plugin.getConfig();
       }
    
    @FisheyLP @bwfcwalshy
     
  11. @NoSpanMan put the plugin.getConfig where you are using the config not just randomly in the constructor.

    EDIT: I just tried your code, works fine for me. No errors.
     
  12. Offline

    NoSpanMan

    Secondary class:
    Code:
    package me.coding.minigame;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.MemorySection;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    
    public class ArenaCreator implements CommandExecutor {
       private Minigame plugin;
       
       public ArenaCreator(Minigame minigame) {
         this.plugin = minigame;
         plugin.getConfig();
       }
       
       public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
         plugin.getConfig();
         Player p = (Player) sender;
         String m = ChatColor.GREEN + "[" + ChatColor.AQUA + "Minigame" + ChatColor.GREEN + "] " + ChatColor.WHITE;
         
         if(cmd.getName().equalsIgnoreCase("fmakearena")) {
           if (p.hasPermission("f.makearena")) {
             getConfig().set("fsfd", p.getName());
           } else {
             p.sendMessage(m);
           }
         }
         return false;
       }
    }
    
     
  13. As I said, change to plugin.getConfig()

    You need this where you are getting you, remove the redundant ones.
     
  14. Offline

    NoSpanMan

    @bwfcwalshy @FisheyLP Can i set these classes in an another package or requires that more code?
     
  15. You can set it in an other package
     
Thread Status:
Not open for further replies.

Share This Page