Resolved Lua auto loot for 3.3.5

Alppha

Member
Alppha Rep
0
0
0
Rep
0
Alppha Vouches
0
0
0
Vouches
0
Posts
19
Likes
0
Bits
2 YEARS
2 YEARS OF SERVICE
LEVEL 1 105 XP
Anybody know any good lua script for auto looting creature after kill?
 

Tommy

Staff
Staff
Tommy Rep
1
0
0
Rep
1
Tommy Vouches
0
0
0
Vouches
0
Posts
185
Likes
101
2 YEARS
2 YEARS OF SERVICE
LEVEL 6 310 XP
There's quite a lot to this question to be done. Yes, it's possible. Probably better to handle within C++ than in Lua due to redundant additions and requirements to handle this.

I wrote a rough draft of what you want. More logic needs to be done on it. E.g. Check if player is on the current quest to get that specific quest item and check if the item should be lootable (basically test items, etc).
Also while testing make sure to have GM off or you're going to think it's not working properly.

More Info:
  • Loads all data from creature_loot_template table within your world database.
  • Inserts all data into CreatureLootTable.
  • Player event when a player kills a creature it will iterate through that table to give the player loot based on the loot data.
  • When the creature dies it will set the creature's flags to UNIT_FLAG_NOT_SELECTABLE so the player cannot loot the creature. That way they cannot receive twice the loot. This will probably be a later headache if the player did not loot higher quality items and they might receive it through the mail.

Script:
Code:
local CreatureLootTable = { }
local UNIT_FIELD_FLAGS = 59
local UNIT_FLAG_NOT_SELECTABLE = 33554432

function OnLoadLootTables(event)
    local t = { }
    local query = WorldDBQuery("SELECT Entry, Item, MinCount, MaxCount, Chance FROM creature_loot_template")
    if query then
        repeat
            t =
            {
                entry = query:GetUInt32(0),
                item = query:GetUInt32(1),
                minCount = query:GetUInt8(2),
                maxCount = query:GetUInt8(3),
                chance = query:GetFloat(4)
            };
            table.insert(CreatureLootTable, t)
        until not query:NextRow()
        print("Completed Loading Loot Tables for custom_auto_loot_creature.lua")
    end
end

function OnKillCreature(event, killer, killed)
    local creatureEntry = killed:GetEntry()
    killed:SetFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_NOT_SELECTABLE) -- Set UnSelectable
    for _,v in pairs(CreatureLootTable) do
        if v.entry == creatureEntry then
            if math.random(0, 100) <= v.chance then
                if killer:AddItem(v.item, math.random(v.minCount, v.maxCount)) ~= nil then
                    killer:SendBroadcastMessage("Looted item!")
                else
                    killer:SendBroadcastMessage("Failed to loot item!")
                end
            end
        end
    end
end

RegisterPlayerEvent(7, OnKillCreature)
RegisterServerEvent(14, OnLoadLootTables)
 
Last edited:
Liked By 2 members :

Alppha

Member
Alppha Rep
0
0
0
Rep
0
Alppha Vouches
0
0
0
Vouches
0
Posts
19
Likes
0
Bits
2 YEARS
2 YEARS OF SERVICE
LEVEL 1 105 XP
Thanks gonna try it!
 

Tommy

Staff
Staff
Tommy Rep
1
0
0
Rep
1
Tommy Vouches
0
0
0
Vouches
0
Posts
185
Likes
101
2 YEARS
2 YEARS OF SERVICE
LEVEL 6 310 XP

3,389

1,271

9,555

428

Top