Major updates/patches, functionality, api
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
37
scripting/commands/commands.go
Normal file
37
scripting/commands/commands.go
Normal file
@ -0,0 +1,37 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
"meow.tf/residentsleeper/commands"
|
||||
)
|
||||
|
||||
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)
|
||||
|
||||
cb := func(ctx *commands.CommandContext) {
|
||||
L.Push(handler)
|
||||
L.Push(lua.LString(ctx.User))
|
||||
|
||||
for _, v := range ctx.Arguments {
|
||||
L.Push(lua.LString(v))
|
||||
}
|
||||
|
||||
L.PCall(1+ctx.ArgumentCount, 0, nil)
|
||||
}
|
||||
|
||||
commands.Register(name, cb)
|
||||
|
||||
return 0
|
||||
}
|
96
scripting/config/config.go
Normal file
96
scripting/config/config.go
Normal file
@ -0,0 +1,96 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"github.com/yuin/gopher-lua"
|
||||
"io/ioutil"
|
||||
luajson "layeh.com/gopher-json"
|
||||
"os"
|
||||
"path"
|
||||
)
|
||||
|
||||
var (
|
||||
BasePath string
|
||||
)
|
||||
|
||||
func Loader(L *lua.LState) int {
|
||||
// register functions to the table
|
||||
mod := L.SetFuncs(L.NewTable(), exports)
|
||||
|
||||
// returns the module
|
||||
L.Push(mod)
|
||||
return 1
|
||||
}
|
||||
|
||||
var exports = map[string]lua.LGFunction{
|
||||
"load": loadFunc,
|
||||
"save": saveFunc,
|
||||
}
|
||||
|
||||
func loadFunc(L *lua.LState) int {
|
||||
name := L.CheckString(1)
|
||||
|
||||
f, err := os.Open(path.Join(BasePath, name+".json"))
|
||||
|
||||
if err != nil {
|
||||
L.Push(lua.LNil)
|
||||
L.Push(lua.LString(err.Error()))
|
||||
return 2
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
|
||||
b, err := ioutil.ReadAll(f)
|
||||
|
||||
if err != nil {
|
||||
L.Push(lua.LNil)
|
||||
L.Push(lua.LString(err.Error()))
|
||||
return 2
|
||||
}
|
||||
|
||||
v, err := luajson.Decode(L, b)
|
||||
|
||||
if err != nil {
|
||||
L.Push(lua.LNil)
|
||||
L.Push(lua.LString(err.Error()))
|
||||
return 2
|
||||
}
|
||||
|
||||
L.Push(v)
|
||||
L.Push(lua.LNil)
|
||||
return 2
|
||||
}
|
||||
|
||||
func saveFunc(L *lua.LState) int {
|
||||
name := L.CheckString(1)
|
||||
value := L.CheckAny(2)
|
||||
|
||||
f, err := os.Create(path.Join(BasePath, name+".json"))
|
||||
|
||||
if err != nil {
|
||||
L.Push(lua.LString(err.Error()))
|
||||
return 1
|
||||
}
|
||||
|
||||
b, err := luajson.Encode(value)
|
||||
|
||||
if err != nil {
|
||||
L.Push(lua.LString(err.Error()))
|
||||
return 1
|
||||
}
|
||||
|
||||
_, err = f.Write(b)
|
||||
|
||||
if err != nil {
|
||||
L.Push(lua.LString(err.Error()))
|
||||
return 1
|
||||
}
|
||||
|
||||
err = f.Close()
|
||||
|
||||
if err != nil {
|
||||
L.Push(lua.LString(err.Error()))
|
||||
return 1
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
37
scripting/event/event.go
Normal file
37
scripting/event/event.go
Normal file
@ -0,0 +1,37 @@
|
||||
package event
|
||||
|
||||
import (
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
luar "layeh.com/gopher-luar"
|
||||
"meow.tf/residentsleeper/events"
|
||||
)
|
||||
|
||||
func Loader(L *lua.LState) int {
|
||||
// register functions to the table
|
||||
mod := L.SetFuncs(L.NewTable(), exports)
|
||||
|
||||
// returns the module
|
||||
L.Push(mod)
|
||||
return 1
|
||||
}
|
||||
|
||||
var exports = map[string]lua.LGFunction{
|
||||
"on": onFunc,
|
||||
}
|
||||
|
||||
func onFunc(L *lua.LState) int {
|
||||
name := L.CheckString(1)
|
||||
handler := L.CheckFunction(2)
|
||||
|
||||
events.On(name, func(args ...interface{}) {
|
||||
L.Push(handler)
|
||||
|
||||
for _, arg := range args {
|
||||
L.Push(luar.New(L, arg))
|
||||
}
|
||||
|
||||
L.PCall(len(args), 0, handler)
|
||||
})
|
||||
|
||||
return 0
|
||||
}
|
33
scripting/minecraft/nbt.go
Normal file
33
scripting/minecraft/nbt.go
Normal file
@ -0,0 +1,33 @@
|
||||
package minecraft
|
||||
|
||||
import (
|
||||
"github.com/ppacher/nbt"
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
func TagToLuaValue(L *lua.LState, tag nbt.Tag) lua.LValue {
|
||||
switch tag.TagID() {
|
||||
case nbt.TagDouble:
|
||||
return lua.LNumber(tag.(*nbt.DoubleTag).Value)
|
||||
case nbt.TagFloat:
|
||||
return lua.LNumber(tag.(*nbt.FloatTag).Value)
|
||||
case nbt.TagInt:
|
||||
return lua.LNumber(tag.(*nbt.IntTag).Value)
|
||||
case nbt.TagLong:
|
||||
return lua.LNumber(tag.(*nbt.LongTag).Value)
|
||||
case nbt.TagString:
|
||||
return lua.LString(tag.(*nbt.StringTag).Value)
|
||||
case nbt.TagCompound:
|
||||
t := L.NewTable()
|
||||
|
||||
c := tag.(*nbt.CompoundTag)
|
||||
|
||||
for key, v := range c.Tags {
|
||||
t.RawSetString(key, TagToLuaValue(L, v))
|
||||
}
|
||||
|
||||
return t
|
||||
}
|
||||
|
||||
return lua.LNil
|
||||
}
|
69
scripting/minecraft/ops.go
Normal file
69
scripting/minecraft/ops.go
Normal file
@ -0,0 +1,69 @@
|
||||
package minecraft
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/yuin/gopher-lua"
|
||||
"meow.tf/residentsleeper/events"
|
||||
"os"
|
||||
"path"
|
||||
)
|
||||
|
||||
var (
|
||||
ops = make([]*minecraftOp, 0)
|
||||
)
|
||||
|
||||
func init() {
|
||||
events.On(events.Init, loadOps)
|
||||
events.On(events.Op, oppedPlayer)
|
||||
events.On(events.Deop, deoppedPlayer)
|
||||
}
|
||||
|
||||
type minecraftOp struct {
|
||||
UUID string `json:"uuid"`
|
||||
Name string `json:"name"`
|
||||
Level int `json:"level"`
|
||||
BypassesPlayerLimit bool `json:"bypassesPlayerLimit"`
|
||||
}
|
||||
|
||||
func loadOps(args ...interface{}) {
|
||||
opPath := path.Join(BasePath, "ops.json")
|
||||
|
||||
f, err := os.Open(opPath)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
|
||||
ops = make([]*minecraftOp, 0)
|
||||
|
||||
if err = json.NewDecoder(f).Decode(&ops); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func oppedPlayer(args ...interface{}) {
|
||||
loadOps()
|
||||
}
|
||||
|
||||
func deoppedPlayer(args ...interface{}) {
|
||||
loadOps()
|
||||
}
|
||||
|
||||
func IsOp(name string) bool {
|
||||
for _, op := range ops {
|
||||
if op.Name == name {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func isOpFunc(L *lua.LState) int {
|
||||
name := L.CheckString(1)
|
||||
|
||||
L.Push(lua.LBool(IsOp(name)))
|
||||
return 1
|
||||
}
|
125
scripting/minecraft/players.go
Normal file
125
scripting/minecraft/players.go
Normal file
@ -0,0 +1,125 @@
|
||||
package minecraft
|
||||
|
||||
import (
|
||||
"compress/gzip"
|
||||
"github.com/ppacher/nbt"
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
"meow.tf/residentsleeper/events"
|
||||
"os"
|
||||
"path"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
BasePath string
|
||||
WorldPath string
|
||||
|
||||
uuids = make(map[string]string)
|
||||
uuidLock sync.RWMutex
|
||||
)
|
||||
|
||||
func init() {
|
||||
events.On(events.Authenticated, onAuthenticated)
|
||||
events.On(events.Leave, onLeave)
|
||||
}
|
||||
|
||||
func onAuthenticated(args ...interface{}) {
|
||||
name := args[0].(string)
|
||||
uuid := args[1].(string)
|
||||
|
||||
uuidLock.Lock()
|
||||
defer uuidLock.Unlock()
|
||||
|
||||
uuids[name] = uuid
|
||||
}
|
||||
|
||||
func onLeave(args ...interface{}) {
|
||||
uuidLock.Lock()
|
||||
defer uuidLock.Unlock()
|
||||
|
||||
delete(uuids, args[0].(string))
|
||||
}
|
||||
|
||||
func GetUUID(name string) (string, bool) {
|
||||
uuidLock.RLock()
|
||||
defer uuidLock.RUnlock()
|
||||
|
||||
uuid, exists := uuids[name]
|
||||
|
||||
return uuid, exists
|
||||
}
|
||||
|
||||
func Loader(L *lua.LState) int {
|
||||
// register functions to the table
|
||||
mod := L.SetFuncs(L.NewTable(), exports)
|
||||
|
||||
// returns the module
|
||||
L.Push(mod)
|
||||
return 1
|
||||
}
|
||||
|
||||
var exports = map[string]lua.LGFunction{
|
||||
"getUUID": getUuidFunc,
|
||||
"loadPlayer": loadPlayerFunc,
|
||||
"isOp": isOpFunc,
|
||||
}
|
||||
|
||||
func getUuidFunc(L *lua.LState) int {
|
||||
name := L.CheckString(1)
|
||||
|
||||
uuid, exists := GetUUID(name)
|
||||
|
||||
if exists {
|
||||
L.Push(lua.LString(uuid))
|
||||
} else {
|
||||
L.Push(lua.LNil)
|
||||
}
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
func loadPlayerFunc(L *lua.LState) int {
|
||||
name := L.CheckString(1)
|
||||
|
||||
uuid, exists := GetUUID(name)
|
||||
|
||||
if !exists {
|
||||
L.Push(lua.LNil)
|
||||
L.Push(lua.LString("User does not exist"))
|
||||
return 2
|
||||
}
|
||||
|
||||
playerPath := path.Join(WorldPath, "playerdata", uuid+".dat")
|
||||
|
||||
f, err := os.Open(playerPath)
|
||||
|
||||
if err != nil {
|
||||
L.Push(lua.LNil)
|
||||
L.Push(lua.LString(err.Error()))
|
||||
return 2
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
|
||||
r, err := gzip.NewReader(f)
|
||||
|
||||
if err != nil {
|
||||
L.Push(lua.LNil)
|
||||
L.Push(lua.LString(err.Error()))
|
||||
return 2
|
||||
}
|
||||
|
||||
defer r.Close()
|
||||
|
||||
tag, err := nbt.ReadNamedTag(r)
|
||||
|
||||
if err != nil {
|
||||
L.Push(lua.LNil)
|
||||
L.Push(lua.LString(err.Error()))
|
||||
return 2
|
||||
}
|
||||
|
||||
L.Push(TagToLuaValue(L, tag))
|
||||
L.Push(lua.LNil)
|
||||
return 2
|
||||
}
|
30
scripting/regexp/regexp.go
Normal file
30
scripting/regexp/regexp.go
Normal file
@ -0,0 +1,30 @@
|
||||
package regexp
|
||||
|
||||
import (
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
luar "layeh.com/gopher-luar"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
func Loader(L *lua.LState) int {
|
||||
// register functions to the table
|
||||
mod := L.SetFuncs(L.NewTable(), exports)
|
||||
|
||||
// returns the module
|
||||
L.Push(mod)
|
||||
return 1
|
||||
}
|
||||
|
||||
var exports = map[string]lua.LGFunction{
|
||||
"MustCompile": compileFunc,
|
||||
}
|
||||
|
||||
func compileFunc(L *lua.LState) int {
|
||||
str := L.CheckString(1)
|
||||
|
||||
re := regexp.MustCompile(str)
|
||||
|
||||
L.Push(luar.New(L, re))
|
||||
|
||||
return 1
|
||||
}
|
Reference in New Issue
Block a user