SettingsManager instructions

Apr 25, 2020
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 net.md_5.bungee.api.ChatColor;
    import org.bukkit.Bukkit;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.java.JavaPlugin;

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

    public class YourClass {
        static FileConfiguration fileConfig;
        static File file;
        static String fileName;

        public static void setup(JavaPlugin plugin, String ymlName) {
            file = new File(plugin.getDataFolder(), ymlName);

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

            fileName = ymlName;
            fileConfig = YamlConfiguration.loadConfiguration(file);
        }

        public static FileConfiguration getConfig() {
            return fileConfig;
        }

        public static void saveConfig() {
            try {
                fileConfig.save(file);
            } catch (IOException e) {
                Bukkit.getServer().getLogger().severe(ChatColor.RED + String.format("Could not save %s!", fileName));
            }
        }

        public static void reloadConfig() {
            fileConfig = YamlConfiguration.loadConfiguration(file);
        }
    }
     
    And if you wanna create/reload/save/get a file, you do the following in the onEnable() method:
    Code (Java):

    YourClass.setup(yourMainClass, "somefile.yml");
    YourClass.getConfig().addDefault("Default", "Default");
    YourClass.getConfig().options().copyDefaults(true);
    YourClass.saveConfig();
     
    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.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.reloadConfig();
            sender.sendMessage
           (ChatColor.translateAlternateColorCodes('&', YourClass.getConfig().getString("ReloadMessage")));
        }
  • Loading...
  • Loading...