Skip to content
MattMX edited this page Oct 31, 2022 · 23 revisions

Getting Started

To use KtBukkitGui in your projects, use Jitpack.

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>1.0<version/>
<dependency/>

To avoid issues, make sure to shade KtGui (build with dependancy)

View other methods

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.

Extending GuiScreen

class CustomGUI : GuiScreen("Example GUI", 4) {

}

Building with an object

val gui = GuiScreen("Example GUI", 4)

Building with DSL

val gui = gui {
    title = "Example GUI"
    rows = 1
    // You can add buttons like this using DSL
    button(this) {
        material(Material.PAPER)
        named("&cThis GUI was built using Kotlin's DSL.")
        lore {
            add("&7For more information on building GUIs using DSL")
            add("&7check the GitHub wiki on KtBukkitGui.")
        }
        slot(4)
    }
}

Adding buttons

You can add as many buttons to the gui as you want, make sure to not go over the amount of slots in that gui!

To look at labelled gui slots, look here.

We will start by adding a Dirt block called "Foo" with a gray colour.

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

Make sure to include childOf in order to register the button to the GUI.

Adding Click events.

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

class CustomGUI : GuiScreen("Example GUI", 4) {
    init {
        GuiButton()
            .click {
                left = { e -> e.whoClicked.sendMessage("Hello World")}
                right = { e -> e.whoClicked.sendMessage("Right click!")}
            } named "&7Foo" slot 0 childOf this
    }
}

The library supports every type of click event.

You can find a list of their names here.

Opening the GUI

In order to give more options, you can make GUIs defined once, and then clone them to behave differently depending on who opens them.

If you are creating a new instance per GUI then simply do the following.

CustomGUI().open(player)

If you are sharing GUIs instead of making a new instance per gui, do this.

// gui is the gui instance
gui.createCopyAndOpen(player)

Creating custom GUI buttons

Creating custom GUI buttons is a nice thing we offer.

To get started, create a new class extending GuiButton or IGuiButton.

class CustomGUIButton() : 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 CustomGuiButton() : GuiButton() {
    private var clicked = false

    override fun thisClicked(e: InventoryClickEvent) {
        if (clicked) return
        clicked = true
        item = ItemBuilder(Material.STONE).name("&7Unavailable").make()
        // Update the item for the player
        update(e.whoClicked as Player)
        // Call this if you want other events to be called
        super.thisClicked(e)
    }

    override fun copy(parent: IGuiScreen): CustomGuiButton {
        val button = CustomGuiButton()
        button.clicked = clicked
        return button
    }

}

For this button to be used in a cloned context, we need to override the copy method and return a cloned button.

This will make it so the button can only be clicked once, then it will turn to stone.

Clone this wiki locally