Updated 1.21.6
This commit is contained in:
77
data/sound/generator/main.go
Normal file
77
data/sound/generator/main.go
Normal file
@ -0,0 +1,77 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
_ "embed"
|
||||
"go/format"
|
||||
"log"
|
||||
"os"
|
||||
"text/template"
|
||||
|
||||
"github.com/Tnze/go-mc/internal/generateutils"
|
||||
"github.com/Tnze/go-mc/nbt"
|
||||
)
|
||||
|
||||
//go:embed sounds.go.tmpl
|
||||
var tempSource string
|
||||
|
||||
var temp = template.Must(template.
|
||||
New("sound_template").
|
||||
Funcs(template.FuncMap{
|
||||
"UpperTheFirst": generateutils.UpperTheFirst,
|
||||
"ToGoTypeName": generateutils.ToGoTypeName,
|
||||
"Generator": func() string { return "generator/main.go" },
|
||||
}).
|
||||
Parse(tempSource),
|
||||
)
|
||||
|
||||
type Sound struct {
|
||||
Name string
|
||||
Id int
|
||||
}
|
||||
|
||||
func main() {
|
||||
var sounds []Sound
|
||||
readItems(&sounds)
|
||||
|
||||
// generate go source file
|
||||
genSourceFile(sounds)
|
||||
}
|
||||
|
||||
func readItems(states *[]Sound) {
|
||||
// open block_states data file
|
||||
f, err := os.Open("sounds.nbt")
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
r, err := gzip.NewReader(f)
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
|
||||
// parse the nbt format
|
||||
if _, err := nbt.NewDecoder(r).Decode(states); err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func genSourceFile(states []Sound) {
|
||||
var source bytes.Buffer
|
||||
if err := temp.Execute(&source, states); err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
|
||||
formattedSource, err := format.Source(source.Bytes())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
err = os.WriteFile("sounds.go", formattedSource, 0o666)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log.Print("Generated sounds.go")
|
||||
}
|
22
data/sound/generator/sounds.go.tmpl
Normal file
22
data/sound/generator/sounds.go.tmpl
Normal file
@ -0,0 +1,22 @@
|
||||
// Code generated by {{Generator}}; DO NOT EDIT.
|
||||
|
||||
package sound
|
||||
|
||||
type (
|
||||
{{- range .}}
|
||||
{{.Name | ToGoTypeName}} struct {}
|
||||
{{- end}}
|
||||
)
|
||||
|
||||
{{- range .}}
|
||||
func ({{.Name | ToGoTypeName}}) ID() string { return {{.Name | printf "%q"}} }
|
||||
func ({{.Name | ToGoTypeName}}) sound() { }
|
||||
{{end}}
|
||||
|
||||
var FromID = map[string]Sound { {{- range .}}
|
||||
{{.Name | printf "%q"}}: {{.Name | ToGoTypeName}}{},{{end}}
|
||||
}
|
||||
|
||||
var ToID = map[ID]Sound { {{- range .}}
|
||||
{{.Id | printf "%d"}}: {{.Name | ToGoTypeName}}{},{{end}}
|
||||
}
|
12
data/sound/sound.go
Normal file
12
data/sound/sound.go
Normal file
@ -0,0 +1,12 @@
|
||||
package sound
|
||||
|
||||
type Sound interface {
|
||||
sound()
|
||||
ID() string
|
||||
}
|
||||
|
||||
// This file stores all possible block states into a TAG_List with gzip compressed.
|
||||
//
|
||||
//go:generate go run ./generator/main.go
|
||||
|
||||
type ID int
|
BIN
data/sound/sounds.nbt
Normal file
BIN
data/sound/sounds.nbt
Normal file
Binary file not shown.
Reference in New Issue
Block a user