Skip to content

Spigot library with Kotlin for easy inventory creation and event handling

License

Notifications You must be signed in to change notification settings

sya-ri/ktInventory

Repository files navigation

ktInventory

Kotlin GitHub License Maven Central KDoc link Build status

Spigot library with Kotlin for easy inventory creation and event handling

Installation

build.gradle.kts

repositories {
    mavenCentral()
}

dependencies {
    implementation("dev.s7a:ktInventory:1.0.0")
}

Usage

DSL

You can easily generate inventory. Basically, use this.

fun openSimpleMenu(plugin: JavaPlugin, player: HumanEntity) {
    plugin.ktInventory("&0&lSelect where to teleport", 1) {
        item(3, ItemStack(Material.RED_BED)) {
            val player = it.whoClicked as? Player ?: return@item
            val respawnLocation = player.respawnLocation
            if (respawnLocation != null) {
                player.teleport(respawnLocation)
            } else {
                player.sendMessage("Not found respawnLocation")
            }
            player.closeInventory()
        }
        item(5, ItemStack(Material.COMPASS)) {
            val player = it.whoClicked as? Player ?: return@item
            player.teleport(player.world.spawnLocation)
            player.closeInventory()
        }
    }.open(player)
}

Provider

Passing a plugin instance as a property makes the dependencies between classes a little complicated. In such cases you can use Provider instead of DSL.

class SimpleMenu(provider: KtInventoryProvider) {
    private val inventory = provider.create("&0&lSelect where to teleport", 1) {
        item(3, ItemStack(Material.RED_BED)) {
            val player = it.whoClicked as? Player ?: return@item
            val respawnLocation = player.respawnLocation
            if (respawnLocation != null) {
                player.teleport(respawnLocation)
            } else {
                player.sendMessage("Not found respawnLocation")
            }
            player.closeInventory()
        }
        item(5, ItemStack(Material.COMPASS)) {
            val player = it.whoClicked as? Player ?: return@item
            player.teleport(player.world.spawnLocation)
            player.closeInventory()
        }
    }

    fun open(player: HumanEntity) {
        inventory.open(player)
    }
}