]> git.lizzy.rs Git - minetest.git/blobdiff - src/server.cpp
Add a setting to enable always flying fast
[minetest.git] / src / server.cpp
index b3cbea6a447ed76c2111ea0a3b923d29365bbccd..f635bc676c9a84f2ba4d4dd6229da58c298539c7 100644 (file)
@@ -27,7 +27,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "constants.h"
 #include "voxel.h"
 #include "config.h"
-#include "servercommand.h"
 #include "filesys.h"
 #include "mapblock.h"
 #include "serverobject.h"
@@ -54,6 +53,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "util/string.h"
 #include "util/pointedthing.h"
 #include "util/mathconstants.h"
+#include "rollback.h"
+#include "util/serialize.h"
 
 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
 
@@ -442,9 +443,12 @@ void RemoteClient::GetNextBlocks(Server *server, float dtime,
        m_nearest_unsent_reset_timer += dtime;
        
        if(m_nothing_to_send_pause_timer >= 0)
-       {
                return;
-       }
+
+       Player *player = server->m_env->getPlayer(peer_id);
+       // This can happen sometimes; clients and players are not in perfect sync.
+       if(player == NULL)
+               return;
 
        // Won't send anything if already sending
        if(m_blocks_sending.size() >= g_settings->getU16
@@ -456,10 +460,6 @@ void RemoteClient::GetNextBlocks(Server *server, float dtime,
 
        //TimeTaker timer("RemoteClient::GetNextBlocks");
        
-       Player *player = server->m_env->getPlayer(peer_id);
-
-       assert(player != NULL);
-
        v3f playerpos = player->getPosition();
        v3f playerspeed = player->getSpeed();
        v3f playerspeeddir(0,0,0);
@@ -934,6 +934,9 @@ Server::Server(
        m_env(NULL),
        m_con(PROTOCOL_ID, 512, CONNECTION_TIMEOUT, this),
        m_banmanager(path_world+DIR_DELIM+"ipban.txt"),
+       m_rollback(NULL),
+       m_rollback_sink_enabled(true),
+       m_enable_rollback_recording(false),
        m_lua(NULL),
        m_itemdef(createItemDefManager()),
        m_nodedef(createNodeDefManager()),
@@ -973,6 +976,10 @@ Server::Server(
        infostream<<"- config: "<<m_path_config<<std::endl;
        infostream<<"- game:   "<<m_gamespec.path<<std::endl;
 
+       // Create rollback manager
+       std::string rollback_path = m_path_world+DIR_DELIM+"rollback.txt";
+       m_rollback = createRollbackManager(rollback_path, this);
+
        // Add world mod search path
        m_modspaths.push_front(m_path_world + DIR_DELIM + "worldmods");
        // Add addon mod search path
@@ -1049,7 +1056,7 @@ Server::Server(
        
        m_env = new ServerEnvironment(new ServerMap(path_world, this), m_lua,
                        this, this);
-
+       
        // Give environment reference to scripting api
        scriptapi_add_environment(m_lua, m_env);
        
@@ -1105,7 +1112,17 @@ Server::~Server()
                        {}
                }
        }
-       
+
+       {
+               JMutexAutoLock envlock(m_env_mutex);
+               JMutexAutoLock conlock(m_con_mutex);
+
+               /*
+                       Execute script shutdown hooks
+               */
+               scriptapi_on_shutdown(m_lua);
+       }
+
        {
                JMutexAutoLock envlock(m_env_mutex);
 
@@ -1137,14 +1154,6 @@ Server::~Server()
                        i = m_clients.getIterator();
                        i.atEnd() == false; i++)
                {
-                       /*// Delete player
-                       // NOTE: These are removed by env destructor
-                       {
-                               u16 peer_id = i.getNode()->getKey();
-                               JMutexAutoLock envlock(m_env_mutex);
-                               m_env->removePlayer(peer_id);
-                       }*/
-                       
                        // Delete client
                        delete i.getNode()->getValue();
                }
@@ -1152,6 +1161,7 @@ Server::~Server()
        
        // Delete things in the reverse order of creation
        delete m_env;
+       delete m_rollback;
        delete m_event;
        delete m_itemdef;
        delete m_nodedef;
@@ -1160,6 +1170,15 @@ Server::~Server()
        // 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)
@@ -1355,9 +1374,9 @@ void Server::AsyncRunStep()
                        /*
                                Send player inventories and HPs if necessary
                        */
-                       if(playersao->m_teleported){
+                       if(playersao->m_moved){
                                SendMovePlayer(client->peer_id);
-                               playersao->m_teleported = false;
+                               playersao->m_moved = false;
                        }
                        if(playersao->m_inventory_not_sent){
                                UpdateCrafting(client->peer_id);
@@ -1551,7 +1570,7 @@ void Server::AsyncRunStep()
                                
                                if(obj)
                                        data_buffer.append(serializeLongString(
-                                                       obj->getClientInitializationData()));
+                                                       obj->getClientInitializationData(client->net_proto_version)));
                                else
                                        data_buffer.append(serializeLongString(""));
 
@@ -1856,6 +1875,10 @@ void Server::AsyncRunStep()
                        counter = 0.0;
                        
                        m_emergethread.trigger();
+
+                       // Update m_enable_rollback_recording here too
+                       m_enable_rollback_recording =
+                                       g_settings->getBool("enable_rollback_recording");
                }
        }
 
@@ -2017,40 +2040,74 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
                        Read and check network protocol version
                */
 
-               u16 net_proto_version = 0;
+               u16 min_net_proto_version = 0;
                if(datasize >= 2+1+PLAYERNAME_SIZE+PASSWORD_SIZE+2)
+                       min_net_proto_version = readU16(&data[2+1+PLAYERNAME_SIZE+PASSWORD_SIZE]);
+
+               // Use same version as minimum and maximum if maximum version field
+               // doesn't exist (backwards compatibility)
+               u16 max_net_proto_version = min_net_proto_version;
+               if(datasize >= 2+1+PLAYERNAME_SIZE+PASSWORD_SIZE+2+2)
+                       max_net_proto_version = readU16(&data[2+1+PLAYERNAME_SIZE+PASSWORD_SIZE+2]);
+
+               // Start with client's maximum version
+               u16 net_proto_version = max_net_proto_version;
+
+               // Figure out a working version if it is possible at all
+               if(max_net_proto_version >= SERVER_PROTOCOL_VERSION_MIN ||
+                               min_net_proto_version <= SERVER_PROTOCOL_VERSION_MAX)
                {
-                       net_proto_version = readU16(&data[2+1+PLAYERNAME_SIZE+PASSWORD_SIZE]);
+                       // If maximum is larger than our maximum, go with our maximum
+                       if(max_net_proto_version > SERVER_PROTOCOL_VERSION_MAX)
+                               net_proto_version = SERVER_PROTOCOL_VERSION_MAX;
+                       // Else go with client's maximum
+                       else
+                               net_proto_version = max_net_proto_version;
                }
 
+               verbosestream<<"Server: "<<peer_id<<" Protocol version: min: "
+                               <<min_net_proto_version<<", max: "<<max_net_proto_version
+                               <<", chosen: "<<net_proto_version<<std::endl;
+
                getClient(peer_id)->net_proto_version = net_proto_version;
 
-               if(net_proto_version == 0)
+               if(net_proto_version < SERVER_PROTOCOL_VERSION_MIN ||
+                               net_proto_version > SERVER_PROTOCOL_VERSION_MAX)
                {
-                       actionstream<<"Server: An old tried to connect from "<<addr_s
+                       actionstream<<"Server: A mismatched client tried to connect from "<<addr_s
                                        <<std::endl;
                        SendAccessDenied(m_con, peer_id, std::wstring(
                                        L"Your client's version is not supported.\n"
                                        L"Server version is ")
-                                       + narrow_to_wide(VERSION_STRING) + L"."
+                                       + narrow_to_wide(VERSION_STRING) + L",\n"
+                                       + L"server's PROTOCOL_VERSION is "
+                                       + narrow_to_wide(itos(SERVER_PROTOCOL_VERSION_MIN))
+                                       + L"..."
+                                       + narrow_to_wide(itos(SERVER_PROTOCOL_VERSION_MAX))
+                                       + L", client's PROTOCOL_VERSION is "
+                                       + narrow_to_wide(itos(min_net_proto_version))
+                                       + L"..."
+                                       + narrow_to_wide(itos(max_net_proto_version))
                        );
                        return;
                }
                
                if(g_settings->getBool("strict_protocol_version_checking"))
                {
-                       if(net_proto_version != PROTOCOL_VERSION)
+                       if(net_proto_version != LATEST_PROTOCOL_VERSION)
                        {
-                               actionstream<<"Server: A mismatched client tried to connect"
-                                               <<" from "<<addr_s<<std::endl;
+                               actionstream<<"Server: A mismatched (strict) client tried to "
+                                               <<"connect from "<<addr_s<<std::endl;
                                SendAccessDenied(m_con, peer_id, std::wstring(
                                                L"Your client's version is not supported.\n"
                                                L"Server version is ")
                                                + narrow_to_wide(VERSION_STRING) + L",\n"
-                                               + L"server's PROTOCOL_VERSION is "
-                                               + narrow_to_wide(itos(PROTOCOL_VERSION))
+                                               + L"server's PROTOCOL_VERSION (strict) is "
+                                               + narrow_to_wide(itos(LATEST_PROTOCOL_VERSION))
                                                + L", client's PROTOCOL_VERSION is "
-                                               + narrow_to_wide(itos(net_proto_version))
+                                               + narrow_to_wide(itos(min_net_proto_version))
+                                               + L"..."
+                                               + narrow_to_wide(itos(max_net_proto_version))
                                );
                                return;
                        }
@@ -2192,11 +2249,12 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
                        Answer with a TOCLIENT_INIT
                */
                {
-                       SharedBuffer<u8> reply(2+1+6+8);
+                       SharedBuffer<u8> reply(2+1+6+8+4);
                        writeU16(&reply[0], TOCLIENT_INIT);
                        writeU8(&reply[2], deployed);
                        writeV3S16(&reply[2+1], floatToInt(playersao->getPlayer()->getPosition()+v3f(0,BS/2,0), BS));
                        writeU64(&reply[2+1+6], m_env->getServerMap().getSeed());
+                       writeF1000(&reply[2+1+6+8], g_settings->getFloat("dedicated_server_step"));
                        
                        // Send as reliable
                        m_con.Send(peer_id, 0, reply, true);
@@ -2222,8 +2280,9 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
                        return;
                }
 
-               getClient(peer_id)->serialization_version
-                               = getClient(peer_id)->pending_serialization_version;
+               RemoteClient *client = getClient(peer_id);
+               client->serialization_version =
+                               getClient(peer_id)->pending_serialization_version;
 
                /*
                        Send some initialization data
@@ -2236,7 +2295,7 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
                SendItemDef(m_con, peer_id, m_itemdef);
                
                // Send node definitions
-               SendNodeDef(m_con, peer_id, m_nodedef);
+               SendNodeDef(m_con, peer_id, m_nodedef, client->net_proto_version);
                
                // Send media announcement
                sendMediaAnnouncement(peer_id);
@@ -2250,10 +2309,13 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
                // 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));
@@ -2287,9 +2349,10 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
                }
                
                // Warnings about protocol version can be issued here
-               if(getClient(peer_id)->net_proto_version < PROTOCOL_VERSION)
+               if(getClient(peer_id)->net_proto_version < LATEST_PROTOCOL_VERSION)
                {
-                       SendChatMessage(peer_id, L"# Server: WARNING: YOUR CLIENT IS OLD AND MAY WORK PROPERLY WITH THIS SERVER!");
+                       SendChatMessage(peer_id, L"# Server: WARNING: YOUR CLIENT'S "
+                                       L"VERSION MAY NOT BE FULLY COMPATIBLE WITH THIS SERVER!");
                }
 
                /*
@@ -2354,6 +2417,9 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
                v3s32 ss = readV3S32(&data[start+2+12]);
                f32 pitch = (f32)readS32(&data[2+12+12]) / 100.0;
                f32 yaw = (f32)readS32(&data[2+12+12+4]) / 100.0;
+               u32 keyPressed = 0;
+               if(datasize >= 2+12+12+4+4+4)
+                       keyPressed = (u32)readU32(&data[2+12+12+4+4]);
                v3f position((f32)ps.X/100., (f32)ps.Y/100., (f32)ps.Z/100.);
                v3f speed((f32)ss.X/100., (f32)ss.Y/100., (f32)ss.Z/100.);
                pitch = wrapDegrees(pitch);
@@ -2363,6 +2429,16 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
                player->setSpeed(speed);
                player->setPitch(pitch);
                player->setYaw(yaw);
+               player->keyPressed=keyPressed;
+               player->control.up = (bool)(keyPressed&1);
+               player->control.down = (bool)(keyPressed&2);
+               player->control.left = (bool)(keyPressed&4);
+               player->control.right = (bool)(keyPressed&8);
+               player->control.jump = (bool)(keyPressed&16);
+               player->control.aux1 = (bool)(keyPressed&32);
+               player->control.sneak = (bool)(keyPressed&64);
+               player->control.LMB = (bool)(keyPressed&128);
+               player->control.RMB = (bool)(keyPressed&256);
                
                /*infostream<<"Server::ProcessData(): Moved player "<<peer_id<<" to "
                                <<"("<<position.X<<","<<position.Y<<","<<position.Z<<")"
@@ -2469,6 +2545,10 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
                        return;
                }
 
+               // If something goes wrong, this player is to blame
+               RollbackScopeActor rollback_scope(m_rollback,
+                               std::string("player:")+player->getName());
+
                /*
                        Note: Always set inventory not sent, to repair cases
                        where the client made a bad prediction.
@@ -2532,30 +2612,6 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
                                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
@@ -2574,19 +2630,6 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
                                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
@@ -2611,20 +2654,6 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
                                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
@@ -2654,6 +2683,10 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
                        message += (wchar_t)readU16(buf);
                }
 
+               // If something goes wrong, this player is to blame
+               RollbackScopeActor rollback_scope(m_rollback,
+                               std::string("player:")+player->getName());
+
                // Get player name of this client
                std::wstring name = narrow_to_wide(player->getName());
                
@@ -2671,36 +2704,16 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
                // Whether to send to other players
                bool send_to_others = false;
                
-               // Parse commands
+               // Commands are implemented in Lua, so only catch invalid
+               // commands that were not "eaten" and send an error back
                if(message[0] == L'/')
                {
-                       size_t strip_size = 1;
-                       if (message[1] == L'#') // support old-style commans
-                               ++strip_size;
-                       message = message.substr(strip_size);
-
-                       WStrfnd f1(message);
-                       f1.next(L" "); // Skip over /#whatever
-                       std::wstring paramstring = f1.next(L"");
-
-                       ServerCommandContext *ctx = new ServerCommandContext(
-                               str_split(message, L' '),
-                               paramstring,
-                               this,
-                               m_env,
-                               player);
-
-                       std::wstring reply(processServerCommand(ctx));
-                       send_to_sender = ctx->flags & SEND_TO_SENDER;
-                       send_to_others = ctx->flags & SEND_TO_OTHERS;
-
-                       if (ctx->flags & SEND_NO_PREFIX)
-                               line += reply;
+                       message = message.substr(1);
+                       send_to_sender = true;
+                       if(message.length() == 0)
+                               line += L"-!- Empty command";
                        else
-                               line += L"Server: " + reply;
-
-                       delete ctx;
-
+                               line += L"-!- Invalid command: " + str_split(message, L' ')[0];
                }
                else
                {
@@ -2872,6 +2885,9 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
                // (definitions and files)
                getClient(peer_id)->definitions_sent = true;
        }
+       else if(command == TOSERVER_RECEIVED_MEDIA) {
+               getClient(peer_id)->definitions_sent = true;
+       }
        else if(command == TOSERVER_INTERACT)
        {
                std::string datastring((char*)&data[2], datasize-2);
@@ -2988,6 +3004,12 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
                        return;
                }
 
+               /*
+                       If something goes wrong, this player is to blame
+               */
+               RollbackScopeActor rollback_scope(m_rollback,
+                               std::string("player:")+player->getName());
+
                /*
                        0: start digging or punch object
                */
@@ -3170,8 +3192,7 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
                                // Placement was handled in lua
 
                                // Apply returned ItemStack
-                               if(g_settings->getBool("creative_mode") == false)
-                                       playersao->setWieldedItem(item);
+                               playersao->setWieldedItem(item);
                        }
                        
                        // If item has node placement prediction, always send the above
@@ -3197,11 +3218,11 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
                                        item, playersao, pointed))
                        {
                                // Apply returned ItemStack
-                               if(g_settings->getBool("creative_mode") == false)
-                                       playersao->setWieldedItem(item);
+                               playersao->setWieldedItem(item);
                        }
 
                } // action == 4
