forked from df-mc/dragonfly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scoreboard.go
73 lines (63 loc) · 2.69 KB
/
scoreboard.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package scoreboard
import (
"fmt"
"strings"
)
// Scoreboard represents a scoreboard that may be sent to a player. The scoreboard is shown on the right side
// of the player's screen.
// Scoreboard implements the io.Writer and io.StringWriter interfaces. fmt.Fprintf and fmt.Fprint may be used
// to write formatted text to the scoreboard.
type Scoreboard struct {
name string
lines []string
}
// New returns a new scoreboard with the display name passed. Once returned, lines may be added to the
// scoreboard to add text to it. The name is formatted according to the rules of fmt.Sprintln.
// Changing the scoreboard after sending it to a player will not update the scoreboard of the player
// automatically: Player.SendScoreboard() must be called again to update it.
func New(name ...interface{}) *Scoreboard {
return &Scoreboard{name: strings.TrimSuffix(fmt.Sprintln(name...), "\n")}
}
// Name returns the display name of the scoreboard, as passed during the construction of the scoreboard.
func (board *Scoreboard) Name() string {
return board.name
}
// Write writes a slice of data as text to the scoreboard. Newlines may be written to create a new line on
// the scoreboard.
func (board *Scoreboard) Write(p []byte) (n int, err error) {
return board.WriteString(string(p))
}
// WriteString writes a string of text to the scoreboard. Newlines may be written to create a new line on
// the scoreboard.
func (board *Scoreboard) WriteString(s string) (n int, err error) {
lines := strings.Split(s, "\n")
board.lines = append(board.lines, lines...)
// Scoreboards can have up to 15 lines. (16 including the title.)
if len(board.lines) >= 15 {
return len(lines), fmt.Errorf("write scoreboard: maximum of 15 lines of text exceeded")
}
return len(lines), nil
}
// Set changes a specific line in the scoreboard and adds empty lines until this index is reached. Set panics if the
// index passed is negative or 15+.
func (board *Scoreboard) Set(index int, s string) {
if index < 0 || index >= 15 {
panic(fmt.Sprintf("index out of range %v", index))
}
if diff := index - (len(board.lines) - 1); diff > 0 {
board.lines = append(board.lines, make([]string, diff)...)
}
// Remove new lines from the string
board.lines[index] = strings.TrimSuffix(strings.TrimSuffix(s, "\n"), "\n")
}
// Remove removes a specific line from the scoreboard. Remove panics if the index passed is negative or 15+.
func (board *Scoreboard) Remove(index int) {
if index < 0 || index >= 15 {
panic(fmt.Sprintf("index out of range %v", index))
}
board.lines = append(board.lines[:index], board.lines[index+1:]...)
}
// Lines returns the data of the Scoreboard as a slice of strings.
func (board *Scoreboard) Lines() []string {
return board.lines
}