Skip to content

Commit

Permalink
mojang player api client
Browse files Browse the repository at this point in the history
  • Loading branch information
nothub committed Feb 4, 2023
1 parent a0cbc12 commit 132b492
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
41 changes: 41 additions & 0 deletions mojang/api.go
Original file line number Diff line number Diff line change
@@ -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"`
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
15 changes: 15 additions & 0 deletions mojang/api_test.go
Original file line number Diff line number Diff line change
@@ -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!")
}
}

0 comments on commit 132b492

Please sign in to comment.