• 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+ Problem with autoloot

Piquenoelmal

I'm in an eternal depression
Joined
Dec 1, 2009
Messages
31
Reaction score
8
Location
São Paulo
Hello, friends. I'm trying to use this Auto Loot system.
I could fix the problems during compiling, but I have a problem with the talks action.

This is the error in console:

As the console returns, the problem is with the method getAutoLootList, but I can't find why it's returning a nil value.
The changes that I made was:
  • player.h
Code:
std::set<uint32_t> autoLootList;

void addAutoLootItem(uint16_t itemId);
void removeAutoLootItem(uint16_t itemId);
bool getAutoLootItem(uint16_t itemId);
  • player.cpp
C++:
void Player::addAutoLootItem(uint16_t itemId)
{
    autoLootList.insert(itemId);
}
void Player::removeAutoLootItem(uint16_t itemId)
{
    autoLootList.erase(itemId);
}
bool Player::getAutoLootItem(const uint16_t itemId)
{
    return autoLootList.find(itemId) != autoLootList.end();
}
  • monsters.cpp
C++:
std::string autolooted = "";

if (owner->getAutoLootItem(item->getID())) {
                    g_game.internalPlayerAddItem(owner, item, true, CONST_SLOT_WHEREEVER);
                    autolooted = autolooted + ", " + item->getNameDescription();
                } else if (g_game.internalAddItem(corpse, item) != RETURNVALUE_NOERROR) {
                   corpse->internalAddThing(item);
               }

std::string lootMsg = corpse->getContentDescription();

if (autolooted != "" and corpse->getContentDescription() == "nothing") {
                lootMsg = autolooted.erase(0,2) + " that was auto looted";
            } else if (autolooted != "") {
                lootMsg =  corpse->getContentDescription() + " and " + autolooted.erase(0,2) + " was auto looted.";
            }
  • luascript.cpp
C++:
//Autoloot wrote by Psychonaut#4421
int LuaScriptInterface::luaPlayerAddAutoLootItem(lua_State* L)
{
    // player:addAutoLootItem(itemId)
    Player* player = getUserdata<Player>(L, 1);
    if (!player) {
        lua_pushnil(L);
        return 1;
    }
    uint16_t itemId;
    if (isNumber(L, 2)) {
        itemId = getNumber<uint16_t>(L, 2);
    } else {
        itemId = Item::items.getItemIdByName(getString(L, 2));
        if (itemId == 0) {
            lua_pushnil(L);
            return 1;
        }
    }
    player->addAutoLootItem(itemId);
    pushBoolean(L, true);
    return 1;
}
int LuaScriptInterface::luaPlayerRemoveAutoLootItem(lua_State* L)
{
    // player:removeAutoLootItem(itemId)
    Player* player = getUserdata<Player>(L, 1);
    if (!player) {
        lua_pushnil(L);
        return 1;
    }
    uint16_t itemId;
    if (isNumber(L, 2)) {
        itemId = getNumber<uint16_t>(L, 2);
    } else {
        itemId = Item::items.getItemIdByName(getString(L, 2));
        if (itemId == 0) {
            lua_pushnil(L);
            return 1;
        }
    }
    player->removeAutoLootItem(itemId);
    pushBoolean(L, true);
    return 1;
}
int LuaScriptInterface::luaPlayerGetAutoLootItem(lua_State* L)
{
    // player:getAutoLootItem(itemId)
    Player* player = getUserdata<Player>(L, 1);
    if (!player) {
        lua_pushnil(L);
        return 1;
    }
    uint16_t itemId;
    if (isNumber(L, 2)) {
        itemId = getNumber<uint16_t>(L, 2);
    } else {
        itemId = Item::items.getItemIdByName(getString(L, 2));
        if (itemId == 0) {
            lua_pushnil(L);
            return 1;
        }
    }
    if (player->getAutoLootItem(itemId)) {
        pushBoolean(L, true);
    } else {
        pushBoolean(L, false);
    }
    return 1;
}
int LuaScriptInterface::luaPlayerGetAutoLootList(lua_State* L)
{
    // player:getAutoLootList()
    Player* player = getUserdata<Player>(L, 1);
    if (player) {
        std::set<uint32_t> value = player->autoLootList;
  
        if (value.size() == 0) {
          lua_pushnil(L);
          return 1;
        }
        int index = 0;
        lua_createtable(L, value.size(), 0);
        for(auto i : value) {
            lua_pushnumber(L, i);
            lua_rawseti(L, -2, ++index);
        }
  
    } else {
        lua_pushnil(L);
    }
    return 1;
}
  • compat.lua
LUA:
function getPlayerAutoLootItem(cid, itemId) local p = Player(cid) return p ~= nil and p:getAutoLootItem(itemId) or false end
function getPlayerAutoLootList(cid) local p = Player(cid) return p ~= nil and p:getAutoLootList() or false end
function removePlayerAutoLootItem(cid, itemId) local p = Player(cid) return p ~= nil and p:removeAutoLootItem(itemId) or false end
function addPlayerAutoLootItem(cid, itemId) local p = Player(cid) return p ~= nil and p:addAutoLootItem(itemId) or false end

The problem is that the getAutoLootList is returning a nil value, right? I tried to change the method, but didn't work.
So, please, any help here would be appreciate.
THANKS! :)
 
Back
Top