Skip to content

Commit

Permalink
server/server.go: Plug back in player data loading.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandertv committed Nov 21, 2024
1 parent c0f9725 commit 874f776
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
9 changes: 8 additions & 1 deletion server/player/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

type Config struct {
Name, XUID, Locale string
GameMode world.GameMode
UUID uuid.UUID
Skin skin.Skin
Data *Data
Expand Down Expand Up @@ -46,7 +47,7 @@ func (conf Config) Apply(data *world.EntityData) {
cooldowns: make(map[string]time.Time),
mc: &entity.MovementComputer{Gravity: 0.08, Drag: 0.02, DragBeforeGravity: true},
heldSlot: new(uint32),
gameMode: world.GameModeSurvival,
gameMode: conf.GameMode,
skin: conf.Skin,
airSupplyTicks: 300,
maxAirSupplyTicks: 300,
Expand All @@ -56,6 +57,12 @@ func (conf Config) Apply(data *world.EntityData) {
}
}

func (conf Config) Finalise(p *Player) {
if conf.Data != nil {
p.load(*conf.Data)
}
}

// Type is a world.EntityType implementation for Player.
var Type ptype

Expand Down
35 changes: 18 additions & 17 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ type Server struct {
}

type incoming struct {
s *session.Session
p *onlinePlayer
w *world.World
conf player.Config
s *session.Session
p *onlinePlayer
w *world.World
}

type onlinePlayer struct {
Expand Down Expand Up @@ -135,6 +136,7 @@ func (srv *Server) Accept() iter.Seq[*player.Player] {
ret := false
<-inc.w.Exec(func(tx *world.Tx) {
p := tx.AddEntity(inc.p.handle).(*player.Player)
inc.conf.Finalise(p)
inc.s.Spawn(p, tx)
ret = !yield(p)
})
Expand Down Expand Up @@ -418,6 +420,7 @@ func (srv *Server) finaliseConn(ctx context.Context, conn session.Conn, l Listen
// may later be modified if the player was saved in the player provider of the
// server.
func (srv *Server) defaultGameData() minecraft.GameData {
gm, _ := world.GameModeID(srv.world.DefaultGameMode())
return minecraft.GameData{
// We set these IDs to 1, because that's how the session will treat them.
EntityUniqueID: 1,
Expand All @@ -429,7 +432,7 @@ func (srv *Server) defaultGameData() minecraft.GameData {
Time: int64(srv.world.Time()),
Difficulty: 2,

PlayerGameMode: packet.GameTypeCreative,
PlayerGameMode: int32(gm),
PlayerPermissions: packet.PermissionLevelMember,
PlayerPosition: vec64To32(srv.world.Spawn().Vec3Centre().Add(mgl64.Vec3{0, 1.62})),

Expand Down Expand Up @@ -475,7 +478,7 @@ func (srv *Server) checkNetIsolation() {

// handleSessionClose handles the closing of a session. It removes the player
// of the session from the server.
func (srv *Server) handleSessionClose(tx *world.Tx, c session.Controllable) {
func (srv *Server) handleSessionClose(_ *world.Tx, c session.Controllable) {
srv.pmu.Lock()
_, ok := srv.p[c.UUID()]
delete(srv.p, c.UUID())
Expand Down Expand Up @@ -509,21 +512,19 @@ func (srv *Server) createPlayer(id uuid.UUID, conn session.Conn, data *player.Da
HandleStop: srv.handleSessionClose,
}.New(conn)

// TODO: Do something with the gamemode and other player data.
_ = gm

conf := player.Config{
Name: conn.IdentityData().DisplayName,
XUID: conn.IdentityData().XUID,
UUID: id,
Locale: conn.ClientData().LanguageCode,
Skin: srv.parseSkin(conn.ClientData()),
Data: data,
Pos: pos,
Session: s,
Name: conn.IdentityData().DisplayName,
XUID: conn.IdentityData().XUID,
UUID: id,
Locale: conn.ClientData().LanguageCode,
Skin: srv.parseSkin(conn.ClientData()),
Data: data,
Pos: pos,
Session: s,
GameMode: gm,
}
handle := world.EntitySpawnOpts{Position: pos, ID: id}.New(player.Type, conf)
return incoming{s: s, w: w, p: &onlinePlayer{name: conf.Name, xuid: conf.XUID, handle: handle}}
return incoming{s: s, w: w, conf: conf, p: &onlinePlayer{name: conf.Name, xuid: conf.XUID, handle: handle}}
}

// createWorld loads a world of the server with a specific dimension, ending
Expand Down

0 comments on commit 874f776

Please sign in to comment.