Skip to content

Commit

Permalink
feat!: add onLoad option to configs, fires on any load
Browse files Browse the repository at this point in the history
feat: Add Plugin.dataPath extension
  • Loading branch information
0ffz committed Mar 16, 2024
1 parent 0a6f229 commit 6f32bf1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.mineinabyss.idofront.config

import kotlinx.serialization.KSerializer
import java.nio.file.Path
import java.nio.file.attribute.FileAttribute
import kotlin.io.path.*
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
Expand All @@ -19,6 +18,7 @@ class Config<T>(
val lazyLoad: Boolean,
val onFirstLoad: (T) -> Unit = {},
val onReload: (T) -> Unit = {},
val onLoad: (T) -> Unit = {},
) : ReadWriteProperty<Any?, T> {
private var loaded: T? = null
private val fileFormat = checkFormat()
Expand All @@ -38,11 +38,11 @@ class Config<T>(

fun getOrLoad(): T {
loaded?.let { return it }
return load().also(onFirstLoad)
return load().also(onFirstLoad).also(onLoad)
}

fun reload(): T {
return load().also(onReload)
return load().also(onReload).also(onLoad)
}

private fun load(): T {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.mineinabyss.idofront.config

import kotlinx.serialization.serializer
import org.bukkit.plugin.Plugin
import java.nio.file.Path

typealias IdofrontConfig<T> = Config<T>
Expand All @@ -16,6 +15,7 @@ inline fun <reified T> config(
mergeUpdates: Boolean = true,
preferredFormat: String = "yml",
lazyLoad: Boolean = false,
noinline onLoad: (T) -> Unit = {},
noinline onFirstLoad: (T) -> Unit = {},
noinline onReload: (T) -> Unit = {},
): Config<T> {
Expand All @@ -29,6 +29,7 @@ inline fun <reified T> config(
formats = formats,
mergeUpdates = mergeUpdates,
lazyLoad = lazyLoad,
onLoad = onLoad,
onFirstLoad = onFirstLoad,
onReload = onReload,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.mineinabyss.idofront.plugin

import org.bukkit.plugin.Plugin

val Plugin.dataPath get() = dataFolder.toPath()

0 comments on commit 6f32bf1

Please sign in to comment.