package minecraft import ( "os" "path/filepath" "reflect" "strconv" "github.com/charmbracelet/log" ) type ( Options struct { MainHand string `toml:"main_hand" txt:"mainHand"` AO bool `toml:"smooth_lighting" txt:"ao"` Fov int `toml:"fov" txt:"fov"` } ) func WriteOptions(options Options, path string) { plainText := "version:9999\n" reflectOptions := reflect.TypeOf(options) for i := range reflectOptions.NumField() { field := reflectOptions.Field(i) txt := field.Tag.Get("txt") if txt == "" { txt = field.Name } value := reflect.ValueOf(options).Field(i) switch field.Type.Kind() { case reflect.String: plainText += txt + ":" + "\"" + value.String() + "\"\n" case reflect.Int: plainText += txt + ":" + strconv.Itoa(int(value.Int())) + "\n" case reflect.Bool: plainText += txt + ":" + strconv.FormatBool(value.Bool()) + "\n" } } dir := filepath.Dir(path) if err := os.MkdirAll(dir, 0755); err != nil { log.Fatal("Failed creating instance directory ", dir, ": ", err) return } if err := os.WriteFile(path, []byte(plainText), 0755); err != nil { log.Fatal("Failed writing options", "error", err) } }