PlayerInventoryUI: separate action handling
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
local ipairs = ipairs
|
||||
local assert = assert
|
||||
local stringMatch = string.match
|
||||
local tableInsert, tableRemoveValue = table.insert, table.remove_value
|
||||
|
||||
local coreCheckPlayerPrivs = core.check_player_privs
|
||||
@@ -8,10 +7,13 @@ local coreColorize = core.colorize
|
||||
local corePosToString = core.pos_to_string
|
||||
local coreGetPlayerByName = core.get_player_by_name
|
||||
|
||||
local BaseUI, Formspec = Smc.requireBaseUI(), Smc.requireFormspec()
|
||||
local CraftSystem = Smc.requireCraftSystem()
|
||||
local SmcGetActiveFormspec = Smc.getActiveFormspec
|
||||
|
||||
local require = Mod.getInfo('smc__ui__player_inventory').require
|
||||
local BaseUI, Formspec = Smc.requireBaseUI(), Smc.requireFormspec()
|
||||
|
||||
local modInfo = Mod.getInfo('smc__ui__player_inventory')
|
||||
local require = modInfo.require
|
||||
local load = modInfo.load
|
||||
|
||||
---The distance at which a player can interact with blocks that have a node-tab.
|
||||
---
|
||||
@@ -415,7 +417,7 @@ PlayerInventoryUI:registerOnFormQuit(closeNodeTab)
|
||||
Smc.registerGlobalStepForEachPlayer(0, function(deltaTime, player)
|
||||
local playerName = player:get_player_name()
|
||||
|
||||
if Smc.getActiveFormspec(playerName) == '' then
|
||||
if SmcGetActiveFormspec(playerName) == '' then
|
||||
local nodePos = PlayerInventoryUI:getContex(playerName, 'node_tab_pos')
|
||||
if nodePos == nil then
|
||||
return
|
||||
@@ -432,609 +434,10 @@ end)
|
||||
|
||||
--- Inventory action handling ---
|
||||
|
||||
-- Handling the press of the inventory mode change button.
|
||||
PlayerInventoryUI:registerOnFormFieldAction('change_compact_mode', function(player, fieldName, fieldValue)
|
||||
local playerName = player:get_player_name()
|
||||
|
||||
local compactMode = PlayerInventoryUI:getContex(playerName, 'compact_mode')
|
||||
|
||||
-- By default, the player will be shown a compact form.
|
||||
if compactMode == nil then
|
||||
compactMode = true
|
||||
end
|
||||
|
||||
PlayerInventoryUI:setContex(playerName, 'compact_mode', (not compactMode))
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
end)
|
||||
|
||||
-- Handling the inventory tab selection button press.
|
||||
PlayerInventoryUI:registerOnFormFieldActionMatch('select_[%a]+_%S+', function(player, fieldName, fieldValue)
|
||||
local tabType, tabName = stringMatch(fieldName, 'select_([%a]+)_(%S+)')
|
||||
assert(tabType == 'right' or tabType == 'left')
|
||||
assert(tabName ~= nil)
|
||||
|
||||
local playerName = player:get_player_name()
|
||||
|
||||
PlayerInventoryUI:setContex(playerName, tabType..'_tab_name', tabName)
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
end)
|
||||
|
||||
-- require('PlayerInventoryUI.actionHandling.general')
|
||||
|
||||
-- Handling input in the items list search bar.
|
||||
PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::%S+::items_list_search_field', function(player, fieldName, fieldValue)
|
||||
local tabType, tabName = stringMatch(fieldName, '(%S+)::(%S+)::items_list_search_field')
|
||||
assert(tabType == 'left' or tabType == 'right')
|
||||
assert(tabName == 'craft_tab' or tabName == 'node_tab' or tabName == 'admin_list_tab')
|
||||
|
||||
local playerName = player:get_player_name()
|
||||
|
||||
---@type PlayerInventoryUI.CraftListTabData|PlayerInventoryUI.AdminListTabData
|
||||
local tabData
|
||||
if tabName == 'craft_tab' or tabName == 'node_tab' then
|
||||
tabData = PlayerInventoryUI:getTabCraftData(playerName, tabType, tabName)
|
||||
elseif tabName == 'admin_list_tab' then
|
||||
tabData = PlayerInventoryUI:getAdminListTabData(playerName, tabType)
|
||||
else
|
||||
error('Invalid tabName: '..tabName)
|
||||
end
|
||||
|
||||
if tabData.searchFieldValue == fieldValue then
|
||||
return
|
||||
end
|
||||
|
||||
tabData.searchFieldValue = fieldValue
|
||||
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
end)
|
||||
|
||||
-- Handling the button press for flipping through the pages of an item list.
|
||||
PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::%S+::items_list_page_[%a]+', function(player, fieldName, fieldValue)
|
||||
local tabType, tabName, pageFlip = stringMatch(fieldName, '(%S+)::(%S+)::items_list_page_([%a]+)')
|
||||
assert(tabType == 'left' or tabType == 'right')
|
||||
assert(tabName == 'craft_tab' or tabName == 'node_tab' or tabName == 'admin_list_tab')
|
||||
assert(pageFlip == 'back' or pageFlip == 'forward')
|
||||
|
||||
---A factor that determines whether the page moves backward or forward
|
||||
---@type integer
|
||||
local pageFactor
|
||||
if pageFlip == 'back' then
|
||||
pageFactor = -1
|
||||
else
|
||||
pageFactor = 1
|
||||
end
|
||||
|
||||
local playerName = player:get_player_name()
|
||||
|
||||
---@type PlayerInventoryUI.CraftListTabData|PlayerInventoryUI.AdminListTabData
|
||||
local tabData
|
||||
if tabName == 'craft_tab' or tabName == 'node_tab' then
|
||||
tabData = PlayerInventoryUI:getTabCraftData(playerName, tabType, tabName)
|
||||
elseif tabName == 'admin_list_tab' then
|
||||
tabData = PlayerInventoryUI:getAdminListTabData(playerName, tabType)
|
||||
else
|
||||
error('Invalid tabName: '..tabName)
|
||||
end
|
||||
|
||||
local pageNumber = tabData.pageNumber or 1
|
||||
pageNumber = pageNumber + pageFactor
|
||||
|
||||
if pageNumber <= 0 then
|
||||
pageNumber = 1
|
||||
end
|
||||
|
||||
tabData.pageNumber = pageNumber
|
||||
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
end)
|
||||
|
||||
-- Handlings the click of the button to move back in the list of alternative recipes.
|
||||
PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::%S+::alt_recipe_[%a]+', function(player, fieldName, fieldValue)
|
||||
local tabType, tabName, pageFlip = stringMatch(fieldName, '(%S+)::(%S+)::alt_recipe_([%a]+)')
|
||||
assert(tabType == 'left' or tabType == 'right')
|
||||
assert(tabName == 'craft_tab' or tabName == 'node_tab')
|
||||
assert(pageFlip == 'back' or pageFlip == 'forward')
|
||||
|
||||
---A factor that determines whether the page moves backward or forward
|
||||
---@type integer
|
||||
local pageFactor
|
||||
if pageFlip == 'back' then
|
||||
pageFactor = -1
|
||||
else
|
||||
pageFactor = 1
|
||||
end
|
||||
|
||||
local playerName = player:get_player_name()
|
||||
local craftListTabData = PlayerInventoryUI:getTabCraftData(playerName, tabType, tabName)
|
||||
local recipeIndex = craftListTabData.selectedRecipeIndex or 1
|
||||
recipeIndex = recipeIndex + pageFactor
|
||||
|
||||
local handler = craftListTabData.handler
|
||||
local craft = handler:getCraftByName(craftListTabData.selectedCraft)
|
||||
local crafts = handler:getLearnedCraftsByOutput(playerName, craft.output)
|
||||
assert(crafts ~= nil)
|
||||
|
||||
if recipeIndex < 1 then
|
||||
recipeIndex = 1
|
||||
end
|
||||
|
||||
if recipeIndex > #crafts then
|
||||
recipeIndex = #crafts
|
||||
end
|
||||
|
||||
craftListTabData.selectedRecipeIndex = recipeIndex
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
end)
|
||||
|
||||
-- Processing closing of the selected item group in the crafting menu (returning to the main list of crafts).
|
||||
PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::%S+::close_group', function(player, fieldName, fieldValue)
|
||||
local tabType, tabName = stringMatch(fieldName, '(%S+)::(%S+)::close_group')
|
||||
assert(tabType == 'left' or tabType == 'right')
|
||||
assert(tabName == 'craft_tab' or tabName == 'node_tab')
|
||||
|
||||
local playerName = player:get_player_name()
|
||||
local craftListTabData = PlayerInventoryUI:getTabCraftData(playerName, tabType, tabName)
|
||||
craftListTabData.selectedGroup = nil
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
end)
|
||||
|
||||
-- Handling the button click for selecting an item for crafting.
|
||||
-- Opens the item description and allows you to craft the selected item.
|
||||
PlayerInventoryUI:registerOnFormFieldActionMatch('.+::%S+::%S+::items_list_button',function(player, fieldName, fieldValue)
|
||||
local name, tabType, tabName = string.match(fieldName, '(.+)::(%S+)::(%S+)::items_list_button')
|
||||
assert(name ~= nil)
|
||||
assert(tabType == 'left' or tabType == 'right')
|
||||
assert(tabName == 'craft_tab' or tabName == 'node_tab' or tabName == 'admin_list_tab')
|
||||
local playerName = player:get_player_name()
|
||||
|
||||
if tabName == 'craft_tab' or tabName == 'node_tab' then
|
||||
local craftListTabData = PlayerInventoryUI:getTabCraftData(playerName, tabType, tabName)
|
||||
craftListTabData.selectedRecipeIndex = 1
|
||||
craftListTabData.selectedCraft = name
|
||||
elseif tabName == 'admin_list_tab' then
|
||||
local craftListTabData = PlayerInventoryUI:getAdminListTabData(playerName, tabType)
|
||||
craftListTabData.selectedItem = name
|
||||
else
|
||||
error('Invalid tabName: '..tabName)
|
||||
end
|
||||
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
end)
|
||||
|
||||
-- Handling the click of the crafting group selection button.
|
||||
-- Causes grouped items to be expanded into a separate list.
|
||||
PlayerInventoryUI:registerOnFormFieldActionMatch('.+::%S+::%S+::group_button',function(player, fieldName, fieldValue)
|
||||
local groupName, tabType, tabName = string.match(fieldName, '(.+)::(%S+)::(%S+)::group_button')
|
||||
assert(groupName ~= nil)
|
||||
assert(tabType == 'left' or tabType == 'right')
|
||||
assert(tabName == 'craft_tab' or tabName == 'node_tab')
|
||||
|
||||
local playerName = player:get_player_name()
|
||||
local craftListTabData = PlayerInventoryUI:getTabCraftData(playerName, tabType, tabName)
|
||||
craftListTabData.selectedGroup = groupName
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
end)
|
||||
|
||||
-- Handling the click to craft an item.
|
||||
-- If the player has crafting materials, they are removed and the crafted item is added to the inventory.
|
||||
--
|
||||
-- Note: Materials are only searched in the player's main inventory.
|
||||
PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::%S+::craft_item', function(player, fieldName, fieldValue)
|
||||
local tabType, tabName = string.match(fieldName, '(%S+)::(%S+)::craft_item')
|
||||
assert(tabType == 'left' or tabType == 'right')
|
||||
assert(tabName == 'craft_tab' or tabName == 'node_tab')
|
||||
|
||||
local playerName = player:get_player_name()
|
||||
|
||||
-- We get the recipe selected in the crafting menu.
|
||||
local craftListTabData = PlayerInventoryUI:getTabCraftData(playerName, tabType, tabName)
|
||||
local handler = craftListTabData.handler
|
||||
local craftName = craftListTabData.selectedCraft
|
||||
assert(craftName ~= nil)
|
||||
local recipeIndex = craftListTabData.selectedRecipeIndex
|
||||
assert(recipeIndex ~= nil)
|
||||
local craft = handler:getCraftByName(craftName)
|
||||
|
||||
local allowCrafting, materialStacks = CraftSystem:checkPossiblePlayerInventoryCrafting(player, craft, recipeIndex, fieldValue)
|
||||
if allowCrafting and handler.baseType == 'primitive' then
|
||||
---@type CraftSystem.PrimitiveCraft
|
||||
craft = craft
|
||||
local inventory = player:get_inventory()
|
||||
|
||||
for _, item in ipairs(materialStacks) do
|
||||
inventory:remove_item('main', item)
|
||||
end
|
||||
|
||||
Smc.putOrDrop(player, inventory, craft.output)
|
||||
elseif allowCrafting and tabName == 'node_tab' and handler.baseType == 'immersive' then
|
||||
---@type CraftSystem.ImmersiveCraft
|
||||
craft = craft
|
||||
local inventory = player:get_inventory()
|
||||
local nodePos = PlayerInventoryUI:getContex(playerName, 'node_tab_pos')
|
||||
|
||||
local isCraftStart = craft:startCraft(nodePos, player, fieldValue)
|
||||
if not isCraftStart then
|
||||
return
|
||||
end
|
||||
|
||||
for _, item in ipairs(materialStacks) do
|
||||
inventory:remove_item('main', item)
|
||||
end
|
||||
|
||||
PlayerInventoryUI:closeForm(playerName)
|
||||
end
|
||||
end)
|
||||
|
||||
-- Handling the addition of an item from the admin list.
|
||||
-- If the player does not have the 'give' privilege, nothing will happen.
|
||||
PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::add_item', function(player, fieldName, fieldValue)
|
||||
local tabType = string.match(fieldName, '(%S+)::add_item')
|
||||
assert(tabType == 'left' or tabType == 'right')
|
||||
|
||||
local playerName = player:get_player_name()
|
||||
if not core.check_player_privs(playerName, 'give') then
|
||||
core.chat_send_player(player:get_player_name(), 'Your attempt to retrieve the item has been reported to the administrators.')
|
||||
return
|
||||
end
|
||||
|
||||
local craftListTabData = PlayerInventoryUI:getAdminListTabData(playerName, tabType)
|
||||
local stack = ItemStack(craftListTabData.selectedItem)
|
||||
stack:set_count(fieldValue)
|
||||
|
||||
---@type InvRef
|
||||
local inventory = player:get_inventory()
|
||||
Smc.putOrDrop(player, inventory, stack)
|
||||
end)
|
||||
|
||||
---Automatically places equipment items into their corresponding slots.
|
||||
---@param player Player
|
||||
---@param inventory InvRef
|
||||
---@param fromList string
|
||||
---@param fromIndex integer
|
||||
local function equipListring(player, inventory, fromList, fromIndex)
|
||||
local itemStack = inventory:get_stack(fromList, fromIndex)
|
||||
assert(itemStack ~= nil)
|
||||
local playerName = player:get_player_name()
|
||||
|
||||
-- We check whether the item has a group that matches the equipment slot.
|
||||
-- If so, we move the item to the slot.
|
||||
--
|
||||
-- We also check if the target slot is empty.
|
||||
-- If it's already occupied, we prevent items from being moved.
|
||||
if core.get_item_group(itemStack:get_name(), 'equip_head') == 1 then
|
||||
if not inventory:get_stack('equip_head', 1):is_empty() then
|
||||
return 0
|
||||
end
|
||||
inventory:set_stack('equip_head', 1, itemStack)
|
||||
inventory:set_stack(fromList, fromIndex, nil)
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
return
|
||||
|
||||
elseif core.get_item_group(itemStack:get_name(), 'equip_torso') == 1 then
|
||||
if not inventory:get_stack('equip_torso', 1):is_empty() then
|
||||
return 0
|
||||
end
|
||||
inventory:set_stack('equip_torso', 1, itemStack)
|
||||
inventory:set_stack(fromList, fromIndex, nil)
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
return
|
||||
|
||||
elseif core.get_item_group(itemStack:get_name(), 'equip_legs') == 1 then
|
||||
if not inventory:get_stack('equip_legs', 1):is_empty() then
|
||||
return 0
|
||||
end
|
||||
inventory:set_stack('equip_legs', 1, itemStack)
|
||||
inventory:set_stack(fromList, fromIndex, nil)
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
return
|
||||
|
||||
elseif core.get_item_group(itemStack:get_name(), 'equip_feet') == 1 then
|
||||
if not inventory:get_stack('equip_feet', 1):is_empty() then
|
||||
return 0
|
||||
end
|
||||
inventory:set_stack('equip_feet', 1, itemStack)
|
||||
inventory:set_stack(fromList, fromIndex, nil)
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
return
|
||||
|
||||
elseif core.get_item_group(itemStack:get_name(), 'equip_head') == 2 then
|
||||
if not inventory:get_stack('equip_head', 2):is_empty() then
|
||||
return 0
|
||||
end
|
||||
inventory:set_stack('equip_head', 2, itemStack)
|
||||
inventory:set_stack(fromList, fromIndex, nil)
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
return
|
||||
|
||||
elseif core.get_item_group(itemStack:get_name(), 'equip_torso') == 2 then
|
||||
if not inventory:get_stack('equip_torso', 2):is_empty() then
|
||||
return 0
|
||||
end
|
||||
inventory:set_stack('equip_torso', 2, itemStack)
|
||||
inventory:set_stack(fromList, fromIndex, nil)
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
return
|
||||
|
||||
elseif core.get_item_group(itemStack:get_name(), 'equip_legs') == 2 then
|
||||
if not inventory:get_stack('equip_legs', 2):is_empty() then
|
||||
return 0
|
||||
end
|
||||
inventory:set_stack('equip_legs', 2, itemStack)
|
||||
inventory:set_stack(fromList, fromIndex, nil)
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
return
|
||||
|
||||
elseif core.get_item_group(itemStack:get_name(), 'equip_feet') == 2 then
|
||||
if not inventory:get_stack('equip_feet', 2):is_empty() then
|
||||
return 0
|
||||
end
|
||||
inventory:set_stack('equip_feet', 2, itemStack)
|
||||
inventory:set_stack(fromList, fromIndex, nil)
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
return
|
||||
|
||||
else
|
||||
-- If an item does not have any equipment groups,
|
||||
-- then we prohibit its movement (the item remains in its original location)
|
||||
return 0
|
||||
end
|
||||
end
|
||||
|
||||
-- We only allow items to be placed in equipment slot
|
||||
-- that have groups that match the slot name and index.
|
||||
core.register_allow_player_inventory_action(function(player, action, inventory, inventoryInfo)
|
||||
if action == 'move' then
|
||||
-- If the move occurs in a special slot, then we add the item to the desired slot.
|
||||
-- This is necessary to bypass the standard listring[] limitation.
|
||||
-- Equipment items will automatically place themselves in their slots
|
||||
-- when you hold down the shift key.
|
||||
if inventoryInfo.to_list == 'listring_equip' then
|
||||
return equipListring(player --[[@as Player]], inventory, inventoryInfo.from_list, inventoryInfo.from_index)
|
||||
end
|
||||
|
||||
-- Checking if a slot is an equipment slot.
|
||||
local equipGroup = string.match(inventoryInfo.to_list, 'equip_%S+')
|
||||
if equipGroup == nil then
|
||||
return
|
||||
end
|
||||
|
||||
-- We receive the item that the player wants to place in the equipment slot.
|
||||
local itemStack = inventory:get_stack(inventoryInfo.from_list, inventoryInfo.from_index)
|
||||
assert(itemStack ~= nil)
|
||||
|
||||
if core.get_item_group(itemStack:get_name(), equipGroup) == inventoryInfo.to_index then
|
||||
return
|
||||
else
|
||||
return 0
|
||||
end
|
||||
elseif action == 'put' then
|
||||
-- Checking if a slot is an equipment slot.
|
||||
local equipGroup = string.match(inventoryInfo.listname, 'equip_%S+')
|
||||
if equipGroup == nil then
|
||||
return
|
||||
end
|
||||
|
||||
-- We receive the item that the player wants to place in the equipment slot.
|
||||
local itemStack = inventoryInfo.stack
|
||||
|
||||
if core.get_item_group(itemStack:get_name(), equipGroup) == inventoryInfo.index then
|
||||
return
|
||||
else
|
||||
return 0
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
---Checks if the inventory list is an inventory list of bags.
|
||||
---@param listName string
|
||||
---@return boolean
|
||||
local function isBagInventoryList(listName)
|
||||
if listName == 'inventory_back' or listName == 'inventory_left_thigh' or listName == 'inventory_right_thigh' then
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
---Returns the copy ItemStack of the bag whose inventory is expanded in the specified inventory list.
|
||||
---@param inventory InvRef
|
||||
---@param listName string
|
||||
---@return ItemStack
|
||||
local function getBagSlotListName(inventory, listName)
|
||||
local bagSlotName = string.match(listName, 'inventory_(%S+)')
|
||||
assert(bagSlotName ~= nil)
|
||||
|
||||
local bagItem = inventory:get_stack('equip_'..bagSlotName, 1)
|
||||
assert(not bagItem:is_empty())
|
||||
|
||||
return bagItem
|
||||
end
|
||||
|
||||
-- We check whether an item contains allow or disallow item groups
|
||||
-- to ensure that only allow items are placed in bags.
|
||||
core.register_allow_player_inventory_action(function(player, action, inventory, inventoryInfo)
|
||||
if action == 'move' and isBagInventoryList(inventoryInfo.to_list) then
|
||||
local bagItem = getBagSlotListName(inventory, inventoryInfo.to_list)
|
||||
local movableItemName = inventory:get_stack(inventoryInfo.from_list, inventoryInfo.from_index):get_name()
|
||||
|
||||
local bagSetting = Smc.getItemDefinition(bagItem).settings
|
||||
local allowedGroups, disallowedGroups = bagSetting._itemstack_inventory_allowed_groups, bagSetting._itemstack_inventory_disallowed_groups
|
||||
|
||||
if allowedGroups ~= nil then
|
||||
for _, group in ipairs(allowedGroups) do
|
||||
if core.get_item_group(movableItemName, group) > 0 then
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
return 0
|
||||
end
|
||||
|
||||
if disallowedGroups ~= nil then
|
||||
for _, group in ipairs(disallowedGroups) do
|
||||
if core.get_item_group(movableItemName, group) > 0 then
|
||||
return 0
|
||||
end
|
||||
end
|
||||
|
||||
return
|
||||
end
|
||||
|
||||
elseif action == 'put' and isBagInventoryList(inventoryInfo.listname) then
|
||||
local bagItem = getBagSlotListName(inventory, inventoryInfo.listname)
|
||||
local movableItemName = inventoryInfo.stack
|
||||
|
||||
local bagSetting = Smc.getItemDefinition(bagItem).settings
|
||||
local allowedGroups, disallowedGroups = bagSetting._itemstack_inventory_allowed_groups, bagSetting._itemstack_inventory_disallowed_groups
|
||||
|
||||
if allowedGroups ~= nil then
|
||||
for _, group in ipairs(allowedGroups) do
|
||||
if core.get_item_group(movableItemName, group) > 0 then
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
return 0
|
||||
end
|
||||
|
||||
if disallowedGroups ~= nil then
|
||||
for _, group in ipairs(disallowedGroups) do
|
||||
if core.get_item_group(movableItemName, group) > 0 then
|
||||
return 0
|
||||
end
|
||||
end
|
||||
|
||||
return
|
||||
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
|
||||
-- (this enables and disables the slot overlay).
|
||||
core.register_on_player_inventory_action(function(player, action, inventory, inventoryInfo)
|
||||
local playerName = player:get_player_name()
|
||||
if action == 'move' then
|
||||
local equipGroup = string.match(inventoryInfo.to_list, 'equip_%S+') or string.match(inventoryInfo.from_list, 'equip_%S+')
|
||||
if equipGroup ~= nil then
|
||||
onBagSlotMove(inventory, inventoryInfo)
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
end
|
||||
elseif action == 'put' then
|
||||
local equipGroup = string.match(inventoryInfo.listname, 'equip_%S+')
|
||||
if equipGroup ~= nil then
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
end
|
||||
elseif action == 'take' then
|
||||
local equipGroup = string.match(inventoryInfo.listname, 'equip_%S+')
|
||||
if equipGroup ~= nil then
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
end
|
||||
end
|
||||
end)
|
||||
load(PlayerInventoryUI, 'PlayerInventoryUI.actionHandling.general')
|
||||
load(PlayerInventoryUI, 'PlayerInventoryUI.actionHandling.itemsList')
|
||||
load(PlayerInventoryUI, 'PlayerInventoryUI.actionHandling.crafts')
|
||||
load(PlayerInventoryUI, 'PlayerInventoryUI.actionHandling.adminList')
|
||||
load(PlayerInventoryUI, 'PlayerInventoryUI.actionHandling.equip')
|
||||
|
||||
return PlayerInventoryUI
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
---@type PlayerInventoryUI
|
||||
local PlayerInventoryUI = ...
|
||||
|
||||
local assert = assert
|
||||
local stringMatch = string.match
|
||||
|
||||
local coreCheckPlayerPrivs = core.check_player_privs
|
||||
local coreChatSendPlayer = core.chat_send_player
|
||||
|
||||
local SmcPutOrDrop = Smc.putOrDrop
|
||||
|
||||
local ItemStack = ItemStack
|
||||
|
||||
-- Handling the addition of an item from the admin list.
|
||||
-- If the player does not have the 'give' privilege, nothing will happen.
|
||||
PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::add_item', function(player, fieldName, fieldValue)
|
||||
local tabType = stringMatch(fieldName, '(%S+)::add_item')
|
||||
assert(tabType == 'left' or tabType == 'right')
|
||||
|
||||
local playerName = player:get_player_name()
|
||||
if not coreCheckPlayerPrivs(playerName, 'give') then
|
||||
coreChatSendPlayer(
|
||||
player:get_player_name(),
|
||||
'Your attempt to retrieve the item has been reported to the administrators.'
|
||||
)
|
||||
return
|
||||
end
|
||||
|
||||
local craftListTabData = PlayerInventoryUI:getAdminListTabData(playerName, tabType)
|
||||
local stack = ItemStack(craftListTabData.selectedItem)
|
||||
stack:set_count(fieldValue)
|
||||
|
||||
---@type InvRef
|
||||
local inventory = player:get_inventory()
|
||||
|
||||
SmcPutOrDrop(player, inventory, stack)
|
||||
end)
|
||||
@@ -0,0 +1,99 @@
|
||||
---@type PlayerInventoryUI
|
||||
local PlayerInventoryUI = ...
|
||||
|
||||
local ipairs = ipairs
|
||||
local assert = assert
|
||||
local stringMatch = string.match
|
||||
|
||||
local SmcPutOrDrop = Smc.putOrDrop
|
||||
|
||||
local CraftSystem = Smc.requireCraftSystem()
|
||||
|
||||
-- Handlings the click of the button to move back in the list of alternative recipes.
|
||||
PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::%S+::alt_recipe_[%a]+', function(player, fieldName, fieldValue)
|
||||
local tabType, tabName, pageFlip = stringMatch(fieldName, '(%S+)::(%S+)::alt_recipe_([%a]+)')
|
||||
assert(tabType == 'left' or tabType == 'right')
|
||||
assert(tabName == 'craft_tab' or tabName == 'node_tab')
|
||||
assert(pageFlip == 'back' or pageFlip == 'forward')
|
||||
|
||||
---A factor that determines whether the page moves backward or forward
|
||||
---@type integer
|
||||
local pageFactor
|
||||
if pageFlip == 'back' then
|
||||
pageFactor = -1
|
||||
else
|
||||
pageFactor = 1
|
||||
end
|
||||
|
||||
local playerName = player:get_player_name()
|
||||
local craftListTabData = PlayerInventoryUI:getTabCraftData(playerName, tabType, tabName)
|
||||
local recipeIndex = craftListTabData.selectedRecipeIndex or 1
|
||||
recipeIndex = recipeIndex + pageFactor
|
||||
|
||||
local handler = craftListTabData.handler
|
||||
local craft = handler:getCraftByName(craftListTabData.selectedCraft)
|
||||
local crafts = handler:getLearnedCraftsByOutput(playerName, craft.output)
|
||||
assert(crafts ~= nil)
|
||||
|
||||
if recipeIndex < 1 then
|
||||
recipeIndex = 1
|
||||
end
|
||||
|
||||
if recipeIndex > #crafts then
|
||||
recipeIndex = #crafts
|
||||
end
|
||||
|
||||
craftListTabData.selectedRecipeIndex = recipeIndex
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
end)
|
||||
|
||||
-- Handling the click to craft an item.
|
||||
-- If the player has crafting materials,
|
||||
-- they are removed and the crafted item is added to the inventory.
|
||||
--
|
||||
-- Note: Materials are only searched in the player's main inventory.
|
||||
PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::%S+::craft_item', function(player, fieldName, fieldValue)
|
||||
local tabType, tabName = stringMatch(fieldName, '(%S+)::(%S+)::craft_item')
|
||||
assert(tabType == 'left' or tabType == 'right')
|
||||
assert(tabName == 'craft_tab' or tabName == 'node_tab')
|
||||
|
||||
local playerName = player:get_player_name()
|
||||
|
||||
-- We get the recipe selected in the crafting menu.
|
||||
local craftListTabData = PlayerInventoryUI:getTabCraftData(playerName, tabType, tabName)
|
||||
local handler = craftListTabData.handler
|
||||
local craftName = craftListTabData.selectedCraft
|
||||
assert(craftName ~= nil)
|
||||
local recipeIndex = craftListTabData.selectedRecipeIndex
|
||||
assert(recipeIndex ~= nil)
|
||||
local craft = handler:getCraftByName(craftName)
|
||||
|
||||
local allowCrafting, materialStacks = CraftSystem:checkPossiblePlayerInventoryCrafting(player, craft, recipeIndex, fieldValue)
|
||||
if allowCrafting and handler.baseType == 'primitive' then
|
||||
---@type CraftSystem.PrimitiveCraft
|
||||
craft = craft
|
||||
local inventory = player:get_inventory()
|
||||
|
||||
for _, item in ipairs(materialStacks) do
|
||||
inventory:remove_item('main', item)
|
||||
end
|
||||
|
||||
SmcPutOrDrop(player, inventory, craft.output)
|
||||
elseif allowCrafting and tabName == 'node_tab' and handler.baseType == 'immersive' then
|
||||
---@type CraftSystem.ImmersiveCraft
|
||||
craft = craft
|
||||
local inventory = player:get_inventory()
|
||||
local nodePos = PlayerInventoryUI:getContex(playerName, 'node_tab_pos')
|
||||
|
||||
local isCraftStart = craft:startCraft(nodePos, player, fieldValue)
|
||||
if not isCraftStart then
|
||||
return
|
||||
end
|
||||
|
||||
for _, item in ipairs(materialStacks) do
|
||||
inventory:remove_item('main', item)
|
||||
end
|
||||
|
||||
PlayerInventoryUI:closeForm(playerName)
|
||||
end
|
||||
end)
|
||||
@@ -0,0 +1,376 @@
|
||||
---@type PlayerInventoryUI
|
||||
local PlayerInventoryUI = ...
|
||||
|
||||
local ipairs = ipairs
|
||||
local assert = assert
|
||||
local stringMatch = string.match
|
||||
|
||||
local coreGetItemGroup = core.get_item_group
|
||||
|
||||
local SmcGetItemDefinition = Smc.getItemDefinition
|
||||
local SmcGetItemInventoryList = Smc.getItemInventoryList
|
||||
local SmcSetItemInventoryList = Smc.setItemInventoryList
|
||||
local SmcGetItemInventoryListSize = Smc.getItemInventoryListSize
|
||||
|
||||
---Automatically places equipment items into their corresponding slots.
|
||||
---@param player Player
|
||||
---@param inventory InvRef
|
||||
---@param fromList string
|
||||
---@param fromIndex integer
|
||||
local function equipListring(player, inventory, fromList, fromIndex)
|
||||
local itemStack = inventory:get_stack(fromList, fromIndex)
|
||||
assert(itemStack ~= nil)
|
||||
local playerName = player:get_player_name()
|
||||
|
||||
-- We check whether the item has a group that matches the equipment slot.
|
||||
-- If so, we move the item to the slot.
|
||||
--
|
||||
-- We also check if the target slot is empty.
|
||||
-- If it's already occupied, we prevent items from being moved.
|
||||
if coreGetItemGroup(itemStack:get_name(), 'equip_head') == 1 then
|
||||
if not inventory:get_stack('equip_head', 1):is_empty() then
|
||||
return 0
|
||||
end
|
||||
inventory:set_stack('equip_head', 1, itemStack)
|
||||
inventory:set_stack(fromList, fromIndex, nil)
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
return
|
||||
|
||||
elseif coreGetItemGroup(itemStack:get_name(), 'equip_torso') == 1 then
|
||||
if not inventory:get_stack('equip_torso', 1):is_empty() then
|
||||
return 0
|
||||
end
|
||||
inventory:set_stack('equip_torso', 1, itemStack)
|
||||
inventory:set_stack(fromList, fromIndex, nil)
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
return
|
||||
|
||||
elseif coreGetItemGroup(itemStack:get_name(), 'equip_legs') == 1 then
|
||||
if not inventory:get_stack('equip_legs', 1):is_empty() then
|
||||
return 0
|
||||
end
|
||||
inventory:set_stack('equip_legs', 1, itemStack)
|
||||
inventory:set_stack(fromList, fromIndex, nil)
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
return
|
||||
|
||||
elseif coreGetItemGroup(itemStack:get_name(), 'equip_feet') == 1 then
|
||||
if not inventory:get_stack('equip_feet', 1):is_empty() then
|
||||
return 0
|
||||
end
|
||||
inventory:set_stack('equip_feet', 1, itemStack)
|
||||
inventory:set_stack(fromList, fromIndex, nil)
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
return
|
||||
|
||||
elseif coreGetItemGroup(itemStack:get_name(), 'equip_head') == 2 then
|
||||
if not inventory:get_stack('equip_head', 2):is_empty() then
|
||||
return 0
|
||||
end
|
||||
inventory:set_stack('equip_head', 2, itemStack)
|
||||
inventory:set_stack(fromList, fromIndex, nil)
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
return
|
||||
|
||||
elseif coreGetItemGroup(itemStack:get_name(), 'equip_torso') == 2 then
|
||||
if not inventory:get_stack('equip_torso', 2):is_empty() then
|
||||
return 0
|
||||
end
|
||||
inventory:set_stack('equip_torso', 2, itemStack)
|
||||
inventory:set_stack(fromList, fromIndex, nil)
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
return
|
||||
|
||||
elseif coreGetItemGroup(itemStack:get_name(), 'equip_legs') == 2 then
|
||||
if not inventory:get_stack('equip_legs', 2):is_empty() then
|
||||
return 0
|
||||
end
|
||||
inventory:set_stack('equip_legs', 2, itemStack)
|
||||
inventory:set_stack(fromList, fromIndex, nil)
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
return
|
||||
|
||||
elseif coreGetItemGroup(itemStack:get_name(), 'equip_feet') == 2 then
|
||||
if not inventory:get_stack('equip_feet', 2):is_empty() then
|
||||
return 0
|
||||
end
|
||||
inventory:set_stack('equip_feet', 2, itemStack)
|
||||
inventory:set_stack(fromList, fromIndex, nil)
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
return
|
||||
|
||||
else
|
||||
-- If an item does not have any equipment groups,
|
||||
-- then we prohibit its movement (the item remains in its original location)
|
||||
return 0
|
||||
end
|
||||
end
|
||||
|
||||
-- We only allow items to be placed in equipment slot
|
||||
-- that have groups that match the slot name and index.
|
||||
core.register_allow_player_inventory_action(function(player, action, inventory, inventoryInfo)
|
||||
if action == 'move' then
|
||||
-- If the move occurs in a special slot, then we add the item to the desired slot.
|
||||
-- This is necessary to bypass the standard listring[] limitation.
|
||||
-- Equipment items will automatically place themselves in their slots
|
||||
-- when you hold down the shift key.
|
||||
if inventoryInfo.to_list == 'listring_equip' then
|
||||
return equipListring(player --[[@as Player]], inventory, inventoryInfo.from_list, inventoryInfo.from_index)
|
||||
end
|
||||
|
||||
-- Checking if a slot is an equipment slot.
|
||||
local equipGroup = stringMatch(inventoryInfo.to_list, 'equip_%S+')
|
||||
if equipGroup == nil then
|
||||
return
|
||||
end
|
||||
|
||||
-- We receive the item that the player wants to place in the equipment slot.
|
||||
local itemStack = inventory:get_stack(inventoryInfo.from_list, inventoryInfo.from_index)
|
||||
assert(itemStack ~= nil)
|
||||
|
||||
if coreGetItemGroup(itemStack:get_name(), equipGroup) == inventoryInfo.to_index then
|
||||
return
|
||||
else
|
||||
return 0
|
||||
end
|
||||
elseif action == 'put' then
|
||||
-- Checking if a slot is an equipment slot.
|
||||
local equipGroup = stringMatch(inventoryInfo.listname, 'equip_%S+')
|
||||
if equipGroup == nil then
|
||||
return
|
||||
end
|
||||
|
||||
-- We receive the item that the player wants to place in the equipment slot.
|
||||
local itemStack = inventoryInfo.stack
|
||||
|
||||
if coreGetItemGroup(itemStack:get_name(), equipGroup) == inventoryInfo.index then
|
||||
return
|
||||
else
|
||||
return 0
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
|
||||
---Checks if the inventory list is an inventory list of bags.
|
||||
---@param listName string
|
||||
---@return boolean
|
||||
local function isBagInventoryList(listName)
|
||||
if listName == 'inventory_back' or
|
||||
listName == 'inventory_left_thigh' or
|
||||
listName == 'inventory_right_thigh' then
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
---Returns the copy ItemStack of the bag whose inventory is
|
||||
---expanded in the specified inventory list.
|
||||
---@param inventory InvRef
|
||||
---@param listName string
|
||||
---@return ItemStack
|
||||
local function getBagSlotListName(inventory, listName)
|
||||
local bagSlotName = stringMatch(listName, 'inventory_(%S+)')
|
||||
assert(bagSlotName ~= nil)
|
||||
|
||||
local bagItem = inventory:get_stack('equip_'..bagSlotName, 1)
|
||||
assert(not bagItem:is_empty())
|
||||
|
||||
return bagItem
|
||||
end
|
||||
|
||||
-- We check whether an item contains allow or disallow item groups
|
||||
-- to ensure that only allow items are placed in bags.
|
||||
core.register_allow_player_inventory_action(function(player, action, inventory, inventoryInfo)
|
||||
if action == 'move' and isBagInventoryList(inventoryInfo.to_list) then
|
||||
local bagItem = getBagSlotListName(inventory, inventoryInfo.to_list)
|
||||
local movableItemName = inventory:get_stack(inventoryInfo.from_list, inventoryInfo.from_index):get_name()
|
||||
|
||||
local bagSetting = SmcGetItemDefinition(bagItem).settings
|
||||
local allowedGroups = bagSetting._itemstack_inventory_allowed_groups
|
||||
local disallowedGroups = bagSetting._itemstack_inventory_disallowed_groups
|
||||
|
||||
if allowedGroups ~= nil then
|
||||
for _, group in ipairs(allowedGroups) do
|
||||
if coreGetItemGroup(movableItemName, group) > 0 then
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
return 0
|
||||
end
|
||||
|
||||
if disallowedGroups ~= nil then
|
||||
for _, group in ipairs(disallowedGroups) do
|
||||
if coreGetItemGroup(movableItemName, group) > 0 then
|
||||
return 0
|
||||
end
|
||||
end
|
||||
|
||||
return
|
||||
end
|
||||
|
||||
elseif action == 'put' and isBagInventoryList(inventoryInfo.listname) then
|
||||
local bagItem = getBagSlotListName(inventory, inventoryInfo.listname)
|
||||
local movableItemName = inventoryInfo.stack
|
||||
|
||||
local bagSetting = SmcGetItemDefinition(bagItem).settings
|
||||
local allowedGroups = bagSetting._itemstack_inventory_allowed_groups
|
||||
local disallowedGroups = bagSetting._itemstack_inventory_disallowed_groups
|
||||
|
||||
if allowedGroups ~= nil then
|
||||
for _, group in ipairs(allowedGroups) do
|
||||
if coreGetItemGroup(movableItemName, group) > 0 then
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
return 0
|
||||
end
|
||||
|
||||
if disallowedGroups ~= nil then
|
||||
for _, group in ipairs(disallowedGroups) do
|
||||
if coreGetItemGroup(movableItemName, group) > 0 then
|
||||
return 0
|
||||
end
|
||||
end
|
||||
|
||||
return
|
||||
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 = stringMatch(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 = SmcGetItemInventoryList(bagItem) or {}
|
||||
|
||||
inventory:set_size(bagInventoryListName, SmcGetItemInventoryListSize(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)
|
||||
|
||||
SmcSetItemInventoryList(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
|
||||
-- (this enables and disables the slot overlay).
|
||||
core.register_on_player_inventory_action(function(player, action, inventory, inventoryInfo)
|
||||
local playerName = player:get_player_name()
|
||||
if action == 'move' then
|
||||
local equipGroup = stringMatch(inventoryInfo.to_list, 'equip_%S+') or
|
||||
stringMatch(inventoryInfo.from_list, 'equip_%S+')
|
||||
|
||||
if equipGroup ~= nil then
|
||||
onBagSlotMove(inventory, inventoryInfo)
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
end
|
||||
elseif action == 'put' then
|
||||
local equipGroup = stringMatch(inventoryInfo.listname, 'equip_%S+')
|
||||
|
||||
if equipGroup ~= nil then
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
end
|
||||
elseif action == 'take' then
|
||||
local equipGroup = stringMatch(inventoryInfo.listname, 'equip_%S+')
|
||||
|
||||
if equipGroup ~= nil then
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
end
|
||||
end
|
||||
end)
|
||||
@@ -1,3 +1,7 @@
|
||||
---@type PlayerInventoryUI
|
||||
local PlayerInventoryUI = ...
|
||||
|
||||
local assert = assert
|
||||
local stringMatch = string.match
|
||||
|
||||
-- Handling the press of the inventory mode change button.
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
---@type PlayerInventoryUI
|
||||
local PlayerInventoryUI = ...
|
||||
|
||||
local assert = assert
|
||||
local stringMatch = string.match
|
||||
|
||||
-- Handling input in the items list search bar.
|
||||
PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::%S+::items_list_search_field', function(player, fieldName, fieldValue)
|
||||
local tabType, tabName = stringMatch(fieldName, '(%S+)::(%S+)::items_list_search_field')
|
||||
assert(tabType == 'left' or tabType == 'right')
|
||||
assert(tabName == 'craft_tab' or tabName == 'node_tab' or tabName == 'admin_list_tab')
|
||||
|
||||
local playerName = player:get_player_name()
|
||||
|
||||
---@type PlayerInventoryUI.CraftListTabData|PlayerInventoryUI.AdminListTabData
|
||||
local tabData
|
||||
if tabName == 'craft_tab' or tabName == 'node_tab' then
|
||||
tabData = PlayerInventoryUI:getTabCraftData(playerName, tabType, tabName)
|
||||
elseif tabName == 'admin_list_tab' then
|
||||
tabData = PlayerInventoryUI:getAdminListTabData(playerName, tabType)
|
||||
else
|
||||
error('Invalid tabName: '..tabName)
|
||||
end
|
||||
|
||||
if tabData.searchFieldValue == fieldValue then
|
||||
return
|
||||
end
|
||||
|
||||
tabData.searchFieldValue = fieldValue
|
||||
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
end)
|
||||
|
||||
-- Handling the button press for flipping through the pages of an item list.
|
||||
PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::%S+::items_list_page_[%a]+', function(player, fieldName, fieldValue)
|
||||
local tabType, tabName, pageFlip = stringMatch(fieldName, '(%S+)::(%S+)::items_list_page_([%a]+)')
|
||||
assert(tabType == 'left' or tabType == 'right')
|
||||
assert(tabName == 'craft_tab' or tabName == 'node_tab' or tabName == 'admin_list_tab')
|
||||
assert(pageFlip == 'back' or pageFlip == 'forward')
|
||||
|
||||
local playerName = player:get_player_name()
|
||||
|
||||
---A factor that determines whether the page moves backward or forward
|
||||
---@type integer
|
||||
local pageFactor
|
||||
if pageFlip == 'back' then
|
||||
pageFactor = -1
|
||||
else
|
||||
pageFactor = 1
|
||||
end
|
||||
|
||||
---@type PlayerInventoryUI.CraftListTabData|PlayerInventoryUI.AdminListTabData
|
||||
local tabData
|
||||
if tabName == 'craft_tab' or tabName == 'node_tab' then
|
||||
tabData = PlayerInventoryUI:getTabCraftData(playerName, tabType, tabName)
|
||||
elseif tabName == 'admin_list_tab' then
|
||||
tabData = PlayerInventoryUI:getAdminListTabData(playerName, tabType)
|
||||
else
|
||||
error('Invalid tabName: '..tabName)
|
||||
end
|
||||
|
||||
local pageNumber = tabData.pageNumber or 1
|
||||
pageNumber = pageNumber + pageFactor
|
||||
if pageNumber <= 0 then
|
||||
pageNumber = 1
|
||||
end
|
||||
|
||||
tabData.pageNumber = pageNumber
|
||||
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
end)
|
||||
|
||||
-- Processing closing of the selected item group in the crafting menu (returning to the main list of crafts).
|
||||
PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::%S+::close_group', function(player, fieldName, fieldValue)
|
||||
local tabType, tabName = stringMatch(fieldName, '(%S+)::(%S+)::close_group')
|
||||
assert(tabType == 'left' or tabType == 'right')
|
||||
assert(tabName == 'craft_tab' or tabName == 'node_tab')
|
||||
|
||||
local playerName = player:get_player_name()
|
||||
|
||||
local craftListTabData = PlayerInventoryUI:getTabCraftData(playerName, tabType, tabName)
|
||||
craftListTabData.selectedGroup = nil
|
||||
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
end)
|
||||
|
||||
-- Handling the button click for selecting an item for crafting.
|
||||
-- Opens the item description and allows you to craft the selected item.
|
||||
PlayerInventoryUI:registerOnFormFieldActionMatch('.+::%S+::%S+::items_list_button', function(player, fieldName, fieldValue)
|
||||
local name, tabType, tabName = string.match(fieldName, '(.+)::(%S+)::(%S+)::items_list_button')
|
||||
assert(name ~= nil)
|
||||
assert(tabType == 'left' or tabType == 'right')
|
||||
assert(tabName == 'craft_tab' or tabName == 'node_tab' or tabName == 'admin_list_tab')
|
||||
|
||||
local playerName = player:get_player_name()
|
||||
|
||||
if tabName == 'craft_tab' or tabName == 'node_tab' then
|
||||
local craftListTabData = PlayerInventoryUI:getTabCraftData(playerName, tabType, tabName)
|
||||
|
||||
craftListTabData.selectedRecipeIndex = 1
|
||||
craftListTabData.selectedCraft = name
|
||||
elseif tabName == 'admin_list_tab' then
|
||||
local craftListTabData = PlayerInventoryUI:getAdminListTabData(playerName, tabType)
|
||||
|
||||
craftListTabData.selectedItem = name
|
||||
else
|
||||
error('Invalid tabName: '..tabName)
|
||||
end
|
||||
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
end)
|
||||
|
||||
-- Handling the click of the crafting group selection button.
|
||||
-- Causes grouped items to be expanded into a separate list.
|
||||
PlayerInventoryUI:registerOnFormFieldActionMatch('.+::%S+::%S+::group_button',function(player, fieldName, fieldValue)
|
||||
local groupName, tabType, tabName = string.match(fieldName, '(.+)::(%S+)::(%S+)::group_button')
|
||||
assert(groupName ~= nil)
|
||||
assert(tabType == 'left' or tabType == 'right')
|
||||
assert(tabName == 'craft_tab' or tabName == 'node_tab')
|
||||
|
||||
local playerName = player:get_player_name()
|
||||
|
||||
local craftListTabData = PlayerInventoryUI:getTabCraftData(playerName, tabType, tabName)
|
||||
|
||||
craftListTabData.selectedGroup = groupName
|
||||
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
end)
|
||||
|
||||
Reference in New Issue
Block a user