Skip to content

Commit

Permalink
Update and apply ktlint
Browse files Browse the repository at this point in the history
  • Loading branch information
Aeltumn committed Jan 6, 2025
1 parent 88b558f commit ec78ed9
Show file tree
Hide file tree
Showing 59 changed files with 604 additions and 448 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ subprojects {

configure<SpotlessExtension> {
kotlin {
ktlint("0.48.2")
ktlint("1.5.0")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,39 @@ 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))
}

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<Material> {
delay(3.seconds)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
}
}
Loading

0 comments on commit ec78ed9

Please sign in to comment.