This repository has been archived by the owner on Jan 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
world_manager.go
134 lines (113 loc) · 2.81 KB
/
world_manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package worldmanager
import (
"fmt"
"github.com/df-mc/dragonfly/server"
"github.com/df-mc/dragonfly/server/world"
"github.com/df-mc/dragonfly/server/world/mcdb"
"github.com/sirupsen/logrus"
"os"
"sync"
)
// WorldManager manages multiple worlds, dragonfly does not have multi-world management itself,
// so we must implement it ourselves.
type WorldManager struct {
s *server.Server
folderPath string
log *logrus.Logger
worldsMu sync.RWMutex
worlds map[string]*world.World
}
// New ...
func New(server *server.Server, folderPath string, log *logrus.Logger) *WorldManager {
_ = os.Mkdir(folderPath, 0644)
defaultWorld := server.World()
return &WorldManager{
s: server,
folderPath: folderPath,
log: log,
worlds: map[string]*world.World{
defaultWorld.Name(): defaultWorld,
},
}
}
// DefaultWorld ...
func (m *WorldManager) DefaultWorld() *world.World {
return m.s.World()
}
// Worlds ...
func (m *WorldManager) Worlds() []*world.World {
m.worldsMu.RLock()
worlds := make([]*world.World, 0, len(m.worlds))
for _, w := range m.worlds {
worlds = append(worlds, w)
}
m.worldsMu.RUnlock()
return worlds
}
// World ...
func (m *WorldManager) World(name string) (*world.World, bool) {
m.worldsMu.RLock()
w, ok := m.worlds[name]
m.worldsMu.RUnlock()
return w, ok
}
// LoadWorld ...
func (m *WorldManager) LoadWorld(folderName, worldName string, simulationDistance int) error {
if _, ok := m.World(worldName); ok {
return fmt.Errorf("world is already loaded")
}
w := world.New(m.log, simulationDistance)
p, err := mcdb.New(m.folderPath + "/" + folderName)
if err != nil {
return fmt.Errorf("error loading world: %v", err)
}
settings := p.Settings()
settings.Name = worldName
p.SaveSettings(settings)
w.Provider(p)
m.worldsMu.Lock()
m.worlds[w.Name()] = w
m.worldsMu.Unlock()
return nil
}
// UnloadWorld ...
func (m *WorldManager) UnloadWorld(w *world.World) error {
if w == m.DefaultWorld() {
return fmt.Errorf("the default world cannot be unloaded")
}
if _, ok := m.World(w.Name()); !ok {
return fmt.Errorf("world isn't loaded")
}
m.log.Debugf("Unloading world '%v'\n", w.Name())
for _, p := range m.s.Players() {
if p.World() == w {
m.DefaultWorld().AddEntity(p)
p.Teleport(m.DefaultWorld().Spawn().Vec3Middle())
}
}
m.worldsMu.Lock()
delete(m.worlds, w.Name())
m.worldsMu.Unlock()
if err := w.Close(); err != nil {
return fmt.Errorf("error closing world: %v", err)
}
m.log.Debugf("Unloaded world '%v'\n", w.Name())
return nil
}
// Close ...
func (m *WorldManager) Close() error {
m.worldsMu.Lock()
for _, w := range m.worlds {
// Let dragonfly close this.
if w == m.DefaultWorld() {
continue
}
m.log.Debugf("Closing world '%v'\n", w.Name())
if err := w.Close(); err != nil {
return err
}
}
m.worlds = nil
m.worldsMu.Unlock()
return nil
}