GLOBAL.setmetatable(env, { __index = function(t, k) return GLOBAL.rawget(GLOBAL, k) end }) local containers = require "containers" -- 让隐士晾肉架支持堆叠 containers.params.meatrack_hermit.acceptsstacks = true -- ============================================================ -- 限制隐士晾肉架上物品堆叠不超过 10 -- ============================================================ local MAX_STACK = 10 -- 方案:拦截 GiveItem,当物品堆叠超过 10 时,只放入 10 个,多余的退回 AddComponentPostInit("container", function(self) if self.inst.prefab ~= "meatrack_hermit" then return end local old_GiveItem = self.GiveItem if old_GiveItem then self.GiveItem = function(self, item, slot, drop_on_fail, ...) if item and item.components.stackable then local current = item.components.stackable:StackSize() -- 如果物品堆叠超过 10,拆分成两部分 if current > MAX_STACK then item.components.stackable:SetStackSize(current - MAX_STACK) local overflow = SpawnPrefab(item.prefab) if overflow and overflow.components.stackable then overflow.components.stackable:SetStackSize(MAX_STACK) end -- 先把不超过 10 的部分放入 old_GiveItem(self, overflow, slot, drop_on_fail, ...) -- 多出来的部分退回玩家背包或丢在地上 return old_GiveItem(self, item, nil, true, ...) end end return old_GiveItem(self, item, slot, drop_on_fail, ...) end end end) -- ============================================================ -- Hook dryingrack 组件,保留堆叠数量 -- ============================================================ local DEFAULT_MEAT_BUILD = "meat_rack_food" local hooked = false local function GetUpValve(fn, name, maxlevel, max, level) if type(fn) ~= "function" then return end maxlevel = maxlevel or 5 level = level or 0 max = max or 20 for i = 1, max, 1 do local upname, upvalue = debug.getupvalue(fn, i) if upname == name then return upvalue end if level < maxlevel and type(upvalue) == "function" then local result = GetUpValve(upvalue, name, maxlevel, max, level + 1) if result then return result end end end end local function SetUpValve(fn, name, set, maxlevel, max, level) if type(fn) ~= "function" then return end maxlevel = maxlevel or 5 level = level or 0 max = max or 20 for i = 1, max, 1 do local upname, upvalue = debug.getupvalue(fn, i) if upname == name then return debug.setupvalue(fn, i, set) end if level < maxlevel and type(upvalue) == "function" then local result = SetUpValve(upvalue, name, set, maxlevel, max, level + 1) if result then return result end end end end AddComponentPostInit("dryingrack", function(self) if hooked then return end hooked = true local OnDoneDrying = GetUpValve(self.ResumeDrying, "OnDoneDrying") if OnDoneDrying == nil then return end local old_OnDoneDrying = OnDoneDrying local function newOnDoneDrying(inst, rack, item) if rack.inst.prefab == "meatrack_hermit" then rack.dryinginfo[item] = nil local slot = rack.container:GetItemSlot(item) local product_name = item.components.dryable and item.components.dryable:GetProduct() or nil if product_name then local product = SpawnPrefab(product_name) if product then local build = item.components.dryable:GetDriedBuildFile() or DEFAULT_MEAT_BUILD if product.components.inventoryitem then product.components.inventoryitem:InheritMoisture( item.components.inventoryitem:GetMoisture(), item.components.inventoryitem:IsWet() ) end local stack = item.components.stackable and item.components.stackable.stacksize or 1 item:Remove() rack.container:GiveItem(product, slot) if stack > 1 and product.components.stackable then product.components.stackable:SetStackSize(stack) end local info = rack.dryinginfo[product] if info == nil then if build ~= DEFAULT_MEAT_BUILD then rack.dryinginfo[product] = { build = build } end if rack.showitemfn then rack.showitemfn(rack.inst, slot, product.prefab, build) end end return product end end end return old_OnDoneDrying(inst, rack, item) end SetUpValve(self.ResumeDrying, "OnDoneDrying", newOnDoneDrying) end)