nodemetadata.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  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 <unordered_set>
  18. #include "metadata.h"
  19. /*
  20. NodeMetadata stores arbitary amounts of data for special blocks.
  21. Used for furnaces, chests and signs.
  22. There are two interaction methods: inventory menu and text input.
  23. Only one can be used for a single metadata, thus only inventory OR
  24. text input should exist in a metadata.
  25. */
  26. class Inventory;
  27. class IItemDefManager;
  28. class NodeMetadata : public Metadata
  29. {
  30. public:
  31. NodeMetadata(IItemDefManager *item_def_mgr);
  32. ~NodeMetadata();
  33. void serialize(std::ostream &os, u8 version, bool disk=true) const;
  34. void deSerialize(std::istream &is, u8 version);
  35. void clear();
  36. bool empty() const;
  37. // The inventory
  38. Inventory *getInventory()
  39. {
  40. return m_inventory;
  41. }
  42. inline bool isPrivate(const std::string &name) const
  43. {
  44. return m_privatevars.count(name) != 0;
  45. }
  46. void markPrivate(const std::string &name, bool set);
  47. private:
  48. int countNonPrivate() const;
  49. Inventory *m_inventory;
  50. std::unordered_set<std::string> m_privatevars;
  51. };
  52. /*
  53. List of metadata of all the nodes of a block
  54. */
  55. class NodeMetadataList
  56. {
  57. public:
  58. ~NodeMetadataList();
  59. void serialize(std::ostream &os, u8 blockver, bool disk=true) const;
  60. void deSerialize(std::istream &is, IItemDefManager *item_def_mgr);
  61. // Add all keys in this list to the vector keys
  62. std::vector<v3s16> getAllKeys();
  63. // Get pointer to data
  64. NodeMetadata *get(v3s16 p);
  65. // Deletes data
  66. void remove(v3s16 p);
  67. // Deletes old data and sets a new one
  68. void set(v3s16 p, NodeMetadata *d);
  69. // Deletes all
  70. void clear();
  71. private:
  72. int countNonEmpty() const;
  73. std::map<v3s16, NodeMetadata *> m_data;
  74. };