Class StateCycleElement<T>

java.lang.Object
com.github.darksoulq.abyssallib.world.gui.element.StateCycleElement<T>
Type Parameters:
T - The type of the value associated with each state.
All Implemented Interfaces:
GuiElement

public class StateCycleElement<T> extends Object implements GuiElement
A GUI element that cycles through a list of predefined states when clicked.

This element supports multiple states, each associated with an icon and a value. Left-clicking cycles forward through the list, while right-clicking cycles backward.

  • Constructor Details

    • StateCycleElement

      public StateCycleElement(List<StateCycleElement.State<T>> states, int initialIndex, Consumer<T> onChange)
      Constructs a new StateCycleElement.
      Parameters:
      states - A list of StateCycleElement.State objects representing the cycle.
      initialIndex - The index of the state to start on.
      onChange - A consumer notified when a new state value is selected.
      Throws:
      IllegalArgumentException - If the provided states list is empty.
  • Method Details

    • render

      @Nullable public @Nullable org.bukkit.inventory.ItemStack render(GuiView view, int slot)
      Renders the icon of 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 for the current index.
    • onClick

      public ActionResult onClick(GuiClickContext ctx)
      Handles the click interaction to cycle through states.

      A Right-Click decrements the index (cycles backward), while any other click type increments the index (cycles forward).

      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 cycle element.
      Specified by:
      onDrag in interface GuiElement
      Parameters:
      ctx - The context of the drag event.
      Returns:
      ActionResult.CANCEL.
    • getCurrentValue

      public T getCurrentValue()
      Gets the value of the currently active state.
      Returns:
      The current state value.
    • of

      public static <T> StateCycleElement<T> of(List<StateCycleElement.State<T>> states, int initialIndex, Consumer<T> onChange)
      Static factory method to create a StateCycleElement.
      Type Parameters:
      T - The value type.
      Parameters:
      states - The list of states.
      initialIndex - The starting index.
      onChange - The change listener.
      Returns:
      A new StateCycleElement instance.