From 778c530d0449d17a93e587ca5a01df3d8ba5eaef Mon Sep 17 00:00:00 2001 From: Angel Date: Tue, 17 Oct 2023 21:50:33 -0400 Subject: [PATCH] cleaned up command arguments --- core_commands/ban.go | 2 +- core_commands/banlist.go | 11 +----- core_commands/gamerule.go | 2 +- core_commands/test.go | 7 ++-- core_commands/unban.go | 2 +- server/commands/builders.go | 69 +++++++++++-------------------------- server/commands/commands.go | 1 + 7 files changed, 27 insertions(+), 67 deletions(-) diff --git a/core_commands/ban.go b/core_commands/ban.go index c7522db..d8bbc6a 100644 --- a/core_commands/ban.go +++ b/core_commands/ban.go @@ -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 { diff --git a/core_commands/banlist.go b/core_commands/banlist.go index e7b5da8..a3f2138 100644 --- a/core_commands/banlist.go +++ b/core_commands/banlist.go @@ -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) diff --git a/core_commands/gamerule.go b/core_commands/gamerule.go index 78d7637..ec820f0 100644 --- a/core_commands/gamerule.go +++ b/core_commands/gamerule.go @@ -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 diff --git a/core_commands/test.go b/core_commands/test.go index 43edfb9..8a12044 100644 --- a/core_commands/test.go +++ b/core_commands/test.go @@ -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") diff --git a/core_commands/unban.go b/core_commands/unban.go index cf2b044..7b52123 100644 --- a/core_commands/unban.go +++ b/core_commands/unban.go @@ -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 { diff --git a/server/commands/builders.go b/server/commands/builders.go index 0ec9db9..c3454f7 100644 --- a/server/commands/builders.go +++ b/server/commands/builders.go @@ -1,11 +1,15 @@ 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 ( @@ -13,24 +17,6 @@ const ( 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 @@ -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{ @@ -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{ @@ -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{ @@ -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{ diff --git a/server/commands/commands.go b/server/commands/commands.go index 9824469..a61ddda 100644 --- a/server/commands/commands.go +++ b/server/commands/commands.go @@ -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 {