Fix crashes

This commit is contained in:
teknomunk 2024-04-09 23:09:10 +00:00
parent 778e42165a
commit d1b5e77ca1
2 changed files with 13 additions and 9 deletions

View file

@ -1188,11 +1188,7 @@ end
function mcl_util.get_luaentity_from_uuid(uuid)
return minetest.luaentities[ mcl_util.get_active_object_id_from_uuid(uuid) ]
end
function mcl_util.get_uuid(obj)
local le = obj:get_luaentity()
if le._uuid then return le._uuid end
function mcl_util.gen_uuid()
-- Generate a random 128-bit ID that can be assumed to be unique
-- To have a 1% chance of a collision, there would have to be 1.6x10^76 IDs generated
-- https://en.wikipedia.org/wiki/Birthday_problem#Probability_table
@ -1200,7 +1196,15 @@ function mcl_util.get_uuid(obj)
for i = 1,16 do
u[#u + 1] = string.format("%02X",math.random(1,255))
end
le._uuid = table.concat(u)
return table.concat(u)
end
function mcl_util.assign_uuid(obj)
assert(obj)
local le = obj:get_luaentity()
if le._uuid then return le._uuid end
le._uuid = mcl_util.gen_uuid()
-- Update the cache with this new id
aoid = mcl_util.get_active_object_id(obj)

View file

@ -107,7 +107,7 @@ function DEFAULT_CART_DEF:on_activate(staticdata, dtime_s)
-- Transfer older data
local data = minetest.deserialize(staticdata) or {}
if not data.uuid then
data.uuid = mcl_util.get_uuid(self.object)
data.uuid = mcl_util.assign_uuid(self.object)
end
self._seq = data.seq or 1
@ -202,7 +202,7 @@ function DEFAULT_CART_DEF:on_step(dtime)
-- Regen
local hp = self.object:get_hp()
local time_now = minetest.get_gametime()
if hp < MINECART_MAX_HP and staticdata.last_regen <= time_now - 1 then
if hp < MINECART_MAX_HP and (staticdata.last_regen or 0) <= time_now - 1 then
staticdata.last_regen = time_now
hp = hp + 1
self.object:set_hp(hp)
@ -308,7 +308,7 @@ function mcl_minecarts.place_minecart(itemstack, pointed_thing, placer)
local entity_id = entity_mapping[itemstack:get_name()]
-- Setup cart data
local uuid = mcl_util.get_uuid(cart)
local uuid = mcl_util.gen_uuid()
data = make_staticdata( nil, railpos, cart_dir )
data.uuid = uuid
data.cart_type = entity_id