-
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add command system & CommandExecuteEvent
- Loading branch information
1 parent
993eb79
commit 2dac5bf
Showing
7 changed files
with
286 additions
and
51 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
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,70 @@ | ||
package proxy | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
. "go.minekube.com/common/minecraft/color" | ||
. "go.minekube.com/common/minecraft/component" | ||
"time" | ||
) | ||
|
||
type serverCmd struct{ proxy *Proxy } | ||
|
||
func (s *serverCmd) Invoke(c *Context) { | ||
if len(c.Args) == 0 { | ||
s.list(c) | ||
return | ||
} | ||
s.connect(c) | ||
} | ||
|
||
// switch server | ||
func (s *serverCmd) connect(c *Context) { | ||
player, ok := c.Source.(Player) | ||
if !ok { | ||
_ = c.Source.SendMessage(&Text{Content: "Only players can connect to a server!", S: Style{Color: Red}}) | ||
return | ||
} | ||
|
||
server := c.Args[0] | ||
rs := s.proxy.Server(server) | ||
if rs == nil { | ||
_ = c.Source.SendMessage(&Text{Content: fmt.Sprintf("Server %q not registered", server), S: Style{Color: Red}}) | ||
return | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*time.Duration(s.proxy.config.ConnectionTimeout)) | ||
defer cancel() | ||
player.CreateConnectionRequest(rs).ConnectWithIndication(ctx) | ||
} | ||
|
||
// list registered servers | ||
func (s *serverCmd) list(c *Context) { | ||
const maxEntries = 50 | ||
var servers []Component | ||
proxyServers := s.proxy.Servers() | ||
for i, s := range proxyServers { | ||
if i+1 == maxEntries { | ||
servers = append(servers, &Text{ | ||
Content: fmt.Sprintf("and %d more...", len(proxyServers)-i+1), | ||
}) | ||
break | ||
} | ||
servers = append(servers, &Text{ | ||
Content: fmt.Sprintf(" %s - %s (%d players)\n", | ||
s.ServerInfo().Name(), s.ServerInfo().Addr(), s.Players().Len()), | ||
S: Style{ClickEvent: RunCommand(fmt.Sprintf("/server %s", s.ServerInfo().Name()))}, | ||
}) | ||
} | ||
_ = c.Source.SendMessage(&Text{ | ||
Content: fmt.Sprintf("\nServers (%d):\n", len(proxyServers)), | ||
S: Style{Color: Green}, | ||
Extra: []Component{&Text{ | ||
S: Style{ | ||
Color: Yellow, | ||
HoverEvent: ShowText(&Text{Content: "Click to connect!", S: Style{Color: Green}}), | ||
}, | ||
Extra: servers, | ||
}}, | ||
}) | ||
} |
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,132 @@ | ||
package proxy | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
"sync" | ||
) | ||
|
||
type CommandManager struct { | ||
mu sync.RWMutex | ||
commands map[string]*registration | ||
} | ||
|
||
// newCommandManager returns a new CommandManager. | ||
func newCommandManager() *CommandManager { | ||
return &CommandManager{commands: map[string]*registration{}} | ||
} | ||
|
||
type registration struct { | ||
cmd Command | ||
aliases []string | ||
} | ||
|
||
// Register registers (and overrides) a command with the root literal name and optional aliases. | ||
func (m *CommandManager) Register(cmd Command, name string, aliases ...string) { | ||
if cmd == nil { | ||
return | ||
} | ||
r := ®istration{ | ||
cmd: cmd, | ||
aliases: append(aliases, name), | ||
} | ||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
m.commands[name] = r | ||
for _, name := range aliases { | ||
m.commands[name] = r | ||
} | ||
} | ||
|
||
// Unregister unregisters a command with its aliases. | ||
func (m *CommandManager) Unregister(name string) { | ||
m.mu.Lock() | ||
r, ok := m.commands[name] | ||
if ok { | ||
for _, name := range r.aliases { | ||
delete(m.commands, name) | ||
} | ||
} | ||
delete(m.commands, name) | ||
m.mu.Unlock() | ||
} | ||
|
||
// Has return true if the command is registered. | ||
func (m *CommandManager) Has(command string) bool { | ||
m.mu.RLock() | ||
_, ok := m.commands[command] | ||
m.mu.RUnlock() | ||
return ok | ||
} | ||
|
||
// Invoke invokes a registered command. | ||
func (m *CommandManager) Invoke(ctx *Context, command string) (found bool, err error) { | ||
if len(command) == 0 { | ||
return false, errors.New("command must not be empty") | ||
} | ||
if ctx == nil { | ||
return false, errors.New("ctx must not be nil") | ||
} | ||
if ctx.Source == nil { | ||
return false, errors.New("ctx source must not be nil") | ||
} | ||
if ctx.Context == nil { | ||
ctx.Context = context.Background() | ||
} | ||
m.mu.RLock() | ||
r, ok := m.commands[command] | ||
m.mu.RUnlock() | ||
if !ok { | ||
return false, nil | ||
} | ||
defer func() { | ||
if r := recover(); r != nil { | ||
err = fmt.Errorf("panic while invoking command: %v", r) | ||
} | ||
}() | ||
r.cmd.Invoke(ctx) | ||
return true, err | ||
} | ||
|
||
// Command is an invokable command. | ||
type Command interface { | ||
Invoke(*Context) | ||
} | ||
|
||
// Func is a shorthand type that implements the Command interface. | ||
type Func func(*Context) | ||
|
||
// Invoke implements Command. | ||
func (f Func) Invoke(c *Context) { | ||
f(c) | ||
} | ||
|
||
// Context is a command invocation context. | ||
type Context struct { | ||
context.Context | ||
Source CommandSource | ||
Args []string | ||
} | ||
|
||
var spaceRegex = regexp.MustCompile(`\s+`) | ||
|
||
// trimSpaces removes all spaces that are to much. | ||
func trimSpaces(s string) string { | ||
s = strings.TrimSpace(s) | ||
return spaceRegex.ReplaceAllString(s, " ") // remove to much spaces in between | ||
} | ||
|
||
func extract(commandline string) (command string, args []string, ok bool) { | ||
split := strings.Split(commandline, " ") | ||
if len(split) != 0 { | ||
command = split[0] | ||
ok = true | ||
} | ||
if len(split) > 1 { | ||
args = split[1:] | ||
} | ||
return | ||
} |
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
Oops, something went wrong.