Skip to content

Commit

Permalink
added YamlFile
Browse files Browse the repository at this point in the history
  • Loading branch information
dkim19375 committed May 22, 2021
1 parent 3bb0182 commit 9eb20d4
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -20,13 +20,15 @@ repositories {
mavenCentral()
maven { url = 'https://jitpack.io' }
maven { url = 'https://m2.dv8tion.net/releases' }
maven { url = 'https://repo.mattstudios.me/artifactory/public' }
}

dependencies {
compileOnly 'net.dv8tion:JDA:4.2.1_262'
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) {
Expand Down
12 changes: 12 additions & 0 deletions src/main/kotlin/me/dkim19375/dkim19375jdautils/BotBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -82,6 +83,11 @@ abstract class BotBase {
*/
open val commands = mutableSetOf<Command>(HelpCommand(this))

/**
* A [Set] of [YamlFiles][YamlFile] to register
*/
open val files = mutableSetOf<YamlFile>()

/**
* An [Events Manager][SpecialEventsManager] useful for handling events such as reaction add listeners
*/
Expand Down Expand Up @@ -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]
*
Expand Down
63 changes: 63 additions & 0 deletions src/main/kotlin/me/dkim19375/dkim19375jdautils/file/YamlFile.kt
Original file line number Diff line number Diff line change
@@ -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 <T : Any> get(property: Property<T>): T = manager.get(property)

@API
fun <T : Any> set(property: Property<T>, 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()
}
}

0 comments on commit 9eb20d4

Please sign in to comment.