Skip to content
This repository has been archived by the owner on Oct 25, 2022. It is now read-only.

Commit

Permalink
Merge pull request #9 from hazae41/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
hazæ41 authored Jun 2, 2019
2 parents 03f4d5a + 1421cea commit 90d1711
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 139 deletions.
94 changes: 25 additions & 69 deletions src/main/kotlin/bukkit/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,49 +16,30 @@ import org.bukkit.util.Vector
import java.io.File
import kotlin.reflect.KProperty


@Deprecated("Use loadConfig() instead")
fun BukkitPlugin.load(file: File, resource: String = file.nameWithoutExtension + "/bukkit.yml") = loadConfig(file, resource)

@Deprecated("Replace with delegated configuration (ConfigFile)")
fun BukkitPlugin.loadConfig(
file: File, resource: String = file.name
): BukkitConfiguration {
saveResource(resource, file)
return BukkitConfiguration.loadConfiguration(file)
?: throw hazae41.minecraft.kotlin.ex("Could not load ${file.name}")
}

fun saveConfig(config: BukkitConfiguration, file: File) = config.save(file)

fun BukkitPlugin.saveResource(resource: String, file: File) {
if (file.exists()) return
file.parentFile.mkdirs()
file.createNewFile()
getResource(resource).copyTo(file.outputStream())
}

