Store your Minecraft configs with your other dotfiles, and load it automatically.
1package minecraft
2
3import (
4 "os"
5 "path/filepath"
6 "reflect"
7 "strconv"
8
9 "github.com/charmbracelet/log"
10)
11
12type (
13 Options struct {
14 MainHand string `toml:"main_hand" txt:"mainHand"`
15 AO bool `toml:"smooth_lighting" txt:"ao"`
16 Fov int `toml:"fov" txt:"fov"`
17 }
18)
19
20func WriteOptions(options Options, path string) {
21 plainText := "version:9999\n"
22
23 reflectOptions := reflect.TypeOf(options)
24
25 for i := range reflectOptions.NumField() {
26 field := reflectOptions.Field(i)
27
28 txt := field.Tag.Get("txt")
29 if txt == "" {
30 txt = field.Name
31 }
32 value := reflect.ValueOf(options).Field(i)
33
34 switch field.Type.Kind() {
35 case reflect.String:
36 plainText += txt + ":" + "\"" + value.String() + "\"\n"
37 case reflect.Int:
38 plainText += txt + ":" + strconv.Itoa(int(value.Int())) + "\n"
39 case reflect.Bool:
40 plainText += txt + ":" + strconv.FormatBool(value.Bool()) + "\n"
41 }
42 }
43
44 dir := filepath.Dir(path)
45 if err := os.MkdirAll(dir, 0755); err != nil {
46 log.Fatal("Failed creating instance directory ", dir, ": ", err)
47 return
48 }
49
50 if err := os.WriteFile(path, []byte(plainText), 0755); err != nil {
51 log.Fatal("Failed writing options", "error", err)
52 }
53}