-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Syntax/structure not final.
- Loading branch information
Showing
6 changed files
with
122 additions
and
7 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
api/src/main/kotlin/com/mattmx/ktgui/scoreboards/ComponentSupplier.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
api/src/main/kotlin/com/mattmx/ktgui/scoreboards/DynamicScoreboardBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
api/src/main/kotlin/com/mattmx/ktgui/scoreboards/ScoreboardComponentSupplier.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 41 additions & 5 deletions
46
plugin/src/main/kotlin/com/mattmx/ktgui/examples/SignalScoreboardExample.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(!"D7068&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) | ||
|
||
!" D7068&m${" ".repeat(l)}+C59E&m D7068&m${" ".repeat(r)}" | ||
} updateEvery 5L | ||
|
||
+line | ||
+!" DFAFFLobby" | ||
+Component.empty() | ||
+!"&f${DateFormat.getTimeInstance().format(Date())}" | ||
|
||
+{ !" DFAFF$ &f${(0..1_000_000_000_000).random().pretty()}" } updateEvery 1.5.seconds | ||
+{ !" DFAFF\uD83D\uDD25 &f%server_online%".placeholders(null) } updateEvery 5.seconds | ||
+{ !" DFAFF⌛ &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) | ||
} | ||
} | ||
} |