^ deprecated: invsize[<W>,<H>;]
list[<inventory location>;<list name>;<X>,<Y>;<W>,<H>;]
+list[<inventory location>;<list name>;<X>,<Y>;<W>,<H>;<starting item index>]
^ Show an inventory list
image[<X>,<Y>;<W>,<H>;<texture name>]
^ When clicked, fields will be sent and the form will quit.
Inventory location:
+
- "context": Selected node metadata (deprecated: "current_name")
- "current_player": Player to whom the menu is shown
- "player:<name>": Any player
- "nodemeta:<X>,<Y>,<Z>": Any node metadata
+- "detached:<name>": A detached inventory
Helper functions
-----------------
minetest.get_inventory(location) -> InvRef
^ location = eg. {type="player", name="celeron55"}
{type="node", pos={x=, y=, z=}}
+ {type="detached", name="creative"}
+minetest.create_detached_inventory(name) -> InvRef
+^ Creates a detached inventory. If it already exists, it is cleared.
Item handling:
minetest.inventorycube(img1, img2, img3)
end)
end)]]
+-- Create a detached inventory
+local inv = minetest.create_detached_inventory("test_inventory")
+inv:set_size("main", 4*6)
+inv:add_item("main", "experimental:tester_tool_1")
+inv:add_item("main", "experimental:tnt 5")
+
minetest.register_chatcommand("test1", {
params = "",
description = "Test 1: Modify player's inventory view",
"list[current_player;main;5,3.5;8,4;]"..
"list[current_player;craft;8,0;3,3;]"..
"list[current_player;craftpreview;12,1;1,1;]"..
+ "list[detached:test_inventory;main;0,0;4,6;0]"..
"button[0.5,7;2,1;button1;Button 1]"..
"button_exit[2.5,7;2,1;button2;Exit Button]"
)
sleep_ms(100);
delete m_inventory_from_server;
+
+ // Delete detached inventories
+ {
+ for(std::map<std::string, Inventory*>::iterator
+ i = m_detached_inventories.begin();
+ i != m_detached_inventories.end(); i++){
+ delete i->second;
+ }
+ }
}
void Client::connect(Address address)
assert(player != NULL);
player->inventory_formspec = deSerializeLongString(is);
}
+ else if(command == TOCLIENT_DETACHED_INVENTORY)
+ {
+ std::string datastring((char*)&data[2], datasize-2);
+ std::istringstream is(datastring, std::ios_base::binary);
+
+ std::string name = deSerializeString(is);
+
+ infostream<<"Client: Detached inventory update: \""<<name<<"\""<<std::endl;
+
+ Inventory *inv = NULL;
+ if(m_detached_inventories.count(name) > 0)
+ inv = m_detached_inventories[name];
+ else{
+ inv = new Inventory(m_itemdef);
+ m_detached_inventories[name] = inv;
+ }
+ inv->deSerialize(is);
+ }
else
{
infostream<<"Client: Ignoring unknown command "
return meta->getInventory();
}
break;
+ case InventoryLocation::DETACHED:
+ {
+ if(m_detached_inventories.count(loc.name) == 0)
+ return NULL;
+ return m_detached_inventories[loc.name];
+ }
+ break;
default:
assert(0);
}
// Privileges
std::set<std::string> m_privileges;
+
+ // Detached inventories
+ // key = name
+ std::map<std::string, Inventory*> m_detached_inventories;
};
#endif // !CLIENT_HEADER
PROTOCOL_VERSION 12:
TOSERVER_INVENTORY_FIELDS
16-bit node ids
+ TOCLIENT_DETACHED_INVENTORY
*/
#define PROTOCOL_VERSION 12
u32 len
u8[len] formspec
*/
+
+ TOCLIENT_DETACHED_INVENTORY = 0x43,
+ /*
+ [0] u16 command
+ u16 len
+ u8[len] name
+ [2] serialized inventory
+ */
};
enum ToServerCommand
<<", pos=("<<pos.X<<","<<pos.Y<<")"
<<", geom=("<<geom.X<<","<<geom.Y<<")"
<<std::endl;
- f.next("]");
+ std::string start_i_s = f.next("]");
+ s32 start_i = 0;
+ if(start_i_s != "")
+ start_i = stoi(start_i_s);
if(bp_set != 2)
- errorstream<<"WARNING: invalid use of button without a size[] element"<<std::endl;
- m_inventorylists.push_back(ListDrawSpec(loc, listname, pos, geom));
+ errorstream<<"WARNING: invalid use of list without a size[] element"<<std::endl;
+ m_inventorylists.push_back(ListDrawSpec(loc, listname, pos, geom, start_i));
}
else if(type == "image")
{
for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
{
+ s32 item_i = i + s.start_item_i;
s32 x = (i%s.geom.X) * spacing.X;
s32 y = (i/s.geom.X) * spacing.Y;
v2s32 p0(x,y);
core::rect<s32> rect = imgrect + s.pos + p0;
if(rect.isPointInside(p))
{
- return ItemSpec(s.inventoryloc, s.listname, i);
+ return ItemSpec(s.inventoryloc, s.listname, item_i);
}
}
}
for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
{
+ u32 item_i = i + s.start_item_i;
+ if(item_i >= ilist->getSize())
+ break;
s32 x = (i%s.geom.X) * spacing.X;
s32 y = (i/s.geom.X) * spacing.Y;
v2s32 p(x,y);
core::rect<s32> rect = imgrect + s.pos + p;
ItemStack item;
if(ilist)
- item = ilist->getItem(i);
+ item = ilist->getItem(item_i);
bool selected = m_selected_item
&& m_invmgr->getInventory(m_selected_item->inventoryloc) == inv
}
ListDrawSpec(const InventoryLocation &a_inventoryloc,
const std::string &a_listname,
- v2s32 a_pos, v2s32 a_geom):
+ v2s32 a_pos, v2s32 a_geom, s32 a_start_item_i):
inventoryloc(a_inventoryloc),
listname(a_listname),
pos(a_pos),
- geom(a_geom)
+ geom(a_geom),
+ start_item_i(a_start_item_i)
{
}
std::string listname;
v2s32 pos;
v2s32 geom;
+ s32 start_item_i;
};
struct ImageDrawSpec
void InventoryLocation::serialize(std::ostream &os) const
{
- switch(type){
- case InventoryLocation::UNDEFINED:
- {
- os<<"undefined";
- }
+ switch(type){
+ case InventoryLocation::UNDEFINED:
+ os<<"undefined";
+ break;
+ case InventoryLocation::CURRENT_PLAYER:
+ os<<"current_player";
break;
- case InventoryLocation::CURRENT_PLAYER:
- {
- os<<"current_player";
- }
+ case InventoryLocation::PLAYER:
+ os<<"player:"<<name;
break;
- case InventoryLocation::PLAYER:
- {
- os<<"player:"<<name;
- }
+ case InventoryLocation::NODEMETA:
+ os<<"nodemeta:"<<p.X<<","<<p.Y<<","<<p.Z;
break;
- case InventoryLocation::NODEMETA:
- {
- os<<"nodemeta:"<<p.X<<","<<p.Y<<","<<p.Z;
- }
+ case InventoryLocation::DETACHED:
+ os<<"detached:"<<name;
break;
- default:
- assert(0);
- }
+ default:
+ assert(0);
+ }
}
void InventoryLocation::deSerialize(std::istream &is)
p.Y = stoi(fn.next(","));
p.Z = stoi(fn.next(","));
}
+ else if(tname == "detached")
+ {
+ type = InventoryLocation::DETACHED;
+ std::getline(is, name, '\n');
+ }
else
{
infostream<<"Unknown InventoryLocation type=\""<<tname<<"\""<<std::endl;
CURRENT_PLAYER,
PLAYER,
NODEMETA,
+ DETACHED,
} type;
- std::string name; // PLAYER
+ std::string name; // PLAYER, DETACHED
v3s16 p; // NODEMETA
InventoryLocation()
type = NODEMETA;
p = p_;
}
+ void setDetached(const std::string &name_)
+ {
+ type = DETACHED;
+ name = name_;
+ }
void applyCurrentPlayer(const std::string &name_)
{
InventoryManager(){}
virtual ~InventoryManager(){}
- // Get an inventory or set it modified (so it will be updated over
- // network or so)
+ // Get an inventory (server and client)
virtual Inventory* getInventory(const InventoryLocation &loc){return NULL;}
- virtual std::string getInventoryOwner(const InventoryLocation &loc){return "";}
+ // Set modified (will be saved and sent over network; only on server)
virtual void setInventoryModified(const InventoryLocation &loc){}
-
- // Used on the client to send an action to the server
+ // Send inventory action to server (only on client)
virtual void inventoryAction(InventoryAction *a){}
};
lua_getfield(L, 1, "pos");
v3s16 pos = check_v3s16(L, -1);
loc.setNodeMeta(pos);
+ } else if(type == "detached"){
+ std::string name = checkstringfield(L, 1, "name");
+ loc.setDetached(name);
}
if(get_server(L)->getInventory(loc) != NULL)
return 1;
}
+// create_detached_inventory(name)
+static int l_create_detached_inventory(lua_State *L)
+{
+ const char *name = luaL_checkstring(L, 1);
+ if(get_server(L)->createDetachedInventory(name) != NULL){
+ InventoryLocation loc;
+ loc.setDetached(name);
+ InvRef::create(L, loc);
+ }else{
+ lua_pushnil(L);
+ }
+ return 1;
+}
+
// get_dig_params(groups, tool_capabilities[, time_from_last_punch])
static int l_get_dig_params(lua_State *L)
{
{"chat_send_player", l_chat_send_player},
{"get_player_privs", l_get_player_privs},
{"get_inventory", l_get_inventory},
+ {"create_detached_inventory", l_create_detached_inventory},
{"get_dig_params", l_get_dig_params},
{"get_hit_params", l_get_hit_params},
{"get_current_modname", l_get_current_modname},
// Deinitialize scripting
infostream<<"Server: Deinitializing scripting"<<std::endl;
script_deinit(m_lua);
+
+ // Delete detached inventories
+ {
+ for(std::map<std::string, Inventory*>::iterator
+ i = m_detached_inventories.begin();
+ i != m_detached_inventories.end(); i++){
+ delete i->second;
+ }
+ }
}
void Server::start(unsigned short port)
// Send inventory
UpdateCrafting(peer_id);
SendInventory(peer_id);
-
+
// Send HP
SendPlayerHP(peer_id);
+ // Send detached inventories
+ sendDetachedInventories(peer_id);
+
// Show death screen if necessary
if(player->hp == 0)
SendDeathscreen(m_con, peer_id, false, v3f(0,0,0));
delete a;
return;
}
-
- // If player is not an admin, check for ownership of src and dst
- /*if(!checkPriv(player->getName(), "server"))
- {
- std::string owner_from = getInventoryOwner(ma->from_inv);
- if(owner_from != "" && owner_from != player->getName())
- {
- infostream<<"WARNING: "<<player->getName()
- <<" tried to access an inventory that"
- <<" belongs to "<<owner_from<<std::endl;
- delete a;
- return;
- }
-
- std::string owner_to = getInventoryOwner(ma->to_inv);
- if(owner_to != "" && owner_to != player->getName())
- {
- infostream<<"WARNING: "<<player->getName()
- <<" tried to access an inventory that"
- <<" belongs to "<<owner_to<<std::endl;
- delete a;
- return;
- }
- }*/
}
/*
Handle restrictions and special cases of the drop action
delete a;
return;
}
- // If player is not an admin, check for ownership
- /*else if(!checkPriv(player->getName(), "server"))
- {
- std::string owner_from = getInventoryOwner(da->from_inv);
- if(owner_from != "" && owner_from != player->getName())
- {
- infostream<<"WARNING: "<<player->getName()
- <<" tried to access an inventory that"
- <<" belongs to "<<owner_from<<std::endl;
- delete a;
- return;
- }
- }*/
}
/*
Handle restrictions and special cases of the craft action
delete a;
return;
}
-
- // If player is not an admin, check for ownership of inventory
- /*if(!checkPriv(player->getName(), "server"))
- {
- std::string owner_craft = getInventoryOwner(ca->craft_inv);
- if(owner_craft != "" && owner_craft != player->getName())
- {
- infostream<<"WARNING: "<<player->getName()
- <<" tried to access an inventory that"
- <<" belongs to "<<owner_craft<<std::endl;
- delete a;
- return;
- }
- }*/
}
// Do the action
return meta->getInventory();
}
break;
+ case InventoryLocation::DETACHED:
+ {
+ if(m_detached_inventories.count(loc.name) == 0)
+ return NULL;
+ return m_detached_inventories[loc.name];
+ }
+ break;
default:
assert(0);
}
setBlockNotSent(blockpos);
}
break;
+ case InventoryLocation::DETACHED:
+ {
+ sendDetachedInventoryToAll(loc.name);
+ }
+ break;
default:
assert(0);
}
}
}
+void Server::sendDetachedInventory(const std::string &name, u16 peer_id)
+{
+ if(m_detached_inventories.count(name) == 0){
+ errorstream<<__FUNCTION_NAME<<": \""<<name<<"\" not found"<<std::endl;
+ return;
+ }
+ Inventory *inv = m_detached_inventories[name];
+
+ std::ostringstream os(std::ios_base::binary);
+ writeU16(os, TOCLIENT_DETACHED_INVENTORY);
+ os<<serializeString(name);
+ inv->serialize(os);
+
+ // Make data buffer
+ std::string s = os.str();
+ SharedBuffer<u8> data((u8*)s.c_str(), s.size());
+ // Send as reliable
+ m_con.Send(peer_id, 0, data, true);
+}
+
+void Server::sendDetachedInventoryToAll(const std::string &name)
+{
+ DSTACK(__FUNCTION_NAME);
+
+ for(core::map<u16, RemoteClient*>::Iterator
+ i = m_clients.getIterator();
+ i.atEnd() == false; i++){
+ RemoteClient *client = i.getNode()->getValue();
+ sendDetachedInventory(name, client->peer_id);
+ }
+}
+
+void Server::sendDetachedInventories(u16 peer_id)
+{
+ DSTACK(__FUNCTION_NAME);
+
+ for(std::map<std::string, Inventory*>::iterator
+ i = m_detached_inventories.begin();
+ i != m_detached_inventories.end(); i++){
+ const std::string &name = i->first;
+ //Inventory *inv = i->second;
+ sendDetachedInventory(name, peer_id);
+ }
+}
+
/*
Something random
*/
m_emerge_queue.addBlock(PEER_ID_INEXISTENT, blockpos, flags);
}
+Inventory* Server::createDetachedInventory(const std::string &name)
+{
+ if(m_detached_inventories.count(name) > 0){
+ infostream<<"Server clearing detached inventory \""<<name<<"\""<<std::endl;
+ delete m_detached_inventories[name];
+ } else {
+ infostream<<"Server creating detached inventory \""<<name<<"\""<<std::endl;
+ }
+ Inventory *inv = new Inventory(m_itemdef);
+ assert(inv);
+ m_detached_inventories[name] = inv;
+ sendDetachedInventoryToAll(name);
+ return inv;
+}
+
// IGameDef interface
// Under envlock
IItemDefManager* Server::getItemDefManager()
void queueBlockEmerge(v3s16 blockpos, bool allow_generate);
+ // Creates or resets inventory
+ Inventory* createDetachedInventory(const std::string &name);
+
// Envlock and conlock should be locked when using Lua
lua_State *getLua(){ return m_lua; }
void sendMediaAnnouncement(u16 peer_id);
void sendRequestedMedia(u16 peer_id,
const core::list<MediaRequest> &tosend);
+
+ void sendDetachedInventory(const std::string &name, u16 peer_id);
+ void sendDetachedInventoryToAll(const std::string &name);
+ void sendDetachedInventories(u16 peer_id);
/*
Something random
*/
std::map<s32, ServerPlayingSound> m_playing_sounds;
s32 m_next_sound_id;
+
+ /*
+ Detached inventories (behind m_env_mutex)
+ */
+ // key = name
+ std::map<std::string, Inventory*> m_detached_inventories;
};
/*