Skip to content

Commit

Permalink
fix timeout on gamemode change of tab list entry
Browse files Browse the repository at this point in the history
  • Loading branch information
robinbraemer committed Jun 17, 2021
1 parent 736e116 commit 86fbff2
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
3 changes: 3 additions & 0 deletions pkg/edition/java/proto/packet/tab_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ func (p *PlayerListItem) Encode(c *proto.PacketContext, wr io.Writer) (err error
return err
}
for _, item := range p.Items {
if item.ID == uuid.Nil {
return errors.New("UUID-less entry serialization attempt - 1.7 component")
}
err = util.WriteUUID(wr, item.ID)
if err != nil {
return err
Expand Down
1 change: 0 additions & 1 deletion pkg/edition/java/proto/state/states.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ func init() {
m(0x32, version.Minecraft_1_16_2),
m(0x36, version.Minecraft_1_17),
)
// TODO add TitleSubtitle packet
Play.ClientBound.Register(&title.Legacy{},
m(0x45, version.Minecraft_1_8),
m(0x45, version.Minecraft_1_9),
Expand Down
6 changes: 2 additions & 4 deletions pkg/edition/java/proto/util/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,11 @@ func WriteStrings(wr io.Writer, a []string) error {
// (or two unsigned 64-bit integers: the most
// significant 64 bits and then the least significant 64 bits)
func WriteUUID(wr io.Writer, uuid uuid.UUID) error {
l1 := binary.BigEndian.Uint64(uuid[:8])
l2 := binary.BigEndian.Uint64(uuid[8:])
err := WriteInt64(wr, int64(l1))
err := WriteUint64(wr, binary.BigEndian.Uint64(uuid[:8]))
if err != nil {
return err
}
return WriteInt64(wr, int64(l2))
return WriteUint64(wr, binary.BigEndian.Uint64(uuid[8:]))
}

func WriteProperties(wr io.Writer, properties []profile.Property) error {
Expand Down
4 changes: 3 additions & 1 deletion pkg/edition/java/proxy/session_backend_play.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ func (b *backendPlaySessionHandler) handlePluginMessage(packet *plugin.Message,

func (b *backendPlaySessionHandler) handlePlayerListItem(p *packet.PlayerListItem, pc *proto.PacketContext) {
// Track changes to tab list of player
b.serverConn.player.tabList.processBackendPacket(p)
if err := b.serverConn.player.tabList.processBackendPacket(p); err != nil {
b.serverConn.log.Error(err, "Error while processing backend PlayerListItem packet, ignored")
}
b.forwardToPlayer(pc, nil)
}

Expand Down
26 changes: 20 additions & 6 deletions pkg/edition/java/proxy/tab_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,26 @@ func (t *tabList) hasEntry(id uuid.UUID) bool {
}

// Processes a tab list entry packet sent from the backend to the client.
func (t *tabList) processBackendPacket(p *packet.PlayerListItem) {
func (t *tabList) processBackendPacket(p *packet.PlayerListItem) error {
// Packet is already forwarded on, so no need to do that here
t.mu.Lock()
defer t.mu.Unlock()
for _, item := range p.Items {
if item.ID == uuid.Nil {
return errors.New("1.7 tab list entry given to modern tab list handler")
}

if p.Action != packet.AddPlayerListItemAction && !t.hasEntry(item.ID) {
// Sometimes UpdateGameMode is sent before AddPlayer so don't want to warn here
continue
}

switch p.Action {
case packet.AddPlayerListItemAction:
// ensure that name and properties are available
if item.Name == "" || item.Properties == nil {
return errors.New("got null game profile for AddPlayerListItemAction")
}
t.entries[item.ID] = &tabListEntry{
tabList: t,
profile: &profile.GameProfile{
Expand All @@ -152,7 +160,7 @@ func (t *tabList) processBackendPacket(p *packet.PlayerListItem) {
case packet.UpdateDisplayNamePlayerListItemAction:
e, ok := t.entries[item.ID]
if ok {
e.SetDisplayName(item.DisplayName)
e.setDisplayName(item.DisplayName)
}
case packet.UpdateLatencyPlayerListItemAction:
e, ok := t.entries[item.ID]
Expand All @@ -162,12 +170,13 @@ func (t *tabList) processBackendPacket(p *packet.PlayerListItem) {
case packet.UpdateGameModePlayerListItemAction:
e, ok := t.entries[item.ID]
if ok {
e.setGameMode(item.GameMode)
return e.setGameMode(item.GameMode)
}
default:
// Nothing we can do here
}
}
return nil
}

func (t *tabList) updateEntry(action packet.PlayerListItemAction, entry *tabListEntry) error {
Expand Down Expand Up @@ -214,7 +223,12 @@ func (t *tabListEntry) DisplayName() component.Component {
return t.displayName
}

func (t *tabListEntry) SetDisplayName(name component.Component) {
func (t *tabListEntry) SetDisplayName(name component.Component) error {
t.setDisplayName(name)
return t.tabList.updateEntry(packet.UpdateDisplayNamePlayerListItemAction, t)
}

func (t *tabListEntry) setDisplayName(name component.Component) {
t.mu.Lock()
t.displayName = name
t.mu.Unlock()
Expand All @@ -238,11 +252,11 @@ func (t *tabListEntry) setLatency(latency time.Duration) {
t.mu.Unlock()
}

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

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

0 comments on commit 86fbff2

Please sign in to comment.