PlayerInventory & PlayerInventoryUI: Adding minimal functionality to the crafting pane. #34
This commit is contained in:
@@ -20,4 +20,8 @@ core.register_chatcommand('show_inv',
|
||||
}
|
||||
)
|
||||
|
||||
local require = Mod.getInfo('smc__core__player').require
|
||||
|
||||
require('Player.inventory')
|
||||
|
||||
return Player
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
---@class Player.GUI
|
||||
local GUI = {}
|
||||
|
||||
return GUI
|
||||
54
mods/CORE/Player/src/Player/inventory.lua
Normal file
54
mods/CORE/Player/src/Player/inventory.lua
Normal file
@@ -0,0 +1,54 @@
|
||||
local PrimitiveCraftHandler = Smc.requireCraftSystemPrimitiveCraftHandler()
|
||||
|
||||
---@type PlayerInventoryUI
|
||||
local PlayerInventoryUI = Mod.getInfo('smc__ui__player_inventory').require('PlayerInventoryUI')
|
||||
|
||||
PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::%S+::item_button',function(player, fieldName, fieldValue)
|
||||
local craftName, paneType = string.match(fieldName, '(%S+)::(%S+)::item_button')
|
||||
assert(craftName ~= nil)
|
||||
assert(paneType == 'left' or paneType == 'right')
|
||||
|
||||
local playerName = player:get_player_name()
|
||||
PlayerInventoryUI:setContex(playerName, paneType..'_pane_craft_selected_craft', craftName)
|
||||
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
end)
|
||||
|
||||
PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::craft_item', function(player, fieldName, fieldValue)
|
||||
---@type InvRef
|
||||
local inv = player:get_inventory()
|
||||
|
||||
local playerName = player:get_player_name()
|
||||
local paneType = string.match(fieldName, '(%S+)::craft_item')
|
||||
assert(paneType == 'left' or paneType == 'right')
|
||||
|
||||
local craftName = PlayerInventoryUI:getContex(playerName, paneType..'_pane_craft_selected_craft')
|
||||
local craft = PrimitiveCraftHandler:getCraftByName(craftName)
|
||||
|
||||
local OutputStack = ItemStack(craft.output)
|
||||
OutputStack:set_count(OutputStack:get_count() * fieldValue)
|
||||
|
||||
local recipeStacks = {}
|
||||
for _, itemName in ipairs(craft.recipe) do
|
||||
local item = ItemStack(itemName)
|
||||
item:set_count(item:get_count() * fieldValue)
|
||||
table.insert(recipeStacks, item)
|
||||
end
|
||||
|
||||
for _, item in ipairs(recipeStacks) do
|
||||
if not inv:contains_item('main', item) then
|
||||
core.chat_send_player(playerName, 'Not enough materials for crafting!')
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
if inv:room_for_item('main', OutputStack) then
|
||||
for _, item in ipairs(recipeStacks) do
|
||||
inv:remove_item('main', item)
|
||||
end
|
||||
|
||||
inv:add_item('main', OutputStack)
|
||||
else
|
||||
core.chat_send_player(playerName, 'There is no space in the inventory!')
|
||||
end
|
||||
end)
|
||||
Reference in New Issue
Block a user