Solved Creating a list in config.yml and updating from separate class

Discussion in 'Plugin Development' started by Darkrunner999, Dec 26, 2013.

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

    Darkrunner999

    Hey guys,
    I'm making a plugin that will read strings from a config.yml but I need a way to list the strings in the config yml to access from a separate CommandExecutor class.

    This is what I've got so far.

    Main.java
    Code:java
    1. public void initialConfigGen() {
    2. config = getConfig();
    3. String nL = System.lineSeparator();
    4. if (config.getBoolean("Options.RegenerateConfig") == true) {
    5. config.options()
    6. .header("=-=-=-= [SwearProtect] Configuration =-=-=-="
    7. + nL + nL);
    8. //I need to add a default list of words here, how would I do that?
    9. config.addDefault("Options.AutoUpdateChecker", true);
    10. config.set("Options.Version", 1.0);
    11. config.addDefault("RegenerateConfig", false);
    12. config.options().copyDefaults(true);
    13. saveConfig();
    14. reloadConfig();
    15. }


    Also, since I'm making a thread, I need some way of accessing and adding to the list from a commandExecutor class.

    Command.java
    Code:java
    1. public class Commands implements CommandExecutor {
    2.  
    3. public static Methods m = new Methods();
    4. FileConfiguration config;
    5.  
    6. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    7. Player player = (Player) sender;
    8. if (cmd.getName().equalsIgnoreCase("swearprotect") && player.hasPermission("sp.commands") || player.isOp()){
    9. if (!(sender instanceof Player)) {
    10. sender.sendMessage("This command can only be sent from a player.");
    11. }else{
    12. m.sendCmdNMessage(player, "Type /swearprotect help for more information!");
    13. }
    14. if (args.length == 1){
    15. if(args[0].equalsIgnoreCase("add")){ //How would I add to a list in config.yml and save //from here? *different class.
    16. config.addDefault(path-to-list, args[1]);
    17. saveConfig();
    18. }
    19. }
    20.  
    21. }
    22. return false;
    23. }
    24. }


    Any help would be much appreciated. Thank you.
     
  2. Offline

    pope_on_dope

    you need to add an instance of your main class to your command class. (a constructor in your commands class)
    Code:
    public Commands(Plugin plugin) {
      this.plugin = plugin;
      this.config = plugin.getConfig();
    }
    
    then from there you can use the config to your liking
     
  3. Offline

    aredherring

    Or better still, without breaking encapsulation, just pass in the Config instance via the constructor rather than the plugin itself.

    If you have to reference the plugin from your separate class you should rethink your design.
     
  4. Offline

    jthort

    Or just use this in the onEnable (This is an integer, same idea though)
    Code:
    config.addDefault("Star Size", 4);
    Then just set it to a static integer or string and use that in the other class.

    Or do what pope_on_dope said
     
  5. Offline

    Darkrunner999

    Thanks everyone! :) *Solved.
    I removed 2 of my classes and threw them with the Main class. Now it can all be accessed from there :)
     
Thread Status:
Not open for further replies.

Share This Page