-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
151 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,5 +33,6 @@ var Commands = &commands.Graph{ | |
stop_cmd, | ||
msg_cmd, | ||
summon_cmd, | ||
test_cmd, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters