6 Commits

16 changed files with 299 additions and 16 deletions

View File

@@ -48,6 +48,14 @@ Settings._rarity = nil
---@type table<string,number>? ---@type table<string,number>?
Settings.groups = nil Settings.groups = nil
---The number of inventory slots that can be stored in an ItemStack.
---@type integer?
Settings._itemstack_inventory_size = nil
---The name of the texture that will be displayed as the background
---for the item when it is equipped on the player as a bag.
---@type string
Settings._itemstack_inventory_background = nil
---Texture shown in the inventory GUI. ---Texture shown in the inventory GUI.

View File

@@ -15,6 +15,9 @@ core.register_on_joinplayer(function(player)
inv:set_size('equip_legs', 2) inv:set_size('equip_legs', 2)
inv:set_size('equip_feet', 2) inv:set_size('equip_feet', 2)
inv:set_size('listring_equip', 1) -- Slot for correct listring. inv:set_size('listring_equip', 1) -- Slot for correct listring.
inv:set_size('equip_back', 1)
inv:set_size('equip_left_thigh', 1)
inv:set_size('equip_right_thigh', 1)
end) end)
core.register_chatcommand('show_inv', core.register_chatcommand('show_inv',

View File

@@ -62,3 +62,30 @@ Item:new({
} }
}) })
Item:new({
settings = {
name = 'bag',
inventory_image = EquipTileSheet:t(4, 1),
groups = {
equip_back = 1,
},
_itemstack_inventory_size = 24,
stack_max = 1,
_itemstack_inventory_background = 'equipment_bag_background_24_slots.png'
}
})
Item:new({
settings = {
name = 'bag_thigh',
inventory_image = EquipTileSheet:t(5, 1),
groups = {
equip_left_thigh = 1,
equip_right_thigh = 1
},
_itemstack_inventory_size = 16,
stack_max = 1,
_itemstack_inventory_background = 'equipment_bag_background_16_slots.png'
}
})

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 24 KiB

After

Width:  |  Height:  |  Size: 24 KiB

View File

@@ -7,6 +7,7 @@ require('Lib.activeFormspecs')
require('Lib.globalStep') require('Lib.globalStep')
require('Lib.color') require('Lib.color')
require('Lib.immersiveCraftData') require('Lib.immersiveCraftData')
require('Lib.itemStackInventory')
require('Lib.getObjectByGUID') require('Lib.getObjectByGUID')
require('Lib.receiveFields') require('Lib.receiveFields')
require('Lib.registeredItems') require('Lib.registeredItems')

View File

@@ -0,0 +1,53 @@
local ITEMSTACK_INVENTORY_LIST_META_KEY = 'itemstack_inventory_list'
---Adds an inventory list to the ItemStack's metadata.
---@param item ItemStack
---@param list ItemStack[]
function Smc.setItemInventoryList(item, list)
if #list ~= Smc.getItemInventoryListSize(item) then
error('The passed inventory list does not match the item\'s inventory list size.')
end
local meta = item:get_meta()
local items = {}
for i, stack in ipairs(list) do
items[i] = stack:to_string()
end
meta:set_string(ITEMSTACK_INVENTORY_LIST_META_KEY, core.serialize(items))
print('LIST: ', dump(core.deserialize(meta:get_string(ITEMSTACK_INVENTORY_LIST_META_KEY))))
end
---Return an inventory list to the ItemStack's metadata.
---
---If the item has no inventory, it will return the `nil`.
---@param item ItemStack
---@return ItemStack[]?
function Smc.getItemInventoryList(item)
local meta = item:get_meta()
---@type ItemStack[]?
local list = core.deserialize(meta:get_string(ITEMSTACK_INVENTORY_LIST_META_KEY))
print('LIST2: ', dump(core.deserialize(meta:get_string(ITEMSTACK_INVENTORY_LIST_META_KEY))))
return list
end
---Returns the size of an item's inventory list, as determined by `_itemstack_inventory_size`.
---
---If the item has no inventory, it will return `0`.
---@param item ItemStack
---@return integer
function Smc.getItemInventoryListSize(item)
return Smc.getItemDefinition(item).settings._itemstack_inventory_size or 0
end
---Returns the name of the bag background texture, as determined by `_itemstack_inventory_background`.
---
---If the item has no texture, it will return empty string.
---@param item ItemStack
---@return string
function Smc.getItemInventoryListBackground(item)
return Smc.getItemDefinition(item).settings._itemstack_inventory_background or ''
end

View File

