Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions server/player/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package player
import (
"fmt"
"github.com/df-mc/dragonfly/server/player/debug"
"image/color"
"math"
"math/rand/v2"
"net"
Expand Down Expand Up @@ -403,6 +404,16 @@ func (p *Player) CloseForm() {
p.session().CloseForm()
}

// ShowLocatorBar enables the vanilla locator bar for the player.
func (p *Player) ShowLocatorBar() {
p.session().EnableLocatorBar(true)
}

// HideLocatorBar disables the vanilla locator bar for the player.
func (p *Player) HideLocatorBar() {
p.session().EnableLocatorBar(false)
}

// ShowCoordinates enables the vanilla coordinates for the player.
func (p *Player) ShowCoordinates() {
p.session().EnableCoordinates(true)
Expand Down Expand Up @@ -2188,6 +2199,16 @@ func (p *Player) Collect(s item.Stack) (int, bool) {
return added, true
}

// Colour returns the player's colour in the locator bar.
func (p *Player) Colour() color.RGBA {
return p.session().Colour()
}

// SetColour changes the player's colour in the locator bar.
func (p *Player) SetColour(colour color.RGBA) {
p.session().SetColour(colour)
}

// Experience returns the amount of experience the player has.
func (p *Player) Experience() int {
return p.experience.Experience()
Expand Down
34 changes: 34 additions & 0 deletions server/session/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package session
import (
"encoding/json"
"fmt"
"github.com/cespare/xxhash/v2"
"github.com/df-mc/dragonfly/server/player/debug"
"github.com/go-gl/mathgl/mgl32"
"golang.org/x/exp/rand"
"image/color"
"maps"
"math"
Expand Down Expand Up @@ -578,6 +580,12 @@ func (s *Session) sendGameRules(gameRules []protocol.GameRule) {
s.writePacket(&packet.GameRulesChanged{GameRules: gameRules})
}

// EnableLocatorBar will either enable or disable locator bar for the player depending on the value given.
func (s *Session) EnableLocatorBar(enable bool) {
//noinspection SpellCheckingInspection
s.sendGameRules([]protocol.GameRule{{Name: "locatorbar", Value: enable}})
}

// EnableCoordinates will either enable or disable coordinates for the player depending on the value given.
func (s *Session) EnableCoordinates(enable bool) {
//noinspection SpellCheckingInspection
Expand All @@ -590,6 +598,21 @@ func (s *Session) EnableInstantRespawn(enable bool) {
s.sendGameRules([]protocol.GameRule{{Name: "doimmediaterespawn", Value: enable}})
}

// Colour returns the colour of player. Colour is used to indicate a player in the locator bar.
func (s *Session) Colour() color.RGBA {
colour := s.colour.Load()
if colour == nil {
return color.RGBA{A: 0xFF}
}
return *colour
}

// SetColour sets the player colour. Colour is used to indicate a player in the locator bar.
func (s *Session) SetColour(colour color.RGBA) {
s.colour.Store(&colour)
sessions.resendList(s)
}

// HandleInventories starts handling the inventories of the Controllable entity of the session. It sends packets when
// slots in the inventory are changed.
func (s *Session) HandleInventories(tx *world.Tx, c Controllable, inv, offHand, enderChest, ui *inventory.Inventory, armour *inventory.Armour, heldSlot *uint32) {
Expand Down Expand Up @@ -1053,6 +1076,17 @@ func protocolToSkin(sk protocol.Skin) (s skin.Skin, err error) {
return
}

// randomColour returns random colour based on displayName's hash.
func randomColour(displayName string) color.RGBA {
r := rand.New(rand.NewSource(xxhash.Sum64String(displayName)))
return color.RGBA{
R: byte(r.Int31n(256)),
G: byte(r.Int31n(256)),
B: byte(r.Int31n(256)),
A: 0xFF,
}
}

// gameTypeFromMode returns the game type ID from the game mode passed.
func gameTypeFromMode(mode world.GameMode) int32 {
if mode.AllowsFlying() && mode.CreativeInventory() {
Expand Down
6 changes: 6 additions & 0 deletions server/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"github.com/df-mc/dragonfly/server/player/debug"
"image/color"
"io"
"log/slog"
"net"
Expand Down Expand Up @@ -91,6 +92,8 @@ type Session struct {
debugShapesAdd chan debug.Shape
debugShapesRemove chan int

colour atomic.Pointer[color.RGBA]

closeBackground chan struct{}
}

Expand Down Expand Up @@ -176,6 +179,9 @@ func (conf Config) New(conn Conn) *Session {
debugShapesAdd: make(chan debug.Shape, 256),
debugShapesRemove: make(chan int, 256),
}
colour := randomColour(conn.IdentityData().DisplayName)
s.colour.Store(&colour)

s.openedWindow.Store(inventory.New(1, nil))
s.openedPos.Store(&cube.Pos{})

Expand Down
11 changes: 11 additions & 0 deletions server/session/session_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ func (l *sessionList) Remove(s *Session) {
l.s = sliceutil.DeleteVal(l.s, s)
}

func (l *sessionList) resendList(s *Session) {
l.mu.Lock()
defer l.mu.Unlock()

for _, other := range l.s {
l.unsendSessionFrom(s, other)
l.sendSessionTo(s, other)
}
}

func (l *sessionList) Lookup(id uuid.UUID) (*Session, bool) {
l.mu.Lock()
defer l.mu.Unlock()
Expand Down Expand Up @@ -74,6 +84,7 @@ func (l *sessionList) sendSessionTo(s, to *Session) {
Username: s.conn.IdentityData().DisplayName,
XUID: s.conn.IdentityData().XUID,
Skin: skinToProtocol(s.joinSkin),
PlayerColour: s.Colour(),
}},
})
}
Expand Down