A Minecraft datapack generator written in go.
at main 68 lines 1.6 kB view raw
1package weave 2 3import ( 4 "tangled.org/cosmeak.tngl.sh/weave/datapack" 5 "tangled.org/cosmeak.tngl.sh/weave/internal/generator" 6) 7 8type Weaver struct { 9 datapack datapack.Datapack 10 namespace string 11 iconPath string // Icon path relative to the root of the project. Default : ./pack.png 12 features []generator.Feature 13} 14 15func MkDatapack(name string, description string, minFormat int, maxFormat int, flags []string) Weaver { 16 // sanitize datapack name into a namespace 17 rawNamespace := []rune{} 18 for _, r := range name { 19 switch { 20 case r >= 'a' && r <= 'z': 21 rawNamespace = append(rawNamespace, r) 22 case r >= 'A' && r <= 'Z': 23 rawNamespace = append(rawNamespace, r+32) // to lowercase 24 case r >= '0' && r <= '9': 25 rawNamespace = append(rawNamespace, r) 26 case r == '_' || r == '-': 27 rawNamespace = append(rawNamespace, '_') 28 case r == ' ': 29 rawNamespace = append(rawNamespace, '_') 30 } 31 } 32 33 namespace := "" 34 if len(rawNamespace) == 0 { 35 namespace = "my datapack" 36 } else { 37 namespace = string(rawNamespace) 38 } 39 40 // construct the weaver 41 return Weaver{ 42 datapack: datapack.MkDatapack( 43 name, 44 description, 45 minFormat, 46 maxFormat, 47 flags, 48 ), 49 namespace: namespace, 50 iconPath: "./pack.png", 51 } 52} 53 54func (w Weaver) Generate(output string) error { 55 features := append(w.Features(), w.datapack) 56 return generator.Generate(w.namespace, features, output) 57} 58 59func (w Weaver) Features() []generator.Feature { 60 features := make([]generator.Feature, len(w.features)) 61 copy(features, w.features) 62 return features 63} 64 65func (w Weaver) AddFeature(feature generator.Feature) Weaver { 66 w.features = append(w.features, feature) 67 return w 68}