github.com/df-mc/dragonfly@v0.9.13/server/player/bossbar/bossbar.go (about) 1 package bossbar 2 3 import ( 4 "fmt" 5 "strings" 6 ) 7 8 // BossBar represents a boss bar that may be sent to a player. It is shown as a purple bar with text above 9 // it. The health shown by the bar may be changed. 10 type BossBar struct { 11 text string 12 health float64 13 c Colour 14 } 15 16 // New creates a new boss bar with the text passed. The text is formatted according to the rules of 17 // fmt.Sprintln. 18 // By default, the boss bar will have a full health bar. To change this, use BossBar.WithHealthPercentage(). 19 // The default colour of the BossBar is Purple. This can be changed using BossBar.WithColour. 20 func New(text ...any) BossBar { 21 return BossBar{text: format(text), health: 1, c: Purple()} 22 } 23 24 // Text returns the text of the boss bar: The text passed when creating the bar using New(). 25 func (bar BossBar) Text() string { 26 return bar.text 27 } 28 29 // WithHealthPercentage sets the health percentage of the boss bar. The value passed must be between 0 and 1. 30 // If a value out of that range is passed, WithHealthPercentage panics. 31 // The new BossBar with the changed health percentage is returned. 32 func (bar BossBar) WithHealthPercentage(v float64) BossBar { 33 if v < 0 || v > 1 { 34 panic("boss bar: value out of range: health percentage must be between 0.0 and 1.0") 35 } 36 bar.health = v 37 return bar 38 } 39 40 // WithColour returns a copy of the BossBar with the Colour passed. 41 func (bar BossBar) WithColour(c Colour) BossBar { 42 bar.c = c 43 return bar 44 } 45 46 // HealthPercentage returns the health percentage of the boss bar. The number returned is a value between 0 47 // and 1, with 0 being an empty boss bar and 1 being a full one. 48 func (bar BossBar) HealthPercentage() float64 { 49 return bar.health 50 } 51 52 // Colour returns the colour of the BossBar. 53 func (bar BossBar) Colour() Colour { 54 return bar.c 55 } 56 57 // format is a utility function to format a list of values to have spaces between them, but no newline at the 58 // end, which is typically used for sending messages, popups and tips. 59 func format(a []any) string { 60 return strings.TrimSuffix(fmt.Sprintln(a...), "\n") 61 }