Merge pull request 'Fix hopper cart crash' (#3821) from fix_cart_hopper_crash into master

Reviewed-on: https://git.minetest.land/MineClone2/MineClone2/pulls/3821
ancientmarinerdev 2023-06-28 20:35:37 +00:00
commit 823f7ee5c7

@ -85,101 +85,93 @@ local function hopper_take_item(self, dtime)
for k, v in pairs(objs) do for k, v in pairs(objs) do
local ent = v:get_luaentity() local ent = v:get_luaentity()
if ent._removed or not ent.itemstring or ent.itemstring == "" then if ent and not ent._removed and ent.itemstring and ent.itemstring ~= "" then
--minetest.log("Ignore this item") local taken_items = false
break
end
-- Don't forget actual hoppers mcl_log("ent.name: " .. tostring(ent.name))
mcl_log("ent pos: " .. tostring(ent.object:get_pos()))
local taken_items = false local inv = mcl_entity_invs.load_inv(self, 5)
if not inv then return false end
mcl_log("ent.name: " .. tostring(ent.name)) local current_itemstack = ItemStack(ent.itemstring)
mcl_log("ent pos: " .. tostring(ent.object:get_pos()))
local inv = mcl_entity_invs.load_inv(self, 5) mcl_log("inv. size: " .. self._inv_size)
if inv:room_for_item("main", current_itemstack) then
mcl_log("Room")
inv:add_item("main", current_itemstack)
ent.object:get_luaentity().itemstring = ""
ent.object:remove()
taken_items = true
else
mcl_log("no Room")
end
if not inv then if not taken_items then
mcl_log("No inv") local items_remaining = current_itemstack:get_count()
return false
end
local current_itemstack = ItemStack(ent.itemstring) -- This will take part of a floating item stack if no slot can hold the full amount
for i = 1, self._inv_size, 1 do
local stack = inv:get_stack("main", i)
mcl_log("inv. size: " .. self._inv_size) mcl_log("i: " .. tostring(i))
if inv:room_for_item("main", current_itemstack) then mcl_log("Items remaining: " .. items_remaining)
mcl_log("Room") mcl_log("Name: " .. tostring(stack:get_name()))
inv:add_item("main", current_itemstack)
ent.object:get_luaentity().itemstring = ""
ent.object:remove()
taken_items = true
else
mcl_log("no Room")
end
if not taken_items then if current_itemstack:get_name() == stack:get_name() then
local items_remaining = current_itemstack:get_count() mcl_log("We have a match. Name: " .. tostring(stack:get_name()))
-- This will take part of a floating item stack if no slot can hold the full amount local room_for = stack:get_stack_max() - stack:get_count()
for i = 1, self._inv_size, 1 do mcl_log("Room for: " .. tostring(room_for))
local stack = inv:get_stack("main", i)
mcl_log("i: " .. tostring(i)) if room_for == 0 then
mcl_log("Items remaining: " .. items_remaining) -- Do nothing
mcl_log("Name: " .. tostring(stack:get_name())) mcl_log("No room")
elseif room_for < items_remaining then
mcl_log("We have more items remaining than space")
if current_itemstack:get_name() == stack:get_name() then items_remaining = items_remaining - room_for
mcl_log("We have a match. Name: " .. tostring(stack:get_name())) stack:set_count(stack:get_stack_max())
inv:set_stack("main", i, stack)
taken_items = true
else
local new_stack_size = stack:get_count() + items_remaining
stack:set_count(new_stack_size)
mcl_log("We have more than enough space. Now holds: " .. new_stack_size)
local room_for = stack:get_stack_max() - stack:get_count() inv:set_stack("main", i, stack)
mcl_log("Room for: " .. tostring(room_for)) items_remaining = 0
if room_for == 0 then ent.object:get_luaentity().itemstring = ""
-- Do nothing ent.object:remove()
mcl_log("No room")
elseif room_for < items_remaining then
mcl_log("We have more items remaining than space")
items_remaining = items_remaining - room_for taken_items = true
stack:set_count(stack:get_stack_max()) break
inv:set_stack("main", i, stack) end
taken_items = true
else
local new_stack_size = stack:get_count() + items_remaining
stack:set_count(new_stack_size)
mcl_log("We have more than enough space. Now holds: " .. new_stack_size)
inv:set_stack("main", i, stack) mcl_log("Count: " .. tostring(stack:get_count()))
items_remaining = 0 mcl_log("stack max: " .. tostring(stack:get_stack_max()))
--mcl_log("Is it empty: " .. stack:to_string())
ent.object:get_luaentity().itemstring = ""
ent.object:remove()
taken_items = true
break
end end
mcl_log("Count: " .. tostring(stack:get_count())) if i == self._inv_size and taken_items then
mcl_log("stack max: " .. tostring(stack:get_stack_max())) mcl_log("We are on last item and still have items left. Set final stack size: " .. items_remaining)
--mcl_log("Is it empty: " .. stack:to_string()) current_itemstack:set_count(items_remaining)
end --mcl_log("Itemstack2: " .. current_itemstack:to_string())
ent.itemstring = current_itemstack:to_string()
if i == self._inv_size and taken_items then end
mcl_log("We are on last item and still have items left. Set final stack size: " .. items_remaining)
current_itemstack:set_count(items_remaining)
--mcl_log("Itemstack2: " .. current_itemstack:to_string())
ent.itemstring = current_itemstack:to_string()
end end
end end
end
--Add in, and delete --Add in, and delete
if taken_items then if taken_items then
mcl_log("Saving") mcl_log("Saving")
mcl_entity_invs.save_inv(ent) mcl_entity_invs.save_inv(ent)
return taken_items return taken_items
else else
mcl_log("No need to save") mcl_log("No need to save")
end
end end
end end
end end