Skip to content

Latest commit

 

History

History
115 lines (91 loc) · 3.43 KB

config.md

File metadata and controls

115 lines (91 loc) · 3.43 KB

ConfigManager

ConfigManager2 is based on jackson.

Support:

  • JSON, YAML, TOML, HOCON, Properties, XML, CSV.
  • Tree-mode & Class-mapping-mode.
  • Bukkit's ConfigurationSerializable like ItemStack, ItemMeta, Location, etc..

Setup:

// In plugin onLoad
val configManager = ConfigManager2(this)

Read config:

// Check if config exists
configManager.touch("config.yml")
// Get config
val config = configManager["config.yml"]
// Visit config
config.root {
    // it == ObjectNode of jackson
    val enableMultiHome = it["multi-home", "enable"].asBoolean()
    val homeLimit = it["multi-home", "limit"].asInt(1) // can set default value
    val inventory = it["inventory"].asBase64Inventory()
    val location = it["location"].asConfigurationSerializable<Location>()
}

// Class mapping mode
data class Config(
    val a: Int,
    val b: String,
)
val c1 = ConfigManager2.parse<Config>("""{"a":1,"b":"a"}""", ConfigManager2.ConfigFileType.Json) // to object
val c2 = ConfigManager2.parse<Config>(File("/root/example/config.yml")) // parse object from file
val c3 = configManager.parse<Config>("config.yml") // parse object from config

Write:

// Write and save
configManager["store.json"].save {
    it.set("last-update", System.currentTimeMillis())
    it.set("last-player", Bukkit.getOfflinePlayers()[0])
    it.set("example-item", ItemStack(Material.APPLE, 64))
    it.setBase64("example-inventory", Bukkit.getOfflinePlayers()[0].player!!.inventory)
    it.remove("foo")
}

// Class mapping mode
ConfigManager2.stringify(Config(1, "a"), ConfigManager2.ConfigFileType.Json) // to string: {"a":1,"b":"a"}
ConfigManager2.save(Config(1, "a"), File("/root/example/config.yml")) // save object to file
configManager.save(Config(1, "a"), "config.yml") // save object to config

Multi-type support:

// Convert type
configManager["1.json"].saveAs("1.xml")

About ObjectNode

Further reading:

locate a noe:

node["123", "456", 12, "a"].asText()
node get "123".asInt()

read value:

node.asInt() / asText() / asBoolean() / ...
node.asList() // ArrayList<Any>
node.asMap() // LinkedHashMap<String, Any>
node.asUUID() // to UUID
node.asMaterial() // to Bukkit Material
node.asType<YourClass>() // to your class
node.asConfigurationSerializable<ItemStack>() // to Bukkit ConfigurationSerializable
node.asBase64Inventory() / asBase64ItemStacks() / asBase64ItemStack()

write value:

node.put("key", 1 / "123" / true / ...)
node.put("key", listOf(1, 2, 3))
node.put("key", mapOf("a" to 1, "b" to 2))
node.put("key", YourClass())
node.put("key", ItemStack(Material.APPLE, 1))
node.putBase64("key", Inventory / ItemStack / Array<ItemStack>)

Reference