Skip to content

Commit c6579e9

Browse files
authored
scoreboard/scoreboard.go: Add ability to set scoreboard sort order to descending (#1112)
1 parent c7baaed commit c6579e9

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

server/player/scoreboard/scoreboard.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ import (
1111
// Scoreboard implements the io.Writer and io.StringWriter interfaces. fmt.Fprintf and fmt.Fprint may be used
1212
// to write formatted text to the scoreboard.
1313
type Scoreboard struct {
14-
name string
15-
lines []string
16-
padding bool
14+
name string
15+
lines []string
16+
padding bool
17+
descending bool
1718
}
1819

1920
// New returns a new scoreboard with the display name passed. Once returned, lines may be added to the
@@ -86,5 +87,19 @@ func (board *Scoreboard) Lines() []string {
8687
lines[i] = " " + line + strings.Repeat(" ", len(board.name)-len(line)-2)
8788
}
8889
}
90+
if board.descending {
91+
slices.Reverse(lines)
92+
}
8993
return lines
9094
}
95+
96+
// Descending returns whether the scoreboard is sorted in descending order.
97+
func (board *Scoreboard) Descending() bool {
98+
return board.descending
99+
}
100+
101+
// SetDescending sets the scoreboard sort order to descending.
102+
// When sending the scoreboard to the player, it will reverse the order of the lines to match when it's ascending.
103+
func (board *Scoreboard) SetDescending() {
104+
board.descending = true
105+
}

server/session/text.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,18 @@ func (s *Session) SendScoreboard(sb *scoreboard.Scoreboard) {
7777

7878
if currentName != sb.Name() {
7979
s.RemoveScoreboard()
80-
s.writePacket(&packet.SetDisplayObjective{
80+
pk := &packet.SetDisplayObjective{
8181
DisplaySlot: "sidebar",
8282
ObjectiveName: sb.Name(),
8383
DisplayName: sb.Name(),
8484
CriteriaName: "dummy",
85-
})
85+
}
86+
if sb.Descending() {
87+
pk.SortOrder = packet.ScoreboardSortOrderDescending
88+
} else {
89+
pk.SortOrder = packet.ScoreboardSortOrderAscending
90+
}
91+
s.writePacket(pk)
8692
name, lines := sb.Name(), append([]string(nil), sb.Lines()...)
8793
s.currentScoreboard.Store(&name)
8894
s.currentLines.Store(&lines)

0 commit comments

Comments
 (0)