Skip to content

Commit

Permalink
feat: More KtConfigFile methods
Browse files Browse the repository at this point in the history
  • Loading branch information
sya-ri committed Jan 29, 2024
1 parent 8ac9087 commit c76890a
Showing 1 changed file with 159 additions and 0 deletions.
159 changes: 159 additions & 0 deletions src/main/kotlin/dev/s7a/ktconfig/KtConfigFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,26 @@ inline fun <reified T : Any> ktConfigFile(
return KtConfigFile<T>(file, setting).load()
}

/**
* Load config from [JavaPlugin.dataFolder]/[path].
*
* @param plugin JavaPlugin
* @param path File path in plugin data folder
* @param setting Config setting
* @param T Config type
* @return Config data or null
* @throws dev.s7a.ktconfig.exception.UnsupportedTypeException
* @throws dev.s7a.ktconfig.exception.TypeMismatchException
* @since 1.0.0
*/
inline fun <reified T : Any> ktConfigFile(
plugin: JavaPlugin,
path: String,
setting: KtConfigSetting = KtConfigSetting(),
): T? {
return KtConfigFile<T>(plugin, path, setting).load()
}

/**
* Load config from [JavaPlugin.dataFolder]/[path].
*
Expand Down Expand Up @@ -83,6 +103,72 @@ inline fun <reified T : Any> ktConfigFile(
return KtConfigFile<T>(file, default, setting).load()
}

/**
* Load config from [JavaPlugin.dataFolder]/[path]. If the file doesn't exist or is empty, save [default].
*
* @param plugin JavaPlugin
* @param path File path in plugin data folder
* @param default Default config data
* @param setting Config setting
* @param T Config type
* @receiver [JavaPlugin]
* @return Config data or [default]
* @throws dev.s7a.ktconfig.exception.UnsupportedTypeException
* @throws dev.s7a.ktconfig.exception.TypeMismatchException
* @since 1.0.0
*/
inline fun <reified T : Any> ktConfigFile(
plugin: JavaPlugin,
path: String,
noinline default: () -> T,
setting: KtConfigSetting = KtConfigSetting(),
): T {
return KtConfigFile(plugin, path, default, setting).load()
}

/**
* Load config from [JavaPlugin.dataFolder]/[path]. If the file doesn't exist or is empty, save [default].
*
* @param plugin JavaPlugin
* @param path File path in plugin data folder
* @param default Default config data
* @param setting Config setting
* @param T Config type
* @return Config data or [default]
* @throws dev.s7a.ktconfig.exception.UnsupportedTypeException
* @throws dev.s7a.ktconfig.exception.TypeMismatchException
* @since 1.0.0
*/
inline fun <reified T : Any> ktConfigFile(
plugin: JavaPlugin,
path: String,
default: T,
setting: KtConfigSetting = KtConfigSetting(),
): T {
return KtConfigFile(plugin, path, default, setting).load()
}

/**
* Load config from [JavaPlugin.dataFolder]/[path]. If the file doesn't exist or is empty, save [default].
*
* @param path File path in plugin data folder
* @param default Default config data
* @param setting Config setting
* @param T Config type
* @receiver [JavaPlugin]
* @return Config data or [default]
* @throws dev.s7a.ktconfig.exception.UnsupportedTypeException
* @throws dev.s7a.ktconfig.exception.TypeMismatchException
* @since 1.0.0
*/
inline fun <reified T : Any> JavaPlugin.ktConfigFile(
path: String,
noinline default: () -> T,
setting: KtConfigSetting = KtConfigSetting(),
): T {
return KtConfigFile(this, path, default, setting).load()
}

/**
* Load config from [JavaPlugin.dataFolder]/[path]. If the file doesn't exist or is empty, save [default].
*
Expand Down Expand Up @@ -121,6 +207,25 @@ inline fun <reified T : Any> saveKtConfigFile(
KtConfigFile<T>(file, setting).save(content)
}

/**
* Save config to [JavaPlugin.dataFolder]/[path].
*
* @param plugin JavaPlugin
* @param path File path in plugin data folder
* @param content Config data
* @param setting Config setting
* @param T Config type
* @since 1.0.0
*/
inline fun <reified T : Any> saveKtConfigFile(
plugin: JavaPlugin,
path: String,
content: T,
setting: KtConfigSetting = KtConfigSetting(),
) {
KtConfigFile<T>(plugin, path, setting).save(content)
}

/**
* Save config to [JavaPlugin.dataFolder]/[path].
*
Expand Down Expand Up @@ -171,6 +276,22 @@ inline fun <reified T : Any> KtConfigFile(
return KtConfigFile(plugin.dataFolder.resolve(path), setting)
}

/**
* Handle config as [File].
*
* @param path File path in plugin data folder
* @param setting Config setting
* @param T Config type
* @receiver [JavaPlugin]
* @since 1.0.0
*/
inline fun <reified T : Any> JavaPlugin.KtConfigFile(
path: String,
setting: KtConfigSetting = KtConfigSetting(),
): KtConfigFile<T> {
return KtConfigFile(dataFolder.resolve(path), setting)
}

/**
* Handle config as [File].
*
Expand Down Expand Up @@ -247,6 +368,44 @@ inline fun <reified T : Any> KtConfigFile(
return KtConfigFile(plugin, path, { default }, setting)
}

/**
* Handle config as [File].
*
* @param path File path in plugin data folder
* @param default Default config data
* @param setting Config setting
* @param T Config type
* @receiver [JavaPlugin]
* @since 1.0.0
*/
@Suppress("FunctionName")
inline fun <reified T : Any> JavaPlugin.KtConfigFile(
path: String,
noinline default: () -> T,
setting: KtConfigSetting = KtConfigSetting(),
): KtConfigFile.Default<T> {
return KtConfigFile(dataFolder.resolve(path), default, setting)
}

/**
* Handle config as [File].
*
* @param path File path in plugin data folder
* @param default Default config data
* @param setting Config setting
* @param T Config type
* @receiver [JavaPlugin]
* @since 1.0.0
*/
@Suppress("FunctionName")
inline fun <reified T : Any> JavaPlugin.KtConfigFile(
path: String,
default: T,
setting: KtConfigSetting = KtConfigSetting(),
): KtConfigFile.Default<T> {
return KtConfigFile(this, path, { default }, setting)
}

/**
* Handle config as [File].
*
Expand Down

0 comments on commit c76890a

Please sign in to comment.