Skip to content

Commit

Permalink
Support convert advancements and pets' owner
Browse files Browse the repository at this point in the history
  • Loading branch information
Tnze committed May 21, 2024
1 parent ba27294 commit 73df16e
Show file tree
Hide file tree
Showing 5 changed files with 393 additions and 147 deletions.
58 changes: 58 additions & 0 deletions examples/playerdataconvert/advancements.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/google/uuid"
)

func readAdvancements(dir string, m map[uuid.UUID]UserCache) {
entries, err := os.ReadDir(dir)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to open playerdata folder: %v", err)
return
}
for _, files := range entries {
filename := files.Name()
if ext := filepath.Ext(filename); ext != ".json" {
fmt.Fprintf(os.Stderr, "Unkown file type: %s\n", ext)
continue
}

// Parse old UUID from filename
oldID, err := uuid.Parse(strings.TrimSuffix(filename, ".json"))
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to parse filename as uuid: %v\n", err)
continue
}

if ver := oldID.Version(); ver != 3 { // v3 is for offline players
fmt.Printf("Ignoring UUID: %v version: %d\n", oldID, ver)
continue
}

newUser, ok := m[oldID]
if !ok {
fmt.Printf("Skip user: %v\n", oldID)
continue
}

content, err := os.ReadFile(filepath.Join(dir, filename))
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to read json file: %v\n", err)
continue
}

newFile := newUser.UUID.String() + ".json"
err = os.WriteFile(filepath.Join(dir, newFile), content, 0o666)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to write json file: %v\n", err)
continue
}

fmt.Printf("Converted advancement file: %s\n", newFile)
}
}
185 changes: 38 additions & 147 deletions examples/playerdataconvert/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,191 +9,82 @@
package main

import (
"compress/gzip"
"encoding/binary"
"encoding/json"
"flag"
"fmt"
"io/fs"
"net/http"
"os"
"path/filepath"

"github.com/Tnze/go-mc/nbt"
"github.com/Tnze/go-mc/nbt/dynbt"
"github.com/google/uuid"
)

var savePath = flag.String("save", "The save folder with \"usercache.json\" file inside", "")
var (
savePath = flag.String("save", ".", "The save folder with \"usercache.json\" file inside")
convertPlayerData = flag.Bool("cplayerdata", true, "Whether convert files at /world/playerdata/*.dat")
convertEntities = flag.Bool("centities", true, "Whether convert pets' Owner at /world/entities/*")
convertAdvancements = flag.Bool("cadvancements", true, "Whether convert advancements at /world/advancements/*")
)

func main() {
flag.Parse()
save, err := os.ReadDir(*savePath)

usercaches, err := readUsercache(filepath.Join(*savePath, "usercache.json"))
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to open dir: %v", err)
fmt.Fprintf(os.Stderr, "Failed to parse usercache file: %v\n", err)
return
}
fmt.Printf("Successfully reading usercache\n")
m := mappingUsers(usercaches)

var usercache fs.DirEntry
for i := range save {
name := save[i].Name()
if name == "usercache.json" && !save[i].IsDir() {
usercache = save[i]
}
if *convertPlayerData {
readPlayerdata(filepath.Join(*savePath, "world", "playerdata"), m)
}
if usercache == nil {
fmt.Fprintf(os.Stderr, "usercache.json not found")
return

if *convertEntities {
readEntities(filepath.Join(*savePath, "world", "entities"), m)
}

if *convertAdvancements {
readAdvancements(filepath.Join(*savePath, "world", "advancements"), m)
}
usercaches := readUsercache(filepath.Join(*savePath, usercache.Name()))
fmt.Printf("Successfully reading usercache\n")
readPlayerdata(filepath.Join(*savePath, "world", "playerdata"), usercaches)
}

type UserCache struct {
Name string `json:"name"`
UUID string `json:"uuid"`
ExpiresOn string `json:"expiresOn"`
Name string `json:"name"`
UUID uuid.UUID `json:"uuid"`
}

func readUsercache(path string) []UserCache {
func readUsercache(path string) ([]UserCache, error) {
data, err := os.ReadFile(path)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to read usercache file: %v\n", err)
return nil
return nil, err
}

var usercache []UserCache
err = json.Unmarshal(data, &usercache)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to parse usercache file: %v\n", err)
return nil
return nil, err
}
return usercache
return usercache, nil
}

