// gen_soundIDs.go generates the enumeration of sound IDs. //+build ignore package main import ( "encoding/json" "fmt" "net/http" "os" "sort" ) const ( protocolURL = "https://pokechu22.github.io/Burger/1.16.4.json" ) type sound struct { ID int64 Name string } func main() { sounds, err := downloadSoundInfo() if err != nil { fmt.Fprintf(os.Stderr, "Error: %v\n", err) os.Exit(1) } fmt.Println("package data") fmt.Println() fmt.Println("//go:generate bash -c \"go run gen_soundIDs.go > soundIDs.go\"") fmt.Println() fmt.Println("// This file is automatically generated by gen_soundIDs.go. DO NOT EDIT.") fmt.Println() fmt.Println("// SoundID represents a sound ID used in the minecraft protocol.") fmt.Println("type SoundID int32") fmt.Println() fmt.Println("// SoundNames - map of ids to names for sounds.") fmt.Println("var SoundNames map[SoundID]string = map[SoundID]string{") for _, v := range sounds { fmt.Printf(` %d: "%s",`, v.ID, v.Name) fmt.Println() } fmt.Println("}") fmt.Println() fmt.Println("// GetSoundNameByID helper method") fmt.Println("func GetSoundNameByID(id SoundID) (string,bool) {") fmt.Println(" name, ok := SoundNames[id]") fmt.Println(" return name, ok") fmt.Println("}") } func downloadSoundInfo() ([]sound, error) { resp, err := http.Get(protocolURL) if err != nil { return nil, err } defer resp.Body.Close() x := []map[string]interface{}{} // if err := json.Unmarshal([]byte(data), &x); err != nil { if err := json.NewDecoder(resp.Body).Decode(&x); err != nil { return nil, err } out := []sound{} for i := range x { if sounds, ok := x[i]["sounds"]; ok { // fmt.Printf("sounds: %#v\n", sounds) for _, value := range sounds.(map[string]interface{}) { // fmt.Printf("%d: %s\n", int64(value.(map[string]interface{})["id"].(float64)), value.(map[string]interface{})["name"]) out = append(out, sound{ID: int64(value.(map[string]interface{})["id"].(float64)), Name: value.(map[string]interface{})["name"].(string)}) } } else { // fmt.Printf("NO SOUNDS FOUND IN DATA!") return nil, fmt.Errorf("No sounds found in data from %s", protocolURL) } } sort.SliceStable(out, func(i, j int) bool { return out[i].ID < out[j].ID }) return out, nil }