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:
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
|
||||
}
|
Reference in New Issue
Block a user