Fix crashes with entities deleted without checking refcount (fix #305).

This bug appeared in commit aecdfce, which exports more entities to Lua.