-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/scoreboard enhancements #430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
32 changes: 32 additions & 0 deletions
32
...r-server/src/main/kotlin/dev/slne/surf/api/paper/server/impl/scoreboard/ScoreboardLine.kt
This file contains hidden or 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,32 @@ | ||
| package dev.slne.surf.api.paper.server.impl.scoreboard | ||
|
|
||
| import net.kyori.adventure.text.Component | ||
| import net.megavex.scoreboardlibrary.api.sidebar.component.SidebarComponent | ||
| import net.megavex.scoreboardlibrary.api.sidebar.component.animation.SidebarAnimation | ||
| import org.bukkit.entity.Player | ||
| import java.util.function.Function | ||
|
|
||
| sealed interface ScoreboardLine { | ||
| class Shared(val component: SidebarComponent) : ScoreboardLine | ||
| class Viewer(val factory: Function<Player, SidebarComponent>) : ScoreboardLine | ||
| } | ||
|
|
||
| /** | ||
| * Holds the value of a mutable line source as of the last [refresh]. | ||
| */ | ||
| class LineSnapshot<T : Any>(private val source: () -> T) { | ||
| lateinit var value: T | ||
| private set | ||
|
|
||
| fun refresh() { | ||
| value = source() | ||
| } | ||
| } | ||
|
|
||
| class ScoreboardDefinition( | ||
| val title: Component, | ||
| val maxLines: Int, | ||
| val lines: List<ScoreboardLine>, | ||
| val snapshots: List<LineSnapshot<*>>, | ||
| val animations: List<SidebarAnimation<Component>> | ||
| ) |
This file contains hidden or 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
47 changes: 25 additions & 22 deletions
47
.../kotlin/dev/slne/surf/api/paper/server/impl/scoreboard/SurfAutoUpdatableScoreboardImpl.kt
This file contains hidden or 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,39 +1,42 @@ | ||
| package dev.slne.surf.api.paper.server.impl.scoreboard | ||
|
|
||
| import com.github.shynixn.mccoroutine.folia.launch | ||
| import com.github.shynixn.mccoroutine.folia.ticks | ||
| import com.github.shynixn.mccoroutine.folia.scope | ||
| import dev.slne.surf.api.core.util.runAtFixedRate | ||
| import dev.slne.surf.api.paper.scoreboard.SurfAutoUpdatableScoreboard | ||
| import dev.slne.surf.api.paper.server.plugin | ||
| import io.papermc.paper.util.Tick | ||
| import kotlinx.coroutines.Job | ||
| import kotlinx.coroutines.delay | ||
| import net.kyori.adventure.text.Component | ||
| import net.megavex.scoreboardlibrary.api.sidebar.component.SidebarComponent | ||
| import net.megavex.scoreboardlibrary.api.sidebar.component.animation.SidebarAnimation | ||
| import kotlin.concurrent.withLock | ||
| import kotlin.time.toKotlinDuration | ||
|
|
||
| open class SurfAutoUpdatableScoreboardImpl( | ||
| title: Component, | ||
| maxLines: Int, | ||
| sidebarComponent: SidebarComponent, | ||
| animations: List<SidebarAnimation<Component>> | ||
| ) : SurfScoreboardImpl(title, maxLines, sidebarComponent, animations), SurfAutoUpdatableScoreboard { | ||
| definition: ScoreboardDefinition | ||
| ) : SurfScoreboardImpl(definition), SurfAutoUpdatableScoreboard { | ||
| private var updater: Job? = null | ||
|
|
||
| override fun enable() { | ||
| super.enable() | ||
|
|
||
| this.updater = launchUpdater() | ||
| lock.withLock { | ||
| super.enable() | ||
| updater = launchUpdater() | ||
| } | ||
| } | ||
|
|
||
| override fun disable() { | ||
| super.disable() | ||
| lock.withLock { | ||
| super.disable() | ||
| updater?.cancel() | ||
| updater = null | ||
| } | ||
| } | ||
|
|
||
| updater!!.cancel() | ||
| private fun launchUpdater(): Job = plugin.scope.runAtFixedRate( | ||
| fiveTicks, | ||
| taskName = "SurfAutoUpdatableScoreboardUpdater" | ||
| ) { | ||
| updateIfEnabled() | ||
| } | ||
|
|
||
| private fun launchUpdater() = plugin.launch { | ||
| while (true) { | ||
| update() | ||
| delay(5.ticks) | ||
| } | ||
| companion object { | ||
| private val fiveTicks = Tick.of(5).toKotlinDuration() | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When one builder containing an updatable line is used to build multiple scoreboards, this closure and
snapshots.toList()retain the same mutableLineSnapshotinstance in every result. The scoreboards have separate locks, so concurrent updates can overwrite one another's snapshot between refresh and draw, causing one scoreboard to display another scoreboard's supplier result. Create the snapshots and the components that capture them perdefinition()/build instead of sharing them through the builder.Useful? React with 👍 / 👎.