This repository has been archived by the owner on Oct 22, 2023. It is now read-only.
forked from ZeppelinMC/Zeppelin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.go
327 lines (319 loc) · 9.8 KB
/
commands.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package main
import (
"bufio"
"fmt"
"os"
"runtime"
"strconv"
"strings"
"github.com/Tnze/go-mc/chat"
"github.com/Tnze/go-mc/data/packetid"
pk "github.com/Tnze/go-mc/net/packet"
"github.com/Tnze/go-mc/offline"
"github.com/google/uuid"
)
func Reload() chat.Message {
playerCache = make(map[string]PlayerPermissions)
groupCache = make(map[string]GroupPermissions)
server.Config = LoadConfig()
server.Players.Whitelist = LoadPlayerList("whitelist.json")
server.Players.OPs = LoadPlayerList("ops.json")
server.Players.BannedPlayers = LoadPlayerList("banned_players.json")
server.Players.BannedIPs = LoadIPBans()
server.Favicon = []byte{}
if server.Config.Whitelist.Enable && server.Config.Whitelist.Enforce {
wmap := make(map[string]bool)
for _, p := range server.Players.Whitelist {
wmap[p.UUID] = true
}
for i := 0; i < len(server.Players.Players) && !wmap[server.Players.PlayerIDs[i]]; i++ {
player := server.Players.Players[server.Players.PlayerIDs[i]]
player.Connection.WritePacket(pk.Marshal(packetid.ClientboundDisconnect, chat.Text(server.Config.Messages.NotInWhitelist)))
player.Connection.Close()
server.Events.Emit("PlayerLeave", player)
}
}
for _, player := range server.Players.Players {
player.Connection.WritePacket(pk.Marshal(packetid.ClientboundCommands, CommandGraph{player.UUID.String}))
}
return chat.Text(server.Config.Messages.ReloadComplete)
}
func CreateSTDINReader() {
reader := bufio.NewReader(os.Stdin)
command, _ := reader.ReadString('\n')
command = strings.TrimSpace(command)
if command == "" {
go CreateSTDINReader()
return
}
server.Logger.Print("%v", server.Command("console", command))
go CreateSTDINReader()
}
func GetArgument(args []string, index int) string {
if len(args) <= index {
return ""
}
return args[index]
}
func (server *Server) Command(executor string, content string) chat.Message {
var executorName string
var executorPlayer *Player
if executor == "console" {
executorName = "Console"
} else {
executorName = server.Players.Players[executor].Name
executorPlayer = server.Players.Players[executor]
}
content = strings.TrimSpace(content)
args := strings.Split(content, " ")
cmd := args[0]
args = args[1:]
command, exists := server.Commands[cmd]
if !exists {
return chat.Text(server.Config.Messages.UnknownCommand)
}
if !server.HasPermissions(executor, command.RequiredPermissions) {
return chat.Text(server.Config.Messages.InsufficientPermissions)
}
switch cmd {
case "reload", "rl":
return Reload()
case "stop":
{
go func() {
server.Logger.Info("Saving world")
server.Players.Lock()
for _, player := range server.Players.Players {
player.Connection.WritePacket(pk.Marshal(packetid.ClientboundDisconnect, chat.Text(server.Config.Messages.ServerClosed)))
player.Connection.Close()
player.Data.Save(player.UUID.String)
}
for _, world := range server.Worlds {
world.TickLock.Lock()
for pos, chunk := range world.Chunks {
chunk.Lock()
world.UnloadChunk(pos)
}
}
os.Exit(0)
}()
return chat.Text("Shutting down server...")
}
case "op":
{
id := GetArgument(args, 0)
if id == "" {
return chat.Text("§cPlease specify a player to op")
}
isOp, op := server.Players.IsOP(id)
if isOp {
return chat.Text(fmt.Sprintf("§c%s is already a server operator", op.Name))
}
player := PlayerBase{}
if _, err := uuid.Parse(id); err == nil { // is uuid
player.UUID = id
if server.Players.Players[id].UUID.String == id {
player.Name = server.Players.Players[id].Name
} else {
exists, p := server.Mojang.FetchUUID(id)
if !exists {
if server.Config.Online {
return chat.Text("§cUnknown player")
}
player.Name = id
} else {
player.Name = p.Name
}
}
} else {
player.Name = id
if server.Players.PlayerNames[id] != "" {
player.UUID = server.Players.PlayerNames[id]
} else {
exists, p := server.Mojang.FetchUsername(id)
if !exists {
if server.Config.Online {
return chat.Text("§cUnknown player")
} else {
player.UUID = fmt.Sprint(offline.NameToUUID(id))
}
} else {
player.UUID = p.UUID
}
}
}
server.Players.OPs = WritePlayerList("ops.json", player)
if server.Players.Players[player.UUID] != nil {
server.Players.Players[player.UUID].Connection.WritePacket(pk.Marshal(packetid.ClientboundCommands, CommandGraph{player.UUID}))
}
server.BroadcastMessageAdmin(executor, chat.Text(fmt.Sprintf("§7[%s: Made %s a server operator]", executorName, player.Name)))
return chat.Text(fmt.Sprintf("Made %s a server operator", player.Name))
}
case "gamemode":
{
gamemode := GetArgument(args, 0)
id := GetArgument(args, 1)
var player *Player
if gamemode == "" {
return chat.Text("§cPlease specify a gamemode")
}
if id == "" {
if executor == "console" {
return chat.Text("§cThe gamemode command can only be used on players")
} else {
id = executor
}
}
var mode float32
switch gamemode {
case "survival":
mode = 0
case "creative":
mode = 1
case "adventure":
mode = 2
case "spectator":
mode = 3
}
if server.Players.PlayerNames[id] == "" {
if server.Players.Players[id].UUID.String != id {
return chat.Text("§cUnknown player")
} else {
player = server.Players.Players[id]
}
} else {
player = server.Players.Players[server.Players.PlayerNames[id]]
}
player.Connection.WritePacket(pk.Marshal(
packetid.ClientboundGameEvent,
pk.UnsignedByte(3),
pk.Float(mode),
))
server.BroadcastMessageAdmin(executor, chat.Text(fmt.Sprintf("§7[%s: Set %s's gamemode to %s]", executorName, player.Name, gamemode)))
if executor == player.UUID.String {
return chat.Text(fmt.Sprintf("Set own gamemode to %s", gamemode))
} else {
return chat.Text(fmt.Sprintf("Set %s's gamemode to %s", player.Name, gamemode))
}
}
case "ram":
var m runtime.MemStats
runtime.ReadMemStats(&m)
return chat.Text(fmt.Sprintf("Allocated: %v MiB, Total Allocated: %v MiB", bToMb(m.Alloc), bToMb(m.TotalAlloc)))
case "teleport", "tp":
{
switch len(args) {
case 1:
{
if executor == "console" {
return chat.Text("§cOnly players can be teleported")
}
is, target := ParseTarget(executorPlayer, args[0])
if !is {
return chat.Text("§cUnknown player at argument 0")
}
executorPlayer.Connection.WritePacket(pk.Marshal(packetid.ClientboundPlayerPosition,
pk.Double(target.Position[0]), //x
pk.Double(target.Position[1]), //y
pk.Double(target.Position[2]), //z
pk.Float(executorPlayer.Rotation[0]), //yaw
pk.Float(executorPlayer.Rotation[1]), //pitch
pk.Byte(0),
pk.VarInt(server.NewTeleportID()),
))
return chat.Text(fmt.Sprintf("Teleported you to %s", target.Name))
}
case 2:
{
is, player := ParseTarget(executorPlayer, args[0])
if !is {
return chat.Text("§cUnknown player at argument 0")
}
is, target := ParseTarget(executorPlayer, args[1])
if !is {
return chat.Text("§cUnknown player at argument 1")
}
player.Connection.WritePacket(pk.Marshal(packetid.ClientboundPlayerPosition,
pk.Double(target.Position[0]), //x
pk.Double(target.Position[1]), //y
pk.Double(target.Position[2]), //z
pk.Float(player.Rotation[0]), //yaw
pk.Float(player.Rotation[1]), //pitch
pk.Byte(0),
pk.VarInt(server.NewTeleportID()),
))
return chat.Text(fmt.Sprintf("Teleported %s to %s", player.Name, target.Name))
}
case 3:
{
x, xerr := strconv.ParseFloat(args[0], 64)
y, yerr := strconv.ParseFloat(args[1], 64)
z, zerr := strconv.ParseFloat(args[2], 64)
if xerr != nil || yerr != nil || zerr != nil {
return chat.Text("§cPassed argument is not a float")
}
executorPlayer.Connection.WritePacket(pk.Marshal(packetid.ClientboundPlayerPosition,
pk.Double(x), //x
pk.Double(y), //y
pk.Double(z), //z
pk.Float(executorPlayer.Rotation[0]), //yaw
pk.Float(executorPlayer.Rotation[1]), //pitch
pk.Byte(0),
pk.VarInt(server.NewTeleportID()),
))
return chat.Text(fmt.Sprintf("Teleported you to %v %v %v", x, y, z))
}
case 4:
{
is, player := ParseTarget(executorPlayer, args[0])
if !is {
return chat.Text("§cUnknown player at argument 0")
}
x, xerr := strconv.ParseFloat(args[1], 64)
y, yerr := strconv.ParseFloat(args[2], 64)
z, zerr := strconv.ParseFloat(args[3], 64)
if xerr != nil || yerr != nil || zerr != nil {
return chat.Text("§cPassed argument is not a float")
}
player.Connection.WritePacket(pk.Marshal(packetid.ClientboundPlayerPosition,
pk.Double(x), //x
pk.Double(y), //y
pk.Double(z), //z
pk.Float(player.Rotation[0]), //yaw
pk.Float(player.Rotation[1]), //pitch
pk.Byte(0),
pk.VarInt(server.NewTeleportID()),
))
return chat.Text(fmt.Sprintf("Teleported %s to %v %v %v", player.Name, x, y, z))
}
default:
return chat.Text("§cInvalid amount of arguments")
}
}
default:
return chat.Text(server.Config.Messages.UnknownCommand)
}
}
func ParseTarget(executor *Player, arg string) (bool, *Player) {
if len(arg) == 2 && strings.HasPrefix(arg, "@") {
t := strings.TrimPrefix(arg, "@")
switch t {
case "s":
{
return true, executor
}
default:
return false, nil
}
}
if player, ok := server.Players.Players[arg]; ok {
return true, player
}
if player, ok := server.Players.PlayerNames[arg]; ok {
return true, server.Players.Players[player]
}
return false, nil
}
func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}