From 60ce81909fd75a85f79a6e20675241cb376e547f Mon Sep 17 00:00:00 2001 From: NichtStudioCode <51272202+NichtStudioCode@users.noreply.github.com> Date: Sat, 28 Jan 2023 12:32:16 +0100 Subject: [PATCH] invui-kotlin module --- invui-kotlin/pom.xml | 65 +++++++++++++++++ .../main/kotlin/de/studiocode/invui/Guis.kt | 72 +++++++++++++++++++ .../de/studiocode/invui/ItemBuilders.kt | 12 ++++ .../main/kotlin/de/studiocode/invui/Items.kt | 8 +++ .../de/studiocode/invui/VirtualInventories.kt | 29 ++++++++ .../invui/adventure/AdventureItemBuilders.kt | 21 ++++++ .../adventure/AdventureWindowContexts.kt | 11 +++ 7 files changed, 218 insertions(+) create mode 100644 invui-kotlin/pom.xml create mode 100644 invui-kotlin/src/main/kotlin/de/studiocode/invui/Guis.kt create mode 100644 invui-kotlin/src/main/kotlin/de/studiocode/invui/ItemBuilders.kt create mode 100644 invui-kotlin/src/main/kotlin/de/studiocode/invui/Items.kt create mode 100644 invui-kotlin/src/main/kotlin/de/studiocode/invui/VirtualInventories.kt create mode 100644 invui-kotlin/src/main/kotlin/de/studiocode/invui/adventure/AdventureItemBuilders.kt create mode 100644 invui-kotlin/src/main/kotlin/de/studiocode/invui/adventure/AdventureWindowContexts.kt diff --git a/invui-kotlin/pom.xml b/invui-kotlin/pom.xml new file mode 100644 index 0000000..7cdc65f --- /dev/null +++ b/invui-kotlin/pom.xml @@ -0,0 +1,65 @@ + + + 4.0.0 + + de.studiocode.invui + InvUI-Parent + 1.0-SNAPSHOT + + + invui-kotlin + + + 1.8.0 + 11 + 11 + + + + + org.jetbrains.kotlin + kotlin-stdlib + ${kotlin.version} + + + org.spigotmc + spigot-api + 1.19.3-R0.1-SNAPSHOT + provided + + + de.studiocode.invui + invui + ${project.version} + + + net.kyori + adventure-api + 4.12.0 + provided + + + + + src/main/kotlin/ + + + org.jetbrains.kotlin + kotlin-maven-plugin + ${kotlin.version} + + + compile + process-sources + + compile + + + + + + + + \ No newline at end of file diff --git a/invui-kotlin/src/main/kotlin/de/studiocode/invui/Guis.kt b/invui-kotlin/src/main/kotlin/de/studiocode/invui/Guis.kt new file mode 100644 index 0000000..9fdc886 --- /dev/null +++ b/invui-kotlin/src/main/kotlin/de/studiocode/invui/Guis.kt @@ -0,0 +1,72 @@ +@file:Suppress("PackageDirectoryMismatch") + +package de.studiocode.invui.gui + +import de.studiocode.invui.item.Item + +/** + * Gets the [SlotElement] placed on that slot. + * + * @param slot The slot index + * @return The [SlotElement] placed on that slot or null if there is none + */ +operator fun GUI.get(slot: Int): SlotElement? = getSlotElement(slot) + +/** + * Gets the [SlotElement] placed on these coordinates. + * + * @param x The x coordinate of the slot + * @param y The y coordinate of the slot + * @return The [SlotElement] placed on that slot or null if there is none + */ +operator fun GUI.get(x: Int, y: Int): SlotElement? = getSlotElement(x, y) + +/** + * Sets the [SlotElement] on that slot. + * + * @param slot The slot index + * @param element The [SlotElement] to set or null to remove the current one + */ +operator fun GUI.set(slot: Int, element: SlotElement?) = setSlotElement(slot, element) + +/** + * Sets the [SlotElement] on these coordinates. + * + * @param x The x coordinate of the slot + * @param y The y coordinate of the slot + * @param element The [SlotElement] to set or null to remove the current one + */ +operator fun GUI.set(x: Int, y: Int, element: SlotElement?) = setSlotElement(x, y, element) + +/** + * Sets the [Item] on that slot. + * + * @param slot The slot index + * @param item The [Item] to set or null to remove the current one + */ +operator fun GUI.set(slot: Int, item: Item?) = setItem(slot, item) + +/** + * Sets the [Item] on these coordinates. + * + * @param x The x coordinate of the slot + * @param y The y coordinate of the slot + * @param item The [Item] to set or null to remove the current one + */ +operator fun GUI.set(x: Int, y: Int, item: Item?) = setItem(x, y, item) + +/** + * Adds the given [elements]. + * + * @param elements The [SlotElements][SlotElement] to add. + */ +@JvmName("plusAssignSlotElements") +operator fun GUI.plusAssign(elements: Iterable) = elements.forEach { addSlotElements(it) } + +/** + * Adds the given [items]. + * + * @param items The [Items][Item] to add. + */ +@JvmName("plusAssignItems") +operator fun GUI.plusAssign(items: Iterable) = items.forEach { addItems(it) } \ No newline at end of file diff --git a/invui-kotlin/src/main/kotlin/de/studiocode/invui/ItemBuilders.kt b/invui-kotlin/src/main/kotlin/de/studiocode/invui/ItemBuilders.kt new file mode 100644 index 0000000..c1be6be --- /dev/null +++ b/invui-kotlin/src/main/kotlin/de/studiocode/invui/ItemBuilders.kt @@ -0,0 +1,12 @@ +@file:Suppress("PackageDirectoryMismatch") + +package de.studiocode.invui.item.builder + +import de.studiocode.inventoryaccess.component.BaseComponentWrapper +import de.studiocode.invui.util.ComponentUtils +import net.md_5.bungee.api.chat.BaseComponent + +/** + * Sets the lore of the item stack. + */ +fun BaseItemBuilder.setLore(lore: List>): T = setLore(lore.map { BaseComponentWrapper(ComponentUtils.withoutPreFormatting(*it)) }) \ No newline at end of file diff --git a/invui-kotlin/src/main/kotlin/de/studiocode/invui/Items.kt b/invui-kotlin/src/main/kotlin/de/studiocode/invui/Items.kt new file mode 100644 index 0000000..6850f23 --- /dev/null +++ b/invui-kotlin/src/main/kotlin/de/studiocode/invui/Items.kt @@ -0,0 +1,8 @@ +@file:Suppress("PackageDirectoryMismatch") + +package de.studiocode.invui.item + +/** + * Calls [Item.notifyWindows] for all items in this [Iterable]. + */ +fun Iterable.notifyWindows() = forEach { it.notifyWindows() } \ No newline at end of file diff --git a/invui-kotlin/src/main/kotlin/de/studiocode/invui/VirtualInventories.kt b/invui-kotlin/src/main/kotlin/de/studiocode/invui/VirtualInventories.kt new file mode 100644 index 0000000..965dba9 --- /dev/null +++ b/invui-kotlin/src/main/kotlin/de/studiocode/invui/VirtualInventories.kt @@ -0,0 +1,29 @@ +@file:Suppress("PackageDirectoryMismatch") + +package de.studiocode.invui.virtualinventory + +import org.bukkit.inventory.ItemStack + +/** + * Gets a copy of [ItemStack] placed on that [slot]. + */ +operator fun VirtualInventory.get(slot: Int): ItemStack? = getItemStack(slot) + +/** + * Adds the given [items] to the inventory. + */ +operator fun VirtualInventory.plusAssign(items: Iterable) { + items.forEach { addItem(null, it) } +} + +/** + * Adds the given [item] to the inventory. + */ +operator fun VirtualInventory.plusAssign(item: ItemStack) { + addItem(null, item) +} + +/** + * Checks if the [VirtualInventory] contains an [ItemStack] similar to the given [item]. + */ +operator fun VirtualInventory.contains(item: ItemStack) = containsSimilar(item) \ No newline at end of file diff --git a/invui-kotlin/src/main/kotlin/de/studiocode/invui/adventure/AdventureItemBuilders.kt b/invui-kotlin/src/main/kotlin/de/studiocode/invui/adventure/AdventureItemBuilders.kt new file mode 100644 index 0000000..27ca816 --- /dev/null +++ b/invui-kotlin/src/main/kotlin/de/studiocode/invui/adventure/AdventureItemBuilders.kt @@ -0,0 +1,21 @@ +@file:Suppress("PackageDirectoryMismatch") + +package de.studiocode.invui.item.builder + +import de.studiocode.inventoryaccess.component.AdventureComponentWrapper +import net.kyori.adventure.text.Component + +/** + * Sets the display name of the item stack. + */ +fun BaseItemBuilder.setDisplayName(displayName: Component): T = setDisplayName(AdventureComponentWrapper(displayName)) + +/** + * Sets the lore the item stack. + */ +fun BaseItemBuilder.setLore(lore: List): T = setLore(lore.map { AdventureComponentWrapper(it) }) + +/** + * Adds lore lines to the item stack. + */ +fun BaseItemBuilder.addLoreLines(vararg components: Component): T = addLoreLines(*components.map { AdventureComponentWrapper(it) }.toTypedArray()) \ No newline at end of file diff --git a/invui-kotlin/src/main/kotlin/de/studiocode/invui/adventure/AdventureWindowContexts.kt b/invui-kotlin/src/main/kotlin/de/studiocode/invui/adventure/AdventureWindowContexts.kt new file mode 100644 index 0000000..0e60fd1 --- /dev/null +++ b/invui-kotlin/src/main/kotlin/de/studiocode/invui/adventure/AdventureWindowContexts.kt @@ -0,0 +1,11 @@ +@file:Suppress("PackageDirectoryMismatch") + +package de.studiocode.invui.window.type.context + +import de.studiocode.inventoryaccess.component.AdventureComponentWrapper +import net.kyori.adventure.text.Component + +/** + * Sets the title of the window. + */ +fun AbstractWindowContext<*>.setTitle(title: Component) = setTitle(AdventureComponentWrapper(title)) \ No newline at end of file