+               
 
                /*
                        Catch invalid actions
@@ -3245,8 +3266,23 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
                        fields[fieldname] = fieldvalue;
                }
 
+               // If something goes wrong, this player is to blame
+               RollbackScopeActor rollback_scope(m_rollback,
+                               std::string("player:")+player->getName());
+
+               // Check the target node for rollback data; leave others unnoticed
+               RollbackNode rn_old(&m_env->getMap(), p, this);
+
                scriptapi_node_on_receive_fields(m_lua, p, formname, fields,
                                playersao);
+
+               // Report rollback data
+               RollbackNode rn_new(&m_env->getMap(), p, this);
+               if(rollback() && rn_new != rn_old){
+                       RollbackAction action;
+                       action.setSetNode(p, rn_old, rn_new);
+                       rollback()->reportAction(action);
+               }
        }
        else if(command == TOSERVER_INVENTORY_FIELDS)
        {
@@ -3318,6 +3354,13 @@ Inventory* Server::getInventory(const InventoryLocation &loc)
                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);
        }
@@ -3352,6 +3395,11 @@ void Server::setInventoryModified(const InventoryLocation &loc)
                setBlockNotSent(blockpos);
        }
        break;
+       case InventoryLocation::DETACHED:
+       {
+               sendDetachedInventoryToAll(loc.name);
+       }
+       break;
        default:
                assert(0);
        }
@@ -3505,7 +3553,7 @@ void Server::SendItemDef(con::Connection &con, u16 peer_id,
 }
 
 void Server::SendNodeDef(con::Connection &con, u16 peer_id,
-               INodeDefManager *nodedef)
+               INodeDefManager *nodedef, u16 protocol_version)
 {
        DSTACK(__FUNCTION_NAME);
        std::ostringstream os(std::ios_base::binary);
@@ -3517,7 +3565,7 @@ void Server::SendNodeDef(con::Connection &con, u16 peer_id,
        */
        writeU16(os, TOCLIENT_NODEDEF);
        std::ostringstream tmp_os(std::ios::binary);
