serverinventorymgr.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2020 Minetest core development team
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. #include "inventorymanager.h"
  18. #include <cassert>
  19. #include <functional>
  20. #include <memory>
  21. #include <unordered_map>
  22. class IItemDefManager;
  23. class ServerEnvironment;
  24. class ServerInventoryManager : public InventoryManager
  25. {
  26. public:
  27. ServerInventoryManager();
  28. virtual ~ServerInventoryManager() = default;
  29. void setEnv(ServerEnvironment *env)
  30. {
  31. assert(!m_env);
  32. m_env = env;
  33. }
  34. Inventory *getInventory(const InventoryLocation &loc);
  35. void setInventoryModified(const InventoryLocation &loc);
  36. // Creates or resets inventory
  37. Inventory *createDetachedInventory(const std::string &name, IItemDefManager *idef,
  38. const std::string &player = "");
  39. bool removeDetachedInventory(const std::string &name);
  40. bool checkDetachedInventoryAccess(const InventoryLocation &loc, const std::string &player) const;
  41. void sendDetachedInventories(const std::string &peer_name, bool incremental,
  42. std::function<void(const std::string &, Inventory *)> apply_cb);
  43. private:
  44. struct DetachedInventory
  45. {
  46. std::unique_ptr<Inventory> inventory;
  47. std::string owner;
  48. };
  49. ServerEnvironment *m_env = nullptr;
  50. std::unordered_map<std::string, DetachedInventory> m_detached_inventories;
  51. };