Class ToggleElement

java.lang.Object
com.github.darksoulq.abyssallib.world.gui.element.ToggleElement
All Implemented Interfaces:
GuiElement

public class ToggleElement extends Object implements GuiElement
A GUI element that toggles between two states (on and off) when clicked.

This element maintains a boolean state and displays a corresponding icon for each state. When a player clicks the element, the state is inverted, the icon is updated, and a toggle listener is notified of the change.

  • Constructor Summary

    Constructors
    Constructor
    Description
    ToggleElement(org.bukkit.inventory.ItemStack onIcon, org.bukkit.inventory.ItemStack offIcon, boolean initialState, Consumer<Boolean> onToggle)
    Constructs a new ToggleElement.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Retrieves the current state of the toggle.
    of(org.bukkit.inventory.ItemStack onIcon, org.bukkit.inventory.ItemStack offIcon, boolean initialState, Consumer<Boolean> onToggle)
    Static factory method to create a ToggleElement.
    Handles the click interaction by flipping the current state.
    Prevents items from being dragged onto this toggle element.
    @Nullable org.bukkit.inventory.ItemStack
    render(GuiView view, int slot)
    Renders the appropriate icon based on the current state.
    void
    setState(boolean state)
    Manually updates the state of the toggle.

    Methods inherited from class Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface GuiElement

    onClick, onDrag
  • Constructor Details

    • ToggleElement

      public ToggleElement(org.bukkit.inventory.ItemStack onIcon, org.bukkit.inventory.ItemStack offIcon, boolean initialState, Consumer<Boolean> onToggle)
      Constructs a new ToggleElement.
      Parameters:
      onIcon - The icon used for the 'on' state.
      offIcon - The icon used for the 'off' state.
      initialState - The starting state of the toggle.
      onToggle - A consumer that accepts the new state after a toggle occurs.
  • Method Details

    • render

      @Nullable public @Nullable org.bukkit.inventory.ItemStack render(GuiView view, int slot)
      Renders the appropriate icon based on the current state.
      Specified by:
      render in interface GuiElement
      Parameters:
      view - The active GUI view.
      slot - The slot index where the element is rendered.
      Returns:
      The ItemStack corresponding to the current state.
    • onClick

      public ActionResult onClick(GuiClickContext ctx)
      Handles the click interaction by flipping the current state.

      After flipping the state, it triggers the onToggle consumer and cancels the inventory action to keep the icon in the slot.

      Specified by:
      onClick in interface GuiElement
      Parameters:
      ctx - The context of the click event.
      Returns:
      ActionResult.CANCEL to prevent the player from taking the icon.
    • onDrag

      public ActionResult onDrag(GuiDragContext ctx)
      Prevents items from being dragged onto this toggle element.
      Specified by:
      onDrag in interface GuiElement
      Parameters:
      ctx - The context of the drag event.
      Returns:
      ActionResult.CANCEL.
    • getState

      public boolean getState()
      Retrieves the current state of the toggle.
      Returns:
      true if on, false if off.
    • setState

      public void setState(boolean state)
      Manually updates the state of the toggle.
      Parameters:
      state - The new state to set.
    • of

      public static ToggleElement of(org.bukkit.inventory.ItemStack onIcon, org.bukkit.inventory.ItemStack offIcon, boolean initialState, Consumer<Boolean> onToggle)
      Static factory method to create a ToggleElement.
      Parameters:
      onIcon - The 'on' icon.
      offIcon - The 'off' icon.
      initialState - The initial state.
      onToggle - The toggle handler.
      Returns:
      A new ToggleElement instance.