From abf2f008b9d06bd94169d53b0c24e119d4ed9e1d Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Sun, 30 Mar 2025 00:36:00 +0100 Subject: [PATCH] Add shop example and fuck with fabricator a little --- QuickStackToBag/Lua/Autorun/init.lua | 145 +++++++++++++++++++++++++-- 1 file changed, 138 insertions(+), 7 deletions(-) diff --git a/QuickStackToBag/Lua/Autorun/init.lua b/QuickStackToBag/Lua/Autorun/init.lua index 7b7e5f6..885d31e 100644 --- a/QuickStackToBag/Lua/Autorun/init.lua +++ b/QuickStackToBag/Lua/Autorun/init.lua @@ -7,6 +7,10 @@ LuaUserData.RegisterType( LuaUserData.MakeFieldAccessible(Descriptors['Barotrauma.Items.Components.ItemContainer'], 'slotRestrictions') LuaUserData.MakeFieldAccessible(Descriptors['Barotrauma.ItemInventory'], 'slots') LuaUserData.MakeFieldAccessible(Descriptors["Barotrauma.CharacterInventory"], "slots") +LuaUserData.RegisterType("Barotrauma.Store") +LuaUserData.RegisterType("Barotrauma.GUIComponent") +LuaUserData.RegisterType("Barotrauma.PurchasedItem") +LuaUserData.RegisterType("Barotrauma.ItemPrefab") -- Simple configuration local CONFIG = { @@ -22,6 +26,26 @@ local MOD_VERSION = "1.1.0" print(MOD_NAME .. " v" .. MOD_VERSION .. " loaded!") +---@param table table +---@param depth number? +function DumpTable(table, depth) + if depth == nil then + depth = 0 + end + if (depth > 200) then + print("Error: Depth > 200 in dumpTable()") + return + end + for k, v in pairs(table) do + if (type(v) == "table") then + print(string.rep(" ", depth) .. k .. ":") + DumpTable(v, depth + 1) + else + print(string.rep(" ", depth) .. k .. ": ", v) + end + end +end + -- Debugging helper function local function debugPrint(message) if CONFIG.DEBUG_MODE then @@ -299,6 +323,7 @@ end -- 6 and 7 are hands -- 9..18 are main slots local inventorySlotsToStack = { 6, 7, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 } +local BAG_SLOT = 8 local function quickStackItems(character) if not character then debugPrint("No character found") @@ -313,6 +338,12 @@ local function quickStackItems(character) return end + -- for i, slot in ipairs(inventory.slots) do + -- if slot.items and #slot.items > 0 then + -- print(string.format("Item at slot %d is %s", i, slot.items[1].Prefab.Identifier.Value)) + -- end + -- end + local itemTree, err = tryBuildItemTree(inventory) if err then debugPrint(string.format("Error building item tree: %s", err)) @@ -370,8 +401,6 @@ local function quickStackItems(character) for _, error in ipairs(errors) do print(string.format("Error stacking item: %s", error)) end - - PrintSelectedRecipeRequirements() end -- Hook into player control to listen for key press @@ -427,12 +456,13 @@ end ---@field targetItem {identifier: string, name: string, amount: number} ---@field requiredItems {amount: number, minCondition: number, maxCondition: number, prefabs: string[]}[] +---@param fabricator Barotrauma.FabricatorComponent ---@return RecipeInfo, string? -local function getSelectedRecipeRequirements() - local openFabricator, err = getOpenFabricator() - if err then return nil, err end +local function getSelectedRecipeRequirements(fabricator) + -- local openFabricator, err = getOpenFabricator() + -- if err then return nil, err end + -- local fabricator = openFabricator.fabricator - local fabricator = openFabricator.fabricator local selectedRecipe = fabricator.SelectedItem if not selectedRecipe then return nil, "No selected recipe found" end @@ -469,9 +499,110 @@ Hook.Patch("Barotrauma.Character", "ControlLocalPlayer", function(instance, ptab local character = instance if not character then return end - local recipe, err = getSelectedRecipeRequirements() + local fabricator, err = getOpenFabricator() + if err then + print(string.format("Error getting open fabricator: %s", err)) + return + end + + local recipe, err = getSelectedRecipeRequirements(fabricator.fabricator) if err then print(string.format("Error getting selected recipe requirements: %s", err)) end DumpTable(recipe) + + local toGet = recipe.requiredItems + ---@type fun(item: Barotrauma.Item): boolean + local filter = function(item) + return true + end + local items = enqueueInventory(character.inventory.slots[BAG_SLOT], {}, filter) + DumpTable(items) end, Hook.HookMethodType.After) + +-- -- Hook into GUI creation for the store interface +-- Hook.Add("gui.storeinterface.created", "storeHandler", function(storeInterface) +-- print("Store opened!") +-- +-- -- Wait a moment for store to initialize +-- Timer.Wait(function() +-- -- Get cargo manager and current store +-- local cargoManager = Game.GameSession.Campaign.CargoManager +-- local currentStore = cargoManager.CurrentStore +-- +-- if currentStore then +-- print("In store: " .. currentStore.Name.Value) +-- +-- -- Display items available in store +-- if currentStore.Stock then +-- print("Store has " .. #currentStore.Stock .. " items for sale") +-- +-- -- Example: Add the first item from stock to cart +-- if #currentStore.Stock > 0 then +-- local firstItem = currentStore.Stock[1] +-- cargoManager:ModifyItemQuantityInBuyCrate(currentStore.Identifier, firstItem.ItemPrefab, 1) +-- print("Added 1 " .. firstItem.ItemPrefab.Name.Value .. " to cart") +-- end +-- end +-- end +-- end, 500) +-- end) +-- +-- -- Check if a player is currently in a store dialog +-- local function isInStore() +-- if Game and Game.GameSession and Game.GameSession.Campaign then +-- local cargoManager = Game.GameSession.Campaign.CargoManager +-- if cargoManager and cargoManager.CurrentStore then +-- return true, cargoManager.CurrentStore +-- end +-- end +-- return false, nil +-- end +-- +-- -- Usage example +-- local inStore, currentStore = isInStore() +-- if inStore then +-- print("Player is in store: " .. currentStore.Name.Value) +-- end +-- +-- -- To modify the quantity of an item in the cart: +-- local function modifyCartItemQuantity(itemPrefab, newQuantity) +-- local cargoManager = Game.GameSession.Campaign.CargoManager +-- local currentStore = cargoManager.CurrentStore +-- +-- -- Find the item in the cart +-- local cartItem = cargoManager:GetBuyCrateItem(currentStore, itemPrefab) +-- +-- if cartItem then +-- -- Change needed = new quantity - current quantity +-- local changeAmount = newQuantity - cartItem.Quantity +-- -- Use the ModifyItemQuantityInBuyCrate method to change quantity +-- cargoManager:ModifyItemQuantityInBuyCrate(currentStore.Identifier, itemPrefab, changeAmount) +-- return true +-- end +-- +-- return false +-- end +-- +-- -- To clear specific item from cart: +-- local function removeItemFromCart(itemPrefab) +-- local cargoManager = Game.GameSession.Campaign.CargoManager +-- local currentStore = cargoManager.CurrentStore +-- +-- -- Find the item in the cart +-- local cartItem = cargoManager:GetBuyCrateItem(currentStore, itemPrefab) +-- +-- if cartItem then +-- -- Remove by setting quantity to negative of current quantity +-- cargoManager:ModifyItemQuantityInBuyCrate(currentStore.Identifier, itemPrefab, -cartItem.Quantity) +-- return true +-- end +-- +-- return false +-- end +-- +-- -- To clear all items from cart: +-- local function clearCart() +-- local cargoManager = Game.GameSession.Campaign.CargoManager +-- cargoManager:ClearItemsInBuyCrate() +-- end