diff --git a/go.mod b/go.mod index 73fc138..52f4dfc 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,10 @@ module github.com/nothub/mrpack-install go 1.19 -require github.com/spf13/cobra v1.6.1 +require ( + github.com/google/uuid v1.3.0 + github.com/spf13/cobra v1.6.1 +) require ( github.com/inconshreveable/mousetrap v1.0.1 // indirect diff --git a/go.sum b/go.sum index 442875a..137c42f 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,6 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc= github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= diff --git a/mojang/api.go b/mojang/api.go index c6b6fa6..e0dfb0b 100644 --- a/mojang/api.go +++ b/mojang/api.go @@ -1,13 +1,19 @@ package mojang import ( + "encoding/hex" "errors" + "github.com/google/uuid" "github.com/nothub/mrpack-install/requester" "time" ) const manifestUrl = "https://launchermeta.mojang.com/mc/game/version_manifest.json" +var playerUrl = func(name string) string { + return "https://api.mojang.com/users/profiles/minecraft/" + name +} + type Manifest struct { Latest struct { Release string `json:"release"` @@ -99,6 +105,11 @@ type Meta struct { Type string `json:"type"` } +type Player struct { + Uuid string `json:"id"` + Name string `json:"name"` +} + func GetManifest() (*Manifest, error) { var manifest Manifest err := requester.DefaultHttpClient.GetJson(manifestUrl, &manifest, nil) @@ -144,3 +155,33 @@ func GetMeta(version string) (*Meta, error) { return &meta, nil } + +func GetPlayer(name string) (*Player, error) { + var player Player + err := requester.DefaultHttpClient.GetJson(playerUrl(name), &player, nil) + if err != nil { + return nil, err + } + + id, err := formatUuid(player.Uuid) + if err != nil { + return nil, err + } + player.Uuid = id + + return &player, nil +} + +func formatUuid(s string) (string, error) { + b, err := hex.DecodeString(s) + if err != nil { + return "", err + } + + id, err := uuid.FromBytes(b) + if err != nil { + return "", err + } + + return id.String(), nil +} diff --git a/mojang/api_test.go b/mojang/api_test.go new file mode 100644 index 0000000..241f580 --- /dev/null +++ b/mojang/api_test.go @@ -0,0 +1,15 @@ +package mojang + +import ( + "testing" +) + +func TestGetPlayerUuid(t *testing.T) { + player, err := GetPlayer("lit_furnace") + if err != nil { + t.Fatal(err.Error()) + } + if player.Name != "lit_furnace" || player.Uuid != "8be60c03-25c5-4e57-ab5d-0081e8736cf8" { + t.Fatal("Wrong player data!") + } +}