This repository has been archived by the owner on Dec 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexample_server.go
142 lines (127 loc) · 4.57 KB
/
example_server.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
135
136
137
138
139
140
141
142
package main
import (
"fmt"
"github.com/df-mc/dragonfly/server"
"github.com/df-mc/dragonfly/server/item"
"github.com/df-mc/dragonfly/server/player"
"github.com/df-mc/dragonfly/server/player/chat"
"github.com/df-mc/dragonfly/server/session"
"github.com/df-mc/dragonfly/server/world"
"github.com/df-mc/dragonfly/server/world/sound"
_ "github.com/flonja/multiversion/protocols" // VERY IMPORTANT
v618 "github.com/flonja/multiversion/protocols/v618"
v622 "github.com/flonja/multiversion/protocols/v622"
v630 "github.com/flonja/multiversion/protocols/v630"
v649 "github.com/flonja/multiversion/protocols/v649"
v662 "github.com/flonja/multiversion/protocols/v662"
"github.com/sandertv/gophertunnel/minecraft"
"github.com/sirupsen/logrus"
)
func runServer() {
log := logrus.New()
log.Formatter = &logrus.TextFormatter{ForceColors: true}
log.Level = logrus.DebugLevel
chat.Global.Subscribe(chat.StdoutSubscriber{})
uc := server.DefaultConfig()
conf, err := uc.Config(log)
if err != nil {
log.Fatalln(err)
}
conf.Listeners = []func(conf server.Config) (server.Listener, error){
func(conf server.Config) (server.Listener, error) {
cfg := minecraft.ListenConfig{
MaximumPlayers: conf.MaxPlayers,
StatusProvider: statusProvider{name: conf.Name},
AuthenticationDisabled: conf.AuthDisabled,
ResourcePacks: conf.Resources,
Biomes: biomes(),
TexturePacksRequired: conf.ResourcesRequired,
AcceptedProtocols: []minecraft.Protocol{v662.New(), v649.New(), v630.New(), v622.New(), v618.New()},
}
l, err := cfg.Listen("raknet", uc.Network.Address)
if err != nil {
return nil, fmt.Errorf("create minecraft listener: %w", err)
}
conf.Log.Infof("Server running on %v.\n", l.Addr())
return listener{l}, nil
},
}
srv := conf.New()
srv.CloseOnProgramEnd()
srv.World().StopTime()
srv.World().SetTime(1000)
srv.Listen()
for srv.Accept(func(p *player.Player) {
p.ShowCoordinates()
p.SetGameMode(world.GameModeCreative)
p.Inventory().Clear()
_, _ = p.Inventory().AddItem(item.NewStack(item.MusicDisc{DiscType: sound.DiscRelic()}, 1))
}) {
}
}
// listener is a Listener implementation that wraps around a minecraft.Listener so that it can be listened on by
// Server.
type listener struct {
*minecraft.Listener
}
// Accept blocks until the next connection is established and returns it. An error is returned if the Listener was
// closed using Close.
func (l listener) Accept() (session.Conn, error) {
conn, err := l.Listener.Accept()
if err != nil {
return nil, err
}
return conn.(session.Conn), err
}
// Disconnect disconnects a connection from the Listener with a reason.
func (l listener) Disconnect(conn session.Conn, reason string) error {
return l.Listener.Disconnect(conn.(*minecraft.Conn), reason)
}
// statusProvider handles the way the server shows up in the server list. The
// online players and maximum players are not changeable from outside the
// server, but the server name may be changed at any time.
type statusProvider struct {
name string
}
// ServerStatus returns the player count, max players and the server's name as
// a minecraft.ServerStatus.
func (s statusProvider) ServerStatus(playerCount, maxPlayers int) minecraft.ServerStatus {
return minecraft.ServerStatus{
ServerName: s.name,
PlayerCount: playerCount,
MaxPlayers: maxPlayers,
}
}
// ashyBiome represents a biome that has any form of ash.
type ashyBiome interface {
// Ash returns the ash and white ash of the biome.
Ash() (ash float64, whiteAsh float64)
}
// sporingBiome represents a biome that has blue or red spores.
type sporingBiome interface {
// Spores returns the blue and red spores of the biome.
Spores() (blueSpores float64, redSpores float64)
}
// biomes builds a mapping of all biome definitions of the server, ready to be set in the biomes field of the server
// listener.
func biomes() map[string]any {
definitions := make(map[string]any)
for _, b := range world.Biomes() {
definition := map[string]any{
"name_hash": b.String(), // This isn't actually a hash despite what the field name may suggest.
"temperature": float32(b.Temperature()),
"downfall": float32(b.Rainfall()),
"rain": b.Rainfall() > 0,
}
if a, ok := b.(ashyBiome); ok {
ash, whiteAsh := a.Ash()
definition["ash"], definition["white_ash"] = float32(ash), float32(whiteAsh)
}
if s, ok := b.(sporingBiome); ok {
blueSpores, redSpores := s.Spores()
definition["blue_spores"], definition["red_spores"] = float32(blueSpores), float32(redSpores)
}
definitions[b.String()] = definition
}
return definitions
}