Skip to content

Commit

Permalink
Dynamic scoreboard, part of #34
Browse files Browse the repository at this point in the history
Syntax/structure not final.
  • Loading branch information
Matt-MX committed Jul 4, 2024
1 parent a141120 commit 947289b
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.mattmx.ktgui.scoreboards

import net.kyori.adventure.text.Component

open class ComponentSupplier(
val supplier: () -> Component
) {

operator fun invoke() = supplier()

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.mattmx.ktgui.scoreboards

import com.mattmx.ktgui.scheduling.TaskTracker
import net.kyori.adventure.text.Component

open class DynamicScoreboardBuilder(
title: Component = Component.empty()
) : ScoreboardBuilder(title) {
private val tasks = TaskTracker()

open infix fun title(supplier: () -> Component) =
title(ScoreboardComponentSupplier(supplier))

open infix fun title(supplier: ScoreboardComponentSupplier) = supplier.apply {
val initialComponent = supplier()
isTitleComponent = true
title = initialComponent
}

operator fun ScoreboardComponentSupplier.unaryPlus() = apply { add(this) }

operator fun (() -> Component).unaryPlus() =
ScoreboardComponentSupplier(this).apply { add(this) }

infix fun add(supplier: ScoreboardComponentSupplier) {
val initialComponent = supplier()
val index = add(initialComponent)
supplier.indexes.add(index)
}

infix fun scoreboardLine(block: () -> Component) =
ScoreboardComponentSupplier(block)

infix fun ScoreboardComponentSupplier.updateEvery(ticks: Long) = apply {
tasks.runAsyncRepeat(ticks) {
val component = invoke()
if (isTitleComponent) {
title = component
}
indexes.forEach { set(it, component) }
}
}

override fun clear() {
super.clear()
tasks.cancelAll()
}
}

fun dynamicScoreboard(title: Component, block: DynamicScoreboardBuilder.() -> Unit) =
DynamicScoreboardBuilder(title).apply(block)

fun dynamicScoreboard(block: DynamicScoreboardBuilder.() -> Unit) =
DynamicScoreboardBuilder().apply(block)
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ open class ScoreboardBuilder(
) {
var title: Component = title
set(value) {
objective.setAutoUpdateDisplay(true)
this.objective.displayName(title)
field = value
}
Expand Down Expand Up @@ -102,7 +103,7 @@ open class ScoreboardBuilder(
/**
* Clears all scoreboard content
*/
fun clear() {
open fun clear() {
scoreboardLines.clear()
scoreboard.resetScores(name)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.mattmx.ktgui.scoreboards

import net.kyori.adventure.text.Component
import java.util.*

class ScoreboardComponentSupplier(
supplier: () -> Component
) : ComponentSupplier(supplier) {
val indexes = arrayListOf<Int>()
var isTitleComponent: Boolean = false
}
4 changes: 3 additions & 1 deletion plugin/src/main/kotlin/com/mattmx/ktgui/KotlinGui.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class KotlinGui : JavaPlugin() {

val animatedScoreboard = AnimatedScoreboardExample()
val scoreboardExample = ScoreboardExample()
val signalScoreboardExample = SignalScoreboardExample()
val examples = hashMapOf(
"animated-scoreboard" to { animatedScoreboard },
"scoreboard" to { scoreboardExample },
Expand All @@ -62,7 +63,8 @@ class KotlinGui : JavaPlugin() {
"java-simple" to { JavaGuiExample() },
"java-new" to { JavaUpdateExample() },
"refresh" to { RefreshBlockExample() },
"config-gui" to { GuiConfigExample() }
"config-gui" to { GuiConfigExample() },
"refresh-scoreboard" to { signalScoreboardExample }
)
GuiHookExample.registerListener(this)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,53 @@
package com.mattmx.ktgui.examples

import com.mattmx.ktgui.scoreboards.scoreboard
import com.mattmx.ktgui.scoreboards.dynamicScoreboard
import com.mattmx.ktgui.utils.not
import com.mattmx.ktgui.utils.placeholders
import com.mattmx.ktgui.utils.pretty
import com.mattmx.ktgui.utils.seconds
import net.kyori.adventure.text.Component
import org.bukkit.entity.Player
import java.text.DateFormat
import java.util.*
import kotlin.math.max
import kotlin.math.min

class SignalScoreboardExample {
class SignalScoreboardExample : Example {

val board = scoreboard(!"&fSignals x Scoreboard") {
+!"&fFirst line"
val board = dynamicScoreboard(!"&#3D7068&lYour Server") {

val le = 40
var i = 0
val line = scoreboardLine {
i = if (i > le) 0 else i + 1

val l = max(0, i - 1)
val r = min(le, le - i + 1)

!" &#3D7068&m${" ".repeat(l)}&#43C59E&m &#3D7068&m${" ".repeat(r)}"
} updateEvery 5L

+line
+!" &#3DFAFFLobby"
+Component.empty()
+!"&f${DateFormat.getTimeInstance().format(Date())}"

+{ !" &#3DFAFF$ &f${(0..1_000_000_000_000).random().pretty()}" } updateEvery 1.5.seconds
+{ !" &#3DFAFF\uD83D\uDD25 &f%server_online%".placeholders(null) } updateEvery 5.seconds
+{ !" &#3DFAFF⌛ &f${DateFormat.getTimeInstance().format(Date())}" } updateEvery 1.seconds

+Component.empty()
+line
}

override fun run(player: Player) {
val shown = board.isShownFor(player)

if (shown) {
board.removeFor(player)
player.sendMessage(!"&fHiding scoreboard")
} else {
player.sendMessage(!"&fShowing scoreboard")
board.showFor(player)
}
}
}

0 comments on commit 947289b

Please sign in to comment.