fun BukkitPlugin.init(vararg configs: ConfigFile) {
configs.forEach { it.init(this) }
fun BukkitPlugin.init(vararg configs: PluginConfigFile) {
configs.forEach {
if(it.file != null) throw ex("Config is already initialized")
val fileName = "${it.path}.yml"
val file = dataFolder[fileName]
saveResource(fileName, file)
it.file = file
}
}

abstract class Config(open var autoSave: Boolean = true) {

private lateinit var _config: ConfigurationSection
var config: ConfigurationSection
set(value) {
_config = value
}
get() {
if (!::_config.isInitialized) reload()
return _config
}
abstract val config: ConfigurationSection
abstract fun save()

open val sections get() = config.sections

abstract fun reload()
abstract fun save()

open operator fun contains(key: String) = config.contains(key)
open operator fun get(key: String) = config[key]
open operator fun set(key: String, value: Any){
Expand Down Expand Up @@ -228,59 +209,34 @@ abstract class Config(open var autoSave: Boolean = true) {
}
}

open class ConfigFile(autoSave: Boolean = true) : Config(autoSave) {

lateinit var file: File

constructor(
file: File, autoSave: Boolean = true
) : this(autoSave) {
this.file = file
}
open class ConfigFile(var file: File?, autoSave: Boolean = true) : Config(autoSave) {

private lateinit var path: String

constructor(
path: String, autoSave: Boolean = true
) : this(autoSave) {
if (!path.endsWith(".yml"))
this.path = "$path.yml"
else this.path = path
}

fun init(plugin: BukkitPlugin, resource: String = path) {
if (::file.isInitialized)
throw ex("Config is already initialized")

file = plugin.dataFolder[path]

if (resource.endsWith(".yml"))
plugin.saveResource(resource, file)
else
plugin.saveResource("$resource.yml", file)
}

override fun reload() {
if (!::file.isInitialized) throw ex("Config is not initialized")
config = BukkitConfiguration.loadConfiguration(file)
?: throw ex("Could not load ${file.name}")
}
override val config: ConfigurationSection
get() {
val file = file ?: throw ex("Config is not initialized")
val config = BukkitConfiguration.loadConfiguration(file)
return config ?: throw ex("Could not load ${file.name}")
}

override fun save() {
val file = file ?: throw ex("Config is not initialized")
val config = config as? FileConfiguration
?: throw ex("Could not save ${config.name} to ${file.name}")
config.save(file)
}
}

open class PluginConfigFile(var path: String, autoSave: Boolean = true): ConfigFile(null, autoSave)

open class ConfigSection(
var parent: Config, var path: String, autoSave: Boolean = true
) : Config(autoSave) {

override fun reload() {
config = parent.config.getConfigurationSection(path)
?: throw ex("Could not load $path from ${parent.config.name}")
}
override val config: ConfigurationSection
get() {
val config = parent.config.getConfigurationSection(path)
return config ?: throw ex("Could not load $path from ${parent.config.name}")
}

override fun save() {
parent.config.set(path, config)
Expand Down
96 changes: 26 additions & 70 deletions src/main/kotlin/bungee/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,7 @@ import net.md_5.bungee.config.Configuration
import java.io.File
import kotlin.reflect.KProperty

val configProvider get() = BungeeConfigurationProvider.getProvider(BungeeYaml::class.java)

@Deprecated("Use loadConfig() instead")
fun BungeePlugin.load(file: File, resource: String = file.nameWithoutExtension + "/bungee.yml") = loadConfig(file, resource)

fun BungeePlugin.loadConfig(
file: File, resource: String = file.name
): Configuration {
saveResource(resource, file)
return configProvider.load(file)
?: throw ex("Could not load ${file.path}")
}
val configProvider get() = BungeeConfigurationProvider.getProvider(BungeeYaml::class.java)!!

fun BungeePlugin.saveResource(resource: String, file: File) {
if (file.exists()) return
Expand All @@ -29,35 +18,26 @@ fun BungeePlugin.saveResource(resource: String, file: File) {
getResourceAsStream(resource).copyTo(file.outputStream())
}

@Deprecated("Use saveConfig() instead")
fun BungeePlugin.save(config: BungeeConfiguration, file: File) = saveConfig(config, file)

fun saveConfig(config: BungeeConfiguration, file: File) = configProvider.save(config, file)

fun BungeeConfiguration.section(path: String) = getSection(path)
val BungeeConfiguration.sections get() = keys.map { section(it) }

fun BungeePlugin.init(vararg configs: ConfigFile) {
configs.forEach { it.init(this) }
fun BungeePlugin.init(vararg configs: PluginConfigFile) {
configs.forEach {
if(file != null) throw ex("Config is already initialized")
val fileName = "${it.path}.yml"
val file = dataFolder[fileName]
saveResource(fileName, file)
it.file = file
}
}

abstract class Config(open var autoSave: Boolean = true) {

private lateinit var _config: Configuration
var config: Configuration
set(value) {
_config = value
}
get() {
if (!::_config.isInitialized) reload()
return _config
}
abstract val config: Configuration
abstract fun save()

open val sections get() = config.sections

abstract fun reload()
abstract fun save()

open operator fun contains(key: String) = config.contains(key)
open operator fun get(key: String) = config[key]
open operator fun set(key: String, value: Any){
Expand Down Expand Up @@ -213,53 +193,29 @@ abstract class Config(open var autoSave: Boolean = true) {
}
}

open class ConfigFile(autoSave: Boolean = true) : Config(autoSave) {

lateinit var file: File
open class ConfigFile(var file: File?, autoSave: Boolean = true) : Config(autoSave) {

constructor(
file: File, autoSave: Boolean = true
) : this(autoSave) {
this.file = file
}

private lateinit var path: String

constructor(
path: String, autoSave: Boolean = true
) : this(autoSave) {
if (!path.endsWith(".yml"))
this.path = "$path.yml"
else this.path = path
}

fun init(plugin: BungeePlugin, resource: String = path) {
if (::file.isInitialized)
throw ex("Config is already initialized")
file = plugin.dataFolder[path]
if (resource.endsWith(".yml"))
plugin.saveResource(resource, file)
else
plugin.saveResource("$resource.yml", file)
}

override fun reload() {
if (!::file.isInitialized) throw ex("Config is not initialized")
config = configProvider.load(file)
?: throw ex("Could not load ${file.name}")
}
override val config: Configuration
get() {
val file = file ?: throw ex("Config is not initialized")
val config = configProvider.load(file)
return config ?: throw ex("Could not load ${file.name}")
}

override fun save() = saveConfig(config, file)
override fun save() = configProvider.save(config, file)
}

open class PluginConfigFile(var path: String, autoSave: Boolean = true): ConfigFile(null, autoSave)

open class ConfigSection(
var parent: Config, var path: String, autoSave: Boolean = true
) : Config(autoSave) {

override fun reload() {
config = parent.config.getSection(path)
?: throw ex("Could not load $path from ${parent.javaClass.name}")
}
override val config: Configuration
get() {
val config = parent.config.getSection(path)
return config ?: throw ex("Could not load $path from ${parent.javaClass.name}")
}

override fun save() {
parent.config.set(path, config)
Expand Down

0 comments on commit 90d1711

Please sign in to comment.