From 0be98906f33ac410a31ec5e73c17de46959f0048 Mon Sep 17 00:00:00 2001 From: oqammx86 <84847714+oq-x@users.noreply.github.com> Date: Wed, 27 Sep 2023 01:28:45 +0300 Subject: [PATCH] base commands --- server/commands/commands.go | 9 +++--- server/network/handlers/ChatCommand.go | 27 +++++++++++++++-- server/permissions.go | 13 +++----- server/player/player.go | 2 ++ server/player_controller.go | 17 ++++++++--- server/server.go | 41 +++++++++++++++++++++----- server/server_config.go | 4 +-- server/session.go | 4 +-- 8 files changed, 85 insertions(+), 32 deletions(-) diff --git a/server/commands/commands.go b/server/commands/commands.go index f912787..65e3b8f 100644 --- a/server/commands/commands.go +++ b/server/commands/commands.go @@ -7,10 +7,11 @@ import ( type Executor interface{} type Command struct { - Name string - Arguments []Argument - Aliases []string - Execute func(Executor, []string) + Name string + Arguments []Argument + Aliases []string + Execute func(Executor, []string) + RequiredPermissions []string } type Properties struct { diff --git a/server/network/handlers/ChatCommand.go b/server/network/handlers/ChatCommand.go index 32b2083..91e2ad7 100644 --- a/server/network/handlers/ChatCommand.go +++ b/server/network/handlers/ChatCommand.go @@ -1,7 +1,28 @@ package handlers -import "fmt" +import ( + "strings" -func ChatCommandPacket(cmd string) { - fmt.Println("yeah!!!!!!!!!!!!!!!!!!") + "github.com/dynamitemc/dynamite/server/commands" +) + +func ChatCommandPacket(controller interface{}, graph commands.Graph, content string) { + args := strings.Split(content, " ") + cmd := args[0] + var command *commands.Command + for _, c := range graph.Commands { + if c.Name == cmd { + command = c + } + + for _, a := range c.Aliases { + if a == cmd { + command = c + } + } + } + if command == nil { + return + } + command.Execute(controller, args) } diff --git a/server/permissions.go b/server/permissions.go index 90bfcc6..a6149de 100644 --- a/server/permissions.go +++ b/server/permissions.go @@ -58,19 +58,14 @@ func getGroup(group string) GroupPermissions { return data } -func (srv *Server) HasPermissions(playerId string, perms []string) bool { - if playerId == "console" { +func (p *PlayerController) HasPermissions(perms []string) bool { + if len(perms) == 0 { return true } - if len(perms) == 0 { + if p.player.Operator { return true } - /*for i := 0; i < len(server.Players.OPs); i++ { - if server.Players.OPs[i].UUID == playerId { - return true - } - }*/ - permissionsPlayer := getPlayer(playerId) + permissionsPlayer := getPlayer(p.UUID) permissionsGroup := getGroup(permissionsPlayer.Group) for _, perm := range perms { if !permissionsPlayer.Permissions[perm] && !permissionsGroup.Permissions[perm] { diff --git a/server/player/player.go b/server/player/player.go index 6620d58..28bc0f2 100644 --- a/server/player/player.go +++ b/server/player/player.go @@ -8,6 +8,8 @@ type Player struct { viewDistance int32 simulationDistance int32 + Operator bool + X, Y, Z float64 Yaw, Pitch float32 OnGround bool diff --git a/server/player_controller.go b/server/player_controller.go index 7001ae6..fb6f7dc 100644 --- a/server/player_controller.go +++ b/server/player_controller.go @@ -1,7 +1,10 @@ package server import ( + "slices" + "github.com/aimjel/minecraft/packet" + "github.com/dynamitemc/dynamite/server/commands" "github.com/dynamitemc/dynamite/server/player" "github.com/dynamitemc/dynamite/server/world" ) @@ -9,6 +12,7 @@ import ( type PlayerController struct { player *player.Player session *Session + Server *Server UUID string } @@ -32,10 +36,6 @@ func (p *PlayerController) JoinDimension(d *world.Dimension) error { return p.session.SendPacket(&packet.SetDefaultSpawnPosition{}) } -func (p *PlayerController) SendAvailableCommands(commands *packet.DeclareCommands) error { - return p.session.SendPacket(commands) -} - func (p *PlayerController) SystemChatMessage(s string) error { return p.session.SendPacket(&packet.SystemChatMessage{Content: s}) } @@ -51,3 +51,12 @@ func (p *PlayerController) Rotation() (yaw float32, pitch float32) { func (p *PlayerController) OnGround() bool { return p.player.OnGround } + +func (p *PlayerController) SendCommands(graph commands.Graph) { + for i, command := range graph.Commands { + if !p.HasPermissions(command.RequiredPermissions) { + graph.Commands = slices.Delete(graph.Commands, i, i+1) + } + } + p.session.SendPacket(graph.Data()) +} diff --git a/server/server.go b/server/server.go index e74ef5d..de41229 100644 --- a/server/server.go +++ b/server/server.go @@ -2,6 +2,8 @@ package server import ( "encoding/hex" + "errors" + "os" "sync" "github.com/dynamitemc/dynamite/util" @@ -53,23 +55,26 @@ func (srv *Server) handleNewConn(conn *minecraft.Conn) { plyr := player.New() sesh := New(conn, plyr) - cntrl := &PlayerController{player: plyr, session: sesh} + cntrl := &PlayerController{player: plyr, session: sesh, Server: srv} cntrl.UUID = util.AddDashesToUUID(hex.EncodeToString(conn.Info.UUID[:])) - if err := cntrl.JoinDimension(srv.world.DefaultDimension()); err != nil { - //TODO log error - conn.Close(err) - panic(err) - return + + for _, op := range srv.Operators { + if op.UUID == cntrl.UUID { + plyr.Operator = true + } } - if err := cntrl.SendAvailableCommands(srv.CommandGraph.Data()); err != nil { + cntrl.SendCommands(srv.CommandGraph) + + if err := cntrl.JoinDimension(srv.world.DefaultDimension()); err != nil { //TODO log error conn.Close(err) + panic(err) return } srv.addPlayer(cntrl) - if err := sesh.HandlePackets(); err != nil { + if err := sesh.HandlePackets(cntrl); err != nil { u := cntrl.UUID srv.Logger.Info("[%s] Player %s (%s) has left the server", conn.RemoteAddr().String(), conn.Info.Name, u) @@ -105,3 +110,23 @@ func (srv *Server) GetCommand(name string) func(commands.Executor, []string) { return cmd } + +func (srv *Server) Reload() error { + // load player data + var files = []string{"whitelist.json", "banned_players.json", "ops.json", "banned_ips.json"} + var addresses = []*[]user{&srv.WhitelistedPlayers, &srv.BannedPlayers, &srv.Operators, &srv.BannedIPs} + for i, file := range files { + u, err := loadUsers(file) + if err != nil && !errors.Is(err, os.ErrNotExist) { + return err + } + + *addresses[i] = u + } + srv.mu.RLock() + defer srv.mu.RUnlock() + for _, p := range srv.Players { + p.SendCommands(srv.CommandGraph) + } + return nil +} diff --git a/server/server_config.go b/server/server_config.go index 32f3144..f1a613f 100644 --- a/server/server_config.go +++ b/server/server_config.go @@ -105,14 +105,14 @@ func (cfg *ServerConfig) Listen(address string, logger logger.Logger, commandGra } var files = []string{"whitelist.json", "banned_players.json", "ops.json", "banned_ips.json"} - var addresses = [][]user{srv.WhitelistedPlayers, srv.BannedPlayers, srv.Operators, srv.BannedIPs} + var addresses = []*[]user{&srv.WhitelistedPlayers, &srv.BannedPlayers, &srv.Operators, &srv.BannedIPs} for i, file := range files { u, err := loadUsers(file) if err != nil && !errors.Is(err, os.ErrNotExist) { return nil, err } - addresses[i] = u + *addresses[i] = u } logger.Debug("Loaded player info") diff --git a/server/session.go b/server/session.go index 358afa6..da5f30a 100644 --- a/server/session.go +++ b/server/session.go @@ -20,7 +20,7 @@ func New(c *minecraft.Conn, s *player.Player) *Session { return &Session{conn: c, state: s} } -func (s *Session) HandlePackets() error { +func (s *Session) HandlePackets(controller *PlayerController) error { for { p, err := s.conn.ReadPacket() if err != nil { @@ -31,7 +31,7 @@ func (s *Session) HandlePackets() error { case *packet.ChatMessageServer: handlers.ChatMessagePacket(pk.Message) case *packet.ChatCommandServer: - handlers.ChatCommandPacket(pk.Command) + handlers.ChatCommandPacket(controller, controller.Server.CommandGraph, pk.Command) } switch p.ID() { case 0x14, 0x15, 0x16, 0x17: