Hilbish/golibs/commander/commander.go

40 lines
697 B
Go
Raw Normal View History

package commander
import (
"github.com/chuckpreslar/emission"
"github.com/yuin/gopher-lua"
)
type Commander struct{
Events *emission.Emitter
}
func New() Commander {
return Commander{
Events: emission.NewEmitter(),
}
}
func (c *Commander) Loader(L *lua.LState) int {
var exports = map[string]lua.LGFunction{
"register": c.register,
}
mod := L.SetFuncs(L.NewTable(), exports)
L.SetGlobal("commanding", &lua.LTable{})
L.SetField(L.GetGlobal("commanding"), "__commands", L.NewTable())
L.Push(mod)
return 1
}
func (c *Commander) register(L *lua.LState) int {
cmdName := L.CheckString(1)
cmd := L.CheckFunction(2)
c.Events.Emit("commandRegister", cmdName, cmd)
return 0
}