github.com/df-mc/dragonfly@v0.9.13/server/session/text.go (about) 1 package session 2 3 import ( 4 "github.com/df-mc/dragonfly/server/player/scoreboard" 5 "github.com/sandertv/gophertunnel/minecraft/protocol" 6 "github.com/sandertv/gophertunnel/minecraft/protocol/packet" 7 "time" 8 ) 9 10 // SendMessage ... 11 func (s *Session) SendMessage(message string) { 12 s.writePacket(&packet.Text{ 13 TextType: packet.TextTypeRaw, 14 Message: message, 15 }) 16 } 17 18 // SendTip ... 19 func (s *Session) SendTip(message string) { 20 s.writePacket(&packet.Text{ 21 TextType: packet.TextTypeTip, 22 Message: message, 23 }) 24 } 25 26 // SendAnnouncement ... 27 func (s *Session) SendAnnouncement(message string) { 28 s.writePacket(&packet.Text{ 29 TextType: packet.TextTypeAnnouncement, 30 Message: message, 31 }) 32 } 33 34 // SendPopup ... 35 func (s *Session) SendPopup(message string) { 36 s.writePacket(&packet.Text{ 37 TextType: packet.TextTypePopup, 38 Message: message, 39 }) 40 } 41 42 // SendJukeboxPopup ... 43 func (s *Session) SendJukeboxPopup(message string) { 44 s.writePacket(&packet.Text{ 45 TextType: packet.TextTypeJukeboxPopup, 46 Message: message, 47 }) 48 } 49 50 // SendToast ... 51 func (s *Session) SendToast(title, message string) { 52 s.writePacket(&packet.ToastRequest{ 53 Title: title, 54 Message: message, 55 }) 56 } 57 58 // SendScoreboard ... 59 func (s *Session) SendScoreboard(sb *scoreboard.Scoreboard) { 60 if s == Nop { 61 return 62 } 63 currentName, currentLines := s.currentScoreboard.Load(), s.currentLines.Load() 64 65 if currentName != sb.Name() { 66 s.RemoveScoreboard() 67 s.writePacket(&packet.SetDisplayObjective{ 68 DisplaySlot: "sidebar", 69 ObjectiveName: sb.Name(), 70 DisplayName: sb.Name(), 71 CriteriaName: "dummy", 72 }) 73 s.currentScoreboard.Store(sb.Name()) 74 s.currentLines.Store(append([]string(nil), sb.Lines()...)) 75 } else { 76 // Remove all current lines from the scoreboard. We can't replace them without removing them. 77 pk := &packet.SetScore{ActionType: packet.ScoreboardActionRemove} 78 for i := range currentLines { 79 pk.Entries = append(pk.Entries, protocol.ScoreboardEntry{ 80 EntryID: int64(i), 81 ObjectiveName: currentName, 82 Score: int32(i), 83 }) 84 } 85 if len(pk.Entries) > 0 { 86 s.writePacket(pk) 87 } 88 } 89 pk := &packet.SetScore{ActionType: packet.ScoreboardActionModify} 90 for k, line := range sb.Lines() { 91 if len(line) == 0 { 92 line = "ยง" + colours[k] 93 } 94 pk.Entries = append(pk.Entries, protocol.ScoreboardEntry{ 95 EntryID: int64(k), 96 ObjectiveName: sb.Name(), 97 Score: int32(k), 98 IdentityType: protocol.ScoreboardIdentityFakePlayer, 99 DisplayName: line, 100 }) 101 } 102 if len(pk.Entries) > 0 { 103 s.writePacket(pk) 104 } 105 } 106 107 // colours holds a list of colour codes to be filled out for empty lines in a scoreboard. 108 var colours = [15]string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"} 109 110 // RemoveScoreboard ... 111 func (s *Session) RemoveScoreboard() { 112 s.writePacket(&packet.RemoveObjective{ObjectiveName: s.currentScoreboard.Load()}) 113 s.currentScoreboard.Store("") 114 s.currentLines.Store([]string{}) 115 } 116 117 // SendBossBar sends a boss bar to the player with the text passed and the health percentage of the bar. 118 // SendBossBar removes any boss bar that might be active before sending the new one. 119 func (s *Session) SendBossBar(text string, colour uint8, healthPercentage float64) { 120 s.RemoveBossBar() 121 s.writePacket(&packet.BossEvent{ 122 BossEntityUniqueID: selfEntityRuntimeID, 123 EventType: packet.BossEventShow, 124 BossBarTitle: text, 125 HealthPercentage: float32(healthPercentage), 126 Colour: uint32(colour), 127 }) 128 } 129 130 // RemoveBossBar removes any boss bar currently active on the player's screen. 131 func (s *Session) RemoveBossBar() { 132 s.writePacket(&packet.BossEvent{ 133 BossEntityUniqueID: selfEntityRuntimeID, 134 EventType: packet.BossEventHide, 135 }) 136 } 137 138 const tickLength = time.Second / 20 139 140 // SetTitleDurations ... 141 func (s *Session) SetTitleDurations(fadeInDuration, remainDuration, fadeOutDuration time.Duration) { 142 s.writePacket(&packet.SetTitle{ 143 ActionType: packet.TitleActionSetDurations, 144 FadeInDuration: int32(fadeInDuration / tickLength), 145 RemainDuration: int32(remainDuration / tickLength), 146 FadeOutDuration: int32(fadeOutDuration / tickLength), 147 }) 148 } 149 150 // SendTitle ... 151 func (s *Session) SendTitle(text string) { 152 s.writePacket(&packet.SetTitle{ActionType: packet.TitleActionSetTitle, Text: text}) 153 } 154 155 // SendSubtitle ... 156 func (s *Session) SendSubtitle(text string) { 157 s.writePacket(&packet.SetTitle{ActionType: packet.TitleActionSetSubtitle, Text: text}) 158 } 159 160 // SendActionBarMessage ... 161 func (s *Session) SendActionBarMessage(text string) { 162 s.writePacket(&packet.SetTitle{ActionType: packet.TitleActionSetActionBar, Text: text}) 163 }