Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions server/cmd/argument.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package cmd

import (
"fmt"
"github.com/df-mc/dragonfly/server/internal/sliceutil"
"github.com/df-mc/dragonfly/server/world"
"github.com/go-gl/mathgl/mgl64"
"math/rand/v2"
"reflect"
"slices"
"sort"
"strconv"
"strings"

"github.com/df-mc/dragonfly/server/internal/sliceutil"
"github.com/df-mc/dragonfly/server/world"
"github.com/go-gl/mathgl/mgl64"
)

// Line represents a command line holding command arguments that were passed upon the execution of the
Expand Down Expand Up @@ -46,7 +47,12 @@ func (line *Line) Next() (string, bool) {
if !ok {
return "", false
}
return v[0], true
val := v[0]
if val == "" {
line.RemoveNext()
return line.Next()
}
return val, true
Comment on lines +50 to +55
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ignoring the empty arg should probably be done in NextN for more consistent behavior? It's a bit odd that one would skip over empty args and the other wouldn't

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would go a step further and do the same for RemoveN

}

// NextN reads the next N arguments from the command line and returns them. If there were not enough arguments
Expand Down Expand Up @@ -83,13 +89,20 @@ func (line *Line) Leftover() []string {

// Len returns the leftover length of the arguments in the command line.
func (line *Line) Len() int {
return len(line.args)
count := 0
for _, arg := range line.args {
if arg != "" {
count++
}
}
return count
}

// parser manages the parsing of a Line, turning the raw arguments into values which are then stored in the
// struct fields.
type parser struct {
currentField string
fields int
}

// parseArgument parses the next argument from the command line passed and sets it to value v passed. If
Expand Down Expand Up @@ -185,6 +198,10 @@ func (p parser) float(line *Line, v reflect.Value) error {

// string ...
func (p parser) string(line *Line, v reflect.Value) error {
if p.fields == 1 {
return p.restAsString(line, v)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this what the Varargs parameter type is for?

}

arg, ok := line.Next()
if !ok {
return line.UsageError()
Expand All @@ -193,6 +210,18 @@ func (p parser) string(line *Line, v reflect.Value) error {
return nil
}

// restAsString ...
func (p parser) restAsString(line *Line, v reflect.Value) error {
args := line.Leftover()
val := strings.Join(args, " ")
// check if value is empty.
if strings.TrimSpace(val) == "" {
return line.UsageError()
}
v.SetString(val)
return nil
}

// bool ...
func (p parser) bool(line *Line, v reflect.Value) error {
arg, ok := line.Next()
Expand Down
7 changes: 5 additions & 2 deletions server/cmd/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package cmd
import (
"encoding/csv"
"fmt"
"github.com/df-mc/dragonfly/server/world"
"go/ast"
"reflect"
"slices"
"strings"

"github.com/df-mc/dragonfly/server/world"
)

// Runnable represents a Command that may be run by a Command source. The Command must be a struct type and
Expand Down Expand Up @@ -246,9 +247,11 @@ func (cmd Command) executeRunnable(v reflect.Value, args string, source Source,
// We iterate over all the fields of the struct: Each of the fields will have an argument parsed to
// produce its value.
signature := v.Elem()
for _, t := range exportedFields(signature) {
fields := exportedFields(signature)
for i, t := range fields {
field := signature.FieldByName(t.Name)
parser.currentField = t.Name
parser.fields = len(fields) - i
opt := optional(field)

val := field
Expand Down
26 changes: 19 additions & 7 deletions server/player/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package player

import (
"fmt"
"github.com/df-mc/dragonfly/server/player/debug"
"github.com/df-mc/dragonfly/server/player/hud"
"math"
"math/rand/v2"
"net"
Expand All @@ -12,6 +10,9 @@ import (
"sync"
"time"

"github.com/df-mc/dragonfly/server/player/debug"
"github.com/df-mc/dragonfly/server/player/hud"

"github.com/df-mc/dragonfly/server/block"
"github.com/df-mc/dragonfly/server/block/cube"
"github.com/df-mc/dragonfly/server/block/model"
Expand Down Expand Up @@ -332,13 +333,24 @@ func (p *Player) ExecuteCommand(commandLine string) {
if p.Dead() {
return
}
args := strings.Split(commandLine, " ")

name, ok := strings.CutPrefix(args[0], "/")
commandLine, ok := strings.CutPrefix(commandLine, "/")
if !ok {
return
}

args := strings.Split(commandLine, " ")

var (
name string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to leave a comment here explaining this is to allow something like / command to work

index int
)
for index, name = range args {
if name != "" {
break
}
}
index++

command, ok := cmd.ByAlias(name)
if !ok {
o := &cmd.Output{}
Expand All @@ -347,10 +359,10 @@ func (p *Player) ExecuteCommand(commandLine string) {
return
}
ctx := event.C(p)
if p.Handler().HandleCommandExecution(ctx, command, args[1:]); ctx.Cancelled() {
if p.Handler().HandleCommandExecution(ctx, command, args[index:]); ctx.Cancelled() {
return
}
command.Execute(strings.Join(args[1:], " "), p, p.tx)
command.Execute(strings.Join(args[index:], " "), p, p.tx)
}

// Transfer transfers the player to a server at the address passed. If the address could not be resolved, an
Expand Down