SettingsManager instructions

Nov 29, 2018
SettingsManager instructions
  • Hey guys, a lot of people are still creating default config.yml files, but I personally dont like.... Just 1 file, so thats why I am making a tutorial on default SettingsManager files, now first I am gonna explain what a SettingsManager is:

    What is a SettingsManager:
    A SettingsManager allows you to create custom files, and custom folders, like "/plugins/PluginName/CustomFolder/CustomFile.yml", now you can of course also do just custom files without the custom folders.

    How to make a SettingsManager:
    I am gonna copy/paste a new class that I am using for a SettingsManager:
    Code (Java):
    package YourPackage;

    import java.io.File;
    import java.io.IOException;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;

    public class YourClass {
        static FileConfiguration YourFile;
        static File pdfile;

        public static void setup() {
            pdfile = new File("plugins/YourPlugin/", "YourFile.yml");

            if (!pdfile.exists()) {
                try {
                    pdfile.createNewFile();
                } catch (IOException e) {
                }
            }

            YourFile = YamlConfiguration.loadConfiguration(pdfile);
        }

        public static FileConfiguration get() {
            return YourFile;
        }

        public static void save() {
            try {
                YourFile.save(pdfile);
            } catch (IOException e) {
                Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not save yourfile.yml!");
            }
        }

        public static void reload() {
            YourFile = YamlConfiguration.loadConfiguration(pdfile);
        }

    }
    And if you wanna create/reload/save/get a file, you do the following in the onEnable() method:
    Code (Java):

    YourClass.setup();
    YourClass.get().addDefault("Default", "Default");
    YourClass.get().options().copyDefaults(true);
    YourClass.save();
     
    That's how you create your settings manager, isn't so hard is it?


    Thanks for reading this page, and good luck making your plugin with custom files.

    - JustMaffie (Bukkit Plugin Developer)


    Usage Examples:

    Let's say your custom config is used to edit messages, booleans, or integers etc. you would want to
    be able to reload the config in game to apply the changes right?
    Here is an example:

    Code (Java):
    public boolean onCommand(CommandSender sender, Command cmd, String cmdLabel, String[] args)
    {
      if (cmd.getName().equalsIgnoreCase("myplugin"))
      {
        if (args.length == 0){
        }
     
        else if ((args[0].equalsIgnoreCase("reload")) && (sender.hasPermission("myplugin.reload")))
        {
            YourClass.getManager().reloadConfig();
        }

    Let's say you are using your custom config to edit a custom message, maybe the reload message.
    Here is an example:

    Code (Java):
    public boolean onCommand(CommandSender sender, Command cmd, String cmdLabel, String[] args)
    {
      if (cmd.getName().equalsIgnoreCase("myplugin"))
      {
        if (args.length == 0){
        }
     
        else if ((args[0].equalsIgnoreCase("reload")) && (sender.hasPermission("myplugin.reload")))
        {
            YourClass.getManager().reloadConfig();
       
            YourClass messages = YourClass.getManager();
            sender.sendMessage
           (ChatColor.translateAlternateColorCodes('&', messages.getConfig().getString("ReloadMessage")));
        }
  • Loading...
  • Loading...