Skip to content

Fix Container::hasParent #4828

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
4 commits merged into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion data/npc/lib/npcsystem/keywordhandler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ if not KeywordHandler then
return true
end

-- Returns the parent of this node or nil if no such node exists.
function KeywordNode:hasParent()
return self.parent ~= nil
end

-- Returns the parent of this node or nil if no such node exists.
function KeywordNode:getParent()
return self.parent
end
Expand Down
14 changes: 13 additions & 1 deletion src/container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,19 @@ std::string Container::getName(bool addArticle /* = false*/) const
return getNameDescription(it, this, -1, addArticle);
}

bool Container::hasParent() const { return getID() != ITEM_BROWSEFIELD && !dynamic_cast<const Player*>(getParent()); }
bool Container::hasContainerParent() const
{
if (getID() == ITEM_BROWSEFIELD) {
return false;
}

if (hasParent()) {
if (auto creature = getParent()->getCreature()) {
return !creature->getPlayer();
}
}
return true;
}

void Container::addItem(Item* item)
{
Expand Down
2 changes: 1 addition & 1 deletion src/container.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Container : public Item, public Cylinder
virtual StoreInbox* getStoreInbox() { return nullptr; }
virtual const StoreInbox* getStoreInbox() const { return nullptr; }

bool hasParent() const override;
bool hasContainerParent() const;

Attr_ReadValue readAttr(AttrTypes_t attr, PropStream& propStream) override;
bool unserializeItemNode(OTB::Loader& loader, const OTB::Node& node, PropStream& propStream) override;
Expand Down
7 changes: 4 additions & 3 deletions src/depotchest.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ class DepotChest final : public Container
public:
explicit DepotChest(uint16_t type, bool paginated = true);

// serialization
// Serialization
void setMaxDepotItems(uint32_t maxitems) { maxDepotItems = maxitems; }

// cylinder implementations
// Cylinder implementations
ReturnValue queryAdd(int32_t index, const Thing& thing, uint32_t count, uint32_t flags,
Creature* actor = nullptr) const override;

Expand All @@ -23,9 +23,10 @@ class DepotChest final : public Container
void postRemoveNotification(Thing* thing, const Cylinder* newParent, int32_t index,
cylinderlink_t link = LINK_OWNER) override;

// overrides
// Item implementations
bool canRemove() const override { return false; }

// Thing implementations
bool hasParent() const override { return getParent(); }
Cylinder* getParent() const override;
Cylinder* getRealParent() const override { return parent; }
Expand Down
15 changes: 8 additions & 7 deletions src/depotlocker.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ class DepotLocker final : public Container
public:
explicit DepotLocker(uint16_t type);

DepotLocker* getDepotLocker() override { return this; }
const DepotLocker* getDepotLocker() const override { return this; }

void removeInbox(Inbox* inbox);
uint16_t getDepotId() const { return depotId; }
void setDepotId(uint16_t depotId) { this->depotId = depotId; }

// serialization
// Serialization
Attr_ReadValue readAttr(AttrTypes_t attr, PropStream& propStream) override;

uint16_t getDepotId() const { return depotId; }
void setDepotId(uint16_t depotId) { this->depotId = depotId; }
// Container implementations
DepotLocker* getDepotLocker() override { return this; }
const DepotLocker* getDepotLocker() const override { return this; }

// cylinder implementations
// Cylinder implementations
ReturnValue queryAdd(int32_t index, const Thing& thing, uint32_t count, uint32_t flags,
Creature* actor = nullptr) const override;

Expand All @@ -35,6 +35,7 @@ class DepotLocker final : public Container
void postRemoveNotification(Thing* thing, const Cylinder* newParent, int32_t index,
cylinderlink_t link = LINK_OWNER) override;

// Item implementations
bool canRemove() const override { return false; }

private:
Expand Down
8 changes: 4 additions & 4 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2371,7 +2371,7 @@ void Game::playerMoveUpContainer(uint32_t playerId, uint8_t cid)
}

player->addContainer(cid, parentContainer);
player->sendContainer(cid, parentContainer, parentContainer->hasParent(), player->getContainerIndex(cid));
player->sendContainer(cid, parentContainer, player->getContainerIndex(cid));
}

void Game::playerUpdateContainer(uint32_t playerId, uint8_t cid)
Expand All @@ -2386,7 +2386,7 @@ void Game::playerUpdateContainer(uint32_t playerId, uint8_t cid)
return;
}

player->sendContainer(cid, container, container->hasParent(), player->getContainerIndex(cid));
player->sendContainer(cid, container, player->getContainerIndex(cid));
}

void Game::playerRotateItem(uint32_t playerId, const Position& pos, uint8_t stackPos, const uint16_t spriteId)
Expand Down Expand Up @@ -2548,7 +2548,7 @@ void Game::playerBrowseField(uint32_t playerId, const Position& pos)
player->closeContainer(dummyContainerId);
} else {
player->addContainer(dummyContainerId, container);
player->sendContainer(dummyContainerId, container, false, 0);
player->sendContainer(dummyContainerId, container, 0);
}
}

