Skip to content

Commit

Permalink
cleaned up command arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
aimjel committed Oct 18, 2023
1 parent 341c9e7 commit 778c530
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 67 deletions.
2 changes: 1 addition & 1 deletion core_commands/ban.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var ban_cmd = &commands.Command{
RequiredPermissions: []string{"server.command.ban"},
Arguments: []commands.Argument{
commands.NewEntityArgument("player", commands.EntityPlayerOnly),
commands.NewStringArgument("reason", commands.StringGreedyPhrase),
commands.NewStrArg("reason", commands.GreedyPhrase),
},
Execute: func(ctx commands.CommandContext) {
if len(ctx.Arguments) == 0 {
Expand Down
11 changes: 1 addition & 10 deletions core_commands/banlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,11 @@ import (
"github.com/dynamitemc/dynamite/server/commands"
)

func point[T any](t T) *T {
return &t
}

var banlist_cmd = &commands.Command{
Name: "banlist",
RequiredPermissions: []string{"server.command.banlist"},
Arguments: []commands.Argument{
commands.NewIntegerArgument("page", struct {
Min *int64
Max *int64
}{
Min: point[int64](1),
}),
commands.NewIntArg("page").Min(0),
},
Execute: func(ctx commands.CommandContext) {
server := getServer(ctx.Executor)
Expand Down
2 changes: 1 addition & 1 deletion core_commands/gamerule.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var gamerule_cmd = &commands.Command{
Name: "gamerule",
RequiredPermissions: []string{"server.command.gamerule"},
Arguments: []commands.Argument{
commands.NewStringArgument("rule", commands.StringSingleWord).
commands.NewStrArg("rule", commands.SingleWord).
SetSuggest(func(ctx commands.SuggestionsContext) {
srv := ctx.Executor.(*server.PlayerController).Server
var matches []packet.SuggestionMatch
Expand Down
7 changes: 2 additions & 5 deletions core_commands/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@ import (
"math"
)

var m, ma uint64 = math.Float64bits(1), math.Float64bits(2)
var low, high uint64 = math.Float64bits(1), math.Float64bits(2)
var test_cmd = &commands.Command{
Name: "test",
Arguments: []commands.Argument{
commands.NewFloatArgument("wqeqwf", struct {
Min *uint64
Max *uint64
}{Min: &m, Max: &ma}),
commands.NewFloatArg("wqeqwf").MinMax(low, high),
},
Execute: func(ctx commands.CommandContext) {
ctx.Reply("hi")
Expand Down
2 changes: 1 addition & 1 deletion core_commands/unban.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var unban_cmd = &commands.Command{
Aliases: []string{"pardon"},
RequiredPermissions: []string{"server.command.unban"},
Arguments: []commands.Argument{
commands.NewStringArgument("player", commands.StringSingleWord),
commands.NewStrArg("player", commands.SingleWord),
},
Execute: func(ctx commands.CommandContext) {
if len(ctx.Arguments) == 0 {
Expand Down
69 changes: 20 additions & 49 deletions server/commands/builders.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,22 @@
package commands

import "github.com/aimjel/minecraft/protocol/types"
import (
"github.com/aimjel/minecraft/protocol/types"
)

type stringArgType byte

const (
StringSingleWord = iota
StringQuotablePhrase
StringGreedyPhrase
SingleWord stringArgType = iota
QuotablePhrase
GreedyPhrase
)

const (
EntitySingle = iota + 1
EntityPlayerOnly
)

type argumentType int

const (
Bool argumentType = iota
Float
Double
Integer
Long
String
)

func NewArgument(name string, t argumentType) Argument {
return Argument{
Name: name,
Parser: Parser{ID: int32(t)},
}
}

func (a Argument) Min(min uint64) Argument {
//todo add checks, dont allow arguments that arent numbers to access this
a.Parser.Properties.Flags |= 1
Expand All @@ -45,7 +31,14 @@ func (a Argument) Max(max uint64) Argument {
return a
}

func NewBoolArgument(name string) Argument {
func (a Argument) MinMax(min, max uint64) Argument {
a.Parser.Properties.Flags |= 0x03
a.Parser.Properties.Min = min
a.Parser.Properties.Max = max
return a
}

func NewBoolArg(name string) Argument {
return Argument{
Name: name,
Parser: Parser{
Expand All @@ -54,19 +47,8 @@ func NewBoolArgument(name string) Argument {
}
}

func NewFloatArgument(name string, properties struct {
Min *uint64
Max *uint64
}) Argument {
func NewFloatArg(name string) Argument {
props := types.CommandProperties{Flags: 0}
if properties.Min != nil {
props.Flags |= 1
props.Min = *properties.Min
}
if properties.Max != nil {
props.Flags |= 2
props.Max = *properties.Max
}
return Argument{
Name: name,
Parser: Parser{
Expand All @@ -76,19 +58,8 @@ func NewFloatArgument(name string, properties struct {
}
}

func NewIntegerArgument(name string, properties struct {
Min *int64
Max *int64
}) Argument {
func NewIntArg(name string) Argument {
props := types.CommandProperties{Flags: 0}
if properties.Min != nil {
props.Flags |= 1
props.Min = uint64(*properties.Min)
}
if properties.Max != nil {
props.Flags |= 2
props.Max = uint64(*properties.Max)
}
return Argument{
Name: name,
Parser: Parser{
Expand All @@ -103,8 +74,8 @@ func (a Argument) SetSuggest(s func(ctx SuggestionsContext)) Argument {
return a
}

func NewStringArgument(name string, properties byte) Argument {
props := types.CommandProperties{Flags: properties}
func NewStrArg(name string, properties stringArgType) Argument {
props := types.CommandProperties{Flags: uint8(properties)}
return Argument{
Name: name,
Parser: Parser{
Expand Down
1 change: 1 addition & 0 deletions server/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type SuggestionsContext struct {
}

func (c *SuggestionsContext) Return(suggestions []pk.SuggestionMatch) {

if p, ok := c.Executor.(interface {
SendCommandSuggestionsResponse(id int32, start int32, length int32, matches []pk.SuggestionMatch)
}); ok {
Expand Down

0 comments on commit 778c530

Please sign in to comment.