Were you, like me, angry that certain metatable metamethods just... Didn't work!??! Well, here's MY solution for you!
It's important that you DON'T modify the .__metatable parameter - as the mod requires it for indexing custom parameters
Mods you wish to be modified by this - must have metatable_metamethods specified in depends or optional_depends - or else this will likely load AFTER the mod
I modify certain functions as well as providing one, to permit the invocation of 3 metamethods:
.__pairs(tbl)
.__ipairs(tbl)
.__len(tbl) - with the provided length function (which permits any table, string, or vector if MT 5.5+)
Adds a new 4th metamethod:
.__setmetatable(table, metatable) - return false to prevent table metatable overwrite, return table for a custom setmetatable return. Return anything else to proceed with normal overwrite
This may hypothetically be laggier, since it's an implementation through Lua - but I personally didn't notice much lag. But then again I come with LuaJIT so who knows.
Related to https://github.com/luanti-org/luanti/issues/15133 and https://github.com/luanti-org/luanti/pull/1277