From 3a6be23addb61372a2d93abba1a563b6030c3693 Mon Sep 17 00:00:00 2001 From: HimbeerserverDE Date: Thu, 9 Sep 2021 20:55:49 +0200 Subject: Finish built-in chat commands --- chatcommands.go | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) (limited to 'chatcommands.go') diff --git a/chatcommands.go b/chatcommands.go index 3f4c233..fcdbcca 100644 --- a/chatcommands.go +++ b/chatcommands.go @@ -22,6 +22,132 @@ func init() { return "" }, }) + proxy.RegisterChatCmd(proxy.ChatCmd{ + Name: "find", + Perm: "cmd_find", + Handler: func(cc *proxy.ClientConn, args ...string) string { + if len(args) != 1 { + return "Usage: find " + } + + clt := proxy.Find(args[0]) + if clt != nil { + return fmt.Sprintf("%s is connected to %s", clt.Name(), clt.ServerName()) + } + + return "Player not connected." + }, + }) + proxy.RegisterChatCmd(proxy.ChatCmd{ + Name: "addr", + Perm: "cmd_addr", + Handler: func(cc *proxy.ClientConn, args ...string) string { + if len(args) != 1 { + return "Usage: addr " + } + + clt := proxy.Find(args[0]) + if clt != nil { + return fmt.Sprintf("%s is at %s", clt.Name(), clt.RemoteAddr()) + } + + return "Player not connected." + }, + }) + proxy.RegisterChatCmd(proxy.ChatCmd{ + Name: "alert", + Perm: "cmd_alert", + Handler: func(cc *proxy.ClientConn, args ...string) string { + msg := strings.Join(args, " ") + for clt := range proxy.Clts() { + clt.SendChatMsg(msg) + } + + return "" + }, + }) + proxy.RegisterChatCmd(proxy.ChatCmd{ + Name: "send", + Perm: "cmd_send", + Handler: func(cc *proxy.ClientConn, args ...string) string { + if len(args) != 2 { + return "Usage: send [name]" + } + + var found bool + for _, srv := range proxy.Conf().Servers { + if srv.Name == args[1] { + found = true + break + } + } + + if !found { + return "Server not existent." + } + + switch args[0] { + case "player": + if args[1] == cc.ServerName() { + return "Player is already connected to this server." + } + + clt := proxy.Find(args[2]) + if clt == nil { + return "Player not connected." + } + + if err := clt.Hop(args[1]); err != nil { + clt.SendChatMsg("Could not switch servers. Reconnect if you encounter any problems. Error:", err.Error()) + } + case "current": + if args[1] == cc.ServerName() { + return "Start and destination are identical." + } + + for clt := range proxy.Clts() { + if clt.ServerName() == cc.ServerName() && clt.ServerName() != args[1] { + if err := clt.Hop(args[1]); err != nil { + clt.SendChatMsg("Could not switch servers. Reconnect if you encounter any problems. Error:", err.Error()) + } + } + } + case "all": + for clt := range proxy.Clts() { + if clt.ServerName() != args[1] { + if err := clt.Hop(args[1]); err != nil { + clt.SendChatMsg("Could not switch servers. Reconnect if you encounter any problems. Error:", err.Error()) + } + } + } + default: + return "Usage: send [name]" + } + + return "" + }, + }) + proxy.RegisterChatCmd(proxy.ChatCmd{ + Name: "players", + Perm: "cmd_players", + Handler: func(cc *proxy.ClientConn, args ...string) string { + srvs := make(map[string][]*proxy.ClientConn) + for clt := range proxy.Clts() { + srv := clt.ServerName() + srvs[srv] = append(srvs[srv], clt) + } + + b := &strings.Builder{} + for srv, clts := range srvs { + b.WriteString(srv + ":\n") + for _, clt := range clts { + b.WriteString("- " + clt.Name() + "\n") + } + } + + return strings.Trim(b.String(), "\n") + }, + }) proxy.RegisterChatCmd(proxy.ChatCmd{ Name: "reload", Perm: "cmd_reload", -- cgit v1.2.3