Skip to content

Commit

Permalink
add SetDisplayName, SetGameMode & SetLatency to TabListEntry
Browse files Browse the repository at this point in the history
  • Loading branch information
robinbraemer committed Jun 17, 2021
1 parent a8e7363 commit af1d6bb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
20 changes: 15 additions & 5 deletions pkg/edition/java/proxy/player/tab_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,21 @@ type TabListEntry interface {
// containing uuid, as well as deciding what is shown as the player head in the tab list.
Profile() profile.GameProfile
// DisplayName returns the optional text displayed for this entry in the TabList,
// otherwise profile.GameProfile.Names is shown and returns nil.
// otherwise if returns nil Profile().Name is shown (but not returned here).
DisplayName() component.Component
// SetDisplayName the text to be displayed for the entry.
// If nil Profile().Name will be shown.
SetDisplayName(component.Component) error
// GameMode returns the game mode the entry has been set to.
// 0 - Survival
// 1 - Creative
// 2 - Adventure
// 3 - Spectator
GameMode() int
// Latency returns the latency for the entry.
// SetGameMode sets the gamemode for the entry.
// See GameMode() for more details.
SetGameMode(int) error
// Latency returns the latency/ping for the entry.
//
// The icon shown in the tab list is calculated
// by the millisecond latency as follows:
Expand All @@ -40,7 +50,7 @@ type TabListEntry interface {
// 600-1000 will display 2 bars
// A latency move than 1 second will display 1 bar
Latency() time.Duration
// TODO SetDisplayName
// TODO SetLatency(time.Duration)
// TODO more...
// SetLatency sets the latency/ping for the entry.
// See Latency() for how it is displayed.
SetLatency(time.Duration) error
}
15 changes: 11 additions & 4 deletions pkg/edition/java/proxy/tab_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (t *tabList) processBackendPacket(p *packet.PlayerListItem) error {
case packet.UpdateGameModePlayerListItemAction:
e, ok := t.entries[item.ID]
if ok {
return e.setGameMode(item.GameMode)
e.setGameMode(item.GameMode)
}
default:
// Nothing we can do here
Expand Down Expand Up @@ -214,7 +214,7 @@ type tabListEntry struct {

mu sync.RWMutex // protects following fields
profile *profile.GameProfile
displayName component.Component
displayName component.Component // nil-able
latency time.Duration
gameMode int
}
Expand Down Expand Up @@ -260,17 +260,24 @@ func (t *tabListEntry) Latency() time.Duration {
return t.latency
}

func (t *tabListEntry) SetLatency(latency time.Duration) error {
t.setLatency(latency)
return t.tabList.updateEntry(packet.UpdateLatencyPlayerListItemAction, t)
}
func (t *tabListEntry) setLatency(latency time.Duration) {
t.mu.Lock()
t.latency = latency
t.mu.Unlock()
}

func (t *tabListEntry) setGameMode(gameMode int) error {
func (t *tabListEntry) SetGameMode(gameMode int) error {
t.setGameMode(gameMode)
return t.tabList.updateEntry(packet.UpdateGameModePlayerListItemAction, t)
}
func (t *tabListEntry) setGameMode(gameMode int) {
t.mu.Lock()
t.gameMode = gameMode
t.mu.Unlock()
return t.tabList.updateEntry(packet.UpdateGameModePlayerListItemAction, t)
}

func newPlayerListItemEntry(entry player.TabListEntry) *packet.PlayerListItemEntry {
Expand Down

0 comments on commit af1d6bb

Please sign in to comment.