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
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordRepresents a single state in the cycle. -
Constructor Summary
ConstructorsConstructorDescriptionStateCycleElement(List<StateCycleElement.State<T>> states, int initialIndex, Consumer<T> onChange) Constructs a new StateCycleElement. -
Method Summary
Modifier and TypeMethodDescriptionGets the value of the currently active state.static <T> StateCycleElement<T> of(List<StateCycleElement.State<T>> states, int initialIndex, Consumer<T> onChange) Static factory method to create a StateCycleElement.onClick(GuiClickContext ctx) Handles the click interaction to cycle through states.onDrag(GuiDragContext ctx) Prevents items from being dragged onto this cycle element.@Nullable org.bukkit.inventory.ItemStackRenders the icon of the current state.Methods inherited from class Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface GuiElement
onClick, onDrag
-
Constructor Details
-
StateCycleElement
public StateCycleElement(List<StateCycleElement.State<T>> states, int initialIndex, Consumer<T> onChange) Constructs a new StateCycleElement.- Parameters:
states- A list ofStateCycleElement.Stateobjects 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
Renders the icon of the current state.- Specified by:
renderin interfaceGuiElement- Parameters:
view- The active GUI view.slot- The slot index where the element is rendered.- Returns:
- The
ItemStackfor the current index.
-
onClick
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:
onClickin interfaceGuiElement- Parameters:
ctx- The context of the click event.- Returns:
ActionResult.CANCELto prevent the player from taking the icon.
-
onDrag
Prevents items from being dragged onto this cycle element.- Specified by:
onDragin interfaceGuiElement- Parameters:
ctx- The context of the drag event.- Returns:
ActionResult.CANCEL.
-
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.
-