• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

TFS 1.X+ containerItem

theduck

Member
Joined
Dec 6, 2018
Messages
246
Reaction score
20
bp.png




Code:
 if AutoLootList.players[playerId] ~= nil then
    local lootList = AutoLootList.players[playerId].lootList
          
        if lootList ~= nil then
            if corpse:getType():isCorpse()    then
                for a = corpse:getSize() - 1, 0, -1 do
                    local containerItem = corpse:getItem(a)
                    if containerItem then
                        local containerItemId = containerItem:getId()
                        for i=1, #lootList do
                            local lootItemId = lootList[i] 
                               
                            if lootItemId == containerItemId   then 
                     
                            local item = ItemType(lootItemId)  
                         containerItem:moveTo(backpack) 
                                 end
                             end
                        end  
                    end
                end
            end
        end  
     end    
end


The problem i'm having is when the backpack slots are over he doesn't put the items into the next backpack he keeps putting the items in the backpack creating infinite slots
 
Solution
So lets create a function that searches recursively for the next available container inside backpack, and move the item there?

function:
Lua:
function getNextAvailableContainer(container)
    if container:getEmptySlots() > 0 then
        return container
    end

    for index = 0, container:getSize()-1, 1 do
        local item = container:getItem(index)

        if item:isContainer() then
            item = getNextAvailableContainer(item)
           
            if item ~= nil then
                return item
            end
        end
    end

    return nil
end

sample usage code:
Lua:
if backpack:isContainer() then
    local availableContainer = getNextAvailableContainer(backpack)

    if availableContainer ~= nil then...
You need to post the bit of the code that defines backpack
Lua:
local backpack = ???

Because that's the bit that needs editing.
The backpack variables needs to change depending on if there are slots available.

Just post the full script.
 
Not sure why you would call an item inside a corpse a container item, as it might not be a container.
I would call it lootItem.

When you move this lootItem to your container, you don't do any checks and just force a moveTo the players backpack.

You can replace:
Lua:
containerItem:moveTo(backpack)

with:
Lua:
if backpack:isContainer() and backpack:getEmptySlots() > 0 then
    containerItem:moveTo(backpack)
else 
    a = 0 -- Stop "for" loop
    -- todo: send cancel message to player explaining his loot backpack is full.
end
 
Not sure why you would call an item inside a corpse a container item, as it might not be a container.
I would call it lootItem.

When you move this lootItem to your container, you don't do any checks and just force a moveTo the players backpack.

You can replace:
Lua:
containerItem:moveTo(backpack)

with:
Lua:
if backpack:isContainer() and backpack:getEmptySlots() > 0 then
    containerItem:moveTo(backpack)
else
    a = 0 -- Stop "for" loop
    -- todo: send cancel message to player explaining his loot backpack is full.
end

I was going to post something similar but with the code as-is it would only iterate one extra backpack.
Where the backpack is originally defined it should iterate down to an empty backpack or return false notify player backpacks are full.
 
Not sure why you would call an item inside a corpse a container item, as it might not be a container.
I would call it lootItem.

When you move this lootItem to your container, you don't do any checks and just force a moveTo the players backpack.

You can replace:
Lua:
containerItem:moveTo(backpack)

with:
Lua:
if backpack:isContainer() and backpack:getEmptySlots() > 0 then
    containerItem:moveTo(backpack)
else
    a = 0 -- Stop "for" loop
    -- todo: send cancel message to player explaining his loot backpack is full.
end




It worked more he does not move to the next backpack that has inside it appears msg whenever the player has the full backpack let's say he has 10 backpack one inside the other he is the first full even though the others are empty he does everything right places the loot plus the full backpack message keeps popping up would have to make it appear only when he no longer had backpack to put
 
Last edited:
So lets create a function that searches recursively for the next available container inside backpack, and move the item there?

function:
Lua:
function getNextAvailableContainer(container)
    if container:getEmptySlots() > 0 then
        return container
    end

    for index = 0, container:getSize()-1, 1 do
        local item = container:getItem(index)

        if item:isContainer() then
            item = getNextAvailableContainer(item)
           
            if item ~= nil then
                return item
            end
        end
    end

    return nil
end

sample usage code:
Lua:
if backpack:isContainer() then
    local availableContainer = getNextAvailableContainer(backpack)

    if availableContainer ~= nil then
        containerItem:moveTo(availableContainer)
    else
        a = 0 -- Stop "for" loop
        -- todo: send cancel message to player explaining his loot backpack is full.
    end
end

But you might want to be mindful of recursive searches inside for loops. Perhaps getNextAvailableContainer should return a list of all available containers (outside of the for loop) and then you just check their empty slots during the loop.
 
Last edited:
Solution
Back
Top