From ec78ed9df83fb42040c9c56ccbc803005b8d0d3c Mon Sep 17 00:00:00 2001 From: Aeltumn Date: Mon, 6 Jan 2025 11:40:04 +0100 Subject: [PATCH] Update and apply ktlint --- build.gradle.kts | 2 +- .../example/CatalogueExampleInterface.kt | 22 +-- .../example/ChangingTitleExampleInterface.kt | 34 ++-- .../example/DelayedRequestExampleInterface.kt | 41 ++--- .../interfaces/example/ExamplePlugin.kt | 149 ++++++++++-------- .../interfaces/example/ExampleUtilities.kt | 14 +- .../example/MovingExampleInterface.kt | 23 +-- .../example/RegistrableInterface.kt | 1 - .../example/TabbedExampleInterface.kt | 74 +++++---- .../noxcrew/interfaces/InterfacesConstants.kt | 78 ++++----- .../noxcrew/interfaces/click/ClickHandler.kt | 12 +- .../noxcrew/interfaces/drawable/Drawable.kt | 1 - .../interfaces/element/CompletedElement.kt | 9 +- .../com/noxcrew/interfaces/element/Element.kt | 1 - .../interfaces/element/StaticElement.kt | 1 - .../noxcrew/interfaces/event/DrawPaneEvent.kt | 1 - .../grid/ChainGridPositionGenerator.kt | 5 +- .../interfaces/grid/GridBoxGenerator.kt | 7 +- .../com/noxcrew/interfaces/grid/GridMap.kt | 30 ++-- .../com/noxcrew/interfaces/grid/GridPoint.kt | 28 ++-- .../interfaces/grid/GridPositionGenerator.kt | 5 +- .../noxcrew/interfaces/grid/HashGridMap.kt | 17 +- .../interfaces/interfaces/ChestInterface.kt | 9 +- .../interfaces/ChestInterfaceBuilder.kt | 15 +- .../interfaces/CombinedInterface.kt | 9 +- .../interfaces/CombinedInterfaceBuilder.kt | 15 +- .../interfaces/interfaces/Interface.kt | 1 - .../interfaces/interfaces/InterfaceBuilder.kt | 6 +- .../interfaces/InterfaceProperties.kt | 1 - .../interfaces/interfaces/PlayerInterface.kt | 6 +- .../interfaces/PlayerInterfaceBuilder.kt | 1 - .../interfaces/interfaces/TitledInterface.kt | 1 - .../inventory/CachedInterfacesInventory.kt | 13 +- .../inventory/ChestInterfacesInventory.kt | 17 +- .../inventory/CombinedInterfacesInventory.kt | 17 +- .../inventory/InterfacesInventory.kt | 17 +- .../inventory/PlayerInterfacesInventory.kt | 17 +- .../noxcrew/interfaces/pane/CombinedPane.kt | 22 +-- .../noxcrew/interfaces/pane/CompletedPane.kt | 4 +- .../noxcrew/interfaces/pane/OrderedPane.kt | 23 +-- .../com/noxcrew/interfaces/pane/Pane.kt | 23 +-- .../com/noxcrew/interfaces/pane/PlayerPane.kt | 6 +- .../interfaces/properties/DelegateTrigger.kt | 23 +-- .../interfaces/properties/EmptyTrigger.kt | 6 +- .../properties/InterfaceProperty.kt | 10 +- .../noxcrew/interfaces/properties/Trigger.kt | 6 +- .../interfaces/transform/ReactiveTransform.kt | 1 - .../transform/builtin/PagedTransformation.kt | 20 ++- .../builtin/PaginationTransformation.kt | 6 +- .../interfaces/utilities/BoundInteger.kt | 21 ++- .../utilities/BukkitInventoryUtilities.kt | 13 +- .../utilities/CollapsablePaneMap.kt | 20 ++- .../utilities/IncrementingInteger.kt | 6 +- .../interfaces/utilities/TitleState.kt | 5 +- .../interfaces/view/AbstractInterfaceView.kt | 70 ++++---- .../interfaces/view/ChestInterfaceView.kt | 20 +-- .../interfaces/view/CombinedInterfaceView.kt | 22 +-- .../noxcrew/interfaces/view/InterfaceView.kt | 1 - .../interfaces/view/PlayerInterfaceView.kt | 24 +-- 59 files changed, 604 insertions(+), 448 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index a613490..9d58f50 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -36,7 +36,7 @@ subprojects { configure { kotlin { - ktlint("0.48.2") + ktlint("1.5.0") } } diff --git a/examples/src/main/kotlin/com/noxcrew/interfaces/example/CatalogueExampleInterface.kt b/examples/src/main/kotlin/com/noxcrew/interfaces/example/CatalogueExampleInterface.kt index a35abd1..5b0e31a 100644 --- a/examples/src/main/kotlin/com/noxcrew/interfaces/example/CatalogueExampleInterface.kt +++ b/examples/src/main/kotlin/com/noxcrew/interfaces/example/CatalogueExampleInterface.kt @@ -10,17 +10,19 @@ import org.bukkit.Material public class CatalogueExampleInterface : RegistrableInterface { override val subcommand: String = "catalogue" - override fun create(): Interface<*, *> = buildCombinedInterface { - rows = 1 + override fun create(): Interface<*, *> = + buildCombinedInterface { + rows = 1 - withTransform { pane, _ -> - pane[3, 3] = StaticElement( - Drawable.drawable(Material.STICK), - ) { (player) -> - runBlocking { - ChangingTitleExampleInterface().create().open(player) - } + withTransform { pane, _ -> + pane[3, 3] = + StaticElement( + Drawable.drawable(Material.STICK), + ) { (player) -> + runBlocking { + ChangingTitleExampleInterface().create().open(player) + } + } } } - } } diff --git a/examples/src/main/kotlin/com/noxcrew/interfaces/example/ChangingTitleExampleInterface.kt b/examples/src/main/kotlin/com/noxcrew/interfaces/example/ChangingTitleExampleInterface.kt index 80e415b..5dace59 100644 --- a/examples/src/main/kotlin/com/noxcrew/interfaces/example/ChangingTitleExampleInterface.kt +++ b/examples/src/main/kotlin/com/noxcrew/interfaces/example/ChangingTitleExampleInterface.kt @@ -10,29 +10,31 @@ import org.bukkit.Material import org.bukkit.inventory.ItemStack public class ChangingTitleExampleInterface : RegistrableInterface { - override val subcommand: String = "changing-title" - override fun create(): Interface<*, *> = buildChestInterface { - rows = 1 + override fun create(): Interface<*, *> = + buildChestInterface { + rows = 1 - // Allow clicking the player inventory but not anything in the top inventory - allowClickingOwnInventoryIfClickingEmptySlotsIsPrevented = true + // Allow clicking the player inventory but not anything in the top inventory + allowClickingOwnInventoryIfClickingEmptySlotsIsPrevented = true - val numberProperty = interfaceProperty(0) - var number by numberProperty + val numberProperty = interfaceProperty(0) + var number by numberProperty - withTransform(numberProperty) { pane, view -> - view.title(Component.text(number)) + withTransform(numberProperty) { pane, view -> + view.title(Component.text(number)) - val item = ItemStack(Material.STICK) - .name("number -> $number") + val item = + ItemStack(Material.STICK) + .name("number -> $number") - pane[0, 4] = StaticElement(drawable(item)) { - number += 1 - } + pane[0, 4] = + StaticElement(drawable(item)) { + number += 1 + } - pane[0, 6] = StaticElement(drawable(Material.ACACIA_SIGN)) + pane[0, 6] = StaticElement(drawable(Material.ACACIA_SIGN)) + } } - } } diff --git a/examples/src/main/kotlin/com/noxcrew/interfaces/example/DelayedRequestExampleInterface.kt b/examples/src/main/kotlin/com/noxcrew/interfaces/example/DelayedRequestExampleInterface.kt index f61e1ec..41dbace 100644 --- a/examples/src/main/kotlin/com/noxcrew/interfaces/example/DelayedRequestExampleInterface.kt +++ b/examples/src/main/kotlin/com/noxcrew/interfaces/example/DelayedRequestExampleInterface.kt @@ -13,7 +13,6 @@ import org.bukkit.Material import kotlin.time.Duration.Companion.seconds public class DelayedRequestExampleInterface : RegistrableInterface { - private companion object { private val BACKING_ELEMENT = StaticElement(Drawable.drawable(Material.GRAY_CONCRETE)) } @@ -21,30 +20,32 @@ public class DelayedRequestExampleInterface : RegistrableInterface { override val subcommand: String = "delayed" @OptIn(DelicateCoroutinesApi::class) - override fun create(): Interface<*, *> = buildChestInterface { - initialTitle = text(subcommand) - rows = 2 - - withTransform { pane, _ -> - suspendingData().forEachIndexed { index, material -> - pane[0, index] = StaticElement(Drawable.drawable(material)) - } - } - - withTransform { pane, _ -> - for (index in 0..8) { - pane[1, index] = BACKING_ELEMENT + override fun create(): Interface<*, *> = + buildChestInterface { + initialTitle = text(subcommand) + rows = 2 + + withTransform { pane, _ -> + suspendingData().forEachIndexed { index, material -> + pane[0, index] = StaticElement(Drawable.drawable(material)) + } } - pane[0, 8] = StaticElement(Drawable.drawable(Material.ENDER_PEARL)) { - // This is very unsafe, it's up to you to set up a way to reliably - // launch coroutines per player in a click handler. - GlobalScope.launch { - it.view.back() + withTransform { pane, _ -> + for (index in 0..8) { + pane[1, index] = BACKING_ELEMENT } + + pane[0, 8] = + StaticElement(Drawable.drawable(Material.ENDER_PEARL)) { + // This is very unsafe, it's up to you to set up a way to reliably + // launch coroutines per player in a click handler. + GlobalScope.launch { + it.view.back() + } + } } } - } private suspend fun suspendingData(): List { delay(3.seconds) diff --git a/examples/src/main/kotlin/com/noxcrew/interfaces/example/ExamplePlugin.kt b/examples/src/main/kotlin/com/noxcrew/interfaces/example/ExamplePlugin.kt index 2b6df75..941817a 100644 --- a/examples/src/main/kotlin/com/noxcrew/interfaces/example/ExamplePlugin.kt +++ b/examples/src/main/kotlin/com/noxcrew/interfaces/example/ExamplePlugin.kt @@ -22,16 +22,18 @@ import org.incendo.cloud.kotlin.coroutines.extension.suspendingHandler import org.incendo.cloud.kotlin.extension.buildAndRegister import org.incendo.cloud.paper.LegacyPaperCommandManager -public class ExamplePlugin : JavaPlugin(), Listener { - +public class ExamplePlugin : + JavaPlugin(), + Listener { private companion object { - private val INTERFACES = listOf( - DelayedRequestExampleInterface(), - ChangingTitleExampleInterface(), - CatalogueExampleInterface(), - MovingExampleInterface(), - TabbedExampleInterface(), - ) + private val INTERFACES = + listOf( + DelayedRequestExampleInterface(), + ChangingTitleExampleInterface(), + CatalogueExampleInterface(), + MovingExampleInterface(), + TabbedExampleInterface(), + ) } private val counterProperty = interfaceProperty(5) @@ -112,93 +114,108 @@ public class ExamplePlugin : JavaPlugin(), Listener { ) } - private fun simpleInterface() = buildChestInterface { - rows = 6 + private fun simpleInterface() = + buildChestInterface { + rows = 6 - withTransform(counterProperty) { pane, _ -> - val item = ItemStack(Material.BEE_NEST) - .name("it's been $counter's ticks") - .description("click to see the ticks now") + withTransform(counterProperty) { pane, _ -> + val item = + ItemStack(Material.BEE_NEST) + .name("it's been $counter's ticks") + .description("click to see the ticks now") - pane[3, 3] = StaticElement(drawable(item)) { - it.player.sendMessage("it's been $counter's ticks") + pane[3, 3] = + StaticElement(drawable(item)) { + it.player.sendMessage("it's been $counter's ticks") + } } - } - withTransform { pane, _ -> - val item = ItemStack(Material.BEE_NEST) - .name("block the interface") - .description("block interaction and message in 5 seconds") + withTransform { pane, _ -> + val item = + ItemStack(Material.BEE_NEST) + .name("block the interface") + .description("block interaction and message in 5 seconds") - pane[5, 3] = StaticElement(drawable(item)) { - completingLater = true + pane[5, 3] = + StaticElement(drawable(item)) { + completingLater = true - runAsync(5) { - it.player.sendMessage("after blocking, it has been $counter's ticks") - complete() - } + runAsync(5) { + it.player.sendMessage("after blocking, it has been $counter's ticks") + complete() + } + } } - } - withTransform { pane, _ -> - forEachInGrid(6, 9) { row, column -> - if (pane.has(row, column)) return@forEachInGrid + withTransform { pane, _ -> + forEachInGrid(6, 9) { row, column -> + if (pane.has(row, column)) return@forEachInGrid - val item = ItemStack(Material.WHITE_STAINED_GLASS_PANE) - .name("row: $row, column: $column") + val item = + ItemStack(Material.WHITE_STAINED_GLASS_PANE) + .name("row: $row, column: $column") - pane[row, column] = StaticElement(drawable(item)) + pane[row, column] = StaticElement(drawable(item)) + } } } - } - private fun playerInterface() = buildPlayerInterface { - // Use modern logic to only cancel the item interaction and not block interactions while - // using this interface - onlyCancelItemInteraction = true + private fun playerInterface() = + buildPlayerInterface { + // Use modern logic to only cancel the item interaction and not block interactions while + // using this interface + onlyCancelItemInteraction = true - // Prioritise block interactions! - prioritiseBlockInteractions = true + // Prioritise block interactions! + prioritiseBlockInteractions = true - withTransform { pane, _ -> - val item = ItemStack(Material.COMPASS).name("interfaces example") + withTransform { pane, _ -> + val item = ItemStack(Material.COMPASS).name("interfaces example") - pane.hotbar[3] = StaticElement(drawable(item)) { (player) -> - player.sendMessage("hello") - } + pane.hotbar[3] = + StaticElement(drawable(item)) { (player) -> + player.sendMessage("hello") + } - pane.offHand = StaticElement(drawable(item)) { (player) -> - player.sendMessage("hey") - } + pane.offHand = + StaticElement(drawable(item)) { (player) -> + player.sendMessage("hey") + } - val armor = ItemStack(Material.STICK) + val armor = ItemStack(Material.STICK) - pane.armor.helmet = StaticElement(drawable(armor.name("helmet").clone())) + pane.armor.helmet = StaticElement(drawable(armor.name("helmet").clone())) - pane.armor.chest = StaticElement(drawable(armor.name("chest").clone())) + pane.armor.chest = StaticElement(drawable(armor.name("chest").clone())) - pane.armor.leggings = StaticElement(drawable(armor.name("leggings").clone())) + pane.armor.leggings = StaticElement(drawable(armor.name("leggings").clone())) - pane.armor.boots = StaticElement(drawable(armor.name("boots").clone())) + pane.armor.boots = StaticElement(drawable(armor.name("boots").clone())) + } } - } - private fun combinedInterface() = buildCombinedInterface { - rows = 6 + private fun combinedInterface() = + buildCombinedInterface { + rows = 6 - withTransform { pane, _ -> - forEachInGrid(10, 9) { row, column -> - val item = ItemStack(Material.WHITE_STAINED_GLASS_PANE) - .name("row: $row, column: $column") + withTransform { pane, _ -> + forEachInGrid(10, 9) { row, column -> + val item = + ItemStack(Material.WHITE_STAINED_GLASS_PANE) + .name("row: $row, column: $column") - pane[row, column] = StaticElement(drawable(item)) { (player) -> - player.sendMessage("row: $row, column: $column") + pane[row, column] = + StaticElement(drawable(item)) { (player) -> + player.sendMessage("row: $row, column: $column") + } } } } - } - private fun runAsync(delay: Int, runnable: Runnable) { + private fun runAsync( + delay: Int, + runnable: Runnable, + ) { Bukkit.getScheduler().runTaskLaterAsynchronously(this, runnable, delay * 20L) } } diff --git a/examples/src/main/kotlin/com/noxcrew/interfaces/example/ExampleUtilities.kt b/examples/src/main/kotlin/com/noxcrew/interfaces/example/ExampleUtilities.kt index 62df922..f68714d 100644 --- a/examples/src/main/kotlin/com/noxcrew/interfaces/example/ExampleUtilities.kt +++ b/examples/src/main/kotlin/com/noxcrew/interfaces/example/ExampleUtilities.kt @@ -4,15 +4,17 @@ import net.kyori.adventure.text.Component import org.bukkit.inventory.ItemStack public fun ItemStack.name(name: String): ItemStack { - itemMeta = itemMeta.also { meta -> - meta.displayName(Component.text(name)) - } + itemMeta = + itemMeta.also { meta -> + meta.displayName(Component.text(name)) + } return this } public fun ItemStack.description(description: String): ItemStack { - itemMeta = itemMeta.also { meta -> - meta.lore(listOf(Component.text(description))) - } + itemMeta = + itemMeta.also { meta -> + meta.lore(listOf(Component.text(description))) + } return this } diff --git a/examples/src/main/kotlin/com/noxcrew/interfaces/example/MovingExampleInterface.kt b/examples/src/main/kotlin/com/noxcrew/interfaces/example/MovingExampleInterface.kt index 9130290..f1092fa 100644 --- a/examples/src/main/kotlin/com/noxcrew/interfaces/example/MovingExampleInterface.kt +++ b/examples/src/main/kotlin/com/noxcrew/interfaces/example/MovingExampleInterface.kt @@ -10,20 +10,21 @@ import org.bukkit.Material public class MovingExampleInterface : RegistrableInterface { override val subcommand: String = "moving" - override fun create(): Interface<*, *> = buildChestInterface { - val countProperty = BoundInteger(4, 1, 7) - var count by countProperty + override fun create(): Interface<*, *> = + buildChestInterface { + val countProperty = BoundInteger(4, 1, 7) + var count by countProperty - // Allow clicking empty slots to allow testing various interactions with tiems and chest interfaces - preventClickingEmptySlots = false + // Allow clicking empty slots to allow testing various interactions with tiems and chest interfaces + preventClickingEmptySlots = false - rows = 1 + rows = 1 - withTransform(countProperty) { pane, _ -> - pane[0, 0] = StaticElement(drawable(Material.RED_CONCRETE)) { count-- } - pane[0, 8] = StaticElement(drawable(Material.GREEN_CONCRETE)) { count++ } + withTransform(countProperty) { pane, _ -> + pane[0, 0] = StaticElement(drawable(Material.RED_CONCRETE)) { count-- } + pane[0, 8] = StaticElement(drawable(Material.GREEN_CONCRETE)) { count++ } - pane[0, count] = StaticElement(drawable(Material.STICK)) + pane[0, count] = StaticElement(drawable(Material.STICK)) + } } - } } diff --git a/examples/src/main/kotlin/com/noxcrew/interfaces/example/RegistrableInterface.kt b/examples/src/main/kotlin/com/noxcrew/interfaces/example/RegistrableInterface.kt index 145b6d1..0748ce7 100644 --- a/examples/src/main/kotlin/com/noxcrew/interfaces/example/RegistrableInterface.kt +++ b/examples/src/main/kotlin/com/noxcrew/interfaces/example/RegistrableInterface.kt @@ -3,7 +3,6 @@ package com.noxcrew.interfaces.example import com.noxcrew.interfaces.interfaces.Interface public interface RegistrableInterface { - public val subcommand: String public fun create(): Interface<*, *> diff --git a/examples/src/main/kotlin/com/noxcrew/interfaces/example/TabbedExampleInterface.kt b/examples/src/main/kotlin/com/noxcrew/interfaces/example/TabbedExampleInterface.kt index bbc226a..f4949cf 100644 --- a/examples/src/main/kotlin/com/noxcrew/interfaces/example/TabbedExampleInterface.kt +++ b/examples/src/main/kotlin/com/noxcrew/interfaces/example/TabbedExampleInterface.kt @@ -11,11 +11,11 @@ import org.bukkit.Material import kotlin.time.Duration.Companion.milliseconds public class TabbedExampleInterface : RegistrableInterface { - private companion object { - private val ELEMENT = StaticElement( - drawable(Material.NETHER_STAR), - ) + private val ELEMENT = + StaticElement( + drawable(Material.NETHER_STAR), + ) } override val subcommand: String = "tabbed" @@ -26,49 +26,53 @@ public class TabbedExampleInterface : RegistrableInterface { rows = 6 withTransform { pane, _ -> - pane[8, 2] = StaticElement(drawable(Material.IRON_INGOT)) { (player) -> - completingLater = true - - runBlocking { - first.open(player) - complete() + pane[8, 2] = + StaticElement(drawable(Material.IRON_INGOT)) { (player) -> + completingLater = true + + runBlocking { + first.open(player) + complete() + } } - } - pane[8, 4] = StaticElement(drawable(Material.GOLD_INGOT)) { (player) -> - completingLater = true + pane[8, 4] = + StaticElement(drawable(Material.GOLD_INGOT)) { (player) -> + completingLater = true - runBlocking { - second.open(player) - complete() + runBlocking { + second.open(player) + complete() + } } - } } } - private val first = buildCombinedInterface { - defaults() + private val first = + buildCombinedInterface { + defaults() - withTransform { pane, _ -> - pane[0, 1] = ELEMENT - } + withTransform { pane, _ -> + pane[0, 1] = ELEMENT + } - withTransform { pane, _ -> - delay(100.milliseconds) - pane[2, 4] = ELEMENT + withTransform { pane, _ -> + delay(100.milliseconds) + pane[2, 4] = ELEMENT + } } - } - private val second = buildCombinedInterface { - defaults() + private val second = + buildCombinedInterface { + defaults() - withTransform { pane, _ -> - pane[1, 2] = ELEMENT - } + withTransform { pane, _ -> + pane[1, 2] = ELEMENT + } - withTransform { pane, _ -> - delay(100.milliseconds) - pane[3, 5] = ELEMENT + withTransform { pane, _ -> + delay(100.milliseconds) + pane[3, 5] = ELEMENT + } } - } } diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/InterfacesConstants.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/InterfacesConstants.kt index bcec163..7d63705 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/InterfacesConstants.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/InterfacesConstants.kt @@ -14,50 +14,50 @@ import java.util.concurrent.atomic.AtomicInteger /** Holds the shared scope used for any interfaces coroutines. */ public object InterfacesConstants { - private val EXCEPTION_LOGGER = LoggerFactory.getLogger("InterfacesExceptionHandler") /** The [CoroutineScope] for any suspending operations performed by interfaces. */ - public val SCOPE: CoroutineScope = CoroutineScope( - CoroutineName("interfaces") + - SupervisorJob() + - CoroutineExceptionHandler { context, exception -> - val details = context[InterfacesCoroutineDetails] + public val SCOPE: CoroutineScope = + CoroutineScope( + CoroutineName("interfaces") + + SupervisorJob() + + CoroutineExceptionHandler { context, exception -> + val details = context[InterfacesCoroutineDetails] - if (details == null) { - EXCEPTION_LOGGER.error("An unknown error occurred in a coroutine!", exception) - } else { - val (player, reason) = details - EXCEPTION_LOGGER.error( - """ - An unknown error occurred in a coroutine! - - Player: ${player ?: "N/A"} (${player?.let(Bukkit::getPlayer)?.name ?: "offline"}) - - Launch reason: $reason - """.trimIndent(), - exception, - ) - } - } + - run { - val threadNumber = AtomicInteger() - val factory = { runnable: Runnable -> - Thread("interfaces-${threadNumber.getAndIncrement()}").apply { - isDaemon = true + if (details == null) { + EXCEPTION_LOGGER.error("An unknown error occurred in a coroutine!", exception) + } else { + val (player, reason) = details + EXCEPTION_LOGGER.error( + """ + An unknown error occurred in a coroutine! + - Player: ${player ?: "N/A"} (${player?.let(Bukkit::getPlayer)?.name ?: "offline"}) + - Launch reason: $reason + """.trimIndent(), + exception, + ) } - } - - System.getProperty("com.noxcrew.interfaces.fixedPoolSize") - ?.toIntOrNull() - ?.coerceAtLeast(2) - ?.let { size -> - if (System.getProperty("com.noxcrew.interfaces.useScheduledPool").toBoolean()) { - Executors.newScheduledThreadPool(size, factory) - } else { - Executors.newFixedThreadPool(size, factory) + } + + run { + val threadNumber = AtomicInteger() + val factory = { runnable: Runnable -> + Thread("interfaces-${threadNumber.getAndIncrement()}").apply { + isDaemon = true } } - ?.asCoroutineDispatcher() - ?: Dispatchers.Default - }, - ) + + System + .getProperty("com.noxcrew.interfaces.fixedPoolSize") + ?.toIntOrNull() + ?.coerceAtLeast(2) + ?.let { size -> + if (System.getProperty("com.noxcrew.interfaces.useScheduledPool").toBoolean()) { + Executors.newScheduledThreadPool(size, factory) + } else { + Executors.newFixedThreadPool(size, factory) + } + }?.asCoroutineDispatcher() + ?: Dispatchers.Default + }, + ) } diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/click/ClickHandler.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/click/ClickHandler.kt index 5fe9415..de76d39 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/click/ClickHandler.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/click/ClickHandler.kt @@ -6,7 +6,6 @@ import kotlinx.coroutines.cancel /** Handles a click action. Receives a [ClickContext] object filled with information about the click. */ public fun interface ClickHandler { - public companion object { /** * An empty click handler that performs no logic. The click event will be cancelled @@ -22,9 +21,13 @@ public fun interface ClickHandler { public val ALLOW: ClickHandler = ClickHandler { cancelled = false } /** Runs a [CompletableClickHandler] with [clickHandler] and [context]. */ - public fun process(clickHandler: ClickHandler, context: ClickContext): Unit = with(clickHandler) { - CompletableClickHandler().handle(context) - } + public fun process( + clickHandler: ClickHandler, + context: ClickContext, + ): Unit = + with(clickHandler) { + CompletableClickHandler().handle(context) + } } /** Handles a click with the given [context]. */ @@ -33,7 +36,6 @@ public fun interface ClickHandler { /** A click handler that be assigned tasks to run on completion. */ public class CompletableClickHandler { - private val deferred = CompletableDeferred(null) /** Whether this handler has been completed (or cancelled). */ diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/drawable/Drawable.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/drawable/Drawable.kt index c20e6d2..13f40a4 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/drawable/Drawable.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/drawable/Drawable.kt @@ -6,7 +6,6 @@ import org.bukkit.inventory.ItemStack /** A drawable is a function that takes in a [Player] and creates an [ItemStack] to show that player. */ public fun interface Drawable { - public companion object { /** Creates a new [Drawable] out of a given [item]. */ public fun drawable(item: ItemStack): Drawable = Drawable { item } diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/element/CompletedElement.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/element/CompletedElement.kt index c18b49b..4a119db 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/element/CompletedElement.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/element/CompletedElement.kt @@ -11,7 +11,8 @@ public data class CompletedElement( ) /** Completes drawing this element for [player]. */ -public suspend fun Element.complete(player: Player): CompletedElement = CompletedElement( - drawable().draw(player), - clickHandler(), -) +public suspend fun Element.complete(player: Player): CompletedElement = + CompletedElement( + drawable().draw(player), + clickHandler(), + ) diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/element/Element.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/element/Element.kt index f729126..7a06cb7 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/element/Element.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/element/Element.kt @@ -6,7 +6,6 @@ import org.bukkit.Material /** An element in an interface that is drawn by [drawable] and triggers [clickHandler]. */ public interface Element { - /** An empty element that shows as air and has no function. */ public companion object EMPTY : Element by StaticElement(Drawable.drawable(Material.AIR)) diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/element/StaticElement.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/element/StaticElement.kt index 06504ae..7da3d65 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/element/StaticElement.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/element/StaticElement.kt @@ -8,7 +8,6 @@ public class StaticElement( private val drawable: Drawable, private val clickHandler: ClickHandler = ClickHandler.EMPTY, ) : Element { - override fun drawable(): Drawable = drawable override fun clickHandler(): ClickHandler = clickHandler diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/event/DrawPaneEvent.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/event/DrawPaneEvent.kt index 82a0e9a..7acd859 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/event/DrawPaneEvent.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/event/DrawPaneEvent.kt @@ -15,7 +15,6 @@ public class DrawPaneEvent( /** Whether any slots in the player inventory were drawn. */ public val isPlayerInventory: Boolean, ) : PlayerEvent(player) { - public companion object { @JvmStatic public val handlerList: HandlerList = HandlerList() diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/grid/ChainGridPositionGenerator.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/grid/ChainGridPositionGenerator.kt index 743b46f..c29d1f5 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/grid/ChainGridPositionGenerator.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/grid/ChainGridPositionGenerator.kt @@ -7,14 +7,11 @@ public data class ChainGridPositionGenerator( /** The second generator. */ private val second: GridPositionGenerator, ) : GridPositionGenerator { - public companion object { /** Adds two grid position generators together. */ public operator fun GridPositionGenerator.plus(second: GridPositionGenerator): ChainGridPositionGenerator = ChainGridPositionGenerator(this, second) } - override fun generate(): List { - return first.generate() + second.generate() - } + override fun generate(): List = first.generate() + second.generate() } diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/grid/GridBoxGenerator.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/grid/GridBoxGenerator.kt index a219bbb..361cb21 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/grid/GridBoxGenerator.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/grid/GridBoxGenerator.kt @@ -8,12 +8,9 @@ public data class GridBoxGenerator( private val min: GridPoint, private val max: GridPoint, ) : GridPositionGenerator { - public constructor(x1: Int, y1: Int, x2: Int, y2: Int) : this(GridPoint(x1, y1), GridPoint(x2, y2)) - override fun iterator(): Iterator = - (min..max).iterator() + override fun iterator(): Iterator = (min..max).iterator() - override fun generate(): List = - (min..max).toList() + override fun generate(): List = (min..max).toList() } diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/grid/GridMap.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/grid/GridMap.kt index 46c44af..ec560c7 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/grid/GridMap.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/grid/GridMap.kt @@ -2,30 +2,38 @@ package com.noxcrew.interfaces.grid /** Stores elements of type [V] at a row and column coordinate in a 2d space. */ public interface GridMap { - /** Sets the value at ([row], [column]) to [value]. */ - public operator fun set(row: Int, column: Int, value: V) + public operator fun set( + row: Int, + column: Int, + value: V, + ) /** Sets the value at ([vector]) to [value]. */ - public operator fun set(vector: GridPoint, value: V) { + public operator fun set( + vector: GridPoint, + value: V, + ) { set(vector.x, vector.y, value) } /** Returns the value at ([row], [column]), or `null` if none exists at the given location. */ - public operator fun get(row: Int, column: Int): V? + public operator fun get( + row: Int, + column: Int, + ): V? /** Returns the value at ([vector]), or `null` if none exists at the given location. */ - public operator fun get(vector: GridPoint): V? { - return get(vector.x, vector.y) - } + public operator fun get(vector: GridPoint): V? = get(vector.x, vector.y) /** Returns whether this map contains a value at ([row], [column]). */ - public fun has(row: Int, column: Int): Boolean + public fun has( + row: Int, + column: Int, + ): Boolean /** Returns whether this map contains a value at ([vector]). */ - public fun has(vector: GridPoint): Boolean { - return has(vector.x, vector.y) - } + public fun has(vector: GridPoint): Boolean = has(vector.x, vector.y) /** Runs [consumer] for each element in this map. */ public fun forEach(consumer: (row: Int, column: Int, V) -> Unit) diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/grid/GridPoint.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/grid/GridPoint.kt index 1ca658e..34160cc 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/grid/GridPoint.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/grid/GridPoint.kt @@ -3,8 +3,10 @@ package com.noxcrew.interfaces.grid import com.noxcrew.interfaces.pane.PlayerPane /** A 2-dimensional vector storing integer components. */ -public data class GridPoint(val x: Int, val y: Int) { - +public data class GridPoint( + val x: Int, + val y: Int, +) { public companion object { /** The possible valid slot range inside the player inventory. */ public val PLAYER_INVENTORY_RANGE: IntRange = 0..40 @@ -27,23 +29,25 @@ public data class GridPoint(val x: Int, val y: Int) { } /** Creates a new [GridPoint] for ([x], [y]). */ - public fun at(x: Int, y: Int): GridPoint = GridPoint(x, y) + public fun at( + x: Int, + y: Int, + ): GridPoint = GridPoint(x, y) } /** Returns a new [GridPoint] equal to this + [other]. */ - public operator fun plus(other: GridPoint): GridPoint = - GridPoint(x + other.x, y + other.y) + public operator fun plus(other: GridPoint): GridPoint = GridPoint(x + other.x, y + other.y) /** Returns a new [GridPoint] equal to this - [other]. */ - public operator fun minus(other: GridPoint): GridPoint = - GridPoint(x - other.x, y - other.y) + public operator fun minus(other: GridPoint): GridPoint = GridPoint(x - other.x, y - other.y) /** Returns a sequence of grid points between this and [other]. */ - public operator fun rangeTo(other: GridPoint): Sequence = sequence { - for (x in x..other.x) { - for (y in y..other.y) { - yield(at(x, y)) + public operator fun rangeTo(other: GridPoint): Sequence = + sequence { + for (x in x..other.x) { + for (y in y..other.y) { + yield(at(x, y)) + } } } - } } diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/grid/GridPositionGenerator.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/grid/GridPositionGenerator.kt index 801e89a..4998418 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/grid/GridPositionGenerator.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/grid/GridPositionGenerator.kt @@ -2,11 +2,8 @@ package com.noxcrew.interfaces.grid /** Generates a set of [GridPoint]'s. */ public fun interface GridPositionGenerator : Iterable { - /** Returns a list of [GridPoint]'s. */ public fun generate(): List - override fun iterator(): Iterator { - return generate().iterator() - } + override fun iterator(): Iterator = generate().iterator() } diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/grid/HashGridMap.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/grid/HashGridMap.kt index 693e416..f056b27 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/grid/HashGridMap.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/grid/HashGridMap.kt @@ -5,18 +5,27 @@ public class HashGridMap( private val backing: MutableMap> = mutableMapOf(), private val innerConstructor: () -> MutableMap = { mutableMapOf() }, ) : GridMap { - - override fun set(row: Int, column: Int, value: V) { + override fun set( + row: Int, + column: Int, + value: V, + ) { val rowView = backing.computeIfAbsent(row) { innerConstructor() } rowView[column] = value } - override fun get(row: Int, column: Int): V? { + override fun get( + row: Int, + column: Int, + ): V? { val rowView = backing[row] ?: return null return rowView[column] } - override fun has(row: Int, column: Int): Boolean { + override fun has( + row: Int, + column: Int, + ): Boolean { val rowView = backing[row] ?: return false return rowView.containsKey(column) } diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/ChestInterface.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/ChestInterface.kt index beb972a..9b63581 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/ChestInterface.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/ChestInterface.kt @@ -11,8 +11,8 @@ public class ChestInterface internal constructor( override val rows: Int, override val initialTitle: Component?, override val builder: ChestInterfaceBuilder, -) : Interface, TitledInterface { - +) : Interface, + TitledInterface { public companion object { /** The maximum number of rows for a chest GUI. */ public const val MAX_NUMBER_OF_ROWS: Int = 6 @@ -22,7 +22,10 @@ public class ChestInterface internal constructor( override fun createPane(): Pane = Pane() - override suspend fun open(player: Player, parent: InterfaceView?): ChestInterfaceView { + override suspend fun open( + player: Player, + parent: InterfaceView?, + ): ChestInterfaceView { val view = ChestInterfaceView(player, this, parent) view.open() return view diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/ChestInterfaceBuilder.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/ChestInterfaceBuilder.kt index a606a0b..99efc78 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/ChestInterfaceBuilder.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/ChestInterfaceBuilder.kt @@ -4,18 +4,17 @@ import com.noxcrew.interfaces.pane.Pane import net.kyori.adventure.text.Component /** Assists in building a [ChestInterface]. */ -public class ChestInterfaceBuilder : - InterfaceBuilder() { - +public class ChestInterfaceBuilder : InterfaceBuilder() { /** Sets the amount of rows for this interface to use. */ public var rows: Int = 0 /** Sets the initial title of this interface. */ public var initialTitle: Component? = null - override fun build(): ChestInterface = ChestInterface( - rows, - initialTitle, - this, - ) + override fun build(): ChestInterface = + ChestInterface( + rows, + initialTitle, + this, + ) } diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/CombinedInterface.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/CombinedInterface.kt index 5d9d567..5ea69f8 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/CombinedInterface.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/CombinedInterface.kt @@ -11,8 +11,8 @@ public class CombinedInterface internal constructor( override val rows: Int, override val initialTitle: Component?, override val builder: CombinedInterfaceBuilder, -) : Interface, TitledInterface { - +) : Interface, + TitledInterface { public companion object { /** The maximum number of rows for a combined interface. */ public const val MAX_NUMBER_OF_ROWS: Int = 9 @@ -24,7 +24,10 @@ public class CombinedInterface internal constructor( override fun createPane(): CombinedPane = CombinedPane(rows) - override suspend fun open(player: Player, parent: InterfaceView?): CombinedInterfaceView { + override suspend fun open( + player: Player, + parent: InterfaceView?, + ): CombinedInterfaceView { val view = CombinedInterfaceView(player, this, parent) view.open() return view diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/CombinedInterfaceBuilder.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/CombinedInterfaceBuilder.kt index 33643e2..ca15054 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/CombinedInterfaceBuilder.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/CombinedInterfaceBuilder.kt @@ -4,18 +4,17 @@ import com.noxcrew.interfaces.pane.CombinedPane import net.kyori.adventure.text.Component /** Assists in building a [CombinedInterface]. */ -public class CombinedInterfaceBuilder : - InterfaceBuilder() { - +public class CombinedInterfaceBuilder : InterfaceBuilder() { /** Sets the amount of rows for this interface to use. */ public var rows: Int = 0 /** Sets the initial title of this interface. */ public var initialTitle: Component? = null - override fun build(): CombinedInterface = CombinedInterface( - rows, - initialTitle, - this, - ) + override fun build(): CombinedInterface = + CombinedInterface( + rows, + initialTitle, + this, + ) } diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/Interface.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/Interface.kt index dc8de03..97c40ea 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/Interface.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/Interface.kt @@ -9,7 +9,6 @@ import org.bukkit.entity.Player /** A created interface that can be opened for a player to create a unique view. */ public interface Interface, P : Pane> { - /** The amount of rows this interface contains. */ public val rows: Int diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/InterfaceBuilder.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/InterfaceBuilder.kt index 51c52a0..ed51606 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/InterfaceBuilder.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/InterfaceBuilder.kt @@ -9,7 +9,6 @@ import com.noxcrew.interfaces.utilities.IncrementingInteger /** Assists in creating a new interface. */ public abstract class InterfaceBuilder

> : InterfaceProperties

() { - private val transformCounter by IncrementingInteger() private val _transforms: MutableCollection> = mutableListOf() @@ -21,7 +20,10 @@ public abstract class InterfaceBuilder

> : Interfac public abstract fun build(): I /** Adds a new transform to the interface that updates whenever [triggers] change. */ - public fun withTransform(vararg triggers: Trigger, transform: Transform

) { + public fun withTransform( + vararg triggers: Trigger, + transform: Transform

, + ) { _transforms += AppliedTransform(transformCounter, triggers.toSet(), transform) } diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/InterfaceProperties.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/InterfaceProperties.kt index 34f218e..bfc7377 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/InterfaceProperties.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/InterfaceProperties.kt @@ -8,7 +8,6 @@ import org.bukkit.inventory.ItemStack /** Stores all shared properties of an interface. */ public open class InterfaceProperties

{ - private companion object { /** All default reasons used for a new close handler. */ private val DEFAULT_REASONS = InventoryCloseEvent.Reason.entries.minus(InventoryCloseEvent.Reason.PLUGIN) diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/PlayerInterface.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/PlayerInterface.kt index e1a6a76..bd1fccc 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/PlayerInterface.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/PlayerInterface.kt @@ -9,7 +9,6 @@ import org.bukkit.entity.Player public class PlayerInterface internal constructor( override val builder: PlayerInterfaceBuilder, ) : Interface { - public companion object { /** The maximum number of rows for a player interface. */ public const val MAX_NUMBER_OF_ROWS: Int = 9 @@ -21,7 +20,10 @@ public class PlayerInterface internal constructor( override fun createPane(): PlayerPane = PlayerPane() - override suspend fun open(player: Player, parent: InterfaceView?): PlayerInterfaceView { + override suspend fun open( + player: Player, + parent: InterfaceView?, + ): PlayerInterfaceView { val view = PlayerInterfaceView(player, this) view.open() return view diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/PlayerInterfaceBuilder.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/PlayerInterfaceBuilder.kt index d08904d..aa8eb26 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/PlayerInterfaceBuilder.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/PlayerInterfaceBuilder.kt @@ -4,6 +4,5 @@ import com.noxcrew.interfaces.pane.PlayerPane /** Assists in building a [PlayerInterface]. */ public class PlayerInterfaceBuilder : InterfaceBuilder() { - override fun build(): PlayerInterface = PlayerInterface(this) } diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/TitledInterface.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/TitledInterface.kt index 24ff1f4..30dc0ce 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/TitledInterface.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/TitledInterface.kt @@ -4,7 +4,6 @@ import net.kyori.adventure.text.Component /** The basis for an interface with a title. */ public interface TitledInterface { - /** The initial title of the interface. */ public val initialTitle: Component? } diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/inventory/CachedInterfacesInventory.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/inventory/CachedInterfacesInventory.kt index 462e0f0..927e5d0 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/inventory/CachedInterfacesInventory.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/inventory/CachedInterfacesInventory.kt @@ -4,8 +4,11 @@ import org.bukkit.inventory.ItemStack /** An [InterfacesInventory] where changes are only applied if the new item stack is different. */ public abstract class CachedInterfacesInventory : InterfacesInventory { - - final override fun set(row: Int, column: Int, item: ItemStack?): Boolean { + final override fun set( + row: Int, + column: Int, + item: ItemStack?, + ): Boolean { val current = get(row, column) if (current == item) { @@ -17,5 +20,9 @@ public abstract class CachedInterfacesInventory : InterfacesInventory { } /** Sets the item at ([row], [column]) to [item]. */ - protected abstract fun setInternal(row: Int, column: Int, item: ItemStack?) + protected abstract fun setInternal( + row: Int, + column: Int, + item: ItemStack?, + ) } diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/inventory/ChestInterfacesInventory.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/inventory/ChestInterfacesInventory.kt index 2f82863..72a71c8 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/inventory/ChestInterfacesInventory.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/inventory/ChestInterfacesInventory.kt @@ -13,19 +13,28 @@ public class ChestInterfacesInventory( title: Component?, rows: Int, ) : CachedInterfacesInventory() { - /** The [chestInventory] being used to place items in. */ public val chestInventory: Inventory = createBukkitInventory(holder, rows, title) - override fun get(row: Int, column: Int): ItemStack? { + override fun get( + row: Int, + column: Int, + ): ItemStack? { val index = gridPointToBukkitIndex(row, column) return chestInventory.getItem(index) } - override fun setInternal(row: Int, column: Int, item: ItemStack?) { + override fun setInternal( + row: Int, + column: Int, + item: ItemStack?, + ) { val index = gridPointToBukkitIndex(row, column) chestInventory.setItem(index, item) } - override fun isPlayerInventory(row: Int, column: Int): Boolean = false + override fun isPlayerInventory( + row: Int, + column: Int, + ): Boolean = false } diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/inventory/CombinedInterfacesInventory.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/inventory/CombinedInterfacesInventory.kt index ccf881b..f20ccc4 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/inventory/CombinedInterfacesInventory.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/inventory/CombinedInterfacesInventory.kt @@ -17,14 +17,16 @@ public class CombinedInterfacesInventory( title: Component?, private val rows: Int, ) : CachedInterfacesInventory() { - private val chestSlots = rows * COLUMNS_IN_CHEST private val playerInventory = player.inventory /** The [chestInventory] being used to place items in. */ public val chestInventory: Inventory = createBukkitInventory(holder, rows, title) - override fun get(row: Int, column: Int): ItemStack? { + override fun get( + row: Int, + column: Int, + ): ItemStack? { val bukkitIndex = gridPointToBukkitIndex(row, column) if (row >= rows) { @@ -35,7 +37,11 @@ public class CombinedInterfacesInventory( return chestInventory.getItem(bukkitIndex) } - override fun setInternal(row: Int, column: Int, item: ItemStack?) { + override fun setInternal( + row: Int, + column: Int, + item: ItemStack?, + ) { val bukkitIndex = gridPointToBukkitIndex(row, column) if (row >= rows) { @@ -47,5 +53,8 @@ public class CombinedInterfacesInventory( chestInventory.setItem(bukkitIndex, item) } - override fun isPlayerInventory(row: Int, column: Int): Boolean = row >= rows + override fun isPlayerInventory( + row: Int, + column: Int, + ): Boolean = row >= rows } diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/inventory/InterfacesInventory.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/inventory/InterfacesInventory.kt index d0116a4..b5d3009 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/inventory/InterfacesInventory.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/inventory/InterfacesInventory.kt @@ -4,13 +4,22 @@ import org.bukkit.inventory.ItemStack /** Represents some inventory that item stacks can be placed into. */ public interface InterfacesInventory { - /** Returns whether ([row], [column]) falls within a player's inventory. */ - public fun isPlayerInventory(row: Int, column: Int): Boolean + public fun isPlayerInventory( + row: Int, + column: Int, + ): Boolean /** Sets the item at ([row], [column]) to [item]. */ - public fun set(row: Int, column: Int, item: ItemStack?): Boolean + public fun set( + row: Int, + column: Int, + item: ItemStack?, + ): Boolean /** Returns the value at ([row], [column]). */ - public fun get(row: Int, column: Int): ItemStack? + public fun get( + row: Int, + column: Int, + ): ItemStack? } diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/inventory/PlayerInterfacesInventory.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/inventory/PlayerInterfacesInventory.kt index 4797347..68c97bd 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/inventory/PlayerInterfacesInventory.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/inventory/PlayerInterfacesInventory.kt @@ -9,18 +9,27 @@ import org.bukkit.inventory.PlayerInventory public class PlayerInterfacesInventory( player: Player, ) : CachedInterfacesInventory() { - private val playerInventory = player.inventory - override fun get(row: Int, column: Int): ItemStack? { + override fun get( + row: Int, + column: Int, + ): ItemStack? { val index = gridPointToBukkitIndex(row, column) return playerInventory.getItem(index) } - override fun setInternal(row: Int, column: Int, item: ItemStack?) { + override fun setInternal( + row: Int, + column: Int, + item: ItemStack?, + ) { val index = gridPointToBukkitIndex(row, column) return playerInventory.setItem(index, item) } - override fun isPlayerInventory(row: Int, column: Int): Boolean = true + override fun isPlayerInventory( + row: Int, + column: Int, + ): Boolean = true } diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/pane/CombinedPane.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/pane/CombinedPane.kt index b992d8a..5b0f914 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/pane/CombinedPane.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/pane/CombinedPane.kt @@ -4,19 +4,19 @@ package com.noxcrew.interfaces.pane public class CombinedPane( public val chestRows: Int, ) : OrderedPane(createMappings(chestRows)) { - public companion object { /** Creates mappings for a combined view with [rows] rows. */ - public fun createMappings(rows: Int): List = buildList { - IntRange(0, rows - 1).forEach(::add) + public fun createMappings(rows: Int): List = + buildList { + IntRange(0, rows - 1).forEach(::add) - // the players hotbar is row 0 in the players inventory, - // for combined interfaces it makes more sense for hotbar - // to be the last row, so reshuffle here. - add(rows + 1) - add(rows + 2) - add(rows + 3) - add(rows) - } + // the players hotbar is row 0 in the players inventory, + // for combined interfaces it makes more sense for hotbar + // to be the last row, so reshuffle here. + add(rows + 1) + add(rows + 2) + add(rows + 3) + add(rows) + } } } diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/pane/CompletedPane.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/pane/CompletedPane.kt index 897118b..ae7706b 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/pane/CompletedPane.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/pane/CompletedPane.kt @@ -21,9 +21,7 @@ public open class CompletedPane : GridMap by HashGridMap() { internal class CompletedOrderedPane( private val ordering: List, ) : CompletedPane() { - - override fun getRaw(vector: GridPoint): CompletedElement? = - ordering.getOrNull(vector.x)?.let { get(it, vector.y) } + override fun getRaw(vector: GridPoint): CompletedElement? = ordering.getOrNull(vector.x)?.let { get(it, vector.y) } } /** Completes a pane for [player] by drawing each element while suspending. */ diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/pane/OrderedPane.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/pane/OrderedPane.kt index bc92031..58ffaae 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/pane/OrderedPane.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/pane/OrderedPane.kt @@ -6,18 +6,21 @@ import com.noxcrew.interfaces.element.Element public abstract class OrderedPane( internal val ordering: List, ) : Pane() { + override fun get( + row: Int, + column: Int, + ): Element? = super.get(orderedRow(row), column) - override fun get(row: Int, column: Int): Element? { - return super.get(orderedRow(row), column) - } + override fun set( + row: Int, + column: Int, + value: Element, + ) = super.set(orderedRow(row), column, value) - override fun set(row: Int, column: Int, value: Element) { - return super.set(orderedRow(row), column, value) - } - - override fun has(row: Int, column: Int): Boolean { - return super.has(orderedRow(row), column) - } + override fun has( + row: Int, + column: Int, + ): Boolean = super.has(orderedRow(row), column) private fun orderedRow(row: Int) = ordering[row] } diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/pane/Pane.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/pane/Pane.kt index 923c962..19ed448 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/pane/Pane.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/pane/Pane.kt @@ -6,25 +6,30 @@ import com.noxcrew.interfaces.grid.HashGridMap /** A grid map of elements. */ public open class Pane : GridMap { - // This has to be manual redirecting instead of using the by // syntax as the by syntax breaks the ability to override // any of the methods! private val gridMap = HashGridMap() - override fun set(row: Int, column: Int, value: Element): Unit = - gridMap.set(row, column, value) + override fun set( + row: Int, + column: Int, + value: Element, + ): Unit = gridMap.set(row, column, value) - override fun get(row: Int, column: Int): Element? = - gridMap[row, column] + override fun get( + row: Int, + column: Int, + ): Element? = gridMap[row, column] - override fun has(row: Int, column: Int): Boolean = - gridMap.has(row, column) + override fun has( + row: Int, + column: Int, + ): Boolean = gridMap.has(row, column) override suspend fun forEachSuspending(consumer: suspend (row: Int, column: Int, Element) -> Unit): Unit = gridMap.forEachSuspending(consumer) - override fun forEach(consumer: (row: Int, column: Int, Element) -> Unit): Unit = - gridMap.forEach(consumer) + override fun forEach(consumer: (row: Int, column: Int, Element) -> Unit): Unit = gridMap.forEach(consumer) } diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/pane/PlayerPane.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/pane/PlayerPane.kt index 46df849..826b3b0 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/pane/PlayerPane.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/pane/PlayerPane.kt @@ -5,7 +5,6 @@ import com.noxcrew.interfaces.grid.GridPoint /** An ordered pane that wraps the player inventory. */ public class PlayerPane : OrderedPane(PANE_ORDERING) { - internal companion object { /** The base ordering of the player inventory to go from logical rows to Bukkit rows. */ internal val PANE_ORDERING = listOf(1, 2, 3, 0, 4) @@ -30,7 +29,10 @@ public class PlayerPane : OrderedPane(PANE_ORDERING) { public operator fun get(slot: Int): Element? = get(3, slot) /** Sets the item in the hotbar at the [slot]-th slot to [value]. */ - public operator fun set(slot: Int, value: Element): Unit = set(3, slot, value) + public operator fun set( + slot: Int, + value: Element, + ): Unit = set(3, slot, value) } public inner class Armor { diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/properties/DelegateTrigger.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/properties/DelegateTrigger.kt index 471538d..a4af8de 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/properties/DelegateTrigger.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/properties/DelegateTrigger.kt @@ -6,11 +6,12 @@ import java.util.concurrent.ConcurrentLinkedDeque /** A trigger that delegates triggers to its listeners. */ public open class DelegateTrigger : Trigger { - - private val updateListeners = Caffeine.newBuilder() - .weakKeys() - .build Unit>>() - .asMap() + private val updateListeners = + Caffeine + .newBuilder() + .weakKeys() + .build Unit>>() + .asMap() override fun trigger() { updateListeners.forEach { (obj, listeners) -> @@ -18,10 +19,14 @@ public open class DelegateTrigger : Trigger { } } - override fun addListener(reference: T, listener: T.() -> Unit) { + override fun addListener( + reference: T, + listener: T.() -> Unit, + ) { @Suppress("UNCHECKED_CAST") - updateListeners.computeIfAbsent(reference) { - ConcurrentLinkedDeque() - }.add(listener as (Any.() -> Unit)) + updateListeners + .computeIfAbsent(reference) { + ConcurrentLinkedDeque() + }.add(listener as (Any.() -> Unit)) } } diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/properties/EmptyTrigger.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/properties/EmptyTrigger.kt index af5b273..f0c0132 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/properties/EmptyTrigger.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/properties/EmptyTrigger.kt @@ -2,12 +2,14 @@ package com.noxcrew.interfaces.properties /** A trigger that does nothing. */ public object EmptyTrigger : Trigger { - override fun trigger() { // no implementation } - override fun addListener(reference: T, listener: T.() -> Unit) { + override fun addListener( + reference: T, + listener: T.() -> Unit, + ) { // no implementation } } diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/properties/InterfaceProperty.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/properties/InterfaceProperty.kt index 1bad5cd..d530cef 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/properties/InterfaceProperty.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/properties/InterfaceProperty.kt @@ -6,9 +6,13 @@ import kotlin.reflect.KProperty /** An observable property that is triggered when its value changes. */ public class InterfaceProperty( defaultValue: T, -) : ObservableProperty(defaultValue), Trigger by DelegateTrigger() { - - override fun afterChange(property: KProperty<*>, oldValue: T, newValue: T) { +) : ObservableProperty(defaultValue), + Trigger by DelegateTrigger() { + override fun afterChange( + property: KProperty<*>, + oldValue: T, + newValue: T, + ) { if (oldValue != newValue) { trigger() } diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/properties/Trigger.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/properties/Trigger.kt index 9d962b5..c5745b4 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/properties/Trigger.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/properties/Trigger.kt @@ -2,7 +2,6 @@ package com.noxcrew.interfaces.properties /** A generic object that can be triggered which will invoke its listeners. */ public interface Trigger { - /** Triggers the listeners to be invoked. */ public fun trigger() @@ -13,5 +12,8 @@ public interface Trigger { * to avoid situations where the existence of [listener] holds the [reference] * captive, preventing it from being garbage collected. */ - public fun addListener(reference: T, listener: T.() -> Unit) + public fun addListener( + reference: T, + listener: T.() -> Unit, + ) } diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/transform/ReactiveTransform.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/transform/ReactiveTransform.kt index fa17582..1f11548 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/transform/ReactiveTransform.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/transform/ReactiveTransform.kt @@ -5,7 +5,6 @@ import com.noxcrew.interfaces.properties.Trigger /** A transform that updates whenever [triggers] are triggered. */ public interface ReactiveTransform

: Transform

{ - /** The triggers this transform listens to. */ public val triggers: Array } diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/transform/builtin/PagedTransformation.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/transform/builtin/PagedTransformation.kt index bb8722f..3601195 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/transform/builtin/PagedTransformation.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/transform/builtin/PagedTransformation.kt @@ -17,14 +17,16 @@ public abstract class PagedTransformation

( private val forward: PaginationButton, extraTriggers: Array = emptyArray(), ) : ReactiveTransform

{ - /** The current page of this transform, bound between 0 and the integer limit. */ protected val boundPage: BoundInteger = BoundInteger(0, 0, Integer.MAX_VALUE) /** The current page of the transform. */ protected var page: Int by boundPage - override suspend fun invoke(pane: P, view: InterfaceView) { + override suspend fun invoke( + pane: P, + view: InterfaceView, + ) { if (boundPage.hasPreceeding()) { applyButton(pane, back) } @@ -35,13 +37,17 @@ public abstract class PagedTransformation

( } /** Places the given [button] in [pane]. */ - protected open fun applyButton(pane: Pane, button: PaginationButton) { + protected open fun applyButton( + pane: Pane, + button: PaginationButton, + ) { val (point, drawable, increments) = button - pane[point] = StaticElement(drawable) { (player, _, click) -> - increments[click]?.let { increment -> page += increment } - button.clickHandler(player) - } + pane[point] = + StaticElement(drawable) { (player, _, click) -> + increments[click]?.let { increment -> page += increment } + button.clickHandler(player) + } } override val triggers: Array = arrayOf(boundPage).plus(extraTriggers) diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/transform/builtin/PaginationTransformation.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/transform/builtin/PaginationTransformation.kt index 7b10bac..d5aae4a 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/transform/builtin/PaginationTransformation.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/transform/builtin/PaginationTransformation.kt @@ -16,7 +16,6 @@ public open class PaginationTransformation

( forward: PaginationButton, extraTriggers: Array = emptyArray(), ) : PagedTransformation

(back, forward, extraTriggers) { - private val values by Delegates.observable(default.toList()) { _, _, _ -> boundPage.max = maxPages() } @@ -25,7 +24,10 @@ public open class PaginationTransformation

( boundPage.max = maxPages() } - override suspend fun invoke(pane: P, view: InterfaceView) { + override suspend fun invoke( + pane: P, + view: InterfaceView, + ) { val positions = positionGenerator.generate() val slots = positions.size diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/utilities/BoundInteger.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/utilities/BoundInteger.kt index 8bc808e..011512e 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/utilities/BoundInteger.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/utilities/BoundInteger.kt @@ -10,11 +10,15 @@ public class BoundInteger( initial: Int, public var min: Int, public var max: Int, -) : ObservableProperty(initial), Trigger { - +) : ObservableProperty(initial), + Trigger { private val delegateTrigger = DelegateTrigger() - override fun beforeChange(property: KProperty<*>, oldValue: Int, newValue: Int): Boolean { + override fun beforeChange( + property: KProperty<*>, + oldValue: Int, + newValue: Int, + ): Boolean { val acceptableRange = min..max if (newValue in acceptableRange) { @@ -29,7 +33,11 @@ public class BoundInteger( return false } - override fun afterChange(property: KProperty<*>, oldValue: Int, newValue: Int) { + override fun afterChange( + property: KProperty<*>, + oldValue: Int, + newValue: Int, + ) { if (oldValue != newValue) { trigger() } @@ -39,7 +47,10 @@ public class BoundInteger( delegateTrigger.trigger() } - override fun addListener(reference: T, listener: T.() -> Unit) { + override fun addListener( + reference: T, + listener: T.() -> Unit, + ) { delegateTrigger.addListener(reference, listener) } diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/utilities/BukkitInventoryUtilities.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/utilities/BukkitInventoryUtilities.kt index db8042f..f8b574d 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/utilities/BukkitInventoryUtilities.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/utilities/BukkitInventoryUtilities.kt @@ -8,15 +8,20 @@ import org.bukkit.inventory.Inventory import org.bukkit.inventory.InventoryHolder /** Converts a ([row], [column]) grid point to an index used by Bukkit. */ -public fun gridPointToBukkitIndex(row: Int, column: Int): Int { - return row * 9 + column -} +public fun gridPointToBukkitIndex( + row: Int, + column: Int, +): Int = row * 9 + column /** Converts a [GridPoint] to an index used by Bukkit. */ public fun gridPointToBukkitIndex(gridPoint: GridPoint): Int = gridPointToBukkitIndex(gridPoint.x, gridPoint.y) /** Runs [function] for all values in a grid of [rows] height and [columns] width. */ -public fun forEachInGrid(rows: Int, columns: Int, function: (row: Int, column: Int) -> Unit) { +public fun forEachInGrid( + rows: Int, + columns: Int, + function: (row: Int, column: Int) -> Unit, +) { for (row in 0 until rows) { for (column in 0 until columns) { function(row, column) diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/utilities/CollapsablePaneMap.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/utilities/CollapsablePaneMap.kt index f041606..0fb158c 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/utilities/CollapsablePaneMap.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/utilities/CollapsablePaneMap.kt @@ -14,23 +14,29 @@ internal class CollapsablePaneMap private constructor( // super methods when overriding methods in the delegate. private val internal: MutableMap, ) : MutableMap by internal { - internal companion object { /** Creates a new collapsable map with [basePane]. */ - internal fun create(basePane: Pane) = CollapsablePaneMap( - basePane, - sortedMapOf(Comparator.reverseOrder()), - ) + internal fun create(basePane: Pane) = + CollapsablePaneMap( + basePane, + sortedMapOf(Comparator.reverseOrder()), + ) } private var cachedPane: CompletedPane? = null - override fun put(key: Int, value: CompletedPane): CompletedPane? { + override fun put( + key: Int, + value: CompletedPane, + ): CompletedPane? { cachedPane = null return internal.put(key, value) } - internal fun collapse(rows: Int, fill: Boolean): CompletedPane { + internal fun collapse( + rows: Int, + fill: Boolean, + ): CompletedPane { cachedPane?.let { pane -> return pane } diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/utilities/IncrementingInteger.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/utilities/IncrementingInteger.kt index 8ac27b0..7398e3c 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/utilities/IncrementingInteger.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/utilities/IncrementingInteger.kt @@ -4,9 +4,11 @@ import kotlin.reflect.KProperty /** An integer that is incremented after each read. */ internal class IncrementingInteger { - private var value: Int = 0 get() = field++ - operator fun getValue(thisRef: Any?, property: KProperty<*>): Int = value + operator fun getValue( + thisRef: Any?, + property: KProperty<*>, + ): Int = value } diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/utilities/TitleState.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/utilities/TitleState.kt index 23d8a8f..cc320fb 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/utilities/TitleState.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/utilities/TitleState.kt @@ -3,8 +3,9 @@ package com.noxcrew.interfaces.utilities import net.kyori.adventure.text.Component /** Manages the state of a title. */ -internal class TitleState(initialState: Component?) { - +internal class TitleState( + initialState: Component?, +) { /** The current value of the title. */ internal var current = initialState set(value) { diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/view/AbstractInterfaceView.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/view/AbstractInterfaceView.kt index 035053c..9fa60a3 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/view/AbstractInterfaceView.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/view/AbstractInterfaceView.kt @@ -44,7 +44,6 @@ public abstract class AbstractInterfaceView Unit, onComplete: suspend (Component) -> Boolean) { + override fun runChatQuery( + timeout: Duration, + onCancel: suspend () -> Unit, + onComplete: suspend (Component) -> Boolean, + ) { InterfacesListeners.INSTANCE.startChatQuery(this, timeout, onCancel, onComplete) } } diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/view/ChestInterfaceView.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/view/ChestInterfaceView.kt index 00d10fe..6989afd 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/view/ChestInterfaceView.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/view/ChestInterfaceView.kt @@ -15,23 +15,23 @@ public class ChestInterfaceView internal constructor( backing: ChestInterface, parent: InterfaceView?, ) : AbstractInterfaceView( - player, - backing, - parent, -), + player, + backing, + parent, + ), InventoryHolder { - private val titleState = TitleState(backing.initialTitle) override fun title(value: Component) { titleState.current = value } - override fun createInventory(): ChestInterfacesInventory = ChestInterfacesInventory( - this, - titleState.current, - backing.rows, - ) + override fun createInventory(): ChestInterfacesInventory = + ChestInterfacesInventory( + this, + titleState.current, + backing.rows, + ) override fun openInventory() { player.openInventory(this.inventory) diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/view/CombinedInterfaceView.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/view/CombinedInterfaceView.kt index db20828..bff4ec7 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/view/CombinedInterfaceView.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/view/CombinedInterfaceView.kt @@ -15,24 +15,24 @@ public class CombinedInterfaceView internal constructor( backing: CombinedInterface, parent: InterfaceView?, ) : AbstractInterfaceView( - player, - backing, - parent, -), + player, + backing, + parent, + ), InventoryHolder { - private val titleState = TitleState(backing.initialTitle) override fun title(value: Component) { titleState.current = value } - override fun createInventory(): CombinedInterfacesInventory = CombinedInterfacesInventory( - this, - player, - titleState.current, - backing.rows, - ) + override fun createInventory(): CombinedInterfacesInventory = + CombinedInterfacesInventory( + this, + player, + titleState.current, + backing.rows, + ) override fun openInventory() { player.openInventory(this.inventory) diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/view/InterfaceView.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/view/InterfaceView.kt index eb3ee1b..eee670a 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/view/InterfaceView.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/view/InterfaceView.kt @@ -13,7 +13,6 @@ import kotlin.time.Duration.Companion.seconds * but this view was created for a specific viewer. */ public interface InterfaceView { - /** The player this view is for. */ public val player: Player diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/view/PlayerInterfaceView.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/view/PlayerInterfaceView.kt index 283b599..b0b35a2 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/view/PlayerInterfaceView.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/view/PlayerInterfaceView.kt @@ -16,16 +16,19 @@ public class PlayerInterfaceView internal constructor( player: Player, backing: PlayerInterface, ) : AbstractInterfaceView( - player, - backing, - null, -) { - + player, + backing, + null, + ) { override fun title(value: Component) { error("PlayerInventoryView's cannot have a title") } - override fun runChatQuery(timeout: Duration, onCancel: suspend () -> Unit, onComplete: suspend (Component) -> Boolean) { + override fun runChatQuery( + timeout: Duration, + onCancel: suspend () -> Unit, + onComplete: suspend (Component) -> Boolean, + ) { error("PlayerInventoryView does not support chat queries") } @@ -60,7 +63,11 @@ public class PlayerInterfaceView internal constructor( onOpen() } - override fun close(coroutineScope: CoroutineScope, reason: InventoryCloseEvent.Reason, changingView: Boolean) { + override fun close( + coroutineScope: CoroutineScope, + reason: InventoryCloseEvent.Reason, + changingView: Boolean, + ) { markClosed(coroutineScope, reason, changingView) // Ensure we update the interface state in the main thread! @@ -70,6 +77,5 @@ public class PlayerInterfaceView internal constructor( } } - override fun isOpen(): Boolean = - InterfacesListeners.INSTANCE.getOpenPlayerInterface(player.uniqueId) == this + override fun isOpen(): Boolean = InterfacesListeners.INSTANCE.getOpenPlayerInterface(player.uniqueId) == this }