Skip to content

Commit

Permalink
fix: check store inbox max capacity (#2109)
Browse files Browse the repository at this point in the history
Resolves #2108
  • Loading branch information
dudantas authored Jan 17, 2024
1 parent 9aaee3b commit e44bd19
Show file tree
Hide file tree
Showing 23 changed files with 98 additions and 34 deletions.
2 changes: 1 addition & 1 deletion data-canary/scripts/actions/tools/magic_gold_converter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ local data = {
local function findItem(self, cylinder, converterItem)
if cylinder == 0 then
cylinder = self:getSlotItem(CONST_SLOT_BACKPACK)
findItem(self, self:getSlotItem(CONST_SLOT_STORE_INBOX), converterItem)
findItem(self, self:getStoreInbox(), converterItem)
end

if cylinder and cylinder:isContainer() then
Expand Down
5 changes: 3 additions & 2 deletions data-otservbr-global/npc/emael.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ local function creatureSayCallback(npc, creature, type, message)
if player:getStorageValue(30020) == 1 then
if player:removeMoney(1000000) then
npcHandler:say("Ah, I see you killed a lot of dangerous creatures. Here's your podium of vigour!", npc, creature)
local inbox = player:getSlotItem(CONST_SLOT_STORE_INBOX)
if inbox then
local inbox = player:getStoreInbox()
local inboxItems = inbox:getItems()
if inbox and #inboxItems <= inbox:getMaxCapacity() then
local decoKit = inbox:addItem(ITEM_DECORATION_KIT, 1)
if decoKit then
decoKit:setAttribute(ITEM_ATTRIBUTE_DESCRIPTION, "Unwrap it in your own house to create a <" .. ItemType(38707):getName() .. ">.")
Expand Down
5 changes: 3 additions & 2 deletions data-otservbr-global/npc/emperor_kruzak.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ local function creatureSayCallback(npc, creature, type, message)
elseif npcHandler:getTopic(playerId) == 3 then -- ARMOR/OUTFIT
if player:getStorageValue(Storage.OutfitQuest.GoldenOutfit) < 1 then
if player:getMoney() + player:getBankBalance() >= 500000000 then
local inbox = player:getSlotItem(CONST_SLOT_STORE_INBOX)
if inbox then
local inbox = player:getStoreInbox()
local inboxItems = inbox:getItems()
if inbox and #inboxItems <= inbox:getMaxCapacity() then
local decoKit = inbox:addItem(ITEM_DECORATION_KIT, 1)
local decoItemName = ItemType(31510):getName()
decoKit:setAttribute(ITEM_ATTRIBUTE_DESCRIPTION, "You bought this item in the Store.\nUnwrap it in your own house to create a " .. decoItemName .. ".")
Expand Down
6 changes: 3 additions & 3 deletions data-otservbr-global/npc/hireling.lua
Original file line number Diff line number Diff line change
Expand Up @@ -517,11 +517,11 @@ function createHirelingType(HirelingName)
local playerId = creature:getId()
local player = Player(creature)
local itType = ItemType(food_id)
local inbox = player:getSlotItem(CONST_SLOT_STORE_INBOX)

local inbox = player:getStoreInbox()
local inboxItems = inbox:getItems()
if player:getFreeCapacity() < itType:getWeight(1) then
npcHandler:say("Sorry, but you don't have enough capacity.", npc, creature)
elseif not inbox then
elseif not inbox or #inboxItems > inbox:getMaxCapacity() then
player:getPosition():sendMagicEffect(CONST_ME_POFF)
npcHandler:say("Sorry, you don't have enough room on your inbox", npc, creature)
elseif not player:removeMoneyBank(15000) then
Expand Down
5 changes: 3 additions & 2 deletions data-otservbr-global/npc/king_tibianus.lua
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ local function creatureSayCallback(npc, creature, type, message)
elseif npcHandler:getTopic(playerId) == 3 then -- ARMOR/OUTFIT
if player:getStorageValue(Storage.OutfitQuest.GoldenOutfit) < 1 then
if player:getMoney() + player:getBankBalance() >= 500000000 then
local inbox = player:getSlotItem(CONST_SLOT_STORE_INBOX)
if inbox then
local inbox = player:getStoreInbox()
local inboxItems = inbox:getItems()
if inbox and #inboxItems <= inbox:getMaxCapacity() then
local decoKit = inbox:addItem(ITEM_DECORATION_KIT, 1)
local decoItemName = ItemType(31510):getName()
decoKit:setAttribute(ITEM_ATTRIBUTE_DESCRIPTION, "Unwrap it in your own house to create a " .. decoItemName .. ".")
Expand Down
5 changes: 3 additions & 2 deletions data-otservbr-global/npc/queen_eloise.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ local function creatureSayCallback(npc, creature, type, message)
elseif npcHandler:getTopic(playerId) == 3 then -- ARMOR/OUTFIT
if player:getStorageValue(Storage.OutfitQuest.GoldenOutfit) < 1 then
if player:getMoney() + player:getBankBalance() >= 500000000 then
local inbox = player:getSlotItem(CONST_SLOT_STORE_INBOX)
if inbox then
local inbox = player:getStoreInbox()
local inboxItems = inbox:getItems()
if inbox and #inboxItems <= inbox:getMaxCapacity() then
local decoKit = inbox:addItem(ITEM_DECORATION_KIT, 1)
local decoItemName = ItemType(31510):getName()
decoKit:setAttribute(ITEM_ATTRIBUTE_DESCRIPTION, "You bought this item in the Store.\nUnwrap it in your own house to create a " .. decoItemName .. ".")
Expand Down
7 changes: 5 additions & 2 deletions data-otservbr-global/npc/walter_jaeger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,17 @@ local function processItemInboxPurchase(player, name, id)
return false
end

local inbox = player:getSlotItem(CONST_SLOT_STORE_INBOX)
if inbox then
local inbox = player:getStoreInbox()
local inboxItems = inbox:getItems()
if inbox and #inboxItems <= inbox:getMaxCapacity() then
local decoKit = inbox:addItem(ITEM_DECORATION_KIT, 1)
if decoKit then
decoKit:setAttribute(ITEM_ATTRIBUTE_DESCRIPTION, "You bought this item with the Walter Jaeger.\nUnwrap it in your own house to create a <" .. name .. ">.")
decoKit:setCustomAttribute("unWrapId", id)
return true
end
else
player:sendTextMessage(MESSAGE_INFO_DESCR, "Please make sure you have free slots in your store inbox.")
end

return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ local data = {
local function finditem(self, cylinder, conv)
if cylinder == 0 then
cylinder = self:getSlotItem(CONST_SLOT_BACKPACK)
finditem(self, self:getSlotItem(CONST_SLOT_STORE_INBOX), conv)
finditem(self, self:getStoreInbox(), conv)
end

if cylinder and cylinder:isContainer() then
Expand Down
6 changes: 3 additions & 3 deletions data/libs/functions/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ function Player.getSubjectVerb(self, past)
end

function Player.findItemInInbox(self, itemId)
local inbox = self:getSlotItem(CONST_SLOT_STORE_INBOX)
local inbox = self:getStoreInbox()
local items = inbox:getItems()
for _, item in pairs(items) do
if item:getId() == itemId then
Expand Down Expand Up @@ -556,7 +556,7 @@ function Player.updateHazard(self)
end

function Player:addItemStoreInboxEx(item, movable, setOwner)
local inbox = self:getSlotItem(CONST_SLOT_STORE_INBOX)
local inbox = self:getStoreInbox()
if not movable then
item:setOwner(self)
item:setAttribute(ITEM_ATTRIBUTE_STORE, systemTime())
Expand Down Expand Up @@ -663,7 +663,7 @@ function Player:setFiendish()
end

function Player:findItemInInbox(itemId, name)
local inbox = self:getSlotItem(CONST_SLOT_STORE_INBOX)
local inbox = self:getStoreInbox()
local items = inbox:getItems()
for _, item in pairs(items) do
if item:getId() == itemId and (not name or item:getName() == name) then
Expand Down
14 changes: 8 additions & 6 deletions data/libs/hireling_lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ local function checkHouseAccess(hireling)

-- Player is not invited anymore, return to lamp
logger.debug("Returning Hireling: {} to owner '{}' Inbox", hireling:getName(), player:getName())
local inbox = player:getSlotItem(CONST_SLOT_STORE_INBOX)
local inbox = player:getStoreInbox()
if not inbox then
return false
end
Expand Down Expand Up @@ -359,8 +359,9 @@ function Hireling:returnToLamp(player_id)
return owner:sendTextMessage(MESSAGE_FAILURE, "You do not have enough capacity.")
end

local inbox = owner:getSlotItem(CONST_SLOT_STORE_INBOX)
if not inbox then
local inbox = owner:getStoreInbox()
local inboxItems = inbox:getItems()
if not inbox or #inboxItems > inbox:getMaxCapacity() then
owner:getPosition():sendMagicEffect(CONST_ME_POFF)
return owner:sendTextMessage(MESSAGE_FAILURE, "You don't have enough room in your inbox.")
end
Expand Down Expand Up @@ -537,8 +538,9 @@ function Player:addNewHireling(name, sex)
return false
end

local inbox = self:getSlotItem(CONST_SLOT_STORE_INBOX)
if not inbox then
local inbox = self:getStoreInbox()
local inboxItems = inbox:getItems()
if not inbox or #inboxItems > inbox:getMaxCapacity() then
self:getPosition():sendMagicEffect(CONST_ME_POFF)
self:sendTextMessage(MESSAGE_FAILURE, "You don't have enough room in your inbox.")
return false
Expand Down Expand Up @@ -628,7 +630,7 @@ function Player:hasHirelings()
end

function Player:findHirelingLamp(hirelingId)
local inbox = self:getSlotItem(CONST_SLOT_STORE_INBOX)
local inbox = self:getStoreInbox()
if not inbox then
return nil
end
Expand Down
2 changes: 1 addition & 1 deletion data/modules/scripts/blessings/blessings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ Blessings.DropLoot = function(player, corpse, chance, skulled)
end
end
if skulled and Blessings.Config.SkulledDeathLoseStoreItem then
local inbox = player:getSlotItem(CONST_SLOT_STORE_INBOX)
local inbox = player:getStoreInbox()
local toBeDeleted = {}
if inbox and inbox:getSize() > 0 then
for i = 0, inbox:getSize() do
Expand Down
5 changes: 3 additions & 2 deletions data/modules/scripts/daily_reward/daily_reward.lua
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,9 @@ function Player.selectDailyReward(self, msg)
end

-- Adding items to store inbox
local inbox = self:getSlotItem(CONST_SLOT_STORE_INBOX)
if not inbox then
local inbox = self:getStoreInbox()
local inboxItems = inbox:getItems()
if not inbox or #inboxItems > inbox:getMaxCapacity() then
self:sendError("You do not have enough space in your store inbox.")
return false
end
Expand Down
11 changes: 7 additions & 4 deletions data/modules/scripts/gamestore/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1573,8 +1573,9 @@ function GameStore.processStackablePurchase(player, offerId, offerCount, offerNa
return error({ code = 0, message = "Please make sure you have free capacity to hold this item." })
end

local inbox = player:getSlotItem(CONST_SLOT_STORE_INBOX)
if inbox then
local inbox = player:getStoreInbox()
local inboxItems = inbox:getItems()
if inbox and #inboxItems <= inbox:getMaxCapacity() then
if (isKeg and offerCount > 500) or offerCount > 100 then
local parcel = inbox:addItem(PARCEL_ID, 1)
parcel:setAttribute(ITEM_ATTRIBUTE_STORE, systemTime())
Expand Down Expand Up @@ -1624,12 +1625,14 @@ function GameStore.processHouseRelatedPurchase(player, offer)
return (itemId >= ITEM_HEALTH_CASK_START and itemId <= ITEM_HEALTH_CASK_END) or (itemId >= ITEM_MANA_CASK_START and itemId <= ITEM_MANA_CASK_END) or (itemId >= ITEM_SPIRIT_CASK_START and itemId <= ITEM_SPIRIT_CASK_END)
end

local inbox = player:getSlotItem(CONST_SLOT_STORE_INBOX)
local itemIds = offer.itemtype
if type(itemIds) ~= "table" then
itemIds = { itemIds }
end
if inbox then

local inbox = player:getStoreInbox()
local inboxItems = inbox:getItems()
if inbox and #inboxItems <= inbox:getMaxCapacity() then
for _, itemId in ipairs(itemIds) do
local decoKit = inbox:addItem(ITEM_DECORATION_KIT, 1)
if decoKit then
Expand Down
2 changes: 1 addition & 1 deletion data/scripts/talkactions/god/inbox_command.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function inboxCommand.onSay(player, words, param)
player:getPosition():sendMagicEffect(CONST_ME_TUTORIALSQUARE)
local target = Creature(param[1])
if target then
local inbox = target:getSlotItem(CONST_SLOT_STORE_INBOX)
local inbox = target:getStoreInbox()
local inboxSize = inbox:getSize()
if inbox and inboxSize > 0 then
if param[2] == "remove" then
Expand Down
5 changes: 3 additions & 2 deletions data/scripts/talkactions/player/reward.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ local function sendExerciseRewardModal(player)
return true
end

local inbox = player:getSlotItem(CONST_SLOT_STORE_INBOX)
if inbox and inbox:getEmptySlots() > 0 and player:getFreeCapacity() >= iType:getWeight() then
local inbox = player:getStoreInbox()
local inboxItems = inbox:getItems()
if inbox and #inboxItems <= inbox:getMaxCapacity() and player:getFreeCapacity() >= iType:getWeight() then
local item = inbox:addItem(it.id, it.charges)
if item then
item:setActionId(IMMOVABLE_ACTION_ID)
Expand Down
10 changes: 10 additions & 0 deletions src/creatures/players/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7813,6 +7813,16 @@ std::shared_ptr<Container> Player::getLootPouch() {
return container;
}

std::shared_ptr<Container> Player::getStoreInbox() const {
auto thing = getThing(CONST_SLOT_STORE_INBOX);
if (!thing) {
return nullptr;
}

auto storeInbox = thing->getContainer();
return storeInbox ? storeInbox : nullptr;
}

bool Player::hasPermittedConditionInPZ() const {
static const std::unordered_set<ConditionType_t> allowedConditions = {
CONDITION_ENERGY,
Expand Down
2 changes: 2 additions & 0 deletions src/creatures/players/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2552,6 +2552,8 @@ class Player final : public Creature, public Cylinder, public Bankable {

bool hasPermittedConditionInPZ() const;

std::shared_ptr<Container> getStoreInbox() const;

private:
friend class PlayerLock;
std::mutex mutex;
Expand Down
4 changes: 4 additions & 0 deletions src/items/containers/container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ std::ostringstream &Container::getContentDescription(std::ostringstream &os, boo
return os;
}

uint32_t Container::getMaxCapacity() const {
return m_maxItems;
}

bool Container::isStoreInbox() const {
return getID() == ITEM_STORE_INBOX;
}
Expand Down
2 changes: 2 additions & 0 deletions src/items/containers/container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ class Container : public Item, public Cylinder {
bool unserializeItemNode(OTB::Loader &loader, const OTB::Node &node, PropStream &propStream, Position &itemPosition) override;
std::string getContentDescription(bool oldProtocol);

uint32_t getMaxCapacity() const;

size_t size() const {
return itemlist.size();
}
Expand Down
17 changes: 17 additions & 0 deletions src/lua/functions/creatures/player/player_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4156,3 +4156,20 @@ int PlayerFunctions::luaPlayerKV(lua_State* L) {
setMetatable(L, -1, "KV");
return 1;
}

int PlayerFunctions::luaPlayerGetStoreInbox(lua_State* L) {
// player:getStoreInbox()
const auto &player = getUserdataShared<Player>(L, 1);
if (!player) {
lua_pushnil(L);
return 1;
}

if (auto item = player->getStoreInbox()) {
pushUserdata<Item>(L, item);
setItemMetatable(L, -1, item);
} else {
pushBoolean(L, false);
}
return 1;
}
2 changes: 2 additions & 0 deletions src/lua/functions/creatures/player/player_functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ class PlayerFunctions final : LuaScriptInterface {
registerMethod(L, "Player", "getVipTime", PlayerFunctions::luaPlayerGetVipTime);

registerMethod(L, "Player", "kv", PlayerFunctions::luaPlayerKV);
registerMethod(L, "Player", "getStoreInbox", PlayerFunctions::luaPlayerGetStoreInbox);

GroupFunctions::init(L);
GuildFunctions::init(L);
Expand Down Expand Up @@ -705,6 +706,7 @@ class PlayerFunctions final : LuaScriptInterface {
static int luaPlayerGetVipTime(lua_State* L);

static int luaPlayerKV(lua_State* L);
static int luaPlayerGetStoreInbox(lua_State* L);

friend class CreatureFunctions;
};
11 changes: 11 additions & 0 deletions src/lua/functions/items/container_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ int ContainerFunctions::luaContainerGetSize(lua_State* L) {
return 1;
}

int ContainerFunctions::luaContainerGetMaxCapacity(lua_State* L) {
// container:getMaxCapacity()
const auto &container = getUserdataShared<Container>(L, 1);
if (container) {
lua_pushnumber(L, container->getMaxCapacity());
} else {
lua_pushnil(L);
}
return 1;
}

int ContainerFunctions::luaContainerGetCapacity(lua_State* L) {
// container:getCapacity()
std::shared_ptr<Container> container = getUserdataShared<Container>(L, 1);
Expand Down
2 changes: 2 additions & 0 deletions src/lua/functions/items/container_functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class ContainerFunctions final : LuaScriptInterface {
registerMetaMethod(L, "Container", "__eq", ContainerFunctions::luaUserdataCompare);

registerMethod(L, "Container", "getSize", ContainerFunctions::luaContainerGetSize);
registerMethod(L, "Container", "getMaxCapacity", ContainerFunctions::luaContainerGetMaxCapacity);
registerMethod(L, "Container", "getCapacity", ContainerFunctions::luaContainerGetCapacity);
registerMethod(L, "Container", "getEmptySlots", ContainerFunctions::luaContainerGetEmptySlots);
registerMethod(L, "Container", "getContentDescription", ContainerFunctions::luaContainerGetContentDescription);
Expand All @@ -37,6 +38,7 @@ class ContainerFunctions final : LuaScriptInterface {
static int luaContainerCreate(lua_State* L);

static int luaContainerGetSize(lua_State* L);
static int luaContainerGetMaxCapacity(lua_State* L);
static int luaContainerGetCapacity(lua_State* L);
static int luaContainerGetEmptySlots(lua_State* L);

Expand Down

0 comments on commit e44bd19

Please sign in to comment.