forked from df-mc/dragonfly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
86 lines (72 loc) · 2.13 KB
/
main.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
package main
import (
"fmt"
"os"
"github.com/df-mc/dragonfly/server"
"github.com/df-mc/dragonfly/server/block"
"github.com/df-mc/dragonfly/server/block/cube"
"github.com/df-mc/dragonfly/server/event"
"github.com/df-mc/dragonfly/server/player"
"github.com/df-mc/dragonfly/server/player/chat"
"github.com/df-mc/dragonfly/server/world"
"github.com/df-mc/dragonfly/server/world/biome"
"github.com/df-mc/dragonfly/server/world/generator"
"github.com/pelletier/go-toml"
"github.com/sirupsen/logrus"
)
func main() {
log := logrus.New()
log.Formatter = &logrus.TextFormatter{ForceColors: true}
log.Level = logrus.DebugLevel
chat.Global.Subscribe(chat.StdoutSubscriber{})
conf, err := readConfig(log)
if err != nil {
log.Fatalln(err)
}
srv := conf.New()
srv.CloseOnProgramEnd()
srv.Listen()
w, err := srv.LoadWorldFromFolder("world2", world.Overworld, "nether", "end", generator.NewFlat(biome.Plains{}, []world.Block{}))
if err != nil {
panic(err)
}
w.SetBlock(cube.Pos{0, 0, 0}, block.Obsidian{}, nil)
for srv.Accept(func(p *player.Player) {
p.Handle(&PlayerHandler{p: p, srv: srv})
}) {
}
}
type PlayerHandler struct {
srv *server.Server
player.NopHandler
p *player.Player
}
func (h *PlayerHandler) HandleChat(ctx *event.Context, message *string) {
ctx.Cancel()
h.p.World().RemoveEntity(h.p)
h.srv.GetWorld(*message).AddEntity(h.p)
}
// readConfig reads the configuration from the config.toml file, or creates the
// file if it does not yet exist.
func readConfig(log server.Logger) (server.Config, error) {
c := server.DefaultConfig()
var zero server.Config
if _, err := os.Stat("config.toml"); os.IsNotExist(err) {
data, err := toml.Marshal(c)
if err != nil {
return zero, fmt.Errorf("encode default config: %v", err)
}
if err := os.WriteFile("config.toml", data, 0644); err != nil {
return zero, fmt.Errorf("create default config: %v", err)
}
return c.Config(log)
}
data, err := os.ReadFile("config.toml")
if err != nil {
return zero, fmt.Errorf("read config: %v", err)
}
if err := toml.Unmarshal(data, &c); err != nil {
return zero, fmt.Errorf("decode config: %v", err)
}
return c.Config(log)
}