Skip to content

Commit

Permalink
new command argument api
Browse files Browse the repository at this point in the history
  • Loading branch information
oq-x committed Nov 5, 2023
1 parent 696bc5b commit 04c0af8
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 43 deletions.
1 change: 1 addition & 0 deletions core_commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ var Commands = &commands.Graph{
stop_cmd,
msg_cmd,
summon_cmd,
test_cmd,
},
}
25 changes: 25 additions & 0 deletions core_commands/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package core_commands

import (
"fmt"

"github.com/aimjel/minecraft/chat"
"github.com/dynamitemc/dynamite/server/commands"
)

var test_cmd = &commands.Command{
Name: "test",
RequiredPermissions: []string{"server.command.test"},
Arguments: []commands.Argument{
commands.NewStrArg("d", commands.SingleWord),
commands.NewBoolArg("gay"),
commands.NewFloatArg("f"),
},
Execute: func(ctx commands.CommandContext) {
d, _ := ctx.GetString("d")
gay, _ := ctx.GetBool("gay")
f, _ := ctx.GetFloat64("f")

ctx.Reply(chat.NewMessage(fmt.Sprintf("%s %v %f", d, gay, f)))
},
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func scanConsole(srv *server.Server) {
continue
}
cmd.Execute(commands.CommandContext{
Command: cmd,
Executor: &server.ConsoleExecutor{Server: srv},
Arguments: args[1:],
FullCommand: command,
Expand Down
41 changes: 0 additions & 41 deletions server/commands/commands.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package commands

import (
"fmt"
"slices"
"strings"

"github.com/aimjel/minecraft/chat"
"github.com/aimjel/minecraft/protocol/types"

pk "github.com/aimjel/minecraft/packet"
"github.com/dynamitemc/dynamite/logger"
)

type SuggestionsContext struct {
Expand All @@ -36,25 +33,6 @@ func (c *SuggestionsContext) Return(suggestions []pk.SuggestionMatch) {
}
}

type CommandContext struct {
Executor interface{}
Arguments []string
Salt, Timestamp int64
ArgumentSignatures []pk.Argument
FullCommand string
}

func (ctx *CommandContext) Reply(message chat.Message) {
if p, ok := ctx.Executor.(interface {
SystemChatMessage(message chat.Message) error
}); ok {
p.SystemChatMessage(message)
} else {
fmt.Print(strings.ReplaceAll(logger.ParseChat(message), "\n", "\n\r"))
fmt.Print("\n\r> ")
}
}

func cond[T any](c bool, t T, f T) T {
if c {
return t
Expand All @@ -63,25 +41,6 @@ func cond[T any](c bool, t T, f T) T {
}
}

func (ctx *CommandContext) Incomplete() {
_, ok := ctx.Executor.(interface {
SystemChatMessage(message chat.Message) error
})
ctx.Reply(chat.NewMessage(fmt.Sprintf("§cUnknown or incomplete command, see below for error"+cond(ok, "", "\r")+"\n§7%s§r§c§o<--[HERE]", ctx.FullCommand)))
}

func (ctx *CommandContext) ErrorHere(msg string) {
_, ok := ctx.Executor.(interface {
SystemChatMessage(message chat.Message) error
})
sp := strings.Split(ctx.FullCommand, " ")
ctx.Reply(chat.NewMessage(fmt.Sprintf("§c%s\n"+cond(ok, "", "\r")+"§7%s §c§n%s§c§o<--[HERE]", msg, strings.Join(sp[:len(sp)-1], " "), sp[len(sp)-1])))
}

func (ctx *CommandContext) Error(msg string) {
ctx.Reply(chat.NewMessage("§c" + msg))
}

const (
ChatModeEnabled = iota
ChatModeCommandsOnly
Expand Down
118 changes: 118 additions & 0 deletions server/commands/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package commands

import (
"fmt"
"slices"
"strconv"
"strings"

"github.com/aimjel/minecraft/chat"
pk "github.com/aimjel/minecraft/packet"
"github.com/dynamitemc/dynamite/logger"
)

type CommandContext struct {
Command *Command
Executor interface{}
Arguments []string
Salt, Timestamp int64
ArgumentSignatures []pk.Argument
FullCommand string
}

func findArgument(a []Argument, n string) int {
for i, arg := range a {
if arg.Name == n {
return i
}
}
return -1
}

func (ctx CommandContext) GetString(name string) (value string, ok bool) {
for i, a := range ctx.Arguments {
arg := ctx.Command.Arguments[i]
if (arg.Parser.ID >= 8 && arg.Parser.ID <= 11) || arg.Parser.ID == 27 {
ctx.Arguments = slices.Delete(ctx.Arguments, i+1, i+3)
}
if arg.Name == name {
return a, true
}
}
return "", false
}

func (ctx CommandContext) GetInt32(name string) (value int32, ok bool) {
s, ok := ctx.GetString(name)
if !ok {
return 0, false
}
i, e := strconv.ParseInt(s, 10, 32)
return int32(i), e == nil
}

func (ctx CommandContext) GetInt64(name string) (value int64, ok bool) {
s, ok := ctx.GetString(name)
if !ok {
return 0, false
}
i, e := strconv.ParseInt(s, 10, 64)
return i, e == nil
}

func (ctx CommandContext) GetFloat32(name string) (value float32, ok bool) {
s, ok := ctx.GetString(name)
if !ok {
return 0, false
}
i, e := strconv.ParseFloat(s, 32)
return float32(i), e == nil
}

func (ctx CommandContext) GetFloat64(name string) (value float64, ok bool) {
s, ok := ctx.GetString(name)
if !ok {
return 0, false
}
i, e := strconv.ParseFloat(s, 64)
return i, e == nil
}

func (ctx CommandContext) GetBool(name string) (value bool, ok bool) {
s, ok := ctx.GetString(name)
if !ok {
return false, false
}
i, e := strconv.ParseBool(s)
return i, e == nil
}

func (ctx *CommandContext) Reply(message chat.Message) {
if p, ok := ctx.Executor.(interface {
SystemChatMessage(message chat.Message) error
}); ok {
p.SystemChatMessage(message)
} else {
fmt.Print(strings.ReplaceAll(logger.ParseChat(message), "\n", "\n\r"))
fmt.Print("\n\r> ")
}
}

func (ctx *CommandContext) Incomplete() {
_, ok := ctx.Executor.(interface {
SystemChatMessage(message chat.Message) error
})
ctx.Reply(chat.NewMessage(fmt.Sprintf("§cUnknown or incomplete command, see below for error"+cond(ok, "", "\r")+"\n§7%s§r§c§o<--[HERE]", ctx.FullCommand)))
}

func (ctx *CommandContext) ErrorHere(msg string) {
_, ok := ctx.Executor.(interface {
SystemChatMessage(message chat.Message) error
})
sp := strings.Split(ctx.FullCommand, " ")
ctx.Reply(chat.NewMessage(fmt.Sprintf("§c%s\n"+cond(ok, "", "\r")+"§7%s §c§n%s§c§o<--[HERE]", msg, strings.Join(sp[:len(sp)-1], " "), sp[len(sp)-1])))
}

func (ctx *CommandContext) Error(msg string) {
ctx.Reply(chat.NewMessage("§c" + msg))
}
2 changes: 2 additions & 0 deletions server/network/handlers/chat_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Controller interface {
UUID() string
Name() string
IP() string
ClearItem(slot int8)
}

func ChatCommandPacket(controller Controller, graph *commands.Graph, log *logger.Logger, content string, timestamp, salt int64, sigs []packet.Argument) {
Expand All @@ -58,6 +59,7 @@ func ChatCommandPacket(controller Controller, graph *commands.Graph, log *logger
return
}
ctx := commands.CommandContext{
Command: command,
Arguments: args[1:],
Executor: controller,
FullCommand: content,
Expand Down
2 changes: 1 addition & 1 deletion server/network/handlers/set_creative_mode_slot.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func SetCreativeModeSlot(controller Controller, state *player.Player, slot int16
if s, ok := state.Inventory().Slot(s); ok {
state.SetPreviousSelectedSlot(s)
}
state.Inventory().DeleteSlot(s)
controller.ClearItem(s)
} else {
i, _ := item.PacketSlotToItem(s, data)
state.Inventory().SetSlot(s, i)
Expand Down
4 changes: 3 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"bytes"
"errors"
"fmt"
"github.com/dynamitemc/dynamite/server/permission"
"os"
"strings"
"sync"
"sync/atomic"

"github.com/dynamitemc/dynamite/server/permission"

"github.com/aimjel/minecraft/chat"
"github.com/aimjel/minecraft/protocol/types"
"github.com/pelletier/go-toml/v2"
Expand Down Expand Up @@ -320,6 +321,7 @@ func (srv *Server) ConsoleCommand(txt string) {
return
}
command.Execute(commands.CommandContext{
Command: command,
Arguments: args[1:],
Executor: &ConsoleExecutor{Server: srv},
FullCommand: content,
Expand Down

0 comments on commit 04c0af8

Please sign in to comment.