Skip to content

Commit

Permalink
added locking chests - clean patch
Browse files Browse the repository at this point in the history
  • Loading branch information
TicklishHoneyBee committed Sep 22, 2011
1 parent ae6d83a commit 582e450
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 5 deletions.
5 changes: 5 additions & 0 deletions doc/mapformat.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,10 @@ Furnace metadata:
Chest metadata:
TBD

Locking Chest metadata:
u16 string_len
u8[string_len] string
TBD

// END

3 changes: 2 additions & 1 deletion src/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{}
Expand Down
18 changes: 18 additions & 0 deletions src/content_craft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
14 changes: 14 additions & 0 deletions src/content_mapnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/content_mapnode.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
64 changes: 64 additions & 0 deletions src/content_nodemeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<<serializeString(m_text);
m_inventory->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
*/
Expand Down
23 changes: 23 additions & 0 deletions src/content_nodemeta.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,29 @@ class ChestNodeMetadata : public NodeMetadata
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:
Expand Down
19 changes: 17 additions & 2 deletions src/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -890,7 +892,7 @@ void Map::updateLighting(core::map<v3s16, MapBlock*> & a_blocks,
/*
*/
void Map::addNodeAndUpdate(v3s16 p, MapNode n,
core::map<v3s16, MapBlock*> &modified_blocks)
core::map<v3s16, MapBlock*> &modified_blocks, std::string &player_name)
{
/*PrintInfo(m_dout);
m_dout<<DTIME<<"Map::addNodeAndUpdate(): p=("
Expand Down Expand Up @@ -1014,8 +1016,20 @@ 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);
}
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,
Expand Down Expand Up @@ -1290,7 +1304,8 @@ bool Map::addNodeWithEvent(v3s16 p, MapNode n)
bool succeeded = true;
try{
core::map<v3s16, MapBlock*> 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<v3s16, MapBlock*>::Iterator
Expand Down
2 changes: 1 addition & 1 deletion src/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ class Map /*: public NodeContainer*/
These handle lighting but not faces.
*/
void addNodeAndUpdate(v3s16 p, MapNode n,
core::map<v3s16, MapBlock*> &modified_blocks);
core::map<v3s16, MapBlock*> &modified_blocks, std::string &player_name);
void removeNodeAndUpdate(v3s16 p,
core::map<v3s16, MapBlock*> &modified_blocks);

Expand Down
42 changes: 41 additions & 1 deletion src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 582e450

Please sign in to comment.