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

Getting Started

To use KtBukkitGui in your projects, use Jitpack.

Maven

        <repository>
            <id>jitpack.io</id>
            <url>https://jitpack.io</url>
        </repository>
        <dependency>
            <groupId>com.github.Matt-MX</groupId>
            <artifactId>KtBukkitGui</artifactId>
            <version>master-SNAPSHOT</version>
        </dependency>

View other methods

Creating a GUI

To create a GUI we can do it as a class or dynamically.

We'll start with a class example.

Create a class that extends GuiScreen, and provide the parameters.

class CustomGUI : GuiScreen("Example GUI", 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)
    }

}

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

You can create more complex things, but we'll keep it at that for now.

Clone this wiki locally