@@ -27,7 +27,7 @@ function Smc.getRegisteredItemsByMod(shortModName)
return table.copy_with_metatables(registeredItemsByMod[shortModName]) return table.copy_with_metatables(registeredItemsByMod[shortModName])
end end
---@param itemName string ---@param itemName ItemStackString
---@return Item|Node ---@return Item|Node
function Smc.getRegisteredItemByName(itemName) function Smc.getRegisteredItemByName(itemName)
return table.copy_with_metatables(registeredItems[itemName]) return table.copy_with_metatables(registeredItems[itemName])
@@ -37,3 +37,17 @@ end
function Smc.getRegisteredItems() function Smc.getRegisteredItems()
return table.copy_with_metatables(registeredItems) return table.copy_with_metatables(registeredItems)
end end
---@param itemName ItemStack|ItemStackString
---@return Item.ItemDefinition|Node.NodeDefinition
function Smc.getItemDefinition(itemName)
if type(itemName) == 'table' or type(itemName) == 'userdata' then
itemName = itemName:get_name()
end
print('ItemName:', itemName)
local item = Smc.getRegisteredItemByName(itemName)
assert(item ~= nil)
return item.itemDef
end

View File

@@ -394,10 +394,10 @@ function table.key_value_list(t, separator)
local result = '' local result = ''
local size = #t local size = table.get_size(t)
local k = 1 local k = 1
for key, value in pairs(t) do for key, value in pairs(t) do
if size < k then if size > k then
result = result..key..'='..toString(value)..separator result = result..key..'='..toString(value)..separator
else else
result = result..key..'='..toString(value) result = result..key..'='..toString(value)

View File

