Add Smc.putOrDrop function

This commit is contained in:
Van
2026-11-09 21:30:23 +03:00
parent 2d3bdf4d02
commit c2e16834d8
3 changed files with 29 additions and 15 deletions

View File

@@ -9,6 +9,7 @@ local require = modInfo.require
-- Independent of SMC methods
require('Lib.activeFormspecs')
require('Lib.globalStep')
require('Lib.inventoryHelpers')
require('Lib.color')
require('Lib.itemStackInventory')
require('Lib.nodeSuperClasses')

View File

@@ -0,0 +1,23 @@
local ItemStack = ItemStack
---Adds the specified stack to the player's inventory, provided there is space available.
---
---If there is no space, it drops the stack on the ground.
---
---Returns `true` if the stack was placed in the inventory, and `false` if it was dropped.
---@param player Player
---@param inventory InvRef
---@param item ItemStack|ItemStackString|ItemStackString
---@return boolean
function Smc.putOrDrop(player, inventory, item)
item = ItemStack(item)
if inventory:room_for_item('main', item) then
inventory:add_item('main', item)
return true
else
core.item_drop(item, player, player:get_pos())
return false
end
end

View File

@@ -611,17 +611,11 @@ PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::%S+::craft_item', functio
craft = craft
local inventory = player:get_inventory()
--TODO: выделить это в функцию Smc.playerTakeOrDrop
--TODO: добавить сброс предмета на землю, если нет места.
if inventory:room_for_item('main', craft.output) then
for _, item in ipairs(materialStacks) do
for _, item in ipairs(materialStacks) do
inventory:remove_item('main', item)
end
inventory:add_item('main', craft.output)
else
core.chat_send_player(player:get_player_name(), 'There is no space in the inventory!')
end
Smc.putOrDrop(player, inventory, craft.output)
elseif tabName == 'node_tab' and allowCrafting and handler.baseType == 'immersive' then
---@type CraftSystem.ImmersiveCraft
craft = craft
@@ -658,12 +652,8 @@ PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::add_item', function(playe
stack:set_count(fieldValue)
---@type InvRef
local inv = player:get_inventory()
-- TODO выделить это в функцию Smc.playerTakeOrDrop
if inv:room_for_item('main', stack) then
inv:add_item('main', stack)
end
local inventory = player:get_inventory()
Smc.putOrDrop(player, inventory, stack)
end)
---Automatically places equipment items into their corresponding slots.