Skip to content

4. GUI State

Tech edited this page Aug 26, 2023 · 1 revision

GUI State


Handling State

Sometimes when handling UIs you need to handle the state of some GUIComponent's or the current state of some interactions, because rendering a GUI is still technically just a function call we are able to create a variable outside the scope of our gui helper function block and have it serve to hold our state.

fun render(): GUI {
  var hasClickedButton = false

  return gui(
    plugin = instance,
    title = Component.text("Rendered UI"),
    type = GUIType.Chest(rows = 3)
  ) { 
    if(!hasClickedButton) {
      slot(1, 1) {
        item = item(Material.STONE_BUTTON) {
          name = Component.text("Click Me!")
          onClick = {
            hasClickedButton = true
            refresh()
          }
        }
      }
    } else {
      slot(1, 1) {
        item = item(Material.BARRIER) {
          name = Component.text("You've clicked the button!")
        }
      }
    }
  }
}

In this example we detect whether the player has clicked our button or not and refresh then conditionally render the barrier if they have clicked onto the button.

Clone this wiki locally