diff options
-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:] } |