Skip to content

Commit

Permalink
base commands
Browse files Browse the repository at this point in the history
  • Loading branch information
oq-x committed Sep 26, 2023
1 parent 69bb602 commit 0be9890
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 32 deletions.
9 changes: 5 additions & 4 deletions server/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
27 changes: 24 additions & 3 deletions server/network/handlers/ChatCommand.go
Original file line number Diff line number Diff line change
@@ -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)
}
13 changes: 4 additions & 9 deletions server/permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand Down
2 changes: 2 additions & 0 deletions server/player/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ type Player struct {
viewDistance int32
simulationDistance int32

Operator bool

X, Y, Z float64
Yaw, Pitch float32
OnGround bool
Expand Down
17 changes: 13 additions & 4 deletions server/player_controller.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package server

import (
"slices"

Check failure on line 4 in server/player_controller.go

View workflow job for this annotation

GitHub Actions / build (1.20.x, ubuntu-latest)

package slices is not in GOROOT (/opt/hostedtoolcache/go/1.20.8/x64/src/slices)

Check failure on line 4 in server/player_controller.go

View workflow job for this annotation

GitHub Actions / build (1.20.x, macos-latest)

package slices is not in GOROOT (/Users/runner/hostedtoolcache/go/1.20.8/x64/src/slices)

"github.com/aimjel/minecraft/packet"
"github.com/dynamitemc/dynamite/server/commands"
"github.com/dynamitemc/dynamite/server/player"
"github.com/dynamitemc/dynamite/server/world"
)

type PlayerController struct {
player *player.Player
session *Session
Server *Server

UUID string
}
Expand All @@ -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})
}
Expand All @@ -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())
}
41 changes: 33 additions & 8 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package server

import (
"encoding/hex"
"errors"
"os"
"sync"

"github.com/dynamitemc/dynamite/util"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions server/server_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions server/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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:
Expand Down

0 comments on commit 0be9890

Please sign in to comment.