-       nodedef->serialize(tmp_os);
+       nodedef->serialize(tmp_os, protocol_version);
        std::ostringstream tmp_os2(std::ios::binary);
        compressZlib(tmp_os.str(), tmp_os2);
        os<<serializeLongString(tmp_os2.str());
@@ -3590,6 +3638,25 @@ void Server::SendChatMessage(u16 peer_id, const std::wstring &message)
        // Send as reliable
        m_con.Send(peer_id, 0, data, true);
 }
+void Server::SendShowFormspecMessage(u16 peer_id, const std::string formspec, const std::string formname)
+{
+       DSTACK(__FUNCTION_NAME);
+
+       std::ostringstream os(std::ios_base::binary);
+       u8 buf[12];
+
+       // Write command
+       writeU16(buf, TOCLIENT_SHOW_FORMSPEC);
+       os.write((char*)buf, 2);
+       os<<serializeLongString(formspec);
+       os<<serializeString(formname);
+
+       // 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::BroadcastChatMessage(const std::wstring &message)
 {
@@ -4038,6 +4105,7 @@ void Server::fillMediaCache()
                paths.push_back(mod.path + DIR_DELIM + "textures");
                paths.push_back(mod.path + DIR_DELIM + "sounds");
                paths.push_back(mod.path + DIR_DELIM + "media");
+               paths.push_back(mod.path + DIR_DELIM + "models");
        }
        std::string path_all = "textures";
        paths.push_back(path_all + DIR_DELIM + "all");
@@ -4063,6 +4131,7 @@ void Server::fillMediaCache()
                                ".png", ".jpg", ".bmp", ".tga",
                                ".pcx", ".ppm", ".psd", ".wal", ".rgb",
                                ".ogg",
+                               ".x", ".b3d", ".md2", ".obj",
                                NULL
                        };
                        if(removeStringEnd(filename, supported_ext) == ""){
@@ -4170,6 +4239,7 @@ void Server::sendMediaAnnouncement(u16 peer_id)
                os<<serializeString(j->name);
                os<<serializeString(j->sha1_digest);
        }
