Skip to content
MattMX edited this page Feb 14, 2024 · 23 revisions

Getting Started

Gradle (Kotlin)
repositories {
    maven("https://maven.pvphub.me/releases")
}
dependencies {
    compileOnly("com.mattmx:ktgui:2.0")
}
Maven
<repository>
  <id>pvphub-releases<id/>
  <name>PvPHub Development<name/>
  <url>https://maven.pvphub.me/releases<url/>
<repository/>
<dependency>
  <groupId>com.mattmx<groupId/>
  <artifactId>ktgui<artifactId/>
  <version>${version}<version/>
<dependency/>

View other dependency managers

To avoid issues, make sure you do not shade KtGUI, and instead download the plugin here; under the Artifacts section, download build-outputs.

You are also required to provide the Kotlin Standard Library your self, I recommend you do this with the use of this plugin

In the onEnable() function of the main class of your plugin, make sure to call

GuiManager.init(this)

Where this is the plugin instance.

Creating a GUI

To create a GUI, we can either extend GuiScreen, or build it dynamically from DSL or otherwise.

The ! bang operator we are using in this example simply calls String#component, translating color codes and formatting, converting your String into a Component, which are required in Paper.

Extending GuiScreen

class CustomAnvilGUI : GuiScreen(!"Example GUI", type = InventoryType.ANVIL) {

}
val guiWithTwoRows = gui {
    title = !"Example GUI"
    rows = 2
}

Adding buttons

You can add anything extending GuiButton to a screen.

To look at labelled gui slots, look here.

class CustomGUI : GuiScreen(!"Example GUI", 4) {
    init {
        GuiButton(Material.DIRT) named !"&7Foo" slot 0 childOf this

        // alternatively:
        button(Material.DIRT) {
            named(!"&7Foo")
        } slot 0
    }
}

Make sure to include childOf in order to register the button to the GUI. The GuiScreen#button() method will do it for you.

Adding Click events.

We've made a button, now let's add some interactivity.

class CustomGUI : GuiScreen(!"Example GUI", 4) {
    init {
        GuiButton(Material.DIRT)
            .click {
                ClickType.LEFT + ClickType.RIGHT {
                    player.sendMessage(!"&cHello, world")
                    material(Material.DIAMOND) // Set material to diamond
                    update() // Apply new item
                }
            } named !"&7Foo" slot 0 childOf this
    }
}

Opening the GUI

CustomGUI().open(player)

Creating custom GUI button components

To get started, create a new class extending GuiButton.

If you wish to support Signals extend SignalButton

class LockedGuiButton() : GuiButton() {

}

This object can now already be added to GUIs with the same functionality as a normal GuiButton object.

Let's add some custom functionality.

class LockedGuiButton() : GuiButton() {
    private var clicked = false

    override fun thisClicked(e: ButtonClickedEvent) {
        if (clicked) return
        clicked = true
        item = (KT ib Material.STONE named !"&7Unavailable").build()
        update()
        super.thisClicked(e)
    }
}

We have created a button that can only be used once, now let's put it into a GUI.