Allay provides a flexible configuration file handler that supports multiple formats. This tutorial will show you how
to use the Config class to manage configuration files in your plugins.
importorg.allaymc.api.utils.config.Config;publicclassMyPluginextendsPlugin{privateConfigconfig;@OverridepublicvoidonEnable(){// Create a config file in the plugin's data folderFileconfigFile=newFile(getPluginContainer().dataFolder().toFile(),"config.yml");config=newConfig(configFile,Config.YAML);}}
importorg.allaymc.api.utils.config.Config;importorg.allaymc.api.utils.config.ConfigSection;ConfigSectiondefaults=newConfigSection();defaults.put("debug",false);defaults.put("maxPlayers",100);defaults.put("welcomeMessage","Welcome to the server!");Configconfig=newConfig(configFile,Config.YAML,defaults);
// Get values with type-specific methodsStringname=config.getString("server.name");intport=config.getInt("server.port");doubleratio=config.getDouble("settings.ratio");booleandebug=config.getBoolean("settings.debug");longtimestamp=config.getLong("lastUpdate");// Get with default value (returned if key doesn't exist)Stringmotd=config.getString("motd","Default MOTD");intmaxPlayers=config.getInt("maxPlayers",20);booleanenabled=config.getBoolean("enabled",true);
// Get typed listsList<String>worlds=config.getStringList("worlds");List<Integer>ports=config.getIntegerList("ports");List<Double>ratios=config.getDoubleList("ratios");List<Boolean>flags=config.getBooleanList("flags");// Get a generic listList<?>items=config.getList("items");
// Get a section as ConfigSectionConfigSectiondbSection=config.getSection("database");Stringhost=dbSection.getString("host");intport=dbSection.getInt("port");// Get all sections at root levelConfigSectionallSections=config.getSections();// Get all sections under a specific pathConfigSectionsubSections=config.getSections("servers");
// Set simple valuesconfig.set("server.name","My Server");config.set("server.port",19132);config.set("settings.debug",true);config.set("settings.ratio",1.5);// Set listsconfig.set("admins",Arrays.asList("Steve","Alex","Notch"));config.set("ports",Arrays.asList(19132,19133,19134));// Set nested values (automatically creates parent sections)config.set("database.credentials.username","newuser");
// Check if a key existsif(config.exists("database.host")){// Key exists}// Check with case-insensitive matchingif(config.exists("DATABASE.HOST",true)){// Key exists (ignoring case)}// Remove a keyconfig.remove("oldSetting");// Get all keysSet<String>keys=config.getKeys();// Include nested keysSet<String>topKeys=config.getKeys(false);// Only top-level keys
// Check value types before gettingif(config.isString("name")){Stringname=config.getString("name");}if(config.isInt("count")){intcount=config.getInt("count");}if(config.isList("items")){List<?>items=config.getList("items");}if(config.isSection("database")){ConfigSectiondb=config.getSection("database");}