Skip to content

Commit

Permalink
Fixes to MoveItem & OnItemMoved events (#3916)
Browse files Browse the repository at this point in the history
  • Loading branch information
Erza authored and DSpeichert committed May 9, 2022
1 parent 770d51e commit a3655d0
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 19 deletions.
2 changes: 1 addition & 1 deletion data/events/scripts/player.lua
Expand Up @@ -41,7 +41,7 @@ function Player:onMoveItem(item, count, fromPosition, toPosition, fromCylinder,
if hasEventCallback(EVENT_CALLBACK_ONMOVEITEM) then
return EventCallback(EVENT_CALLBACK_ONMOVEITEM, self, item, count, fromPosition, toPosition, fromCylinder, toCylinder)
end
return true
return RETURNVALUE_NOERROR
end

function Player:onItemMoved(item, count, fromPosition, toPosition, fromCylinder, toCylinder)
Expand Down
14 changes: 6 additions & 8 deletions data/scripts/eventcallbacks/player/default_onMoveItem.lua
Expand Up @@ -5,14 +5,13 @@ ec.onMoveItem = function(self, item, count, fromPosition, toPosition, fromCylind
local tile = Tile(toPosition)
if (fromPosition.x ~= CONTAINER_POSITION and toPosition.x ~= CONTAINER_POSITION) or tile and not tile:getHouse() then
if tile and not tile:getHouse() then
self:sendCancelMessage(RETURNVALUE_NOTPOSSIBLE)
return false
return RETURNVALUE_NOTPOSSIBLE
end
end
end

if toPosition.x ~= CONTAINER_POSITION then
return true
return RETURNVALUE_NOERROR
end

if item:getTopParent() == self and bit.band(toPosition.y, 0x40) == 0 then
Expand All @@ -22,22 +21,21 @@ ec.onMoveItem = function(self, item, count, fromPosition, toPosition, fromCylind
elseif itemType:getWeaponType() == WEAPON_SHIELD and toPosition.y == CONST_SLOT_RIGHT then
moveItem = self:getSlotItem(CONST_SLOT_LEFT)
if moveItem and bit.band(ItemType(moveItem:getId()):getSlotPosition(), SLOTP_TWO_HAND) == 0 then
return true
return RETURNVALUE_NOERROR
end
end

if moveItem then
local parent = item:getParent()
if parent:isContainer() and parent:getSize() == parent:getCapacity() then
self:sendTextMessage(MESSAGE_STATUS_SMALL, Game.getReturnMessage(RETURNVALUE_CONTAINERNOTENOUGHROOM))
return false
return RETURNVALUE_CONTAINERNOTENOUGHROOM
else
return moveItem:moveTo(parent)
return moveItem:moveTo(parent) and RETURNVALUE_NOERROR or RETURNVALUE_NOTPOSSIBLE
end
end
end

return true
return RETURNVALUE_NOERROR
end

ec:register()
18 changes: 14 additions & 4 deletions src/events.cpp
Expand Up @@ -576,16 +576,16 @@ bool Events::eventPlayerOnLookInShop(Player* player, const ItemType* itemType, u
return scriptInterface.callFunction(4);
}

bool Events::eventPlayerOnMoveItem(Player* player, Item* item, uint16_t count, const Position& fromPosition, const Position& toPosition, Cylinder* fromCylinder, Cylinder* toCylinder)
ReturnValue Events::eventPlayerOnMoveItem(Player* player, Item* item, uint16_t count, const Position& fromPosition, const Position& toPosition, Cylinder* fromCylinder, Cylinder* toCylinder)
{
// Player:onMoveItem(item, count, fromPosition, toPosition) or Player.onMoveItem(self, item, count, fromPosition, toPosition, fromCylinder, toCylinder)
if (info.playerOnMoveItem == -1) {
return true;
return RETURNVALUE_NOERROR;
}

if (!scriptInterface.reserveScriptEnv()) {
std::cout << "[Error - Events::eventPlayerOnMoveItem] Call stack overflow" << std::endl;
return false;
return RETURNVALUE_NOTPOSSIBLE;
}

ScriptEnvironment* env = scriptInterface.getScriptEnv();
Expand All @@ -607,7 +607,17 @@ bool Events::eventPlayerOnMoveItem(Player* player, Item* item, uint16_t count, c
LuaScriptInterface::pushCylinder(L, fromCylinder);
LuaScriptInterface::pushCylinder(L, toCylinder);

return scriptInterface.callFunction(7);
ReturnValue returnValue;
if (scriptInterface.protectedCall(L, 7, 1) != 0) {
returnValue = RETURNVALUE_NOTPOSSIBLE;
LuaScriptInterface::reportError(nullptr, LuaScriptInterface::popString(L));
} else {
returnValue = LuaScriptInterface::getNumber<ReturnValue>(L, -1);
lua_pop(L, 1);
}

scriptInterface.resetScriptEnv();
return returnValue;
}

void Events::eventPlayerOnItemMoved(Player* player, Item* item, uint16_t count, const Position& fromPosition, const Position& toPosition, Cylinder* fromCylinder, Cylinder* toCylinder)
Expand Down
2 changes: 1 addition & 1 deletion src/events.h
Expand Up @@ -90,7 +90,7 @@ class Events
void eventPlayerOnLookInBattleList(Player* player, Creature* creature, int32_t lookDistance);
void eventPlayerOnLookInTrade(Player* player, Player* partner, Item* item, int32_t lookDistance);
bool eventPlayerOnLookInShop(Player* player, const ItemType* itemType, uint8_t count, const std::string& description);
bool eventPlayerOnMoveItem(Player* player, Item* item, uint16_t count, const Position& fromPosition, const Position& toPosition, Cylinder* fromCylinder, Cylinder* toCylinder);
ReturnValue eventPlayerOnMoveItem(Player* player, Item* item, uint16_t count, const Position& fromPosition, const Position& toPosition, Cylinder* fromCylinder, Cylinder* toCylinder);
void eventPlayerOnItemMoved(Player* player, Item* item, uint16_t count, const Position& fromPosition, const Position& toPosition, Cylinder* fromCylinder, Cylinder* toCylinder);
bool eventPlayerOnMoveCreature(Player* player, Creature* creature, const Position& fromPosition, const Position& toPosition);
void eventPlayerOnReportRuleViolation(Player* player, const std::string& targetName, uint8_t reportType, uint8_t reportReason, const std::string& comment, const std::string& translation);
Expand Down
14 changes: 9 additions & 5 deletions src/game.cpp
Expand Up @@ -1078,8 +1078,9 @@ ReturnValue Game::internalMoveItem(Cylinder* fromCylinder, Cylinder* toCylinder,
{
Player* actorPlayer = actor ? actor->getPlayer() : nullptr;
if (actorPlayer && fromPos && toPos) {
if (!g_events->eventPlayerOnMoveItem(actorPlayer, item, count, *fromPos, *toPos, fromCylinder, toCylinder)) {
return RETURNVALUE_NOTPOSSIBLE;
const ReturnValue ret = g_events->eventPlayerOnMoveItem(actorPlayer, item, count, *fromPos, *toPos, fromCylinder, toCylinder);
if (ret != RETURNVALUE_NOERROR) {
return ret;
}
}

Expand Down Expand Up @@ -1117,8 +1118,11 @@ ReturnValue Game::internalMoveItem(Cylinder* fromCylinder, Cylinder* toCylinder,
//check if we can add it to source cylinder
ret = fromCylinder->queryAdd(fromCylinder->getThingIndex(item), *toItem, toItem->getItemCount(), 0);
if (ret == RETURNVALUE_NOERROR) {
if (actorPlayer && fromPos && toPos && !g_events->eventPlayerOnMoveItem(actorPlayer, toItem, count, *toPos, *fromPos, toCylinder, fromCylinder)) {
return RETURNVALUE_NOTPOSSIBLE;
if (actorPlayer && fromPos && toPos) {
const ReturnValue eventRet = g_events->eventPlayerOnMoveItem(actorPlayer, toItem, toItem->getItemCount(), *toPos, *fromPos, toCylinder, fromCylinder);
if (eventRet != RETURNVALUE_NOERROR) {
return eventRet;
}
}

//check how much we can move
Expand Down Expand Up @@ -1270,7 +1274,7 @@ ReturnValue Game::internalMoveItem(Cylinder* fromCylinder, Cylinder* toCylinder,
}

if (actorPlayer && fromPos && toPos) {
g_events->eventPlayerOnItemMoved(actorPlayer, item, count, *fromPos, *toPos, fromCylinder, toCylinder);
g_events->eventPlayerOnItemMoved(actorPlayer, moveItem, count, *fromPos, *toPos, fromCylinder, toCylinder);
}

return ret;
Expand Down

0 comments on commit a3655d0

Please sign in to comment.