func readPlayerdata(dir string, users []UserCache) {
func mappingUsers(users []UserCache) map[uuid.UUID]UserCache {
m := make(map[uuid.UUID]UserCache)
for _, user := range users {
nbtdata, err := readNbtData(dir, &user)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to read %s's nbt data\n", user.Name)
continue
}
name := user.Name
// // You can add your maps here
// if v, ok := offlineOnlineMaps[name]; ok {
// name = v
// }

// Get old UUID
uuidInts := nbtdata.Get("UUID").IntArray()
uuidBytes, err := intArrayToUUID(uuidInts)
name, id, err := usernameToUUID(name)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to read %s's UUID\n", user.Name)
continue
}

if ver := uuidBytes.Version(); ver != 3 { // v3 is for offline players
fmt.Printf("Ignoring UUID: %v version: %d\n", uuidBytes, ver)
fmt.Fprintf(os.Stderr, "Unable to fetch username for %s from Mojang server: %v\n", name, err)
continue
}

// Get new UUID
name, id, err := usernameToUUID(user.Name)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to fetch username for %s from Mojang server: %v\n", user.Name, err)
continue
}

fmt.Printf("[%s] %v -> %v\n", name, uuidBytes, id)

// Update UUID
ints := uuidToIntArray(id)
nbtdata.Set("UUID", dynbt.NewIntArray(ints[:]))

// Create new .dat file
err = writeNbtData(dir, id.String(), &nbtdata)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to write %s's .dat file: %v\n", name, err)
continue
}
}
}

func readNbtData(dir string, user *UserCache) (dynbt.Value, error) {
file, err := os.Open(filepath.Join(dir, user.UUID+".dat"))
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to read %s's userdata: %v\n", user.Name, err)
}
defer file.Close()

r, err := gzip.NewReader(file)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to decompress %s's userdata: %v\n", user.Name, err)
}

var nbtdata dynbt.Value
_, err = nbt.NewDecoder(r).Decode(&nbtdata)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to parse %s's userdata: %v\n", user.Name, err)
}
return nbtdata, nil
}

func writeNbtData(dir string, id string, nbtdata *dynbt.Value) error {
newDatFilePath := filepath.Join(dir, id+".dat")
file, err := os.Create(newDatFilePath)
if err != nil {
return err
}

w := gzip.NewWriter(file)
err = nbt.NewEncoder(w).Encode(&nbtdata, "")
if err != nil {
return err
}

err = w.Close()
if err != nil {
return err
}

err = file.Close()
if err != nil {
return err
}
return nil
}

func usernameToUUID(name string) (string, uuid.UUID, error) {
var id uuid.UUID
resp, err := http.Get("https://api.mojang.com/users/profiles/minecraft/" + name)
if err != nil {
return "", id, err
}

var body struct {
Name string `json:"name"`
ID string `json:"id"`
}
err = json.NewDecoder(resp.Body).Decode(&body)
if err != nil {
return "", id, err
}

id, err = uuid.Parse(body.ID)
return body.Name, id, err
}

func intArrayToUUID(uuidInts []int32) (id uuid.UUID, err error) {
if uuidLen := len(uuidInts); uuidLen != 4 {
err = fmt.Errorf("invalid UUID len: %d * int32", uuidLen)
return
}
for i, v := range uuidInts {
binary.BigEndian.PutUint32(id[i*4:], uint32(v))
}
return
}

func uuidToIntArray(id uuid.UUID) (ints [4]int32) {
for i := range ints {
ints[i] = int32(binary.BigEndian.Uint32(id[i*4:]))
fmt.Printf("[%s] %v -> %v\n", name, user.UUID, id)
m[user.UUID] = UserCache{name, id}
}
return
return m
}
Loading

0 comments on commit 73df16e

Please sign in to comment.