Skip to content

Commit

Permalink
Apply spotless
Browse files Browse the repository at this point in the history
  • Loading branch information
Aeltumn committed Jan 6, 2025
1 parent 8240ca0 commit 8903c48
Show file tree
Hide file tree
Showing 48 changed files with 273 additions and 565 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,18 @@ 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 @@ -12,29 +12,28 @@ 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 @@ -20,32 +20,31 @@ 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))
}
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
}
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()
}
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 @@ -114,108 +114,102 @@ public class ExamplePlugin :
)
}

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")

pane[3, 3] =
StaticElement(drawable(item)) {
it.player.sendMessage("it's been $counter's ticks")
}
}

withTransform(counterProperty) { pane, _ ->
val item =
ItemStack(Material.BEE_NEST)
.name("it's been $counter's ticks")
.description("click to see the ticks now")
withTransform { pane, _ ->
val item =
ItemStack(Material.BEE_NEST)
.name("block the interface")
.description("block interaction and message in 5 seconds")

pane[3, 3] =
StaticElement(drawable(item)) {
it.player.sendMessage("it's been $counter's ticks")
}
}
pane[5, 3] =
StaticElement(drawable(item)) {
completingLater = true

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

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 @@ -10,21 +10,20 @@ 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))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,9 @@ 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]. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@ import org.bukkit.entity.Player
import org.bukkit.inventory.ItemStack

/** A drawn element that includes an [itemStack] to show and a [clickHandler] to run. */
public data class CompletedElement(
public val itemStack: ItemStack?,
public val clickHandler: ClickHandler,
)
public data class CompletedElement(public val itemStack: ItemStack?, public val clickHandler: ClickHandler,)

/** 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(),
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import com.noxcrew.interfaces.click.ClickHandler
import com.noxcrew.interfaces.drawable.Drawable

/** A static element that uses [drawable] and a set [clickHandler]. */
public class StaticElement(
private val drawable: Drawable,
private val clickHandler: ClickHandler = ClickHandler.EMPTY,
) : Element {
public class StaticElement(private val drawable: Drawable, private val clickHandler: ClickHandler = ClickHandler.EMPTY,) : Element {
override fun drawable(): Drawable = drawable

override fun clickHandler(): ClickHandler = clickHandler
Expand Down
Loading

0 comments on commit 8903c48

Please sign in to comment.