diff options
author | HimbeerserverDE <himbeerserverde@gmail.com> | 2023-06-11 14:04:04 +0200 |
---|---|---|
committer | HimbeerserverDE <himbeerserverde@gmail.com> | 2023-06-11 14:04:04 +0200 |
commit | 1b31a1c30e9cb2a05f1db1b3e1a40a9d8f340632 (patch) | |
tree | 9005858603696095057374b834abea612700ffdb | |
parent | 632688cd4fb970432ad03bbbdda41636eb0cc492 (diff) |
prependFormspec: apply to item_image and item_image_button
fixes the recipe tab showing unknown item textures in mtg
-rw-r--r-- | formspec.go | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/formspec.go b/formspec.go index 3ffa7ac..79d23ea 100644 --- a/formspec.go +++ b/formspec.go @@ -5,6 +5,8 @@ import ( "strings" ) +var itemName = regexp.MustCompile("(item_image\\[[0-9.]+,[0-9.]+;[0-9.]+,[0-9]+;)([a-zA-Z0-9-_.:]*)(\\])") +var itemButtonName = regexp.MustCompile("(item_image_button\\[[0-9.]+,[0-9.]+;[0-9.]+,[0-9.]+;)([a-zA-Z0-9-_.:]+)(;[a-zA-Z0-9-_.:]+;[^\\[\\]]*\\])") var textureName = regexp.MustCompile("[a-zA-Z0-9-_.]*\\.[a-zA-Z-_.]+") func (sc *ServerConn) prependFormspec(fs *string) { @@ -24,4 +26,28 @@ func (sc *ServerConn) prependFormspec(fs *string) { *fs += seps[i] } } + + *fs = ReplaceAllStringSubmatchFunc(itemName, *fs, func(groups []string) string { + return groups[1] + sc.name + "_" + groups[2] + groups[3] + }) + *fs = ReplaceAllStringSubmatchFunc(itemButtonName, *fs, func(groups []string) string { + return groups[1] + sc.name + "_" + groups[2] + groups[3] + }) +} + +func ReplaceAllStringSubmatchFunc(re *regexp.Regexp, str string, repl func([]string) string) string { + result := "" + lastIndex := 0 + + for _, v := range re.FindAllSubmatchIndex([]byte(str), -1) { + groups := []string{} + for i := 0; i < len(v); i += 2 { + groups = append(groups, str[v[i]:v[i+1]]) + } + + result += str[lastIndex:v[0]] + repl(groups) + lastIndex = v[1] + } + + return result + str[lastIndex:] } |