-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
63 lines (48 loc) · 1.22 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
package main
import (
"encoding/json"
"fmt"
"github.com/szerookii/gocrafty/gocrafty"
"github.com/szerookii/gocrafty/gocrafty/logger"
"github.com/szerookii/gocrafty/gocrafty/minecraft"
"os"
)
func main() {
log := logger.Default()
log.Infof("Starting Gocrafty for Minecraft v%s", minecraft.Version)
conf, err := readConfig()
if err != nil {
log.Fatal("read config: %v", err)
}
log.SetLevel(conf.LogLevel)
srv := gocrafty.NewServer(conf)
err = srv.Listen()
if err != nil {
log.Fatal("listen: %v", err)
}
for {
srv.Accept()
}
}
func readConfig() (*gocrafty.ServerConfig, error) {
c := gocrafty.DefaultConfig()
var zero *gocrafty.ServerConfig
if _, err := os.Stat("config.json"); os.IsNotExist(err) {
data, err := json.MarshalIndent(c, "", "\t")
if err != nil {
return zero, fmt.Errorf("encode default config: %v", err)
}
if err := os.WriteFile("config.json", data, 0644); err != nil {
return zero, fmt.Errorf("create default config: %v", err)
}
return &c, nil
}
data, err := os.ReadFile("config.json")
if err != nil {
return zero, fmt.Errorf("read config: %v", err)
}
if err := json.Unmarshal(data, &c); err != nil {
return zero, fmt.Errorf("decode config: %v", err)
}
return &c, nil
}