Class ToggleItemComponent
java.lang.Object
io.github.toberocat.guiengine.components.AbstractGuiComponent
io.github.toberocat.guiengine.components.provided.toggle.ToggleItemComponent
- All Implemented Interfaces:
GuiComponent
,Selectable
,GuiEvents
Represents a toggle item component in a GUI. A toggle item can have multiple options, and clicking on the component
cycles through these options.
Created: 29.04.2023
- Author:
- Tobias Madlberger (Tobias)
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
The type identifier for a ToggleItemComponent.Fields inherited from class io.github.toberocat.guiengine.components.AbstractGuiComponent
api, clickFunctions, closeFunctions, context, dragFunctions, height, id, offsetX, offsetY, priority, width
-
Constructor Summary
ConstructorsConstructorDescriptionToggleItemComponent
(int offsetX, int offsetY, int width, int height, @NotNull RenderPriority priority, @NotNull String id, @NotNull List<GuiFunction> clickFunctions, @NotNull List<GuiFunction> dragFunctions, @NotNull List<GuiFunction> closeFunctions, boolean hidden, ParserContext options, int selected) Constructs a ToggleItemComponent with the specified parameters. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clickedComponent
(@NotNull org.bukkit.event.inventory.InventoryClickEvent event) Called when a player clicks on a component in the GUI.Get the parser context containing the options for the toggle item.int
Get the index of the currently selected option.String[]
Get the selection model containing available options.@NotNull String
getType()
Get the type of the GUI component.void
onViewInit
(@NotNull Map<String, String> placeholders) Called when the GUI is initialized and the viewer (player) opens it.void
render
(@NotNull org.bukkit.entity.Player viewer, @NotNull org.bukkit.inventory.ItemStack @NotNull [][] inventory) Render the GUI component for a specific player and inventory configuration.void
serialize
(@NotNull GeneratorContext gen, @NotNull com.fasterxml.jackson.databind.SerializerProvider serializers) Serialize the GUI component using the provided GeneratorContext and SerializerProvider.void
setSelected
(int selected) Set the index of the currently selected option.Methods inherited from class io.github.toberocat.guiengine.components.AbstractGuiComponent
closedComponent, draggedComponent, getClickFunctions, getCloseFunctions, getDragFunctions, getId, height, hidden, offsetX, offsetY, renderPriority, setApi, setContext, setHidden, setOffsetX, setOffsetY, width
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.github.toberocat.guiengine.components.GuiComponent
addActions, isInComponent
-
Field Details
-
TYPE
The type identifier for a ToggleItemComponent.- See Also:
-
-
Constructor Details
-
ToggleItemComponent
public ToggleItemComponent(int offsetX, int offsetY, int width, int height, @NotNull @NotNull RenderPriority priority, @NotNull @NotNull String id, @NotNull @NotNull List<GuiFunction> clickFunctions, @NotNull @NotNull List<GuiFunction> dragFunctions, @NotNull @NotNull List<GuiFunction> closeFunctions, boolean hidden, ParserContext options, int selected) Constructs a ToggleItemComponent with the specified parameters.- Parameters:
offsetX
- The X-coordinate offset of the component's position in the GUI.offsetY
- The Y-coordinate offset of the component's position in the GUI.width
- The width of the component.height
- The height of the component.priority
- The render priority of the component.id
- The unique identifier of the component.clickFunctions
- A list of GUI functions to be executed when the component is clicked.dragFunctions
- A list of GUI functions to be executed when the component is dragged.closeFunctions
- A list of GUI functions to be executed when the component is closed.hidden
- Whether the component is initially hidden or not.options
- The parser context containing the options for the toggle item.selected
- The index of the initially selected option.
-
-
Method Details
-
serialize
public void serialize(@NotNull @NotNull GeneratorContext gen, @NotNull @NotNull com.fasterxml.jackson.databind.SerializerProvider serializers) throws IOException Description copied from interface:GuiComponent
Serialize the GUI component using the provided GeneratorContext and SerializerProvider. This method is used to serialize the component's properties to a JSON representation.- Specified by:
serialize
in interfaceGuiComponent
- Overrides:
serialize
in classAbstractGuiComponent
- Parameters:
gen
- The GeneratorContext used for serialization.serializers
- The SerializerProvider used for serialization.- Throws:
IOException
- If there is an error during serialization.
-
onViewInit
Description copied from interface:GuiEvents
Called when the GUI is initialized and the viewer (player) opens it. This method is intended to handle any necessary initialization tasks before the GUI is shown to the player. The `placeholders` parameter can be used to store additional information specific to the GUI. The default behavior is to do nothing.- Specified by:
onViewInit
in interfaceGuiEvents
- Parameters:
placeholders
- A map containing placeholder data for the GUI.
-
clickedComponent
public void clickedComponent(@NotNull @NotNull org.bukkit.event.inventory.InventoryClickEvent event) Description copied from interface:GuiEvents
Called when a player clicks on a component in the GUI. The default behavior is to cancel the event and deny its result. If the action does not involve dropping an item, the event handling is skipped.- Specified by:
clickedComponent
in interfaceGuiEvents
- Overrides:
clickedComponent
in classAbstractGuiComponent
- Parameters:
event
- The `InventoryClickEvent` representing the click event.
-
getType
Description copied from interface:GuiComponent
Get the type of the GUI component.- Specified by:
getType
in interfaceGuiComponent
- Returns:
- The type of the GUI component.
-
render
public void render(@NotNull @NotNull org.bukkit.entity.Player viewer, @NotNull @NotNull org.bukkit.inventory.ItemStack @NotNull [][] inventory) Description copied from interface:GuiComponent
Render the GUI component for a specific player and inventory configuration. This method is responsible for rendering the component to the graphical user interface.- Specified by:
render
in interfaceGuiComponent
- Parameters:
viewer
- The player for whom the component should be rendered.inventory
- The inventory configuration used for rendering the GUI.
-
getOptions
Get the parser context containing the options for the toggle item.- Returns:
- The parser context.
-
getSelectionModel
Description copied from interface:Selectable
Get the selection model containing available options. This method returns an array of strings representing the available options that can be selected.- Specified by:
getSelectionModel
in interfaceSelectable
- Returns:
- An array of strings representing the selection model.
-
getSelected
public int getSelected()Get the index of the currently selected option.- Specified by:
getSelected
in interfaceSelectable
- Returns:
- The index of the selected option.
-
setSelected
public void setSelected(int selected) Set the index of the currently selected option.- Specified by:
setSelected
in interfaceSelectable
- Parameters:
selected
- The index of the option to set as selected.
-