Skip to content

Commit

Permalink
Merge pull request #5 from xap3y/dev
Browse files Browse the repository at this point in the history
0.2
  • Loading branch information
xap3y authored Jul 3, 2024
2 parents fdc90b5 + 7797486 commit c805546
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/main/examples/DocsExamples.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
public class Example {

// This is the result of the created gui below: https://github.com/xap3y/XaGUI/blob/main/docs/image.png

// @Deprecated
public static void openGui(Player player, XaGui instance) {

GuiMenu menu = instance.createMenu("&a&lExample 1", 3);
Expand Down
24 changes: 19 additions & 5 deletions src/main/kotlin/eu/xap3y/xagui/GuiMenu.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

package eu.xap3y.xagui

import eu.xap3y.xagui.exceptions.PageOutOfBoundException
import eu.xap3y.xagui.exceptions.RowsOutOfBoundException
import eu.xap3y.xagui.exceptions.SlotOutOfBoundException
import eu.xap3y.xagui.interfaces.*
Expand Down Expand Up @@ -327,6 +328,7 @@ class GuiMenu(private val plugin: JavaPlugin, private val title: String, private

/**
* Clear the button in a slot
*
* @param page The page of the menu
* @param slot The slot to clear
*/
Expand All @@ -344,6 +346,7 @@ class GuiMenu(private val plugin: JavaPlugin, private val title: String, private

/**
* Clear all buttons in the menu
*
* @param page The page of the menu
*/
override fun clearAllSlots(page: Int) {
Expand All @@ -353,12 +356,14 @@ class GuiMenu(private val plugin: JavaPlugin, private val title: String, private

/**
* Get the plugin that owns the menu
*
* @return The plugin that owns the menu
*/
override fun getOwner(): JavaPlugin = plugin

/**
* Unlock a slot so the player can take items from it
*
* @param slot The slot to unlock
* @throws SlotOutOfBoundException If the slot is out of bounds
*/
Expand All @@ -369,6 +374,7 @@ class GuiMenu(private val plugin: JavaPlugin, private val title: String, private

/**
* Unlock a slot so the player can take items from it
*
* @param page The page of the menu
* @param slot The slot to unlock
* @throws SlotOutOfBoundException If the slot is out of bounds
Expand All @@ -381,6 +387,7 @@ class GuiMenu(private val plugin: JavaPlugin, private val title: String, private

/**
* Lock a slot
*
* @param slot The slot to lock
* @throws SlotOutOfBoundException If the slot is out of bounds
*/
Expand All @@ -391,6 +398,7 @@ class GuiMenu(private val plugin: JavaPlugin, private val title: String, private

/**
* Lock a slot
*
* @param page The page of the menu
* @param slot The slot to lock
* @throws SlotOutOfBoundException If the slot is out of bounds
Expand All @@ -403,6 +411,7 @@ class GuiMenu(private val plugin: JavaPlugin, private val title: String, private

/**
* Open the menu for a player
*
* @param player The player to open the menu for
*/
override fun open(player: Player) {
Expand All @@ -411,6 +420,7 @@ class GuiMenu(private val plugin: JavaPlugin, private val title: String, private

/**
* Open a specific page of the menu for a player
*
* @param page The page to open
* @param player The player to open the menu for
*/
Expand All @@ -420,21 +430,25 @@ class GuiMenu(private val plugin: JavaPlugin, private val title: String, private

/**
* Switch the page of the menu for a player
* @param page The page to switch to
*
* @param pageIndex The index of a page to switch to
* @param player The player to switch the page for
* @throws PageOutOfBoundException If the page is out of bounds
*/
override fun switchPage(page: Int, player: Player) {
@Throws(PageOutOfBoundException::class)
override fun switchPage(pageIndex: Int, player: Player) {
if (pageIndex > totalPages-1 || pageIndex < 0) throw PageOutOfBoundException()
val oldPage: Int = currentOpenedPage
currentOpenedPage = page
currentOpenedPage = pageIndex
val inv: Inventory = invMapping[currentOpenedPage] ?: return
stickySlots.forEach {
setSlot(page, it, pageMapping[0]?.get(it) ?: return@forEach)
setSlot(pageIndex, it, pageMapping[0]?.get(it) ?: return@forEach)
}
Bukkit.getScheduler().runTask(plugin, Runnable {
//plugin.server.pluginManager.callEvent(GuiPageSwitchEvent(player, page, currentOpenedPage, inv, invMapping[currentOpenedPage]))
player.openInventory(inv)
})
this.onPageSwitchAction?.onPageSwitch(GuiPageSwitchModel(player, page, oldPage))
this.onPageSwitchAction?.onPageSwitch(GuiPageSwitchModel(player, pageIndex, oldPage))
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/eu/xap3y/xagui/XaGui.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.bukkit.plugin.java.JavaPlugin

/**
* The main class of the library
*
* @param plugin The plugin instance
*/
class XaGui(private val plugin: JavaPlugin) {
Expand All @@ -17,6 +18,7 @@ class XaGui(private val plugin: JavaPlugin) {

/**
* Create a new menu
*
* @param name The name of the menu
* @param rows The number of rows in the menu
* @return The created menu
Expand All @@ -28,6 +30,7 @@ class XaGui(private val plugin: JavaPlugin) {

/**
* Create a new menu
*
* @param name The name of the menu
* @param rows The number of rows in the menu
* @param pages The number of pages in the menu
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package eu.xap3y.xagui.exceptions

/**
* Exception thrown when the page is out of bounds
*/
class PageOutOfBoundException: Exception()
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import org.bukkit.event.inventory.InventoryClickEvent

/**
* Interface for button listeners
*
* @see GuiButtonInterface
*/
interface ButtonListener {
Expand Down
18 changes: 6 additions & 12 deletions src/main/kotlin/eu/xap3y/xagui/interfaces/GuiButtonInterface.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import org.bukkit.inventory.ItemStack

/**
* Interface for buttons in a GUI
*
* @see ButtonListener
*/
@Suppress("INAPPLICABLE_JVM_NAME")
interface GuiButtonInterface {

fun getIcon(): Material
Expand All @@ -26,25 +26,19 @@ interface GuiButtonInterface {

fun setName(name: String): GuiButton

@JvmName("setLoreList")
fun setLore(newLore: List<String>): GuiButton
fun setLoreList(newLore: List<String>): GuiButton

@JvmName("setLoreArgs")
fun setLore(vararg args: String): GuiButton

@JvmName("setLoreArray")
fun setLore(array: Array<String>): GuiButton
fun setLoreArray(array: Array<String>): GuiButton

fun addLoreLine(line: String): GuiButton

@JvmName("addLoreList")
fun addLore(lines: List<String>): GuiButton
fun addLoreList(lines: List<String>): GuiButton

@JvmName("addLoreArgs")
fun addLore(vararg args: String): GuiButton

@JvmName("addLoreArray")
fun addLore(array: Array<String>): GuiButton
fun addLoreArray(array: Array<String>): GuiButton

fun clearLore(): GuiButton

Expand All @@ -64,6 +58,6 @@ interface GuiButtonInterface {

fun setDurability(durability: Short): GuiButton

var listener: ButtonListener?
fun getClickListener(): ButtonListener?

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import org.bukkit.event.inventory.InventoryClickEvent

/**
* Interface for click event listeners
*
* @see GuiInterface
*/
interface GuiClickInterface {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import org.bukkit.event.inventory.InventoryCloseEvent

/**
* Interface for close event listeners
*
* @see GuiInterface
*/
interface GuiCloseInterface {
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/eu/xap3y/xagui/interfaces/GuiInterface.kt
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ interface GuiInterface {

fun open(page: Int, player: Player)

fun switchPage(page: Int, player: Player)
fun switchPage(pageIndex: Int, player: Player)

fun getMaxPages(): Int

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import org.bukkit.event.inventory.InventoryOpenEvent

/**
* Interface for open event listeners
*
* @see GuiInterface
*/
interface GuiOpenInterface {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import eu.xap3y.xagui.models.GuiPageSwitchModel

/**
* Interface for page switch event listeners
*
* @see GuiInterface
*/
interface GuiPageSwitchInterface {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class GuiMenuListener(private val plugin: JavaPlugin): Listener {

val button: GuiButtonInterface = clickedInventory.getSlot(e.slot) ?: return

button.listener?.onClick(e)
button.getClickListener()?.onClick(e)

}

Expand Down
Loading

0 comments on commit c805546

Please sign in to comment.