forked from TyphoonMC/TyphoonCore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.go
282 lines (256 loc) · 6.2 KB
/
command.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package typhoon
import (
"log"
"strings"
)
type CommandNodeType uint8
const (
commandNodeTypeRoot = iota
CommandNodeTypeLiteral
CommandNodeTypeArgument
)
type CommandSuggestionType string
const (
CommandSuggestionNone CommandSuggestionType = ""
CommandSuggestionAskServer CommandSuggestionType = "minecraft:ask_server"
CommandSuggestionAllRecipes CommandSuggestionType = "minecraft:all_recipes"
CommandSuggestionAvailableSounds CommandSuggestionType = "minecraft:available_sounds"
CommandSuggestionSummonableEntities CommandSuggestionType = "minecraft:summonable_entities"
)
type CommandParser interface {
GetId() string
IsMultiple() bool
IsValid(string) bool
IsArrayValid([]string) bool
GetSuggestion() CommandSuggestionType
Complete(string) []string
writeProperties(*Player) error
}
type CommandNode struct {
Type CommandNodeType
Execute func(player *Player, args []string)
Children []*CommandNode
RedirectNode *CommandNode
Name string
Parser CommandParser
}
func CommandNodeLiteral(
name string,
children []*CommandNode,
execute func(player *Player, args []string)) *CommandNode {
return &CommandNode{
CommandNodeTypeLiteral,
execute,
children,
nil,
name,
nil,
}
}
func CommandNodeArgument(
name string,
children []*CommandNode,
parser CommandParser,
execute func(player *Player, args []string)) *CommandNode {
return &CommandNode{
CommandNodeTypeArgument,
execute,
children,
nil,
name,
parser,
}
}
func (c *Core) DeclareCommand(graph *CommandNode) {
c.rootCommand.Children = append(c.rootCommand.Children, graph)
c.compileCommands()
}
func (c *Core) onCommand(player *Player, command string) {
args := strings.Split(command, " ")
node := &c.rootCommand
if !c.analyseCommand(player, args, node, 0) {
m := ChatMessage("Unknown command")
m.SetColor(&ChatColorRed)
player.SendMessage(m)
}
}
func (c *Core) analyseCommand(player *Player, args []string, node *CommandNode, step int) bool {
if len(args) > step {
for _, child := range node.Children {
switch child.Type {
case CommandNodeTypeLiteral:
if child.Name == args[step] {
return c.analyseCommand(player, args, child, step+1)
}
case CommandNodeTypeArgument:
if child.Parser.IsMultiple() {
if child.Parser.IsArrayValid(args[step:]) {
return c.analyseCommand(player, args, child, step+1)
}
} else {
if child.Parser.IsValid(args[step]) {
return c.analyseCommand(player, args, child, step+1)
}
}
}
}
}
if node.Execute != nil {
node.Execute(player, args)
return true
}
return false
}
func (c *Core) onTabCommand(player *Player, command string) {
args := strings.Split(command, " ")
node := &c.rootCommand
player.WritePacket(&PacketPlayTabComplete{
c.analyseTabCommand(player, args, node, 0),
})
}
func commandJoin(args []string, attr string) string {
str := attr
if len(args) == 0 {
str = "/" + str
}
return str
}
func (c *Core) analyseTabCommand(player *Player, args []string, node *CommandNode, step int) []string {
strs := make([]string, 0)
if len(args) > step {
for _, child := range node.Children {
switch child.Type {
case CommandNodeTypeLiteral:
if len(args)-1 == step && strings.HasPrefix(child.Name, args[step]) {
strs = append(strs, commandJoin(args[:len(args)-1], child.Name))
} else {
if child.Name == args[step] {
strs = append(strs, c.analyseTabCommand(player, args, child, step+1)...)
}
}
case CommandNodeTypeArgument:
if child.Parser.IsMultiple() {
if child.Parser.IsArrayValid(args[step:]) {
strs = append(strs, strings.Join(args, " "))
}
} else {
strs = append(strs, child.Parser.Complete(args[step])...)
}
}
}
}
return strs
}
type commandNode struct {
Type CommandNodeType
Execute bool
Children []int
RedirectNode int
Name string
Parser CommandParser
}
func countNodes(node *CommandNode) int {
i := 1
for _, n := range node.Children {
i += countNodes(n)
}
return i
}
func nodeRefs(refs map[*CommandNode]int, node *CommandNode, index int) int {
refs[node] = index
i := 1
for _, n := range node.Children {
i += nodeRefs(refs, n, index+i)
}
return i
}
func compileNode(compile []commandNode, refs map[*CommandNode]int, node *CommandNode) {
children := make([]int, len(node.Children))
for i, n := range node.Children {
compileNode(compile, refs, n)
children[i] = refs[n]
}
redirect := -1
if node.RedirectNode != nil {
redirect = refs[node.RedirectNode]
}
compile[refs[node]] = commandNode{
node.Type,
node.Execute != nil,
children,
redirect,
node.Name,
node.Parser,
}
}
func (c *Core) compileCommands() {
count := countNodes(&c.rootCommand)
commands := make([]commandNode, count)
refs := make(map[*CommandNode]int, count)
nodeRefs(refs, &c.rootCommand, 0)
compileNode(commands, refs, &c.rootCommand)
c.compiledCommands = commands
}
func (node *commandNode) flags() uint8 {
flags := uint8(node.Type)
if node.Execute {
flags |= 0x04
}
if node.RedirectNode != -1 {
flags |= 0x08
}
if node.Parser != nil && node.Parser.GetSuggestion() != CommandSuggestionNone {
flags |= 0x10
}
return flags
}
func (node *commandNode) writeTo(player *Player) (err error) {
err = player.WriteUInt8(node.flags())
if err != nil {
log.Print(err)
return
}
err = player.WriteVarInt(len(node.Children))
if err != nil {
log.Print(err)
return
}
for _, child := range node.Children {
err = player.WriteVarInt(child)
if err != nil {
log.Print(err)
return
}
}
if node.RedirectNode != -1 {
err = player.WriteVarInt(node.RedirectNode)
if err != nil {
log.Print(err)
return
}
}
if node.Type == CommandNodeTypeArgument ||
node.Type == CommandNodeTypeLiteral {
err = player.WriteString(node.Name)
if err != nil {
log.Print(err)
return
}
}
if node.Type == CommandNodeTypeArgument {
err = player.WriteString(node.Parser.GetId())
if err != nil {
log.Print(err)
return
}
node.Parser.writeProperties(player)
}
if node.Parser != nil && node.Parser.GetSuggestion() != CommandSuggestionNone {
err = player.WriteString(string(node.Parser.GetSuggestion()))
if err != nil {
log.Print(err)
return
}
}
return
}