+       os<<serializeString(g_settings->get("remote_media"));
 
        // Make data buffer
        std::string s = os.str();
@@ -4177,7 +4247,6 @@ void Server::sendMediaAnnouncement(u16 peer_id)
 
        // Send as reliable
        m_con.Send(peer_id, 0, data, true);
-
 }
 
 struct SendableMedia
@@ -4309,6 +4378,51 @@ void Server::sendRequestedMedia(u16 peer_id,
        }
 }
 
+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
 */
@@ -4362,9 +4476,7 @@ void Server::UpdateCrafting(u16 peer_id)
 
        // Get a preview for crafting
        ItemStack preview;
-       // No crafting in creative mode
-       if(g_settings->getBool("creative_mode") == false)
-               getCraftingResult(&player->inventory, preview, false, this);
+       getCraftingResult(&player->inventory, preview, false, this);
 
        // Put the new preview in
        InventoryList *plist = player->inventory.getList("craftpreview");
@@ -4393,9 +4505,10 @@ std::wstring Server::getStatusString()
        // Uptime
        os<<L", uptime="<<m_uptime.get();
        // Information about clients
+       core::map<u16, RemoteClient*>::Iterator i;
+       bool first;
        os<<L", clients={";
-       for(core::map<u16, RemoteClient*>::Iterator
-               i = m_clients.getIterator();
+       for(i = m_clients.getIterator(), first = true;
                i.atEnd() == false; i++)
        {
                // Get client and check that it is valid
@@ -4410,7 +4523,11 @@ std::wstring Server::getStatusString()
                if(player != NULL)
                        name = narrow_to_wide(player->getName());
                // Add name to information string
-               os<<name<<L",";
+               if(!first)
+                       os<<L",";
+               else
+                       first = false;
+               os<<name;
        }
        os<<L"}";
        if(((ServerMap*)(&m_env->getMap()))->isSavingEnabled() == false)
@@ -4480,6 +4597,20 @@ void Server::notifyPlayer(const char *name, const std::wstring msg)
        SendChatMessage(player->peer_id, std::wstring(L"Server: -!- ")+msg);
 }
 
