fix data race
This commit is contained in:
@ -18,7 +18,7 @@ import (
|
||||
)
|
||||
|
||||
var motd = chat.Message{Text: "A Minecraft Server ", Extra: []chat.Message{{Text: "Powered by go-mc", Color: "yellow"}}}
|
||||
var addr = flag.String("Address", ":25565", "Listening address")
|
||||
var addr = flag.String("Address", "127.0.0.1:25565", "Listening address")
|
||||
var iconPath = flag.String("ServerIcon", "./server-icon.png", "The path to server icon")
|
||||
var maxPlayer = flag.Int("MaxPlayer", 16384, "The maximum number of players")
|
||||
var regionPath = flag.String("Regions", "./save/testdata/region/", "The region files")
|
||||
|
Binary file not shown.
@ -3,6 +3,7 @@ package server
|
||||
import (
|
||||
"context"
|
||||
_ "embed"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@ -23,6 +24,7 @@ type GamePlay interface {
|
||||
type Game struct {
|
||||
*ecs.World
|
||||
*ecs.Dispatcher
|
||||
WorldLocker sync.Mutex
|
||||
handlers map[int32][]*PacketHandler
|
||||
components []Component
|
||||
}
|
||||
@ -68,7 +70,9 @@ func (g *Game) Run(ctx context.Context) {
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
g.WorldLocker.Lock()
|
||||
g.Dispatcher.Run(g.World)
|
||||
g.WorldLocker.Unlock()
|
||||
case <-ctx.Done():
|
||||
return
|
||||
}
|
||||
@ -76,6 +80,7 @@ func (g *Game) Run(ctx context.Context) {
|
||||
}
|
||||
|
||||
func (g *Game) AcceptPlayer(name string, id uuid.UUID, protocol int32, conn *net.Conn) {
|
||||
g.WorldLocker.Lock()
|
||||
eid := g.CreateEntity(
|
||||
Client{
|
||||
Conn: conn,
|
||||
@ -90,6 +95,7 @@ func (g *Game) AcceptPlayer(name string, id uuid.UUID, protocol int32, conn *net
|
||||
)
|
||||
c := ecs.GetComponent[Client](g.World).GetValue(eid)
|
||||
p := ecs.GetComponent[Player](g.World).GetValue(eid)
|
||||
g.WorldLocker.Unlock()
|
||||
defer c.packetQueue.Close()
|
||||
|
||||
go func() {
|
||||
|
@ -20,6 +20,7 @@ type playerSpawnSystem struct {
|
||||
func (p playerSpawnSystem) Update(w *ecs.World) {
|
||||
clients := ecs.GetComponent[server.Client](w)
|
||||
players := ecs.GetComponent[server.Player](w)
|
||||
pos, rot := ecs.GetComponent[server.Pos](w), ecs.GetComponent[server.Rot](w)
|
||||
profiles := ecs.GetComponent[PlayerProfile](w)
|
||||
dimensionRes := ecs.GetResource[world.DimensionList](w)
|
||||
players.AndNot(profiles.BitSetLike).Range(func(eid ecs.Index) {
|
||||
@ -35,6 +36,15 @@ func (p playerSpawnSystem) Update(w *ecs.World) {
|
||||
panic("dimension " + profile.Dimension + " not found")
|
||||
}
|
||||
profiles.SetValue(eid, PlayerProfile{Dim: dim})
|
||||
pos.SetValue(eid, server.Pos{
|
||||
X: profile.Pos[0],
|
||||
Y: profile.Pos[1],
|
||||
Z: profile.Pos[2],
|
||||
})
|
||||
rot.SetValue(eid, server.Rot{
|
||||
Yaw: profile.Rotation[0],
|
||||
Pitch: profile.Rotation[1],
|
||||
})
|
||||
client.WritePacket(server.Packet758(pk.Marshal(
|
||||
packetid.ClientboundLogin,
|
||||
pk.Int(eid), // Entity ID
|
||||
@ -59,7 +69,11 @@ func (p playerSpawnSystem) Update(w *ecs.World) {
|
||||
|
||||
func SpawnSystem(g *server.Game, playerdataPath string) {
|
||||
ecs.Register[PlayerProfile, *ecs.HashMapStorage[PlayerProfile]](g.World)
|
||||
g.Dispatcher.Add(playerSpawnSystem{storage: storage{playerdataPath}}, "go-mc:player:SpawnSystem", nil)
|
||||
g.Dispatcher.Add(
|
||||
playerSpawnSystem{storage: storage{playerdataPath}},
|
||||
"go-mc:player:SpawnSystem",
|
||||
nil,
|
||||
)
|
||||
}
|
||||
|
||||
// PosAndRotSystem add a system to g.Dispatcher that
|
||||
|
Reference in New Issue
Block a user