Config File

Discussion in 'Plugin Development' started by InspectorFacepalm, May 12, 2013.

Thread Status:
Not open for further replies.
  1. So I'm making something that's configurable, I did everything right, because I did this on my last plugin for my server, and it worked, I made a random plugin because I was having fun with config's and I decided to make a plugin with a configurable message
    So this is what I did to generate the config.yml
    Code:
            getConfig().options().copyDefaults(true);
            saveConfig();
    
    And then I add this into the config, and add this to the command
    Code:
    String message = getConfig().getString("message");
    message = ChatColor.translateAlternateColorCodes('&', message);
    sender.sendMessage(message);
    
    So I added it to my config.yml and everything, saved it, exported added it to my server,
    And I check the config.yml and it shows as a blank text.
    Anyone know how to fix this?
     
  2. You're not adding the value to the config.yml

    Code:
    getConfig().options().copyDefaults(true);
            saveConfig();
    should be

    Code:
    getConfig().options().copyDefaults(true);
    getConfig().addDefault(pathtostring, value);
    //example: getConfig().addDefault("test", "Loltest");
    saveConfig();
     
  3. Offline

    killerremijn11

  4. Offline

    killerremijn11

    open up your compiled plugin jar with winrar. in there should be a the default .config file, is that empty by any chance?
     
  5. Nope it's not empty
    works for that

    nope doesn't work

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  6. Bye chance, is this in it's own Function? Or in the onEnable()?
     
  7. onEnable
     
  8. Code:java
    1.  
    2. public void loadConfig(){
    3. getConfig().options().copyDefaults(true);
    4. String[] exceptions = {"glass"};
    5. String[] commandstofilter = {"/me", "/msg", "/reply", "/r", "/tell", "/t"};
    6. String[] usernameexceptions = {""};
    7. String[] replaceer = {"meeper", "derper", "smeghead", ""};
    8. String message = "Please don't cuss!";
    9. int maxcaps = 75;
    10. int mintriggersize = 10;
    11. getConfig().addDefault("blockedwords", Arrays.asList(blockedwords));
    12. getConfig().addDefault("commandsto.filter", Arrays.asList(commandstofilter));
    13. getConfig().addDefault("replacewith.er", Arrays.asList(replaceer));
    14. getConfig().addDefault("cussword.replacewith", "meep");
    15. getConfig().addDefault("usernameexceptions", Arrays.asList(usernameexceptions));
    16. getConfig().addDefault("max.caps", maxcaps);
    17. getConfig().addDefault("exceptions", Arrays.asList(exceptions));
    18. getConfig().addDefault("urlexceptions", Arrays.asList(urlexceptions));
    19. getConfig().addDefault("mintriggersize", mintriggersize);
    20. getConfig().addDefault("filteradminchat", "false");
    21. getConfig().addDefault("message", message);
    22. getConfig().addDefault("prefix", "&3CleanChat");
    23. saveConfig();
    24. }
    25.  


    That's my config function. It exports it to its own directory in the plugins folder. Are you on Linux? If so you might have to chmod it to give it read/write perms.

    Just to test if you are, open up the terminal and CD to your Server folder, then type
    chmod -R 777<dir>
    -R is just to recursively chmod all files in that directory.
     
  9. Offline

    Sagacious_Zed Bukkit Docs

    As a general rule on security, recursively giving the entire directory 777 is a bad idea. This means that everyone can read write and execute things in that directory.
     
  10. Yes, that's why I said for testing purposes. If you feel better just give the /plugins folder write permissions with

    chmod +w <dir>
     
  11. ergh, here's my whole code if you like, nothing works for me
    Code:
    import java.io.PrintStream;
    import java.util.logging.Level;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Server;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    @SuppressWarnings("unused")
    public class Config extends JavaPlugin
    {
          public void onEnable()
          {
            getLogger().log(Level.INFO, "[test");
            getServer().getPluginManager().registerEvents((Listener) this, this);
            getConfig().options().copyDefaults(true);
            saveConfig();
            }
    
          public void onDisable() {
            getLogger().log(Level.INFO, "[test");
            saveConfig();
           
          }
    
      public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
      {
        if (label.equalsIgnoreCase("test"))
        {
    
            sender.sendMessage(ChatColor.GOLD +
    " config"); 
            String message1 = getConfig().getString("message");
            message1 = ChatColor.translateAlternateColorCodes('&', message1);
            sender.sendMessage(message1);
    
          
    
    
          return true;
        }
       
        }
        return false;
      }
     
  12. Okay, see where it says "getConfig().options().copyDefaults(true);"? On the next line after that add

    getConfig().addDefault(pathtostring, value);
    //example: getConfig().addDefault("test", "Loltest");
     
  13. dont work
     
Thread Status:
Not open for further replies.

Share This Page