Thanks for the effort and sharing!
Just a few issues, which I feel is important, that I found:
You should run $values through $db->escape_string, not htmlspecialchars. Otherwise, this plugin is vulnerable to SQL injection. htmlspecialchars should only be used at display time, however, as you're inserting this directly into Javascript, you really need more sanitisation over this variable.
$uid = $mybb->user[uid]; should be $uid = $mybb->user['uid']; You have some other instances of dropping quotes around array indicies in templates - all of these should have quotes around them.
In general, you should use $db->update_query, instead of $db->query for updates, unless you have some special circumstance, in which case, you should use $db->write_query
You shouldn't be escaping $ characters in find_replace_templatesets replacements, since you're using single quote strings.
Also, this is actually incorrect:
Simply don't set these ID fields on DB insertion.
This is probably a bit convoluted:
Try this instead:
Hope that helps