+bool Server::showFormspec(const char *playername, const std::string &formspec, const std::string &formname)
+{
+       Player *player = m_env->getPlayer(playername);
+
+       if(!player)
+       {
+               infostream<<"showFormspec: couldn't find player:"<<playername<<std::endl;
+               return false;
+       }
+
+       SendShowFormspecMessage(player->peer_id, formspec, formname);
+       return true;
+}
+
 void Server::notifyPlayers(const std::wstring msg)
 {
        BroadcastChatMessage(msg);
@@ -4493,6 +4624,88 @@ void Server::queueBlockEmerge(v3s16 blockpos, bool allow_generate)
        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;
+}
+
+class BoolScopeSet
+{
+public:
+       BoolScopeSet(bool *dst, bool val):
+               m_dst(dst)
+       {
+               m_orig_state = *m_dst;
+               *m_dst = val;
+       }
+       ~BoolScopeSet()
+       {
+               *m_dst = m_orig_state;
+       }
+private:
+       bool *m_dst;
+       bool m_orig_state;
+};
+
+// actions: time-reversed list
+// Return value: success/failure
+bool Server::rollbackRevertActions(const std::list<RollbackAction> &actions,
+               std::list<std::string> *log)
+{
+       infostream<<"Server::rollbackRevertActions(len="<<actions.size()<<")"<<std::endl;
+       ServerMap *map = (ServerMap*)(&m_env->getMap());
+       // Disable rollback report sink while reverting
+       BoolScopeSet rollback_scope_disable(&m_rollback_sink_enabled, false);
+       
+       // Fail if no actions to handle
+       if(actions.empty()){
+               log->push_back("Nothing to do.");
+               return false;
+       }
+
+       int num_tried = 0;
+       int num_failed = 0;
+       
+       for(std::list<RollbackAction>::const_iterator
+                       i = actions.begin();
+                       i != actions.end(); i++)
+       {
+               const RollbackAction &action = *i;
+               num_tried++;
+               bool success = action.applyRevert(map, this, this);
+               if(!success){
+                       num_failed++;
+                       std::ostringstream os;
+                       os<<"Revert of step ("<<num_tried<<") "<<action.toString()<<" failed";
+                       infostream<<"Map::rollbackRevertActions(): "<<os.str()<<std::endl;
+                       if(log)
+                               log->push_back(os.str());
+               }else{
+                       std::ostringstream os;
+                       os<<"Successfully reverted step ("<<num_tried<<") "<<action.toString();
+                       infostream<<"Map::rollbackRevertActions(): "<<os.str()<<std::endl;
+                       if(log)
+                               log->push_back(os.str());
+               }
+       }
+       
+       infostream<<"Map::rollbackRevertActions(): "<<num_failed<<"/"<<num_tried
+                       <<" failed"<<std::endl;
+
+       // Call it done if less than half failed
+       return num_failed <= num_tried/2;
+}
+
 // IGameDef interface
 // Under envlock
 IItemDefManager* Server::getItemDefManager()
@@ -4511,6 +4724,10 @@ ITextureSource* Server::getTextureSource()
 {
        return NULL;
 }
+IShaderSource* Server::getShaderSource()
+{
+       return NULL;
+}
 u16 Server::allocateUnknownNodeId(const std::string &name)
 {
        return m_nodedef->allocateDummy(name);
@@ -4523,6 +4740,14 @@ MtEventManager* Server::getEventManager()
 {
        return m_event;
 }
+IRollbackReportSink* Server::getRollbackReportSink()
+{
+       if(!m_enable_rollback_recording)
+               return NULL;
+       if(!m_rollback_sink_enabled)
+               return NULL;
+       return m_rollback;
+}
 
 IWritableItemDefManager* Server::getWritableItemDefManager()
 {
@@ -4684,10 +4909,6 @@ PlayerSAO* Server::emergePlayer(const char *name, u16 peer_id)
 
        scriptapi_on_joinplayer(m_lua, playersao);
 
-       /* Creative mode */
-       if(g_settings->getBool("creative_mode"))
-               playersao->createCreativeInventory();
-
        return playersao;
 }