-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform_add.go
70 lines (62 loc) · 1.39 KB
/
form_add.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
package main
import (
"github.com/df-mc/dragonfly/server/player"
"github.com/df-mc/dragonfly/server/player/form"
"golang.org/x/text/message"
"net"
)
func newAddForm(p *message.Printer) form.Form {
return form.New(addForm{
Address: form.NewInput(
p.Sprintf("server.address.text"),
"",
p.Sprintf("server.address.placeholder"),
),
Name: form.NewInput(
p.Sprintf("server.name.text"),
"",
"",
),
m: p,
}, p.Sprintf("menu.add.title"))
}
type addForm struct {
Address form.Input
Name form.Input
m *message.Printer
}
func (f addForm) Submit(s form.Submitter) {
p, ok := s.(*player.Player)
if !ok {
return
}
if address := f.Address.Value(); address != "" {
address = addressWithPort(address)
addr, err := net.ResolveUDPAddr("udp", address)
if err != nil {
p.Message(f.m.Sprintf("server.error.resolve", err))
return
}
v, ok := handlers.Load(p.UUID())
if !ok {
return
}
h := v.(*handler)
prof := h.prof.Load()
if len(prof.Servers) >= 20 {
h.p.Message(f.m.Sprintf("server.error.limit", 20))
return
}
if prof.shouldRemember(addr, address) {
prof.Servers = append(prof.Servers, serverProfile{
Name: serverProfileName(f.Name.Value()),
Address: addr,
RawAddress: address,
})
h.prof.Store(prof)
h.p.Message(f.m.Sprintf("menu.add.success"))
} else {
h.p.Message(f.m.Sprintf("menu.add.alreadyExists"))
}
}
}