• 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!

RevScripts auto deposit gold from monsters [tfs 1.5] 8.6

beenii

Well-Known Member
Joined
Jul 26, 2010
Messages
580
Solutions
1
Reaction score
57
Hello I was wondering if anyone has this code available

my onDropLoot:
Lua:
local ec = EventCallback

ec.onDropLoot = function(self, corpse)
    if configManager.getNumber(configKeys.RATE_LOOT) == 0 then
        return
    end

    local player = Player(corpse:getCorpseOwner())
    local mType = self:getType()
    if not player or player:getStamina() > 840 then
        local monsterLoot = mType:getLoot()
        for i = 1, #monsterLoot do
            local item = corpse:createLootItem(monsterLoot[i])
            if not item then
                print('[Warning] DropLoot:', 'Could not add loot item to corpse.')
            end
        end

        if player then
            local text = ("Loot of %s: %s"):format(mType:getNameDescription(), corpse:getContentDescription())
            local party = player:getParty()
            if party then
                party:broadcastPartyLoot(text)
            else
                player:sendTextMessage(MESSAGE_INFO_DESCR, text)
            end
        end
    else
        local text = ("Loot of %s: nothing (due to low stamina)"):format(mType:getNameDescription())
        local party = player:getParty()
        if party then
            party:broadcastPartyLoot(text)
        else
            player:sendTextMessage(MESSAGE_INFO_DESCR, text)
        end
    end
end

ec:register()


my function Container.createLootItem(self, item):


Lua:
function Container.createLootItem(self, item)
    if self:getEmptySlots() == 0 then
        return true
    end

    local itemCount = 0
    local randvalue = getLootRandom()
    local itemType = ItemType(item.itemId)
    
    if randvalue < item.chance then
        if itemType:isStackable() then
            itemCount = randvalue % item.maxCount + 1
        else
            itemCount = 1
        end
    end

    while itemCount > 0 do
        local count = math.min(100, itemCount)
        
        local subType = count
        if itemType:isFluidContainer() then
            subType = math.max(0, item.subType)
        end
        
        local tmpItem = Game.createItem(item.itemId, subType)
        if not tmpItem then
            return false
        end

        if tmpItem:isContainer() then
            for i = 1, #item.childLoot do
                if not tmpItem:createLootItem(item.childLoot[i]) then
                    tmpItem:remove()
                    return false
                end
            end

            if #item.childLoot > 0 and tmpItem:getSize() == 0 then
                tmpItem:remove()
                return true
            end
        end

        if item.subType ~= -1 then
            tmpItem:setAttribute(ITEM_ATTRIBUTE_CHARGES, item.subType)
        end

        if item.actionId ~= -1 then
            tmpItem:setActionId(item.actionId)
        end

        if item.text and item.text ~= "" then
            tmpItem:setText(item.text)
        end

        local ret = self:addItemEx(tmpItem)
        if ret ~= RETURNVALUE_NOERROR then
            tmpItem:remove()
        end

        itemCount = itemCount - count
    end
    return true
end

try this one, but it gave me null item errors:

and with the fix in same thread, also downgrades the function to tfs 1.3.
loop print in console:
[Warning] DropLoot: Could not add loot item to corpse.
 
Solution
have you done this?

lib/core/container.lua
Lua:
function Container.removeCoins(self)
    local gold = 0
    if self:getItemHoldingCount() > 0 then
        for _, item in ipairs(self:getItems()) do
            if item:isContainer() then
                gold = gold + item:removeCoins()
                -- if the bag gets empty after collecting gold, then we remove it
                if item:getItemHoldingCount() == 0 then
                    item:remove()
                end
            else
                if (item:getWorth() > 0) then
                    gold = gold + item:getWorth()
                    item:remove()
                end
            end
        end
    end
    return gold
end
...
have you done this?

lib/core/container.lua
Lua:
function Container.removeCoins(self)
    local gold = 0
    if self:getItemHoldingCount() > 0 then
        for _, item in ipairs(self:getItems()) do
            if item:isContainer() then
                gold = gold + item:removeCoins()
                -- if the bag gets empty after collecting gold, then we remove it
                if item:getItemHoldingCount() == 0 then
                    item:remove()
                end
            else
                if (item:getWorth() > 0) then
                    gold = gold + item:getWorth()
                    item:remove()
                end
            end
        end
    end
    return gold
end

scripts/eventcallbacks/monster/onDropLoot.lua, replace local text
Lua:
local gold = corpse:removeCoins()
local text = ("Loot of %s: %s"):format(mType:getNameDescription(), corpse:getContentDescription())

if (gold > 0) then
    player:setBankBalance(player:getBankBalance() + gold)
    text = ("%s, x%d gold coins were sent to your bank balance."):format(text, gold)
end

It uses getWorth() so you should have worth attribute on items.xml

Edit: Also to not confuse party members, you can edit this line too
Lua:
party:broadcastPartyLoot(text)

For this one
Lua:
party:broadcastPartyLoot(text:gsub("your (bank)", player:getName():lower().."'s %1"))
 
Last edited:
Solution
lib/core/container.lua
Lua:
function Container.removeCoins(self)
    local gold = 0
    if self:getItemHoldingCount() > 0 then
        for _, item in ipairs(self:getItems()) do
            if item:isContainer() then
                gold = gold + item:removeCoins()
                -- if the bag gets empty after collecting gold, then we remove it
                if item:getItemHoldingCount() == 0 then
                    item:remove()
                end
            else
                if (item:getWorth() > 0) then
                    gold = gold + item:getWorth()
                    item:remove()
                end
            end
        end
    end
    return gold
end

scripts/eventcallbacks/monster/onDropLoot.lua, replace local text
Lua:
local gold = corpse:removeCoins()
local text = ("Loot of %s: %s"):format(mType:getNameDescription(), corpse:getContentDescription())

if (gold > 0) then
    player:setBankBalance(player:getBankBalance() + gold)
    text = ("%s, x%d gold coins were sent to your bank balance."):format(text, gold)
end

It uses getWorth() so you should have worth attribute on items.xml

Edit: Also to not confuse party members, you can edit this line too
Lua:
party:broadcastPartyLoot(text)

For this one
Lua:
party:broadcastPartyLoot(text:gsub("your (bank)", player:getName():lower().."'s %1"))

Great system! Thanks for it ^^
 
Back
Top