Sueta

KotLib

0.2.12

A kotlin libraries which included stdlib, reflect, coroutine, exposed and some of my apis

·Plugin

This libraries is used as dependency for all of my Kotlin plugins.

The download link has been moved to Github Releases

The download button from spigot page is just for plugin version. If you wish an API version, check out the section below.

The libraries Includes:

  • kotlin-stdlib
  • kotlin-reflect
  • kotlinx-coroutine
  • exposed (very good kotlin SQL framework)
  • some of my Apis, some rewrote from mc-utils

All should be the latest version, you can keep tracking the version from here

This plugin can support 1.13 below if you only use as kotlin libraries.

Use As Dependency

Maven

<repository>
            <id>chu77</id>
            <url>https://nexus.chu77.xyz/repository/plugins/</url>
</repository>

Click here for the dependency

If you don't want to use Maven / Groovy, you can instant download from here: (the page is just from the Dependency link)

Known Bugs:
- serializing ConfigurationSerializable object with Map (e.g. Map<String, ItemStack>) will cause an error. I currently have no idea how to solve right now, but uses a wrapper to wrap the ConfigurationSerializable object can solve the problem. (e.g. Map<String, ItemWrapper> where data class ItemWrapper(var item: ItemStack) )

Single Pool Transaction (with HikariCP)

single_pool_enable: false
data_source: "mysql"
driver: "com.mysql.jdbc.Driver"
host: "localhost"
port: 3306
database: "database"
username: "username"
password: "password"
use_ssl: false
pool:
  name: KotLib
  min_size: 5
  max_size: 10
  connection_timeout: 30000
  idle_timeout: 60000
  max_life_time: 600000
# jdbc:mysql://localhost:3306/database
KotLib.transaction {
        // do sth sql statement
}

New Libraries

ItemStack

val stone = itemStack(Material.STONE,
                display = "&eSTONE",
                lore = listOf("normal", "&bstone"),
                amount = 20) { stack ->
            val click = when(action){
                Action.RIGHT_CLICK_AIR, Action.RIGHT_CLICK_BLOCK -> "right"
                Action.LEFT_CLICK_AIR, Action.LEFT_CLICK_BLOCK -> "left"
                else -> "middle"
            }
            player.sendMessage("you $click clicked ${stack.type}")
        }
        val apple = itemStack(Material.APPLE,
                consumeEvent = {
                    player.sendMessage("you just ate an apple")
                })

Inventory

val stone = itemStack(Material.STONE)
        val apple = itemStack(Material.APPLE)
        val bread = itemStack(Material.BREAD)
        val button = itemStack(Material.STONE, display = "this is a button")
        val inv = createGUI(rows = 6,title = "this is title",
                fills = mapOf(
                        0..4 to Clicker(stone),
                        5..8 to Clicker(apple),
                        (6 row 1)..(6 row 9) to Clicker(bread)
                )) {
            mapOf(
                    3 row 5 to Clicker(button){ player, _ -> player.sendMessage("You clicked a button !")},
                    35 to Clicker(button){player, _ -> player.sendMessage("Another button!") }
            )
        }

BigCommands

class Test : BukkitPlugin() {

    override fun onEnable() {
        registerCmd(Main)
    }

    object Main : BukkitCommand(
            name = "main",
            description = "main command",
            permission = "main.use",
            child = arrayOf(
                   BukkitCommand(
                           name = "use",
                           description = "main use command",
                           permission = "main.use.use",
                           placeholders = arrayOf("item")){
                       commandSender, strings -> commandSender.sendMessage("I used ${strings[0]}")
                   },
                    BukkitCommand(
                            name = "say",
                            description = "main say command",
                            permission = "main.use.say",
                            placeholders = arrayOf("messages")){
                        commandSender, strings -> commandSender.sendMessage("I said ${strings.joinToString(" ")}")
                    }
            )
    )
}

Configuration

private lateinit var test: Test

    @Resource(locate = "test.yml")
    data class Test(
            var name: String,
            var weight: Double,
            var members: Map<String, Person>,
            var stack: ItemStack?,
            var color: ChatColor,
            var loc: Location?) : ConfigFile()

    data class Person(
            var name: String,
            var weight: Double)

    override fun enable() {
        val manager = KotLib.getConfigFactory(this)
                .register(kClassOf<Test>())
                .dump()

        test = manager.getConfig(kClassOf())
        warning(test.name)
        warning("${test.color}${test.name}")
        warning(test.weight.toString())
        warning(test.members.toString())
        test.stack ?.also {
            warning(it.toString())
        }

        test.loc ?.also {
            warning(it.toString())
        }

        val testCommand = object : BukkitCommand("test", "test commands",
                child = arrayOf(
                        BukkitCommand("save", "save config") { sender, _ ->
                            run {
                                test.name = "BITCH"
                                test.weight = 99.9
                                test.members = mapOf("fuck" to Person("fucker", 44.4), "shit" to Person("shiter", 11.1))
                                test.stack = itemStack(Material.APPLE,
                                            display = "&cBeautiful Apple",
                                            lore = listOf("want", "to", "try", "&bTHAT ?"))
                                test.loc = Location(Bukkit.getWorlds()[0], 1.0, 2.0, 3.0, 4.4F, 5.5F)
                                test.save()
                                sender.sendMessage("something changed!")
                            }
                        },
                        BukkitCommand("reload", "reload config") { commandSender, _ ->
                            run {
                                test.reload()
                                commandSender.sendMessage("reloaded , latest is")
                                commandSender.sendMessage(test.toString())
                            }
                        }
                )){}

        registerCmd(testCommand)
    }

