Class ToggleElement
java.lang.Object
com.github.darksoulq.abyssallib.world.gui.element.ToggleElement
- All Implemented Interfaces:
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
ConstructorsConstructorDescriptionToggleElement(org.bukkit.inventory.ItemStack onIcon, org.bukkit.inventory.ItemStack offIcon, boolean initialState, Consumer<Boolean> onToggle) Constructs a new ToggleElement. -
Method Summary
Modifier and TypeMethodDescriptionbooleangetState()Retrieves the current state of the toggle.static ToggleElementof(org.bukkit.inventory.ItemStack onIcon, org.bukkit.inventory.ItemStack offIcon, boolean initialState, Consumer<Boolean> onToggle) Static factory method to create a ToggleElement.onClick(GuiClickContext ctx) Handles the click interaction by flipping the current state.onDrag(GuiDragContext ctx) Prevents items from being dragged onto this toggle element.@Nullable org.bukkit.inventory.ItemStackRenders the appropriate icon based on the current state.voidsetState(boolean state) Manually updates the state of the toggle.Methods inherited from class Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods 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
Renders the appropriate icon based on the current state.- Specified by:
renderin interfaceGuiElement- Parameters:
view- The active GUI view.slot- The slot index where the element is rendered.- Returns:
- The
ItemStackcorresponding to the current state.
-
onClick
Handles the click interaction by flipping the current state.After flipping the state, it triggers the
onToggleconsumer and cancels the inventory action to keep the icon in the slot.- 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 toggle element.- Specified by:
onDragin interfaceGuiElement- Parameters:
ctx- The context of the drag event.- Returns:
ActionResult.CANCEL.
-
getState
public boolean getState()Retrieves the current state of the toggle.- Returns:
trueif on,falseif 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.
-