Make players check inventory modification properly

This commit is contained in:
ShadowNinja 2014-09-02 12:53:20 -04:00
parent 8b3135a643
commit e9c9b66ae9
6 changed files with 39 additions and 9 deletions

@ -478,6 +478,7 @@ Player *ServerEnvironment::loadPlayer(const std::string &playername)
if (newplayer) { if (newplayer) {
addPlayer(player); addPlayer(player);
} }
player->setModified(false);
return player; return player;
} }

@ -831,6 +831,7 @@ Inventory::~Inventory()
void Inventory::clear() void Inventory::clear()
{ {
m_dirty = true;
for(u32 i=0; i<m_lists.size(); i++) for(u32 i=0; i<m_lists.size(); i++)
{ {
delete m_lists[i]; delete m_lists[i];
@ -840,6 +841,7 @@ void Inventory::clear()
void Inventory::clearContents() void Inventory::clearContents()
{ {
m_dirty = true;
for(u32 i=0; i<m_lists.size(); i++) for(u32 i=0; i<m_lists.size(); i++)
{ {
InventoryList *list = m_lists[i]; InventoryList *list = m_lists[i];
@ -852,12 +854,14 @@ void Inventory::clearContents()
Inventory::Inventory(IItemDefManager *itemdef) Inventory::Inventory(IItemDefManager *itemdef)
{ {
m_dirty = false;
m_itemdef = itemdef; m_itemdef = itemdef;
} }
Inventory::Inventory(const Inventory &other) Inventory::Inventory(const Inventory &other)
{ {
*this = other; *this = other;
m_dirty = false;
} }
Inventory & Inventory::operator = (const Inventory &other) Inventory & Inventory::operator = (const Inventory &other)
@ -865,6 +869,7 @@ Inventory & Inventory::operator = (const Inventory &other)
// Gracefully handle self assignment // Gracefully handle self assignment
if(this != &other) if(this != &other)
{ {
m_dirty = true;
clear(); clear();
m_itemdef = other.m_itemdef; m_itemdef = other.m_itemdef;
for(u32 i=0; i<other.m_lists.size(); i++) for(u32 i=0; i<other.m_lists.size(); i++)
@ -945,6 +950,7 @@ void Inventory::deSerialize(std::istream &is)
InventoryList * Inventory::addList(const std::string &name, u32 size) InventoryList * Inventory::addList(const std::string &name, u32 size)
{ {
m_dirty = true;
s32 i = getListIndex(name); s32 i = getListIndex(name);
if(i != -1) if(i != -1)
{ {
@ -990,6 +996,7 @@ bool Inventory::deleteList(const std::string &name)
s32 i = getListIndex(name); s32 i = getListIndex(name);
if(i == -1) if(i == -1)
return false; return false;
m_dirty = true;
delete m_lists[i]; delete m_lists[i];
m_lists.erase(m_lists.begin() + i); m_lists.erase(m_lists.begin() + i);
return true; return true;

@ -279,18 +279,30 @@ public:
// A shorthand for adding items. Returns leftover item (possibly empty). // A shorthand for adding items. Returns leftover item (possibly empty).
ItemStack addItem(const std::string &listname, const ItemStack &newitem) ItemStack addItem(const std::string &listname, const ItemStack &newitem)
{ {
m_dirty = true;
InventoryList *list = getList(listname); InventoryList *list = getList(listname);
if(list == NULL) if(list == NULL)
return newitem; return newitem;
return list->addItem(newitem); return list->addItem(newitem);
} }
bool checkModified() const
{
return m_dirty;
}
void setModified(const bool x)
{
m_dirty = x;
}
private: private:
// -1 if not found // -1 if not found
const s32 getListIndex(const std::string &name) const; const s32 getListIndex(const std::string &name) const;
std::vector<InventoryList*> m_lists; std::vector<InventoryList*> m_lists;
IItemDefManager *m_itemdef; IItemDefManager *m_itemdef;
bool m_dirty;
}; };
#endif #endif

@ -64,6 +64,7 @@ Player::Player(IGameDef *gamedef, const char *name):
craft->setWidth(3); craft->setWidth(3);
inventory.addList("craftpreview", 1); inventory.addList("craftpreview", 1);
inventory.addList("craftresult", 1); inventory.addList("craftresult", 1);
inventory.setModified(false);
// Can be redefined via Lua // Can be redefined via Lua
inventory_formspec = "size[8,7.5]" inventory_formspec = "size[8,7.5]"
@ -203,6 +204,7 @@ void Player::deSerialize(std::istream &is, std::string playername)
playername + " not found!"); playername + " not found!");
} }
m_dirty = true;
//args.getS32("version"); // Version field value not used //args.getS32("version"); // Version field value not used
std::string name = args.get("name"); std::string name = args.get("name");
strlcpy(m_name, name.c_str(), PLAYERNAME_SIZE); strlcpy(m_name, name.c_str(), PLAYERNAME_SIZE);
@ -235,8 +237,6 @@ void Player::deSerialize(std::istream &is, std::string playername)
inventory.getList("craftresult")->changeItem(0, ItemStack()); inventory.getList("craftresult")->changeItem(0, ItemStack());
} }
} }
m_dirty = false;
} }
u32 Player::addHud(HudElement *toadd) u32 Player::addHud(HudElement *toadd)
@ -299,7 +299,7 @@ void RemotePlayer::save(std::string savedir)
if (!fs::safeWriteToFile(path, ss.str())) { if (!fs::safeWriteToFile(path, ss.str())) {
infostream << "Failed to write " << path << std::endl; infostream << "Failed to write " << path << std::endl;
} }
m_dirty = false; setModified(false);
return; return;
} }
// Open file and deserialize // Open file and deserialize
@ -317,7 +317,7 @@ void RemotePlayer::save(std::string savedir)
if (!fs::safeWriteToFile(path, ss.str())) { if (!fs::safeWriteToFile(path, ss.str())) {
infostream << "Failed to write " << path << std::endl; infostream << "Failed to write " << path << std::endl;
} }
m_dirty = false; setModified(false);
return; return;
} }
path = savedir + m_name + itos(i); path = savedir + m_name + itos(i);

@ -226,9 +226,16 @@ public:
void serialize(std::ostream &os); void serialize(std::ostream &os);
void deSerialize(std::istream &is, std::string playername); void deSerialize(std::istream &is, std::string playername);
bool checkModified() bool checkModified() const
{ {
return m_dirty; return m_dirty || inventory.checkModified();
}
void setModified(const bool x)
{
m_dirty = x;
if (x == false)
inventory.setModified(x);
} }
bool touching_ground; bool touching_ground;

@ -5039,13 +5039,16 @@ PlayerSAO* Server::emergePlayer(const char *name, u16 peer_id)
if (!player) { if (!player) {
newplayer = true; newplayer = true;
player = new RemotePlayer(this, name); player = new RemotePlayer(this, name);
/* Set player position */ // Set player position
infostream<<"Server: Finding spawn place for player \"" infostream<<"Server: Finding spawn place for player \""
<<name<<"\""<<std::endl; <<name<<"\""<<std::endl;
v3f pos = findSpawnPos(m_env->getServerMap()); v3f pos = findSpawnPos(m_env->getServerMap());
player->setPosition(pos); player->setPosition(pos);
/* Add player to environment */ // Make sure the player is saved
player->setModified(true);
// Add player to environment
m_env->addPlayer(player); m_env->addPlayer(player);
} }