-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_transfer.go
70 lines (61 loc) · 1.5 KB
/
command_transfer.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 (
"fmt"
"github.com/df-mc/dragonfly/server/cmd"
"github.com/df-mc/dragonfly/server/player"
"net"
"reflect"
)
type transferCommand struct {
Address rememberedAddress `cmd:"address"`
}
func (c transferCommand) Run(src cmd.Source, o *cmd.Output) {
p := src.(*player.Player)
v, _ := handlers.Load(p.UUID())
h := v.(*handler)
m := h.m.Load()
if err := p.Transfer(addressWithPort(string(c.Address))); err != nil {
o.Errorf(m.Sprintf("server.error.resolve", err))
}
}
func (c transferCommand) Allow(src cmd.Source) bool {
p, ok := src.(*player.Player)
if !ok {
o := new(cmd.Output)
o.Errorf("This command can only be executed from in-game.")
src.SendCommandOutput(o)
return false
}
_, ok = handlers.Load(p.UUID())
return ok
}
type rememberedAddress string
func (rememberedAddress) Type() string { return "address" }
func (param rememberedAddress) Parse(line *cmd.Line, v reflect.Value) error {
arg, ok := line.Next()
if !ok {
return cmd.ErrInsufficientArgs
}
if _, _, err := net.SplitHostPort(addressWithPort(arg)); err != nil {
return fmt.Errorf("unable to parse server address: %w", err)
}
v.SetString(arg)
return nil
}
func (param rememberedAddress) Options(src cmd.Source) []string {
p, ok := src.(*player.Player)
if !ok {
return nil
}
v, ok := handlers.Load(p.UUID())
if !ok {
return nil
}
h := v.(*handler)
servers := h.prof.Load().Servers
s := make([]string, len(servers))
for i, server := range servers {
s[i] = server.Address.String()
}
return s
}