From 582e450bad08006dff141216351b81d3eb9270b7 Mon Sep 17 00:00:00 2001 From: darkrose Date: Thu, 22 Sep 2011 19:11:48 +1000 Subject: [PATCH 1/3] added locking chests - clean patch --- doc/mapformat.txt | 5 ++++ src/client.cpp | 3 +- src/content_craft.cpp | 18 +++++++++++ src/content_mapnode.cpp | 14 +++++++++ src/content_mapnode.h | 1 + src/content_nodemeta.cpp | 64 ++++++++++++++++++++++++++++++++++++++++ src/content_nodemeta.h | 23 +++++++++++++++ src/map.cpp | 19 ++++++++++-- src/map.h | 2 +- src/server.cpp | 42 +++++++++++++++++++++++++- 10 files changed, 186 insertions(+), 5 deletions(-) diff --git a/doc/mapformat.txt b/doc/mapformat.txt index a0241804..206f5f35 100644 --- a/doc/mapformat.txt +++ b/doc/mapformat.txt @@ -85,5 +85,10 @@ Furnace metadata: Chest metadata: TBD +Locking Chest metadata: + u16 string_len + u8[string_len] string + TBD + // END diff --git a/src/client.cpp b/src/client.cpp index a5ed6f61..2e3984ba 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -1963,7 +1963,8 @@ void Client::addNode(v3s16 p, MapNode n) try { //TimeTaker timer3("Client::addNode(): addNodeAndUpdate"); - m_env.getMap().addNodeAndUpdate(p, n, modified_blocks); + std::string st = std::string(""); + m_env.getMap().addNodeAndUpdate(p, n, modified_blocks, st); } catch(InvalidPositionException &e) {} diff --git a/src/content_craft.cpp b/src/content_craft.cpp index 8e8b17a9..866c1e53 100644 --- a/src/content_craft.cpp +++ b/src/content_craft.cpp @@ -297,6 +297,24 @@ InventoryItem *craft_get_result(InventoryItem **items) } } + // Locking Chest + { + ItemSpec specs[9]; + specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD); + specs[1] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD); + specs[2] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD); + specs[3] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD); + specs[4] = ItemSpec(ITEM_CRAFT, "steel_ingot"); + specs[5] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD); + specs[6] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD); + specs[7] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD); + specs[8] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD); + if(checkItemCombination(items, specs)) + { + return new MaterialItem(CONTENT_LOCKABLE_CHEST, 1); + } + } + // Furnace { ItemSpec specs[9]; diff --git a/src/content_mapnode.cpp b/src/content_mapnode.cpp index 09a84156..38c5099e 100644 --- a/src/content_mapnode.cpp +++ b/src/content_mapnode.cpp @@ -573,6 +573,20 @@ void content_mapnode_init() f->initial_metadata = new ChestNodeMetadata(); setWoodLikeDiggingProperties(f->digging_properties, 1.0); + i = CONTENT_LOCKABLE_CHEST; + f = &content_features(i); + f->param_type = CPT_FACEDIR_SIMPLE; + f->setAllTextures("chest_side.png"); + f->setTexture(0, "chest_top.png"); + f->setTexture(1, "chest_top.png"); + f->setTexture(5, "chest_lock.png"); // Z- + f->setInventoryTexture("chest_lock.png"); + //f->setInventoryTextureCube("chest_top.png", "chest_side.png", "chest_side.png"); + f->dug_item = std::string("MaterialItem2 ")+itos(i)+" 1"; + if(f->initial_metadata == NULL) + f->initial_metadata = new LockingChestNodeMetadata(); + setWoodLikeDiggingProperties(f->digging_properties, 1.0); + i = CONTENT_FURNACE; f = &content_features(i); f->param_type = CPT_FACEDIR_SIMPLE; diff --git a/src/content_mapnode.h b/src/content_mapnode.h index 35dcc20e..366c9b1a 100644 --- a/src/content_mapnode.h +++ b/src/content_mapnode.h @@ -48,6 +48,7 @@ MapNode mapnode_translate_to_internal(MapNode n_from, u8 version); #define CONTENT_SIGN_WALL 14 #define CONTENT_CHEST 15 #define CONTENT_FURNACE 16 +#define CONTENT_LOCKABLE_CHEST 17 #define CONTENT_FENCE 21 diff --git a/src/content_nodemeta.cpp b/src/content_nodemeta.cpp index 1552c8e1..e79ff6d5 100644 --- a/src/content_nodemeta.cpp +++ b/src/content_nodemeta.cpp @@ -117,6 +117,70 @@ std::string ChestNodeMetadata::getInventoryDrawSpecString() "list[current_player;main;0,5;8,4;]"; } +/* + LockingChestNodeMetadata +*/ + +// Prototype +LockingChestNodeMetadata proto_LockingChestNodeMetadata; + +LockingChestNodeMetadata::LockingChestNodeMetadata() +{ + NodeMetadata::registerType(typeId(), create); + + m_inventory = new Inventory(); + m_inventory->addList("0", 8*4); +} +LockingChestNodeMetadata::~LockingChestNodeMetadata() +{ + delete m_inventory; +} +u16 LockingChestNodeMetadata::typeId() const +{ + return CONTENT_LOCKABLE_CHEST; +} +NodeMetadata* LockingChestNodeMetadata::create(std::istream &is) +{ + LockingChestNodeMetadata *d = new LockingChestNodeMetadata(); + d->setOwner(deSerializeString(is)); + d->m_inventory->deSerialize(is); + return d; +} +NodeMetadata* LockingChestNodeMetadata::clone() +{ + LockingChestNodeMetadata *d = new LockingChestNodeMetadata(); + *d->m_inventory = *m_inventory; + return d; +} +void LockingChestNodeMetadata::serializeBody(std::ostream &os) +{ + os<serialize(os); +} +std::string LockingChestNodeMetadata::infoText() +{ + return "Locking Chest"; +} +bool LockingChestNodeMetadata::nodeRemovalDisabled() +{ + /* + Disable removal if chest contains something + */ + InventoryList *list = m_inventory->getList("0"); + if(list == NULL) + return false; + if(list->getUsedSlots() == 0) + return false; + return true; +} +std::string LockingChestNodeMetadata::getInventoryDrawSpecString() +{ + return + "invsize[8,9;]" + "list[current_name;0;0,0;8,4;]" + "list[current_player;main;0,5;8,4;]"; +} + /* FurnaceNodeMetadata */ diff --git a/src/content_nodemeta.h b/src/content_nodemeta.h index 50decd91..af337412 100644 --- a/src/content_nodemeta.h +++ b/src/content_nodemeta.h @@ -62,6 +62,29 @@ private: Inventory *m_inventory; }; +class LockingChestNodeMetadata : public NodeMetadata +{ +public: + LockingChestNodeMetadata(); + ~LockingChestNodeMetadata(); + + virtual u16 typeId() const; + static NodeMetadata* create(std::istream &is); + virtual NodeMetadata* clone(); + virtual void serializeBody(std::ostream &os); + virtual std::string infoText(); + virtual Inventory* getInventory() {return m_inventory;} + virtual bool nodeRemovalDisabled(); + virtual std::string getInventoryDrawSpecString(); + + std::string getOwner(){ return m_text; } + void setOwner(std::string t){ m_text = t; } + +private: + Inventory *m_inventory; + std::string m_text; +}; + class FurnaceNodeMetadata : public NodeMetadata { public: diff --git a/src/map.cpp b/src/map.cpp index 7de79c75..a8e7235c 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -28,6 +28,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "porting.h" #include "mapgen.h" #include "nodemetadata.h" +#include "content_nodemeta.h" +#include "content_mapnode.h" /* SQLite format specification: @@ -890,7 +892,7 @@ void Map::updateLighting(core::map & a_blocks, /* */ void Map::addNodeAndUpdate(v3s16 p, MapNode n, - core::map &modified_blocks) + core::map &modified_blocks, std::string &player_name) { /*PrintInfo(m_dout); m_dout<clone(); + /* lockable chest, insert the owner's name */ + if (meta->typeId() == CONTENT_LOCKABLE_CHEST) + { + LockingChestNodeMetadata *lcm = (LockingChestNodeMetadata*)meta; + lcm->setOwner(player_name); + } setNodeMetadata(p, meta); } + else if (n.getContent() == CONTENT_LOCKABLE_CHEST) + { + LockingChestNodeMetadata *lcm = new LockingChestNodeMetadata(); + lcm->setOwner(player_name); + setNodeMetadata(p, (NodeMetadata*)lcm); + } /* If node is under sunlight and doesn't let sunlight through, @@ -1290,7 +1304,8 @@ bool Map::addNodeWithEvent(v3s16 p, MapNode n) bool succeeded = true; try{ core::map modified_blocks; - addNodeAndUpdate(p, n, modified_blocks); + std::string st = std::string(""); + addNodeAndUpdate(p, n, modified_blocks, st); // Copy modified_blocks to event for(core::map::Iterator diff --git a/src/map.h b/src/map.h index e0b67eb6..6248a566 100644 --- a/src/map.h +++ b/src/map.h @@ -205,7 +205,7 @@ public: These handle lighting but not faces. */ void addNodeAndUpdate(v3s16 p, MapNode n, - core::map &modified_blocks); + core::map &modified_blocks, std::string &player_name); void removeNodeAndUpdate(v3s16 p, core::map &modified_blocks); diff --git a/src/server.cpp b/src/server.cpp index 14d8942c..689a2b53 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -2873,7 +2873,8 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) { MapEditEventIgnorer ign(&m_ignore_map_edit_events); - m_env.getMap().addNodeAndUpdate(p_over, n, modified_blocks); + std::string p_name = std::string(player->getName()); + m_env.getMap().addNodeAndUpdate(p_over, n, modified_blocks, p_name); } /* Set blocks not sent to far players @@ -3200,7 +3201,46 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) } // Disallow moving items if not allowed to build else if((getPlayerPrivs(player) & PRIV_BUILD) == 0) + { return; + } + // if it's a locking chest, only allow the owner or server admins to move items + else if (ma->from_inv != "current_player" && (getPlayerPrivs(player) & PRIV_SERVER) == 0) + { + Strfnd fn(ma->from_inv); + std::string id0 = fn.next(":"); + if(id0 == "nodemeta") + { + v3s16 p; + p.X = stoi(fn.next(",")); + p.Y = stoi(fn.next(",")); + p.Z = stoi(fn.next(",")); + NodeMetadata *meta = m_env.getMap().getNodeMetadata(p); + if(meta && meta->typeId() == CONTENT_LOCKABLE_CHEST) { + LockingChestNodeMetadata *lcm = (LockingChestNodeMetadata*)meta; + if (lcm->getOwner() != player->getName()) + return; + } + } + } + else if (ma->to_inv != "current_player" && (getPlayerPrivs(player) & PRIV_SERVER) == 0) + { + Strfnd fn(ma->to_inv); + std::string id0 = fn.next(":"); + if(id0 == "nodemeta") + { + v3s16 p; + p.X = stoi(fn.next(",")); + p.Y = stoi(fn.next(",")); + p.Z = stoi(fn.next(",")); + NodeMetadata *meta = m_env.getMap().getNodeMetadata(p); + if(meta && meta->typeId() == CONTENT_LOCKABLE_CHEST) { + LockingChestNodeMetadata *lcm = (LockingChestNodeMetadata*)meta; + if (lcm->getOwner() != player->getName()) + return; + } + } + } } if(disable_action == false) From 79cb754358691e170c87457b2da60f6e6900f517 Mon Sep 17 00:00:00 2001 From: darkrose Date: Thu, 22 Sep 2011 19:12:17 +1000 Subject: [PATCH 2/3] added locking chest texture --- data/chest_lock.png | Bin 0 -> 224 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 data/chest_lock.png diff --git a/data/chest_lock.png b/data/chest_lock.png new file mode 100644 index 0000000000000000000000000000000000000000..ae038fae5435ba60b88b6db73296c451cd5e827e GIT binary patch literal 224 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61SBU+%rFB|jKx9jP7LeL$-D$|I14-?iy0XB zj({-ZRBb+KpdfpRr>`sfZB9;hIl=nwBzd4vk*AAeh{pNkoQwno&J6}YAfphqlbKnV znOV}zVnKITw}1W5PpZt!V$YK{ItCOdNi|Q9XPS6}z2o!Y(^F^iuuB#EX1pBQC|p=6 zwVU_8-WnxeW@T%APN{qA&p*HNrNyA*D|6V=A9GsRFT^o_+tR|$z~CY;Ua=wf`9z@Y N44$rjF6*2UngFd7L+t Date: Fri, 23 Sep 2011 18:04:48 +1000 Subject: [PATCH 3/3] remove content_* things from map.cpp --- src/content_nodemeta.h | 4 ++-- src/map.cpp | 15 +-------------- src/nodemetadata.h | 3 +++ 3 files changed, 6 insertions(+), 16 deletions(-) diff --git a/src/content_nodemeta.h b/src/content_nodemeta.h index af337412..e2033431 100644 --- a/src/content_nodemeta.h +++ b/src/content_nodemeta.h @@ -77,8 +77,8 @@ public: virtual bool nodeRemovalDisabled(); virtual std::string getInventoryDrawSpecString(); - std::string getOwner(){ return m_text; } - void setOwner(std::string t){ m_text = t; } + virtual std::string getOwner(){ return m_text; } + virtual void setOwner(std::string t){ m_text = t; } private: Inventory *m_inventory; diff --git a/src/map.cpp b/src/map.cpp index a8e7235c..32675f08 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -28,8 +28,6 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "porting.h" #include "mapgen.h" #include "nodemetadata.h" -#include "content_nodemeta.h" -#include "content_mapnode.h" /* SQLite format specification: @@ -1016,20 +1014,9 @@ void Map::addNodeAndUpdate(v3s16 p, MapNode n, if(meta_proto) { NodeMetadata *meta = meta_proto->clone(); - /* lockable chest, insert the owner's name */ - if (meta->typeId() == CONTENT_LOCKABLE_CHEST) - { - LockingChestNodeMetadata *lcm = (LockingChestNodeMetadata*)meta; - lcm->setOwner(player_name); - } + meta->setOwner(player_name); setNodeMetadata(p, meta); } - else if (n.getContent() == CONTENT_LOCKABLE_CHEST) - { - LockingChestNodeMetadata *lcm = new LockingChestNodeMetadata(); - lcm->setOwner(player_name); - setNodeMetadata(p, (NodeMetadata*)lcm); - } /* If node is under sunlight and doesn't let sunlight through, diff --git a/src/nodemetadata.h b/src/nodemetadata.h index de682f9b..d81ade96 100644 --- a/src/nodemetadata.h +++ b/src/nodemetadata.h @@ -65,6 +65,9 @@ public: // Used to make custom inventory menus. // See format in guiInventoryMenu.cpp. virtual std::string getInventoryDrawSpecString(){return "";} + // primarily used for locking chests, but others can play too + virtual std::string getOwner(){ return std::string(""); } + virtual void setOwner(std::string t){ } protected: static void registerType(u16 id, Factory f);