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

[Creatureevent] onMoveItem(cid, item, count, toContainer, fromContainer, ...)

Summ

(\/)(;,,;)(\/) Y not?
Staff member
Global Moderator
Joined
Oct 15, 2008
Messages
4,152
Solutions
12
Reaction score
1,107
Location
Germany :O
Hey guys.
This creatureevent is required for a shop system I am going to release this week.
It basically offers every information you need and is more advanced than the default onThrow().

Let's start:
____________________________________
Creatureevent.cpp

Below:
Code:
else if(type == "throw")
    _type = CREATURE_EVENT_THROW;
Add:
Code:
else if(type == "moveitem")
    _type = CREATURE_EVENT_MOVEITEM;
____________________________________
Below:
Code:
case CREATURE_EVENT_THROW:
    return "onThrow";
Add:
Code:
case CREATURE_EVENT_MOVEITEM:
    return "onMoveItem";
____________________________________
Below:
Code:
case CREATURE_EVENT_THROW:
    return "cid, item, fromPosition, toPosition";
Add:
Code:
case CREATURE_EVENT_MOVEITEM:
    return "cid, item, count, toContainer, fromContainer, fromPos, toPos";
____________________________________
After this method:
Code:
uint32_t CreatureEvent::executeThrow(Player* player, Item* item, const Position& fromPosition, const Position& toPosition)
Add this method:
Code:
uint32_t CreatureEvent::executeMoveItem(Player* player, Item* item, uint8_t count, const Position& fromPos, const Position& toPos, Item* toContainer, Item* fromContainer, int16_t fstack)
{
    //onMoveItem(cid, item, count, toContainer, fromContainer, fromPos, toPos)
    if(m_interface->reserveEnv())
    {
        ScriptEnviroment* env = m_interface->getEnv();
        if(m_scripted == EVENT_SCRIPT_BUFFER)
        {
            env->setRealPos(player->getPosition());
            std::stringstream scriptstream;
            scriptstream << "local cid = " << env->addThing(player) << std::endl;

            env->streamThing(scriptstream, "item", item, env->addThing(item));
            scriptstream << "local count = " << count << std::endl;
            env->streamThing(scriptstream, "toContainer", toContainer, env->addThing(toContainer));
            env->streamThing(scriptstream, "fromContainer", fromContainer, env->addThing(fromContainer));
            env->streamPosition(scriptstream, "fromPos", fromPos, fstack);
            env->streamPosition(scriptstream, "toPos", toPos, 0);


            scriptstream << m_scriptData;
            bool result = true;
            if(m_interface->loadBuffer(scriptstream.str()))
            {
                lua_State* L = m_interface->getState();
                result = m_interface->getGlobalBool(L, "_result", true);
            }

            m_interface->releaseEnv();
            return result;
        }
        else
        {
            #ifdef __DEBUG_LUASCRIPTS__
            char desc[30];
            sprintf(desc, "%s", player->getName().c_str());
            env->setEvent(desc);
            #endif

            env->setScriptId(m_scriptId, m_interface);
            env->setRealPos(player->getPosition());

            lua_State* L = m_interface->getState();
            m_interface->pushFunction(m_scriptId);

            lua_pushnumber(L, env->addThing(player));

            LuaInterface::pushThing(L, item, env->addThing(item));
            lua_pushnumber(L, count);
            LuaInterface::pushThing(L, toContainer, env->addThing(toContainer));
            LuaInterface::pushThing(L, fromContainer, env->addThing(fromContainer));
            LuaInterface::pushPosition(L, fromPos, fstack);
                        LuaInterface::pushPosition(L, toPos, 0);

            bool result = m_interface->callFunction(7);
            m_interface->releaseEnv();
            return result;
        }
    }
    else
    {
        std::clog << "[Error - CreatureEvent::executeMoveItem] Call stack overflow." << std::endl;
        return 0;
    }
}
____________________________________
creatureevent.h

Below:
Code:
uint32_t executePrepareDeath(Creature* creature, DeathList deathList);
Add:
Code:
uint32_t executeMoveItem(Player* player, Item* item, uint8_t count, const Position& fromPos, const Position& toPos, Item* toContainer, Item* fromContainer, int16_t fstack);
____________________________________
Below:
Code:
CREATURE_EVENT_PREPAREDEATH
Add:
Code:
CREATURE_EVENT_MOVEITEM
(Don't forget to add , after CREATURE_EVENT_PREPAREDEATH)
____________________________________
game.cpp

After:
Code:
if(!canThrowObjectTo(mapFromPos, mapToPos) && !player->hasCustomFlag(PlayerCustomFlag_CanThrowAnywhere))
{
    player->sendCancelMessage(RET_CANNOTTHROW);
    return false;
}
Add:
Code:
bool success = true;
CreatureEventList moveitemEvents = player->getCreatureEvents(CREATURE_EVENT_MOVEITEM);
for(CreatureEventList::iterator it = moveitemEvents.begin(); it != moveitemEvents.end(); ++it)
{
    Item* toContainer = toCylinder->getItem();
    Item* fromContainer = fromCylinder->getItem();
    if(!(*it)->executeMoveItem(player, item, count, fromPos, toPos, (toContainer ? toContainer : 0), (fromContainer ? fromContainer : 0), fromStackpos) && success)
        success = false;
}

if(!success)
    return false;
____________________________________
____________________________________

Example / How to use:

data/creaturescripts/creaturescripts.xml:
XML:
    <event type="moveitem" name="MoveItem" event="script" value="moveitem.lua"/>

data/creaturescripts/scripts/moveitem.lua:
Code:
function onMoveItem(cid, item, count, toContainer, fromContainer, fromPos, toPos)
    if item.actionid == 1000 then
        return false
    end

    return true
end

data/creaturescripts/scripts/login.lua:
Code:
registerCreatureEvent(cid, "MoveItem")

-> Block items with actionid 1000 from being moved (Really simple example)
 
Last edited:
Added that, but doesn't really change anything if you aren't using buffers so I never noticed.
 
Updated the formatting to new forum code tags.
If you are still using 0.4 you might want to try this out :)
 
@Summ this can be used to prevent drop items to the floor from inventory or containers? I mean that if is possible that a specific item id only can move in the backpack, inventory, depot but can't be thrown to the floor.
 
Last edited:
@Summ this can be used to prevent drop items to the floor from inventory or containers? I mean that if is possible that a specific item id only can move in the backpack, inventory, depot but can't be thrown to the floor.

I think its possible, just use the variables "(cid, item, count, toContainer, fromContainer, fromPos, toPos)" to check whatever u want.
 
I saw some problems with stackable items i mean it's not able to get moved item
 
Please how to convert moveitem to tfs 1.0 ? for version 10.76 ? please
 
Back
Top