2020-07-10 01:01:29 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
lua "github.com/yuin/gopher-lua"
|
|
|
|
"meow.tf/residentsleeper/commands"
|
2021-06-12 05:28:34 +00:00
|
|
|
"meow.tf/residentsleeper/scripting/eventloop"
|
2020-07-10 01:01:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Loader(L *lua.LState) int {
|
|
|
|
mod := L.SetFuncs(L.NewTable(), exports)
|
|
|
|
L.Push(mod)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
var exports = map[string]lua.LGFunction{
|
|
|
|
"register": commandRegister,
|
|
|
|
}
|
|
|
|
|
|
|
|
func commandRegister(L *lua.LState) int {
|
|
|
|
name := L.CheckString(1)
|
|
|
|
|
|
|
|
handler := L.CheckFunction(2)
|
|
|
|
|
2021-06-12 05:28:34 +00:00
|
|
|
loop := eventloop.FromState(L)
|
|
|
|
|
2020-07-10 01:01:29 +00:00
|
|
|
cb := func(ctx *commands.CommandContext) {
|
2021-06-12 05:28:34 +00:00
|
|
|
loop.RunOnLoop(func(L *lua.LState) {
|
|
|
|
L.Push(handler)
|
|
|
|
L.Push(lua.LString(ctx.User))
|
2020-07-10 01:01:29 +00:00
|
|
|
|
2021-06-12 05:28:34 +00:00
|
|
|
for _, v := range ctx.Arguments {
|
|
|
|
L.Push(lua.LString(v))
|
|
|
|
}
|
2020-07-10 01:01:29 +00:00
|
|
|
|
2021-06-12 05:28:34 +00:00
|
|
|
L.PCall(1+ctx.ArgumentCount, 0, nil)
|
|
|
|
})
|
2020-07-10 01:01:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
commands.Register(name, cb)
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|