Skip to content

Kotlin extensions

Denis Istratov edited this page Jul 29, 2023 · 20 revisions

Repo and repo builder

For class

@Serializable
data class Test(val id: Int, val name: String)

Sample way

import me.redtea.carcadex.kotlin.extensions.*

val json = Json { prettyPrint = true }
    
val repo = repo<String, Test>(File("data")) {
    save {
        json.encodeToString(it)
    }
    
    load {
        json.decodeFromString(it)
    }
}

Alternate case

import me.redtea.carcadex.kotlin.extensions.*

val json = Json { prettyPrint = true }

val repo = repo<String, Test>(File("data/test")) {
    save = json::encodeToString
    load = json::decodeFromString
    filenameExtension = "json" //no reqiured, default - empty string
}

Using plugin

import me.redtea.carcadex.kotlin.extensions.*

val plugin: Plugin = this
val json = Json { prettyPrint = true }

val repo = repo<String, Test>("tests", plugin) {
    save = json::encodeToString
    load = json::decodeFromString

   fileExt = "json" //not requires
}

//or

val testRepo = repo<String, Test>(plugin) { ... } //foldername it will be delivered automatically by the name of the class of value

Using schema

import me.redtea.carcadex.kotlin.extensions.*

val schema: SchemaStrategy<String, Test> = ..

val repo = repo<String, Test> {
    schemaStrategy = schema

    //also you can use all optional functions of RepoBuilder
    concurrent()
}

Other Repo extensions

Also you can use simple set syntax

val repo = ...
repo["Key"] = Value("Key", ...)

Messages builder

messages {
    legacy = false //will legacy format be used? not requires, default - false
    this.plugin = this //your plugin, requires
    
    //Chose one:
    fileName = "messages.yml" //name of messages file, not requires, default - messages.yml
    file = File("messages") //messages file, not requires, default - File(plugin.dataFolder, fileName)
    
    verifier = FileMessageVerifier(..) //messages verifier, not requires, it has default verifier if set plugin and fileName
}

Other functions

fun CommandSender.send(message: Message) //sends message to player

fun matchMaterial(material: String): Material //get material from string

Clone this wiki locally