| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- package config
- import (
- "encoding/json"
- "fmt"
- "io/ioutil"
- "os"
- "strconv"
- "strings"
- )
- func initOps(serverFolder string) Op {
- jsonFile, err := os.Open(serverFolder + "ops.json")
- if err != nil {
- fmt.Println(err)
- }
- byte, _ := ioutil.ReadAll(jsonFile)
- var dataArray Op
- json.Unmarshal(byte, &dataArray)
- return dataArray
- }
- //ReadAllOps is exported function
- func (mch *Handler) ReadAllOps() Op {
- return mch.ops
- }
- //WriteOps is exported function
- func (mch *Handler) WriteOps(UUID string, Name string, Level int, BypassesPlayerLimit bool) bool {
- newItem := Op{}
- newItem = append(newItem, struct {
- UUID string `json:"uuid"`
- Name string `json:"name"`
- Level int `json:"level"`
- BypassesPlayerLimit bool `json:"bypassesPlayerLimit"`
- }{UUID, Name, Level, BypassesPlayerLimit})
- mch.ops = append(mch.ops, newItem...)
- return true
- }
- //ReadOps is exported function
- func (mch *Handler) ReadOps(search string, field string) Op {
- var list Op
- for _, item := range mch.ops {
- fieldValue := ""
- switch strings.ToLower(field) {
- case "uuid":
- fieldValue = item.UUID
- case "name":
- fieldValue = item.Name
- case "level":
- fieldValue = strconv.Itoa(item.Level)
- case "bypassesplayerlimit":
- fieldValue = strconv.FormatBool(item.BypassesPlayerLimit)
- default:
- fieldValue = ""
- }
- if fieldValue == search {
- list = append(list, item)
- }
- }
- return list
- }
- //RemoveOps is exported function
- func (mch *Handler) RemoveOps(search string, field string) bool {
- var list Op
- for i, item := range mch.ops {
- fieldValue := ""
- switch strings.ToLower(field) {
- case "uuid":
- fieldValue = item.UUID
- case "name":
- fieldValue = item.Name
- case "level":
- fieldValue = strconv.Itoa(item.Level)
- case "bypassesplayerlimit":
- fieldValue = strconv.FormatBool(item.BypassesPlayerLimit)
- default:
- fieldValue = ""
- }
- if fieldValue == search {
- list = append(list[:i], list[i+1])
- }
- }
- return true
- }
- //SaveAllOps is exported function
- func (mch *Handler) SaveAllOps() bool {
- JSON, _ := json.Marshal(mch.ops)
- err := ioutil.WriteFile(mch.serverFolder+"/banned-players.json", JSON, 0777)
- if err != nil {
- fmt.Println(err)
- }
- return true
- }
|