Make inventory use the util functions

This commit is contained in:
AFCMS 2023-11-22 09:11:50 +01:00 committed by the-real-herowl
parent 4836418cf6
commit e324a1a74b

View file

@ -3,56 +3,6 @@ mcl_inventory = {}
dofile(minetest.get_modpath(minetest.get_current_modname()) .. "/creative.lua")
dofile(minetest.get_modpath(minetest.get_current_modname()) .. "/survival.lua")
--local mod_player = minetest.get_modpath("mcl_player")
--local mod_craftguide = minetest.get_modpath("mcl_craftguide")
---Returns a single itemstack in the given inventory to the main inventory, or drop it when there's no space left.
---@param itemstack mt.ItemStack
---@param dropper mt.ObjectRef
---@param pos mt.Vector
---@param inv mt.InvRef
local function return_item(itemstack, dropper, pos, inv)
if dropper:is_player() then
-- Return to main inventory
if inv:room_for_item("main", itemstack) then
inv:add_item("main", itemstack)
else
-- Drop item on the ground
local v = dropper:get_look_dir()
local p = vector.offset(pos, 0, 1.2, 0)
p.x = p.x + (math.random(1, 3) * 0.2)
p.z = p.z + (math.random(1, 3) * 0.2)
local obj = minetest.add_item(p, itemstack)
if obj then
v.x = v.x * 4
v.y = v.y * 4 + 2
v.z = v.z * 4
obj:set_velocity(v)
obj:get_luaentity()._insta_collect = false
end
end
else
-- Fallback for unexpected cases
minetest.add_item(pos, itemstack)
end
return itemstack
end
---Return items in the given inventory list (name) to the main inventory, or drop them if there is no space left.
---@param player mt.PlayerObjectRef
---@param name string
local function return_fields(player, name)
local inv = player:get_inventory()
local list = inv:get_list(name)
if not list then return end
for i, stack in ipairs(list) do
return_item(stack, player, player:get_pos(), inv)
stack:clear()
inv:set_stack(name, i, stack)
end
end
---@param player mt.PlayerObjectRef
---@param armor_change_only? boolean
local function set_inventory(player, armor_change_only)
@ -72,9 +22,9 @@ end
-- Drop items in craft grid and reset inventory on closing
minetest.register_on_player_receive_fields(function(player, formname, fields)
if fields.quit then
return_fields(player, "craft")
return_fields(player, "enchanting_lapis")
return_fields(player, "enchanting_item")
mcl_util.move_player_list(player, "craft")
mcl_util.move_player_list(player, "enchanting_lapis")
mcl_util.move_player_list(player, "enchanting_item")
if not minetest.is_creative_enabled(player:get_player_name()) and (formname == "" or formname == "main") then
set_inventory(player)
end
@ -88,9 +38,9 @@ end
-- Drop crafting grid items on leaving
minetest.register_on_leaveplayer(function(player)
return_fields(player, "craft")
return_fields(player, "enchanting_lapis")
return_fields(player, "enchanting_item")
mcl_util.move_player_list(player, "craft")
mcl_util.move_player_list(player, "enchanting_lapis")
mcl_util.move_player_list(player, "enchanting_item")
end)
minetest.register_on_joinplayer(function(player)
@ -116,9 +66,9 @@ minetest.register_on_joinplayer(function(player)
items remaining in the crafting grid from the previous join; this is likely
when the server has been shutdown and the server didn't clean up the player
inventories. ]]
return_fields(player, "craft")
return_fields(player, "enchanting_item")
return_fields(player, "enchanting_lapis")
mcl_util.move_player_list(player, "craft")
mcl_util.move_player_list(player, "enchanting_lapis")
mcl_util.move_player_list(player, "enchanting_item")
end)
---@param player mt.PlayerObjectRef