aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSmallJoker <mk939@ymail.com>2022-01-04 18:39:27 +0100
committerSmallJoker <mk939@ymail.com>2022-01-04 18:39:27 +0100
commitd33ab97434633683196cee2c4593160736899124 (patch)
tree319ed072a3389b63839d8f723ba8be6674dbe1cb /src
parent196562870552b156265feb83043da2bc21bd6246 (diff)
downloadminetest-d33ab97434633683196cee2c4593160736899124.tar.gz
minetest-d33ab97434633683196cee2c4593160736899124.tar.bz2
minetest-d33ab97434633683196cee2c4593160736899124.zip
Inventory: Add ServerEnv checks for calls during script init
This fixes 'minetest.get_inventory' calls to players or nodes during the load phase.
Diffstat (limited to 'src')
-rw-r--r--src/server/serverinventorymgr.cpp28
1 files changed, 17 insertions, 11 deletions
diff --git a/src/server/serverinventorymgr.cpp b/src/server/serverinventorymgr.cpp
index 3aee003b4..63d1645cb 100644
--- a/src/server/serverinventorymgr.cpp
+++ b/src/server/serverinventorymgr.cpp
@@ -39,24 +39,29 @@ ServerInventoryManager::~ServerInventoryManager()
Inventory *ServerInventoryManager::getInventory(const InventoryLocation &loc)
{
+ // No m_env check here: allow creation and modification of detached inventories
+
switch (loc.type) {
case InventoryLocation::UNDEFINED:
case InventoryLocation::CURRENT_PLAYER:
break;
case InventoryLocation::PLAYER: {
+ if (!m_env)
+ return nullptr;
+
RemotePlayer *player = m_env->getPlayer(loc.name.c_str());
if (!player)
return NULL;
+
PlayerSAO *playersao = player->getPlayerSAO();
- if (!playersao)
- return NULL;
- return playersao->getInventory();
+ return playersao ? playersao->getInventory() : nullptr;
} break;
case InventoryLocation::NODEMETA: {
+ if (!m_env)
+ return nullptr;
+
NodeMetadata *meta = m_env->getMap().getNodeMetadata(loc.p);
- if (!meta)
- return NULL;
- return meta->getInventory();
+ return meta ? meta->getInventory() : nullptr;
} break;
case InventoryLocation::DETACHED: {
auto it = m_detached_inventories.find(loc.name);
@@ -151,12 +156,13 @@ bool ServerInventoryManager::removeDetachedInventory(const std::string &name)
const std::string &owner = inv_it->second.owner;
if (!owner.empty()) {
- RemotePlayer *player = m_env->getPlayer(owner.c_str());
-
- if (player && player->getPeerId() != PEER_ID_INEXISTENT)
- m_env->getGameDef()->sendDetachedInventory(
- nullptr, name, player->getPeerId());
+ if (m_env) {
+ RemotePlayer *player = m_env->getPlayer(owner.c_str());
+ if (player && player->getPeerId() != PEER_ID_INEXISTENT)
+ m_env->getGameDef()->sendDetachedInventory(
+ nullptr, name, player->getPeerId());
+ }
} else if (m_env) {
// Notify all players about the change as soon ServerEnv exists
m_env->getGameDef()->sendDetachedInventory(