Expand All @@ -2569,7 +2569,7 @@ void Game::playerSeekInContainer(uint32_t playerId, uint8_t containerId, uint16_
}

player->setContainerIndex(containerId, index);
player->sendContainer(containerId, container, container->hasParent(), index);
player->sendContainer(containerId, container, index);
}

void Game::playerUpdateHouseWindow(uint32_t playerId, uint8_t listId, uint32_t windowTextId, const std::string& text)
Expand Down
5 changes: 3 additions & 2 deletions src/inbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Inbox final : public Container
public:
explicit Inbox(uint16_t type);

// cylinder implementations
// Cylinder implementations
ReturnValue queryAdd(int32_t index, const Thing& thing, uint32_t count, uint32_t flags,
Creature* actor = nullptr) const override;

Expand All @@ -20,9 +20,10 @@ class Inbox final : public Container
void postRemoveNotification(Thing* thing, const Cylinder* newParent, int32_t index,
cylinderlink_t link = LINK_OWNER) override;

// overrides
// Item implementations
bool canRemove() const override { return false; }

// Thing implementations
bool hasParent() const override { return getParent(); }
Cylinder* getParent() const override;
Cylinder* getRealParent() const override { return parent; }
Expand Down
5 changes: 2 additions & 3 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1031,7 +1031,7 @@ void Player::sendRemoveContainerItem(const Container* container, uint16_t slot)
uint16_t& firstIndex = openContainer.index;
if (firstIndex > 0 && firstIndex >= container->size() - 1) {
firstIndex -= container->capacity();
sendContainer(it.first, container, false, firstIndex);
sendContainer(it.first, container, firstIndex);
}

client->sendRemoveContainerItem(it.first, std::max<uint16_t>(slot, firstIndex),
Expand Down Expand Up @@ -1451,11 +1451,10 @@ void Player::onSendContainer(const Container* container)
return;
}

bool hasParent = container->hasParent();
for (const auto& it : openContainers) {
const OpenContainer& openContainer = it.second;
if (openContainer.container == container) {
client->sendContainer(it.first, container, hasParent, openContainer.index);
client->sendContainer(it.first, container, openContainer.index);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -720,10 +720,10 @@ class Player final : public Creature, public Cylinder
void sendAddContainerItem(const Container* container, const Item* item);
void sendUpdateContainerItem(const Container* container, uint16_t slot, const Item* newItem);
void sendRemoveContainerItem(const Container* container, uint16_t slot);
void sendContainer(uint8_t cid, const Container* container, bool hasParent, uint16_t firstIndex)
void sendContainer(uint8_t cid, const Container* container, uint16_t firstIndex)
{
if (client) {
client->sendContainer(cid, container, hasParent, firstIndex);
client->sendContainer(cid, container, firstIndex);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/protocolgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1888,7 +1888,7 @@ void ProtocolGame::sendIcons(uint32_t icons)
writeToOutputBuffer(msg);
}

void ProtocolGame::sendContainer(uint8_t cid, const Container* container, bool hasParent, uint16_t firstIndex)
void ProtocolGame::sendContainer(uint8_t cid, const Container* container, uint16_t firstIndex)
{
NetworkMessage msg;
msg.addByte(0x6E);
Expand All @@ -1904,7 +1904,7 @@ void ProtocolGame::sendContainer(uint8_t cid, const Container* container, bool h
}

msg.addByte(container->capacity());
msg.addByte(hasParent ? 0x01 : 0x00);
msg.addByte(container->hasContainerParent() ? 0x01 : 0x00);
msg.addByte(0x00); // show search icon (boolean)
msg.addByte(container->isUnlocked() ? 0x01 : 0x00); // Drag and drop
msg.addByte(container->hasPagination() ? 0x01 : 0x00); // Pagination
Expand Down
2 changes: 1 addition & 1 deletion src/protocolgame.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ class ProtocolGame final : public Protocol
void sendUpdateContainerItem(uint8_t cid, uint16_t slot, const Item* item);
void sendRemoveContainerItem(uint8_t cid, uint16_t slot, const Item* lastItem);

void sendContainer(uint8_t cid, const Container* container, bool hasParent, uint16_t firstIndex);
void sendContainer(uint8_t cid, const Container* container, uint16_t firstIndex);
void sendEmptyContainer(uint8_t cid);
void sendCloseContainer(uint8_t cid);

Expand Down
4 changes: 3 additions & 1 deletion src/storeinbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ class StoreInbox final : public Container
public:
explicit StoreInbox(uint16_t type);

// Container implementations
StoreInbox* getStoreInbox() override { return this; }
const StoreInbox* getStoreInbox() const override { return this; }

// cylinder implementations
// Cylinder implementations
ReturnValue queryAdd(int32_t index, const Thing& thing, uint32_t count, uint32_t flags,
Creature* actor = nullptr) const override;

Expand All @@ -23,6 +24,7 @@ class StoreInbox final : public Container
void postRemoveNotification(Thing* thing, const Cylinder* newParent, int32_t index,
cylinderlink_t link = LINK_OWNER) override;

// Item implementations
bool canRemove() const override { return false; }
};

Expand Down
Loading