Store your Minecraft configs with your other dotfiles, and load it automatically.

feat: options loading poc using reflection

Changed files
+52 -2
config
minecraft
+5
config.toml
···
+
[options]
+
main_hand = "left"
+
ao = false
+
fov = 1
+
[mods]
sodium = "AANobbMI"
fabric-api = "P7dR8mSH"
+3
config/config.go
···
"os"
"github.com/BurntSushi/toml"
+
"potassium.sh/dot-mining/minecraft"
)
type (
Config struct {
+
Options minecraft.Options `toml:options`
Fabric Loader `toml:fabric`
Mods map[string]string `toml:mods`
}
+
Loader struct {
Mods map[string]string `toml:mods`
}
+2 -2
main.go
···
import (
"potassium.sh/dot-mining/config"
-
"potassium.sh/dot-mining/modrinth"
+
"potassium.sh/dot-mining/minecraft"
)
func main() {
config := config.LoadConfig()
-
modrinth.LoadMods(config.Mods)
+
minecraft.WriteOptions(config.Options, "")
}
+42
minecraft/options.go
···
+
package minecraft
+
+
import (
+
"fmt"
+
"reflect"
+
"strconv"
+
)
+
+
+
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 := 0; i < reflectOptions.NumField(); i++ {
+
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"
+
}
+
}
+
fmt.Print(plainText)
+
}