Fix crashes

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

View File

@ -1216,11 +1216,7 @@ end
function mcl_util.get_luaentity_from_uuid(uuid) function mcl_util.get_luaentity_from_uuid(uuid)
return minetest.luaentities[ mcl_util.get_active_object_id_from_uuid(uuid) ] return minetest.luaentities[ mcl_util.get_active_object_id_from_uuid(uuid) ]
end end
function mcl_util.get_uuid(obj) function mcl_util.gen_uuid()
local le = obj:get_luaentity()
if le._uuid then return le._uuid end
-- Generate a random 128-bit ID that can be assumed to be unique -- 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 -- 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 -- https://en.wikipedia.org/wiki/Birthday_problem#Probability_table
@ -1228,7 +1224,15 @@ function mcl_util.get_uuid(obj)
for i = 1,16 do for i = 1,16 do
u[#u + 1] = string.format("%02X",math.random(1,255)) u[#u + 1] = string.format("%02X",math.random(1,255))
end 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 -- Update the cache with this new id
aoid = mcl_util.get_active_object_id(obj) 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 -- Transfer older data
local data = minetest.deserialize(staticdata) or {} local data = minetest.deserialize(staticdata) or {}
if not data.uuid then if not data.uuid then
data.uuid = mcl_util.get_uuid(self.object) data.uuid = mcl_util.assign_uuid(self.object)
end end
self._seq = data.seq or 1 self._seq = data.seq or 1
@ -202,7 +202,7 @@ function DEFAULT_CART_DEF:on_step(dtime)
-- Regen -- Regen
local hp = self.object:get_hp() local hp = self.object:get_hp()
local time_now = minetest.get_gametime() 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 staticdata.last_regen = time_now
hp = hp + 1 hp = hp + 1
self.object:set_hp(hp) 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()] local entity_id = entity_mapping[itemstack:get_name()]
-- Setup cart data -- Setup cart data
local uuid = mcl_util.get_uuid(cart) local uuid = mcl_util.gen_uuid()
data = make_staticdata( nil, railpos, cart_dir ) data = make_staticdata( nil, railpos, cart_dir )
data.uuid = uuid data.uuid = uuid
data.cart_type = entity_id data.cart_type = entity_id