diff --git a/build.gradle b/build.gradle index 79b3495..ab547e5 100644 --- a/build.gradle +++ b/build.gradle @@ -8,7 +8,7 @@ sourceCompatibility = targetCompatibility = JavaVersion.VERSION_1_8 compileJava.options.encoding 'UTF-8' group 'me.dkim19375' -version '2.4.0' +version '2.5.0' //noinspection GrUnresolvedAccess compileKotlin.kotlinOptions { @@ -20,6 +20,7 @@ repositories { mavenCentral() maven { url = 'https://jitpack.io' } maven { url = 'https://m2.dv8tion.net/releases' } + maven { url = 'https://repo.mattstudios.me/artifactory/public' } } dependencies { @@ -27,6 +28,7 @@ dependencies { api 'commons-io:commons-io:2.8.0' api 'org.apache.commons:commons-lang3:3.12.0' api 'com.github.minndevelopment:jda-ktx:+' + api 'me.mattstudios:triumph-config:1.0.5-SNAPSHOT' } task copyFileToBot(type: Copy) { diff --git a/src/main/kotlin/me/dkim19375/dkim19375jdautils/BotBase.kt b/src/main/kotlin/me/dkim19375/dkim19375jdautils/BotBase.kt index a29ff68..c78b52c 100644 --- a/src/main/kotlin/me/dkim19375/dkim19375jdautils/BotBase.kt +++ b/src/main/kotlin/me/dkim19375/dkim19375jdautils/BotBase.kt @@ -8,6 +8,7 @@ import me.dkim19375.dkim19375jdautils.command.HelpCommand import me.dkim19375.dkim19375jdautils.command.OTHER_TYPE import me.dkim19375.dkim19375jdautils.event.CustomListener import me.dkim19375.dkim19375jdautils.event.EventListener +import me.dkim19375.dkim19375jdautils.file.YamlFile import me.dkim19375.dkim19375jdautils.impl.CustomJDABuilder import me.dkim19375.dkim19375jdautils.managers.SpecialEventsManager import net.dv8tion.jda.api.JDA @@ -82,6 +83,11 @@ abstract class BotBase { */ open val commands = mutableSetOf(HelpCommand(this)) + /** + * A [Set] of [YamlFiles][YamlFile] to register + */ + open val files = mutableSetOf() + /** * An [Events Manager][SpecialEventsManager] useful for handling events such as reaction add listeners */ @@ -167,6 +173,12 @@ abstract class BotBase { } } + @API + open fun reloadFiles() = files.forEach(YamlFile::reload) + + @API + open fun saveFiles() = files.forEach(YamlFile::save) + /** * The method which is called them a command is being sent to all of the [Command]s in [commands] * diff --git a/src/main/kotlin/me/dkim19375/dkim19375jdautils/file/YamlFile.kt b/src/main/kotlin/me/dkim19375/dkim19375jdautils/file/YamlFile.kt new file mode 100644 index 0000000..2e2ccc0 --- /dev/null +++ b/src/main/kotlin/me/dkim19375/dkim19375jdautils/file/YamlFile.kt @@ -0,0 +1,63 @@ +package me.dkim19375.dkim19375jdautils.file + +import me.dkim19375.dkim19375jdautils.annotation.API +import me.mattstudios.config.SettingsHolder +import me.mattstudios.config.SettingsManager +import me.mattstudios.config.properties.Property +import java.io.File +import java.nio.file.Path +import java.nio.file.Paths +import kotlin.io.path.createDirectories +import kotlin.io.path.createFile +import kotlin.io.path.notExists + +@API +open class YamlFile(@API val properties: SettingsHolder, @API val fileName: String) { + @API + val path: Path + @API + val file: File + get() = path.toFile() + @API + val manager: SettingsManager + + init { + val array = fileName.replace("\\", "/").split("/").toTypedArray() + if (array.isEmpty()) { + throw IllegalArgumentException("The file path cannot be empty!") + } + val first = array[0] + val rest = array.drop(1) + val path = Paths.get(first, *rest.toTypedArray()) + this.path = path + if (path.notExists()) { + path.parent.createDirectories() + path.createFile() + } + manager = SettingsManager.from(file).configurationData(properties.javaClass).create() + } + + @API + fun get(property: Property): T = manager.get(property) + + @API + fun set(property: Property, value: T) = manager.set(property, value) + + @API + fun reload() { + if (path.notExists()) { + path.parent.createDirectories() + path.createFile() + } + manager.reload() + } + + @API + fun save() { + if (path.notExists()) { + path.parent.createDirectories() + path.createFile() + } + manager.save() + } +} \ No newline at end of file