@@ -555,6 +555,111 @@ core.register_allow_player_inventory_action(function(player, action, inventory,
end end
end) end)
---Returns the name of the inventory list that will contain the items in the bag.
---@param equipList string
---@return string
local function getBagInventoryListName(equipList)
local bagInventoryList = string.match(equipList, 'equip_(%S+)')
assert(bagInventoryList ~= nil)
return 'inventory_'..bagInventoryList
end
---Creates a corresponding inventory list by getting it from the item's metadata.
---@param inventory InvRef
---@param equipList string
---@param bagItem ItemStack
local function onBagEquipped(inventory, equipList, bagItem)
local bagInventoryListName = getBagInventoryListName(equipList)
local bagInventoryList = Smc.getItemInventoryList(bagItem) or {}
inventory:set_size(bagInventoryListName, Smc.getItemInventoryListSize(bagItem))
inventory:set_list(bagInventoryListName, bagInventoryList)
end
---Saves the inventory list corresponding to the equipped bag to the item's metadata.
---@param inventory InvRef
---@param equipList string
---@param bagItem ItemStack
local function onBagUnequipped(inventory, equipList, bagItem)
local bagInventoryListName = getBagInventoryListName(equipList)
local bagInventoryList = inventory:get_list(bagInventoryListName)
Smc.setItemInventoryList(bagItem, bagInventoryList)
inventory:set_size(bagInventoryListName, 0)
end
---Checks if a slot is a bag slot by matching the inventory list name.
---@param equipGroup string
---@return boolean
local function isBagSlot(equipGroup)
if equipGroup == 'equip_back' or equipGroup == 'equip_left_thigh' or equipGroup == 'equip_right_thigh' then
return true
else
return false
end
end
---Handling the movement of bags slots in the player's inventory.
---@param inventory InvRef
---@param inventoryInfo table
local function onBagSlotMove(inventory, inventoryInfo)
-- We get the copy items currently at the destination and source positions.
local newItem = inventory:get_stack(inventoryInfo.to_list, inventoryInfo.to_index)
local oldItem = inventory:get_stack(inventoryInfo.from_list, inventoryInfo.from_index)
-- Handling the case where both slots are bag slots.
if isBagSlot(inventoryInfo.to_list) and isBagSlot(inventoryInfo.from_list) then
-- If the target slot is empty (we put a bag from one slot to another),
-- then we unequip the bag and equip it again
if oldItem:is_empty() then
onBagUnequipped(inventory, inventoryInfo.from_list, newItem)
onBagEquipped(inventory, inventoryInfo.to_list, newItem)
-- If a slot is occupied, we swap bags.
-- We unequip them, then equip them in their correct slots.
else
onBagUnequipped(inventory, inventoryInfo.to_list, oldItem)
onBagUnequipped(inventory, inventoryInfo.from_list, newItem)
onBagEquipped(inventory, inventoryInfo.from_list, oldItem)
onBagEquipped(inventory, inventoryInfo.to_list, newItem)
end
-- Handling the case where only the target slot is a bag slot
-- (equipping a bag from other inventory slots).
elseif isBagSlot(inventoryInfo.to_list) and not isBagSlot(inventoryInfo.from_list) then
-- If the target slot is empty (we put a bag from one slot to another),
-- then we just equip the bag.
if oldItem:is_empty() then
onBagEquipped(inventory, inventoryInfo.to_list, newItem)
-- Else we swap the bags.
-- We unequip the old bag and equip the new one.
else
onBagUnequipped(inventory, inventoryInfo.to_list, oldItem)
onBagEquipped(inventory, inventoryInfo.to_list, newItem)
end
-- Handling the case where a bag is moved from a bag slot to a non-bag slot.
elseif not isBagSlot(inventoryInfo.to_list) and isBagSlot(inventoryInfo.from_list) then
-- If the target slot is empty,
-- then we just unequip the bag.
if oldItem:is_empty() then
onBagUnequipped(inventory, inventoryInfo.from_list, newItem)
end
-- The case where the slot is occupied should not be handling here.
-- It is handling in the previous else-if block.
end
-- We are updating the item stacks in the inventory,
-- since we were previously working with copies
-- (we need to save the items with new metadata).
inventory:set_stack(inventoryInfo.to_list, inventoryInfo.to_index, newItem)
inventory:set_stack(inventoryInfo.from_list, inventoryInfo.from_index, oldItem)
end
-- TODO: скорее всего будет проблема со сваппингом сумок из сундуков
-- Возможно стоит запретить свапать сумки
-- Update the interface if the player has moved something from the equipment slots -- Update the interface if the player has moved something from the equipment slots
-- (this enables and disables the slot overlay). -- (this enables and disables the slot overlay).
core.register_on_player_inventory_action(function(player, action, inventory, inventoryInfo) core.register_on_player_inventory_action(function(player, action, inventory, inventoryInfo)
@@ -562,6 +667,7 @@ core.register_on_player_inventory_action(function(player, action, inventory, inv
if action == 'move' then if action == 'move' then
local equipGroup = string.match(inventoryInfo.to_list, 'equip_%S+') or string.match(inventoryInfo.from_list, 'equip_%S+') local equipGroup = string.match(inventoryInfo.to_list, 'equip_%S+') or string.match(inventoryInfo.from_list, 'equip_%S+')
if equipGroup ~= nil then if equipGroup ~= nil then
onBagSlotMove(inventory, inventoryInfo)
PlayerInventoryUI:showForm(playerName) PlayerInventoryUI:showForm(playerName)
end end
elseif action == 'put' then elseif action == 'put' then

View File

@@ -6,10 +6,76 @@ local Formspec = Smc.requireFormspec()
---@return Formspec ---@return Formspec
local function getBagTab(self, playerName, tabType) local function getBagTab(self, playerName, tabType)
local BagTab = Formspec:new() local BagTab = Formspec:new()
local BagLists = Formspec:new()
local player = core.get_player_by_name(playerName)
assert(player ~= nil)
local inventory = player:get_inventory()
local inventoryBackSize = inventory:get_size('inventory_back') or 0
local inventoryLeftThighSize = inventory:get_size('inventory_left_thigh') or 0
local inventoryRightThighSize = inventory:get_size('inventory_right_thigh') or 0
local containerHeight = 0.0
if inventoryBackSize > 0 then
local bag = inventory:get_stack('equip_back', 1)
if not bag:is_empty() then
local background = Smc.getItemInventoryListBackground(bag)
local lines = math.ceil(inventoryBackSize/8)
BagLists
:image(0.5, 0, 9.875, (lines)*1.25+1, background)
:itemImage(0.5625, 0.0625, 1, 1, bag:get_name())
:label(1.7625, 0.5, bag:get_definition()._title or '')
:list('current_player', 'inventory_back', 0.5625, 1.25, 8, 4)
containerHeight = containerHeight + lines * 1.25 + 1
end
end
if inventoryLeftThighSize > 0 then
local bag = inventory:get_stack('equip_left_thigh', 1)
if not bag:is_empty() then
local background = Smc.getItemInventoryListBackground(bag)
local lines = math.ceil(inventoryLeftThighSize/8)
BagLists
:image(0.5, containerHeight, 9.875, (lines)*1.25+1, background)
:itemImage(0.5625, containerHeight + 0.0625, 1, 1, bag:get_name())
:label(1.7625, containerHeight + 0.5, bag:get_definition()._title or '')
:list('current_player', 'inventory_left_thigh', 0.5625, containerHeight + 1.25, 8, 4)
containerHeight = containerHeight + lines * 1.25 + 1
end
end
if inventoryRightThighSize > 0 then
local bag = inventory:get_stack('equip_right_thigh', 1)
if not bag:is_empty() then
local background = Smc.getItemInventoryListBackground(bag)
local lines = math.ceil(inventoryRightThighSize/8)
BagLists
:image(0.5, containerHeight, 9.875, (lines)*1.25+1, background)
:itemImage(0.5625, containerHeight + 0.0625, 1, 1, bag:get_name())
:label(1.7625, containerHeight + 0.5, bag:get_definition()._title or '')
:list('current_player', 'inventory_right_thigh', 0.5625, containerHeight + 1.25, 8, 4)
containerHeight = containerHeight + lines * 1.25 + 1
end
end
BagTab BagTab
:image(0, 0, 11, 9, 'player_inventory_tab_background.png') :image(0, 0, 11, 9, 'player_inventory_tab_background.png')
:label(2,2, 'BAG PANE') :scrollContainer(BagLists, 0, 0.5, 10.5, 8, 'bag_tab_scrollbar', 'vertical')
if containerHeight > 8 then
BagTab
:scrollbarOptions({min = 0, max = math.round((containerHeight-8)*10+10)})
:scrollbar(10.5, 0.5, 0.25, 8, 'vertical', 'bag_tab_scrollbar')
end
return BagTab return BagTab
end end

View File

@@ -15,12 +15,17 @@ local function getEquipTab(self, playerName, tabType)
EquipTab EquipTab
:image(0, 0, 11, 9, 'player_inventory_equip_tab_background.png') :image(0, 0, 11, 9, 'player_inventory_equip_tab_background.png')
-- Equipment slots: -- Equipment slots:
:label(equipListsX, 0.75, 'cloth') :list('current_player', 'equip_head', 56/16, 45/16, 1, 1)
:label(equipListsX+1.25, 0.75, 'armor') :list('current_player', 'equip_head', 44/16, 33/16, 1, 1, 1)
:list('current_player', 'equip_head', equipListsX, 1, 2, 1) :list('current_player', 'equip_torso', 104/16, 45/16, 1, 1)
:list('current_player', 'equip_torso', equipListsX, 2.25, 2, 1) :list('current_player', 'equip_torso', 116/16, 33/16, 1, 1, 1)
:list('current_player', 'equip_legs', equipListsX, 3.5, 2, 1) :list('current_player', 'equip_legs', 56/16, 65/16, 1, 1)
:list('current_player', 'equip_feet', equipListsX, 4.75, 2, 1) :list('current_player', 'equip_legs', 44/16, 77/16, 1, 1, 1)
:list('current_player', 'equip_feet', 104/16, 65/16, 1, 1)
:list('current_player', 'equip_feet', 116/16, 77/16, 1, 1, 1)
:list('current_player', 'equip_left_thigh', 60/16, 97/16, 1, 1)
:list('current_player', 'equip_back', 80/16, 97/16, 1, 1)
:list('current_player', 'equip_right_thigh', 100/16, 97/16, 1, 1)
-- Hidden equipment slot for correct listing -- Hidden equipment slot for correct listing
-- (distribute equipment across its slots while holding shift): -- (distribute equipment across its slots while holding shift):
:list('current_player', 'listring_equip', 0, 0, 1, 1) :list('current_player', 'listring_equip', 0, 0, 1, 1)
@@ -46,16 +51,16 @@ local function getEquipTab(self, playerName, tabType)
-- If the equipment slots are empty, then we apply an overlay. -- If the equipment slots are empty, then we apply an overlay.
-- TODO: оверлей одежды -- TODO: оверлей одежды
if inv:get_stack('equip_head', 2):is_empty() then if inv:get_stack('equip_head', 2):is_empty() then
EquipTab:image(equipListsX+1.25, 1, 1, 1, slotsOverlayTileSheet:t(0,0)) EquipTab:image(44/16, 33/16, 1, 1, slotsOverlayTileSheet:t(0,0))
end end
if inv:get_stack('equip_torso', 2):is_empty() then if inv:get_stack('equip_torso', 2):is_empty() then
EquipTab:image(equipListsX+1.25, 2.25, 1, 1, slotsOverlayTileSheet:t(0,1)) EquipTab:image(116/16, 33/16, 1, 1, slotsOverlayTileSheet:t(0,1))
end end
if inv:get_stack('equip_legs', 2):is_empty() then if inv:get_stack('equip_legs', 2):is_empty() then
EquipTab:image(equipListsX+1.25, 3.5, 1, 1, slotsOverlayTileSheet:t(0,2)) EquipTab:image(44/16, 77/16, 1, 1, slotsOverlayTileSheet:t(0,2))
end end
if inv:get_stack('equip_feet', 2):is_empty() then if inv:get_stack('equip_feet', 2):is_empty() then
EquipTab:image(equipListsX+1.25, 4.75, 1, 1, slotsOverlayTileSheet:t(0,3)) EquipTab:image(116/16, 77/16, 1, 1, slotsOverlayTileSheet:t(0,3))
end end
return EquipTab return EquipTab

Binary file not shown.

Before

Width:  |  Height:  |  Size: 100 KiB

After

Width:  |  Height:  |  Size: 100 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.2 KiB

After

Width:  |  Height:  |  Size: 4.2 KiB