Data Configuration

@DataResource(folder = "RecordData")
data class Record(@PrimaryKey val id: Int, var comments: String, var timestamp: Long) : DataFile

class RecordController(d: Dao<Record, Int>) : Dao<Record, Int> by d

override fun enable() {

        val manager = KotLib.getConfigFactory(this)
                .registerDao(Record::class, RecordController::class)
                .dump()

        val con = manager.getDao(RecordController::class)

        val testCommand = object : BukkitCommand("test", "test commands",
                child = arrayOf(
                        BukkitCommand("create", "create record",
                                placeholders = arrayOf("id", "comments")) { sender, args ->
                            run {
                                val id = args[0].toIntOrNull() ?: return@BukkitCommand
                                val comments = args.copyFrom(1).joinToString(" ")
                                con.save {
                                    Record(id, comments, System.currentTimeMillis())
                                }
                                sender.sendMessage("added $id by comments $comments")
                            }
                        },
                        BukkitCommand("delete", "delete record",
                                placeholders = arrayOf("id")) { sender, args ->
                            run {
                                val id = args[0].toIntOrNull() ?: return@BukkitCommand
                                if (con.delete(id)) {
                                    sender.sendMessage("Delete Success")
                                } else {
                                    sender.sendMessage("id $id not exist !")
                                }
                            }
                        },
                        BukkitCommand("update", "update record",
                                placeholders = arrayOf("id", "comments")) { sender, args ->
                            run {
                                val id = args[0].toIntOrNull() ?: return@BukkitCommand
                                val comments = args.copyFrom(1).joinToString(" ")
                                con.update(id) {
                                    this.comments = comments
                                    this.timestamp = System.currentTimeMillis()
                                } ?: let { sender.sendMessage("id $id not exist!"); return@BukkitCommand }
                                sender.sendMessage("Edit success!")
                            }
                        },
                        BukkitCommand("find", "find record",
                                placeholders = arrayOf("id")) { sender, args ->
                            val id = args[0].toIntOrNull() ?: return@BukkitCommand
                            val rec = con.findById(id) ?: let { sender.sendMessage("id $id not exist !") ; return@BukkitCommand }
                            sender.sendMessage("id: ${rec.id}")
                            sender.sendMessage("comments: ${rec.comments}")
                            sender.sendMessage("time: ${rec.timestamp}")
                        }
                )) {}

        registerCmd(testCommand)
    }

Data Configuration with Foreign Key

class KotLibTester : BukkitPlugin() {

    @DataResource(folder = "Address")
    data class Address(
            @PrimaryKey val name: String,
            val address: String
    ) : DataFile

    @DataResource(folder = "School")
    data class School(
            @PrimaryKey val name: String,
            @ForeignKey(Address::class) var locate: String?,
            @ForeignObject var address: Address? = null
    ) : DataFile


    class AddressController(dao: Dao<Address, String>) : Dao<Address, String> by dao

    class SchoolController(dao: Dao<School, String>) : Dao<School, String> by dao

    override fun enable() {
        val manager = KotLib.getConfigFactory(this)
                .registerDao(Address::class, AddressController::class)
                .registerDao(School::class, SchoolController::class)
                .dump()

        val addressController = manager.getDao(AddressController::class)

        val schoolController = manager.getDao(SchoolController::class)

        addressController.save { Address("A", "city A, HK") }
        addressController.save { Address("B", "city B, HK") }
        addressController.save { Address("C", "city C HK") }

        schoolController.save { School("A school", "A") }
        schoolController.save { School("B school", "B") }
        schoolController.save { School("C school", "C") }

        // Normal
        kLogger.info("=== Test 1 ===")
        schoolController.findAll().forEach { s -> kLogger.info(s.toString())}

        schoolController.update("A school") { locate = "C" }

        // Changed foreign key
        kLogger.info("=== Test 2 ===")
        schoolController.findAll().forEach { s -> kLogger.info(s.toString())}

        addressController.delete("B")

        // Removed address B
        kLogger.info("=== Test 3 ===")
        schoolController.findAll().forEach { s -> kLogger.info(s.toString())}

        schoolController.update("C school") { locate = null }

        // Removed foreign key
        kLogger.info("=== Test 4 ===")
        schoolController.findAll().forEach { s -> kLogger.info(s.toString())}

    }
}

Result:

Similar projects

Plugin →