Skip to content

Commit

Permalink
New command argument builders
Browse files Browse the repository at this point in the history
  • Loading branch information
oq-x committed Sep 27, 2023
1 parent c754bfb commit 796771d
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 38 deletions.
13 changes: 2 additions & 11 deletions core_commands/gamemode.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,8 @@ var gamemode_cmd = &commands.Command{
Name: "gamemode",
RequiredPermissions: []string{"server.command.gamemode"},
Arguments: []commands.Argument{
{
Name: "mode",
ParserID: 39,
},
{
Name: "player",
ParserID: 6,
Properties: commands.Properties{
Flags: 0x02,
},
},
commands.NewGamemodeArgument("mode"),
commands.NewEntityArgument("player", commands.EntityPlayerOnly),
},
Execute: func(ctx commands.CommandContext) {
if len(ctx.Arguments) == 0 {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (

require (
github.com/Shopify/go-lua v0.0.0-20221004153744-91867de107cf
github.com/aimjel/minecraft v0.0.0-20230927182639-1fa3dd7aeebc
github.com/aimjel/minecraft v0.0.0-20230927222750-0a432da348ec
github.com/dop251/goja v0.0.0-20230919151941-fc55792775de
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ github.com/aimjel/minecraft v0.0.0-20230927163915-b3907034c0a8 h1:Vl58C0lpKTDgSZ
github.com/aimjel/minecraft v0.0.0-20230927163915-b3907034c0a8/go.mod h1:/Y9/YBqxNOU7IFInEjDbsCVkdPNYJCPxC26jK+w3Phc=
github.com/aimjel/minecraft v0.0.0-20230927182639-1fa3dd7aeebc h1:irjh4/oH9vWvkOr8xyQGudFeOYVpuzy9dgDnTkevtvg=
github.com/aimjel/minecraft v0.0.0-20230927182639-1fa3dd7aeebc/go.mod h1:/Y9/YBqxNOU7IFInEjDbsCVkdPNYJCPxC26jK+w3Phc=
github.com/aimjel/minecraft v0.0.0-20230927222750-0a432da348ec h1:aFUC1sZUBSlOtIniOxDWN2orepj9/dSxSAdqVtdVNcE=
github.com/aimjel/minecraft v0.0.0-20230927222750-0a432da348ec/go.mod h1:/Y9/YBqxNOU7IFInEjDbsCVkdPNYJCPxC26jK+w3Phc=
github.com/chzyer/logex v1.2.0/go.mod h1:9+9sk7u7pGNWYMkh0hdiL++6OeibzJccyQU4p4MedaY=
github.com/chzyer/readline v1.5.0/go.mod h1:x22KAscuvRqlLoK9CsoYsmxoXZMMFVyOl86cAH8qUic=
github.com/chzyer/test v0.0.0-20210722231415-061457976a23/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
Expand Down
73 changes: 53 additions & 20 deletions server/commands/builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ const (
StringGreedyPhrase
)

func NewCommand(name string, execute func(ctx CommandContext), arguments ...Argument) *Command {
return &Command{Name: name, Execute: execute, Arguments: arguments}
}
const (
EntitySingle = iota + 1
EntityPlayerOnly
)

func NewBoolArgument(name string) Argument {
return Argument{
Name: name,
ParserID: 0,
Name: name,
Parser: Parser{
ID: 0,
},
}
}

Expand All @@ -31,9 +34,11 @@ func NewFloatArgument(name string, properties struct {
props.Max = *properties.Max
}
return Argument{
Name: name,
ParserID: 1,
Properties: props,
Name: name,
Parser: Parser{
ID: 1,
Properties: props,
},
}
}

Expand All @@ -51,9 +56,11 @@ func NewDoubleArgument(name string, properties struct {
props.Max = *properties.Max
}
return Argument{
Name: name,
ParserID: 2,
Properties: props,
Name: name,
Parser: Parser{
ID: 2,
Properties: props,
},
}
}

Expand All @@ -71,9 +78,11 @@ func NewIntegerArgument(name string, properties struct {
props.Max = uint64(*properties.Max)
}
return Argument{
Name: name,
ParserID: 3,
Properties: props,
Name: name,
Parser: Parser{
ID: 3,
Properties: props,
},
}
}

Expand All @@ -91,17 +100,41 @@ func NewLongArgument(name string, properties struct {
props.Max = uint64(*properties.Max)
}
return Argument{
Name: name,
ParserID: 4,
Properties: props,
Name: name,
Parser: Parser{
ID: 4,
Properties: props,
},
}
}

func NewStringArgument(name string, properties byte) Argument {
props := Properties{Flags: properties}
return Argument{
Name: name,
ParserID: 5,
Properties: props,
Name: name,
Parser: Parser{
ID: 5,
Properties: props,
},
}
}

func NewEntityArgument(name string, properties byte) Argument {
props := Properties{Flags: properties}
return Argument{
Name: name,
Parser: Parser{
ID: 6,
Properties: props,
},
}
}

func NewGamemodeArgument(name string) Argument {
return Argument{
Name: name,
Parser: Parser{
ID: 39,
},
}
}
16 changes: 10 additions & 6 deletions server/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,20 @@ type Command struct {
}

type Properties struct {
Flags uint8 `json:",omitempty"`
Min, Max uint64 `json:",omitempty"`
Identifier string `json:",omitempty"`
Flags uint8
Min, Max uint64
Identifier string
}

type Parser struct {
ID int32 `js:"id"`
Properties Properties `js:"properties"`
}

type Argument struct {
Name string `js:"name"`
ParserID int32 `js:"parserId"`
SuggestionType string `js:"suggestionType"`
Properties Properties
Parser Parser `js:"parser"`
}

type Graph struct {
Expand Down Expand Up @@ -110,7 +114,7 @@ func (graph Graph) Data() *pk.DeclareCommands {
for _, argument := range command.Arguments {
parent := len(packet.Nodes) - 1
packet.Nodes[parent].Children = append(packet.Nodes[parent].Children, int32(len(packet.Nodes)))
node := pk.Node{Flags: 2, Name: argument.Name, Properties: argument.Properties, ParserID: argument.ParserID}
node := pk.Node{Flags: 2, Name: argument.Name, Properties: argument.Parser.Properties, ParserID: argument.Parser.ID}
if argument.SuggestionType != "" {
node.Flags |= 0x10
node.SuggestionsType = argument.SuggestionType
Expand Down

0 comments on commit 796771d

Please sign in to comment.