summaryrefslogblamecommitdiffstats
path: root/source/Plugin_NewLua.cpp
blob: 51d16c839993b269fc899ea5f85b9a4e17c316c2 (plain) (tree)
1
2
3
4
5
6
7
8
9



                                                                                              
                          
                          


          
                           


                    
 



 

                                                                                                                       
 


                                                                   
 







                                 
                                        
                           





 
                                     
 
                                        
                                  
                
                                    





                                                                             

         
                                                                            









                                                                                        
                                               
                 
                                           




                                     
                                                   
         
                                   




                                                               
                                                         
         
                                   


                             
                                           
         

                                                                                                               


                             
                                                                 








                                
                                        
                                                                                                 
         
                       
         
 
                                                   







                                     

                                                                       
                                             
         
                       
         


                                             
                                              





 




                                                                                                                                                                                        
                                             












                                                             
                                                   












                                                                
                                                                    



                                                                       
                                             






                                                            
                                                   



                             





                                                                






                       
                                                                                   

                                        
                                                                                  
                               
                                             







                                                          
                                                   












                                                                




                                                                                                             
                                             








                                                                  
                                                   












                                                                
                                                                                                              



                                                                                   
                                             






                                                              
                                                                  
 
                                                   






                                                                





 




                                                                                  
                                             







                                                          
                                                   

















                                                                                   
                                             







                                                          
                                                   












                                                                
                                                                               
 
                                        
                                                                                    
                               
                                             
         
                             
         
 
                                                            
                                                            
 
                                                   

















                                                                                                                           
                                             







                                                                            
                                                   
         
                             
         
 
                                                                

                               





 
                                                                               
 
                                        

                                                                             
                                             
         
                             
         
 
                                                            
                                                         
 
                                                   
         
                             
         
 
                                                                

                               





 




                                                                                        
                                             


















                                                                                             
                                                   












                                                                
                                                                                      
 
                                        

                                                                            
                                             
         
                             
         
 


                                                                  
                                                   












                                                                
                                                                      

                                        
                                                                          
                               
                                             



                             
                                                             

                                                             
                                                   

















                                                                                                         
                                             







                                                                   
                                                   

















                                                                                                                                                                         
                                             




                                                             



                                                    

                                                    
 
                                                   
         
                             
         
 
                                                                

                               





 
                                                                                                                                                                       
 
                                        

                                                                                      
                                             
         
                             
         
 
                                                             



                                                    

                                                    
 
                                                   
         
                             
         
 
                                                                

                               





 
                                                       
 
                                        

                                                                                
                                             
         
                             
         
 
                                                             
 
                                                   
         
                             
         
 
                                                                

                               





 
                                                       
 


                                                                                
                                             
         
                             
         
 
                                                             
 
                                                   
         
                             
         
 
                                                                
                               






                       
                                                                                                                                     
 


                                                                                    
                                             


                             
 






                                                             
                                                   


                             
 


                                                                





 
                                                      
 
                                        
                                                                                
                               
                                             
         
                             
         
 
                                                             
 
                                                   
         
                             


                                                                
                               






                       
                                                                                                                                                                                                                    
 


                                                                                      
                                             


                             
 









                                                             
 
                                                    






                                                                





 
                                                                                                                                                                                                                     
 
                                        

                                                                                       
                                             


                             
 









                                                             
 
                                                    



                             
                                                                

                               





 
                                                                                                                                                                    
 
                                        
                                                                                     
                               
                                             
         
                             
         
 







                                                             
 
                                                   
         
                             
         
 
                                                                
                               






                       




                                                                                               
                                             






                                                             
                                                   












                                                                
                                                         

                                        

                                                                                  
                                             
         




                                                             
                                                   

                             

         













                                                                                 
                                             





                                                             
                                                   


                             
        


                                                                





 
                                                            

                                        

                                                                                      
                                             
         
                             
         
 
                                                             
 
                                                   
         
                             
         
 
                                                                
                               






                       
                                                                                                                                                                                                                  

                                        

                                                                                    
                                             
         
                             
         
 









                                                             
 
                                                    



                             
                                                                
                               






                       




                                                                                                                                                                   
                                             












                                                             
                                                   

















                                                                                                                                                                                                                   
                                             














                                                             
                                                    

















                                                                                                                                                                   
                                             












                                                             
                                                   













                                                                                                                       

                                        

                                                                                
                                             
         
                             
         




                                                                            
                                                   



                             
                                                                
                               






                       
                                                                                                                      

                                        

                                                                               
                                             
         
                             
         




                                                                            
                                                   



                             
                                                                
                               






                       
                                                                               
 
                                        

                                                                              
                                             
         
                             
         
 
                                                               
                                                                      
 
                                                   



                             
                                                                 
                               






                       





                                                                                                           

                                        

                                                                               
                                             




                                                                  







                                                                    
 
                                                   



                             
                                                                
                               






                       


                                                  

                                                                                   


                                        

                                                                                
                                             











                                                                  
                                                                    
 
                                                             



                             
        
                                                                






















                                                             
                                                       

                                        

                                                                                  
                                             



                             
                                                           
 
                                                   



                             
                                                                
                               






                       




                                                                                  
                                             






                                                           
                                                   
















                                                                       





                                                                                     
                                                                                                                                           





                                          
                                                              
         

                                                                                                 
         
        


                                             



                                                            
                                                         
         
                                                                                                    





                                                               







                      
                                                                                                           




                                                                      


                                                                                                                                                     





                                          


                                                                                         










                                                                                             

                         
                                                         
         
                                                                                                    


                             







                                                                    







                      


















                                                                                                              


















                                                                                                                            
                                                                  
 

                                                    
         

                                                                                                                                            

                             






                                                  
         
                            
         












                                                                                                                                                         
 















                                                                                                              






































                                                                                                             

                                     





 
                                                                        
 
                                        







                                                                                        
                                                                                      
         
                                                                                         







                                   
                                                                         

                                                                                                   
                                                                         
                                   
                                                                                        
                                                         



















                                                                                                                                
                                                                          








                      
                                                                                                        
 

                                        
         

                                                                                         
         
                                                  
                             
                                             
 
                                            
 
                                 
                    


 


 









                                                                        









                                                                               









                                                            
                                                                                                                 
 

                                      



                                                                                               
                                                            

                         
                                                         
         
                                                                                                    











                                                                    









                                                                                               
                                                         
         
                                                                                                    





         

#include "Globals.h"  // NOTE: MSVC stupidness requires this to be the same across all modules

#define LUA_USE_POSIX
#include "Plugin_NewLua.h"
#include "CommandOutput.h"

extern "C"
{
	#include "lualib.h"
}

#include "tolua++.h"





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cPlugin_NewLua:

cPlugin_NewLua::cPlugin_NewLua(const AString & a_PluginDirectory) :
	cPlugin(a_PluginDirectory),
	m_LuaState(Printf("plugin %s", GetName().c_str()))
{
}





cPlugin_NewLua::~cPlugin_NewLua()
{
	cCSLock Lock(m_CriticalSection);
	m_LuaState.Close();
}





bool cPlugin_NewLua::Initialize(void)
{
	cCSLock Lock(m_CriticalSection);
	if (!m_LuaState.IsValid())
	{	
		m_LuaState.Create();
		
		// Inject the identification global variables into the state:
		lua_pushlightuserdata(m_LuaState, this);
		lua_setglobal(m_LuaState, LUA_PLUGIN_INSTANCE_VAR_NAME);
		lua_pushstring(m_LuaState, GetName().c_str());
		lua_setglobal(m_LuaState, LUA_PLUGIN_NAME_VAR_NAME);
	}

	std::string PluginPath = FILE_IO_PREFIX + GetLocalDirectory() + "/";

	// Load all files for this plugin, and execute them
	AStringList Files = GetDirectoryContents(PluginPath.c_str());
	for (AStringList::const_iterator itr = Files.begin(); itr != Files.end(); ++itr)
	{
		if (itr->rfind(".lua") == AString::npos)
		{
			continue;
		}
		AString Path = PluginPath + *itr;
		if (!m_LuaState.LoadFile(Path))
		{
			m_LuaState.Close();
			return false;
		}
	}  // for itr - Files[]

	// Call intialize function
	if (!m_LuaState.PushFunction("Initialize"))
	{
		m_LuaState.Close();
		return false;
	}

	tolua_pushusertype(m_LuaState, this, "cPlugin_NewLua");
	
	if (!m_LuaState.CallFunction(1, 1, "Initialize"))
	{
		m_LuaState.Close();
		return false;
	}

	if (!lua_isboolean(m_LuaState, -1))
	{
		LOGWARNING("Error in plugin %s: Initialize() must return a boolean value!", GetName().c_str());
		m_LuaState.Close();
		return false;
	}

	bool bSuccess = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	return bSuccess;
}





void cPlugin_NewLua::OnDisable()
{
	cCSLock Lock(m_CriticalSection);
	if (!m_LuaState.PushFunction("OnDisable", false)) // false = don't log error if not found
	{
		return;
	}

	m_LuaState.CallFunction(0, 0, "OnDisable");
}





void cPlugin_NewLua::Tick(float a_Dt)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_TICK);
	if (!m_LuaState.PushFunction(FnName))
	{
		return;
	}

	tolua_pushnumber( m_LuaState, a_Dt );

	m_LuaState.CallFunction(1, 0, FnName);
}





bool cPlugin_NewLua::OnBlockToPickups(cWorld * a_World, cEntity * a_Digger, int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, cItems & a_Pickups)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_BLOCK_TO_PICKUPS);
	ASSERT(FnName != NULL);
	if (!m_LuaState.PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, a_World, "cWorld");
	tolua_pushusertype(m_LuaState, a_Digger, "cEntity");
	tolua_pushnumber  (m_LuaState, a_BlockX);
	tolua_pushnumber  (m_LuaState, a_BlockY);
	tolua_pushnumber  (m_LuaState, a_BlockZ);
	tolua_pushnumber  (m_LuaState, a_BlockType);
	tolua_pushnumber  (m_LuaState, a_BlockMeta);
	tolua_pushusertype(m_LuaState, &a_Pickups, "cItems");

	if (!m_LuaState.CallFunction(8, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}





bool cPlugin_NewLua::OnChat(cPlayer * a_Player, AString & a_Message)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_CHAT);
	ASSERT(FnName != NULL);
	if (!m_LuaState.PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, a_Player, "cPlayer");
	tolua_pushstring  (m_LuaState, a_Message.c_str());

	if (!m_LuaState.CallFunction(2, 2, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -2, 0) > 0);
	if (lua_isstring(m_LuaState, -1))
	{
		a_Message = tolua_tostring(m_LuaState, -1, "");
	}
	lua_pop(m_LuaState, 2);
	return bRetVal;
}





bool cPlugin_NewLua::OnChunkAvailable(cWorld * a_World, int a_ChunkX, int a_ChunkZ)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_CHUNK_AVAILABLE);
	ASSERT(FnName != NULL);
	if (!m_LuaState.PushFunction(FnName))
	{
		return false;
	}
	
	tolua_pushusertype(m_LuaState, a_World, "cWorld");
	tolua_pushnumber  (m_LuaState, a_ChunkX);
	tolua_pushnumber  (m_LuaState, a_ChunkZ);
	
	if (!m_LuaState.CallFunction(3, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}





bool cPlugin_NewLua::OnChunkGenerated(cWorld * a_World, int a_ChunkX, int a_ChunkZ, cChunkDesc * a_ChunkDesc)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_CHUNK_GENERATED);
	ASSERT(FnName != NULL);
	if (!m_LuaState.PushFunction(FnName))
	{
		return false;
	}
	
	tolua_pushusertype(m_LuaState, a_World,     "cWorld");
	tolua_pushnumber  (m_LuaState, a_ChunkX);
	tolua_pushnumber  (m_LuaState, a_ChunkZ);
	tolua_pushusertype(m_LuaState, a_ChunkDesc, "cChunkDesc");
	
	if (!m_LuaState.CallFunction(4, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}





bool cPlugin_NewLua::OnChunkGenerating(cWorld * a_World, int a_ChunkX, int a_ChunkZ, cChunkDesc * a_pLuaChunk)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_CHUNK_GENERATING);
	ASSERT(FnName != NULL);
	if (!m_LuaState.PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, a_World,     "cWorld");
	tolua_pushnumber  (m_LuaState, a_ChunkX);
	tolua_pushnumber  (m_LuaState, a_ChunkZ);
	tolua_pushusertype(m_LuaState, a_pLuaChunk, "cChunkDesc");

	if (!m_LuaState.CallFunction(4, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}





bool cPlugin_NewLua::OnChunkUnloaded(cWorld * a_World, int a_ChunkX, int a_ChunkZ)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_CHUNK_UNLOADED);
	ASSERT(FnName != NULL);
	if (!m_LuaState.PushFunction(FnName))
	{
		return false;
	}
	
	tolua_pushusertype(m_LuaState, a_World, "cWorld");
	tolua_pushnumber  (m_LuaState, a_ChunkX);
	tolua_pushnumber  (m_LuaState, a_ChunkZ);
	
	if (!m_LuaState.CallFunction(3, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}





bool cPlugin_NewLua::OnChunkUnloading(cWorld * a_World, int a_ChunkX, int a_ChunkZ)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_CHUNK_UNLOADING);
	ASSERT(FnName != NULL);
	if (!m_LuaState.PushFunction(FnName))
	{
		return false;
	}
	
	tolua_pushusertype(m_LuaState, a_World, "cWorld");
	tolua_pushnumber  (m_LuaState, a_ChunkX);
	tolua_pushnumber  (m_LuaState, a_ChunkZ);
	
	if (!m_LuaState.CallFunction(3, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}





bool cPlugin_NewLua::OnCollectingPickup(cPlayer * a_Player, cPickup * a_Pickup)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_COLLECTING_PICKUP);
	ASSERT(FnName != NULL);
	if (!m_LuaState.PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, a_Player, "cPlayer");
	tolua_pushusertype(m_LuaState, a_Pickup, "cPickup");

	if (!m_LuaState.CallFunction(2, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}





bool cPlugin_NewLua::OnCraftingNoRecipe(const cPlayer * a_Player, const cCraftingGrid * a_Grid, cCraftingRecipe * a_Recipe)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_CRAFTING_NO_RECIPE);
	ASSERT(FnName != NULL);
	if (!m_LuaState.PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, (void *)a_Player, "cPlayer");
	tolua_pushusertype(m_LuaState, (void *)a_Grid,   "cCraftingGrid");
	tolua_pushusertype(m_LuaState, (void *)a_Recipe, "cCraftingRecipe");

	if (!m_LuaState.CallFunction(3, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}





bool cPlugin_NewLua::OnDisconnect(cPlayer * a_Player, const AString & a_Reason)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_DISCONNECT);
	ASSERT(FnName != NULL);
	if (!m_LuaState.PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, a_Player, "cPlayer");
	tolua_pushstring  (m_LuaState, a_Reason.c_str());

	if (!m_LuaState.CallFunction(2, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}





bool cPlugin_NewLua::OnExecuteCommand(cPlayer * a_Player, const AStringVector & a_Split)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_EXECUTE_COMMAND);
	ASSERT(FnName != NULL);
	if (!m_LuaState.PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, a_Player, "cPlayer");

	// Push the split:
	lua_createtable(m_LuaState, a_Split.size(), 0);
	int newTable = lua_gettop(m_LuaState);
	int index = 1;
	std::vector<std::string>::const_iterator iter = a_Split.begin(), end = a_Split.end();
	while(iter != end)
	{
		tolua_pushstring(m_LuaState, (*iter).c_str());
		lua_rawseti(m_LuaState, newTable, index);
		++iter;
		++index;
	}

	if (!m_LuaState.CallFunction(2, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}





bool cPlugin_NewLua::OnHandshake(cClientHandle * a_Client, const AString & a_Username)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_HANDSHAKE);
	ASSERT(FnName != NULL);
	if (!m_LuaState.PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, a_Client, "cClientHandle");
	tolua_pushstring  (m_LuaState, a_Username.c_str());

	if (!m_LuaState.CallFunction(2, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}





bool cPlugin_NewLua::OnKilling(cEntity & a_Victim, cEntity * a_Killer)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_KILLING);
	ASSERT(FnName != NULL);
	if (!m_LuaState.PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, &a_Victim, "cEntity");
	tolua_pushusertype(m_LuaState, a_Killer,  "cEntity");

	if (!m_LuaState.CallFunction(2, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}





bool cPlugin_NewLua::OnLogin(cClientHandle * a_Client, int a_ProtocolVersion, const AString & a_Username)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_LOGIN);
	ASSERT(FnName != NULL);
	if (!m_LuaState.PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype (m_LuaState, a_Client, "cClientHandle");
	tolua_pushnumber   (m_LuaState, a_ProtocolVersion);
	tolua_pushcppstring(m_LuaState, a_Username);

	if (!m_LuaState.CallFunction(3, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}





bool cPlugin_NewLua::OnPlayerBreakingBlock(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_PLAYER_BREAKING_BLOCK);
	ASSERT(FnName != NULL);
	if (!m_LuaState.PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, &a_Player, "cPlayer");
	tolua_pushnumber  (m_LuaState, a_BlockX);
	tolua_pushnumber  (m_LuaState, a_BlockY);
	tolua_pushnumber  (m_LuaState, a_BlockZ);
	tolua_pushnumber  (m_LuaState, a_BlockFace);
	tolua_pushnumber  (m_LuaState, a_BlockType);
	tolua_pushnumber  (m_LuaState, a_BlockMeta);

	if (!m_LuaState.CallFunction(7, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}





bool cPlugin_NewLua::OnPlayerBrokenBlock(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_PLAYER_BROKEN_BLOCK);
	ASSERT(FnName != NULL);
	if (!m_LuaState.PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, &a_Player, "cPlayer");
	tolua_pushnumber  (m_LuaState, a_BlockX);
	tolua_pushnumber  (m_LuaState, a_BlockY);
	tolua_pushnumber  (m_LuaState, a_BlockZ);
	tolua_pushnumber  (m_LuaState, a_BlockFace);
	tolua_pushnumber  (m_LuaState, a_BlockType);
	tolua_pushnumber  (m_LuaState, a_BlockMeta);

	if (!m_LuaState.CallFunction(7, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}





bool cPlugin_NewLua::OnPlayerEating(cPlayer & a_Player)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_PLAYER_EATING);
	ASSERT(FnName != NULL);
	if (!m_LuaState.PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, &a_Player, "cPlayer");

	if (!m_LuaState.CallFunction(1, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}





bool cPlugin_NewLua::OnPlayerJoined(cPlayer & a_Player)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_PLAYER_JOINED);
	ASSERT(FnName != NULL);
	if (!m_LuaState.PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, &a_Player, "cPlayer");

	if (!m_LuaState.CallFunction(1, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}





bool cPlugin_NewLua::OnPlayerLeftClick(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, char a_Status)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_PLAYER_LEFT_CLICK);
	ASSERT(FnName != NULL);
	if (!m_LuaState.PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, &a_Player, "cPlayer");
	tolua_pushnumber  (m_LuaState, a_BlockX);
	tolua_pushnumber  (m_LuaState, a_BlockY);
	tolua_pushnumber  (m_LuaState, a_BlockZ);
	tolua_pushnumber  (m_LuaState, a_BlockFace);
	tolua_pushnumber  (m_LuaState, a_Status);

	if (!m_LuaState.CallFunction(6, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}





bool cPlugin_NewLua::OnPlayerMoved(cPlayer & a_Player)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_PLAYER_MOVING);
	ASSERT(FnName != NULL);
	if (!m_LuaState.PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, &a_Player, "cPlayer");

	if (!m_LuaState.CallFunction(1, 1, FnName))
	{
		return false;
	}
	
	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}





bool cPlugin_NewLua::OnPlayerPlacedBlock(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_PLAYER_PLACED_BLOCK);
	ASSERT(FnName != NULL);
	if (!m_LuaState.PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, &a_Player, "cPlayer");
	tolua_pushnumber  (m_LuaState, a_BlockX);
	tolua_pushnumber  (m_LuaState, a_BlockY);
	tolua_pushnumber  (m_LuaState, a_BlockZ);
	tolua_pushnumber  (m_LuaState, a_BlockFace);
	tolua_pushnumber  (m_LuaState, a_CursorX);
	tolua_pushnumber  (m_LuaState, a_CursorY);
	tolua_pushnumber  (m_LuaState, a_CursorZ);
	tolua_pushnumber  (m_LuaState, a_BlockType);
	tolua_pushnumber  (m_LuaState, a_BlockMeta);

	if (!m_LuaState.CallFunction(10, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}





bool cPlugin_NewLua::OnPlayerPlacingBlock(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_PLAYER_PLACING_BLOCK);
	ASSERT(FnName != NULL);
	if (!m_LuaState.PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, &a_Player, "cPlayer");
	tolua_pushnumber  (m_LuaState, a_BlockX);
	tolua_pushnumber  (m_LuaState, a_BlockY);
	tolua_pushnumber  (m_LuaState, a_BlockZ);
	tolua_pushnumber  (m_LuaState, a_BlockFace);
	tolua_pushnumber  (m_LuaState, a_CursorX);
	tolua_pushnumber  (m_LuaState, a_CursorY);
	tolua_pushnumber  (m_LuaState, a_CursorZ);
	tolua_pushnumber  (m_LuaState, a_BlockType);
	tolua_pushnumber  (m_LuaState, a_BlockMeta);

	if (!m_LuaState.CallFunction(10, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}





bool cPlugin_NewLua::OnPlayerRightClick(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_PLAYER_RIGHT_CLICK);
	ASSERT(FnName != NULL);
	if (!m_LuaState.PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, &a_Player, "cPlayer");
	tolua_pushnumber  (m_LuaState, a_BlockX);
	tolua_pushnumber  (m_LuaState, a_BlockY);
	tolua_pushnumber  (m_LuaState, a_BlockZ);
	tolua_pushnumber  (m_LuaState, a_BlockFace);
	tolua_pushnumber  (m_LuaState, a_CursorX);
	tolua_pushnumber  (m_LuaState, a_CursorY);
	tolua_pushnumber  (m_LuaState, a_CursorZ);

	if (!m_LuaState.CallFunction(8, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}





bool cPlugin_NewLua::OnPlayerRightClickingEntity(cPlayer & a_Player, cEntity & a_Entity)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_PLAYER_RIGHT_CLICKING_ENTITY);
	ASSERT(FnName != NULL);
	if (!m_LuaState.PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, &a_Player, "cPlayer");
	tolua_pushusertype(m_LuaState, &a_Entity, "cEntity");

	if (!m_LuaState.CallFunction(2, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}





bool cPlugin_NewLua::OnPlayerShooting(cPlayer & a_Player)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_PLAYER_SHOOTING);
	ASSERT(FnName != NULL);
	if (!m_LuaState.PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, &a_Player, "cPlayer");

	if (!m_LuaState.CallFunction(1, 1, FnName))
	{
		return false;
	}
	
	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}





bool cPlugin_NewLua::OnPlayerSpawned(cPlayer & a_Player)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_PLAYER_SPAWNED);
	ASSERT(FnName != NULL);
	if (!m_LuaState.PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, &a_Player, "cPlayer");

	if (!m_LuaState.CallFunction(1, 1, FnName))
	{
		return false;
	}
	
	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}





bool cPlugin_NewLua::OnPlayerTossingItem(cPlayer & a_Player)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_PLAYER_TOSSING_ITEM);
	ASSERT(FnName != NULL);
	if (!m_LuaState.PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, &a_Player, "cPlayer");

	if (!m_LuaState.CallFunction(1, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}





bool cPlugin_NewLua::OnPlayerUsedBlock(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_PLAYER_USED_BLOCK);
	ASSERT(FnName != NULL);
	if (!m_LuaState.PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, &a_Player, "cPlayer");
	tolua_pushnumber  (m_LuaState, a_BlockX);
	tolua_pushnumber  (m_LuaState, a_BlockY);
	tolua_pushnumber  (m_LuaState, a_BlockZ);
	tolua_pushnumber  (m_LuaState, a_BlockFace);
	tolua_pushnumber  (m_LuaState, a_CursorX);
	tolua_pushnumber  (m_LuaState, a_CursorY);
	tolua_pushnumber  (m_LuaState, a_CursorZ);
	tolua_pushnumber  (m_LuaState, a_BlockType);
	tolua_pushnumber  (m_LuaState, a_BlockMeta);

	if (!m_LuaState.CallFunction(10, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}





bool cPlugin_NewLua::OnPlayerUsedItem(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ) 
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_PLAYER_USED_ITEM);
	ASSERT(FnName != NULL);
	if (!m_LuaState.PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, &a_Player, "cPlayer");
	tolua_pushnumber  (m_LuaState, a_BlockX);
	tolua_pushnumber  (m_LuaState, a_BlockY);
	tolua_pushnumber  (m_LuaState, a_BlockZ);
	tolua_pushnumber  (m_LuaState, a_BlockFace);
	tolua_pushnumber  (m_LuaState, a_CursorX);
	tolua_pushnumber  (m_LuaState, a_CursorY);
	tolua_pushnumber  (m_LuaState, a_CursorZ);

	if (!m_LuaState.CallFunction(8, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}





bool cPlugin_NewLua::OnPlayerUsingBlock(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_PLAYER_USING_BLOCK);
	ASSERT(FnName != NULL);
	if (!m_LuaState.PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, &a_Player, "cPlayer");
	tolua_pushnumber  (m_LuaState, a_BlockX);
	tolua_pushnumber  (m_LuaState, a_BlockY);
	tolua_pushnumber  (m_LuaState, a_BlockZ);
	tolua_pushnumber  (m_LuaState, a_BlockFace);
	tolua_pushnumber  (m_LuaState, a_CursorX);
	tolua_pushnumber  (m_LuaState, a_CursorY);
	tolua_pushnumber  (m_LuaState, a_CursorZ);
	tolua_pushnumber  (m_LuaState, a_BlockType);
	tolua_pushnumber  (m_LuaState, a_BlockMeta);

	if (!m_LuaState.CallFunction(10, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}





bool cPlugin_NewLua::OnPlayerUsingItem(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_PLAYER_USING_ITEM);
	ASSERT(FnName != NULL);
	if (!m_LuaState.PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, &a_Player, "cPlayer");
	tolua_pushnumber  (m_LuaState, a_BlockX);
	tolua_pushnumber  (m_LuaState, a_BlockY);
	tolua_pushnumber  (m_LuaState, a_BlockZ);
	tolua_pushnumber  (m_LuaState, a_BlockFace);
	tolua_pushnumber  (m_LuaState, a_CursorX);
	tolua_pushnumber  (m_LuaState, a_CursorY);
	tolua_pushnumber  (m_LuaState, a_CursorZ);

	if (!m_LuaState.CallFunction(8, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}





bool cPlugin_NewLua::OnPostCrafting(const cPlayer * a_Player, const cCraftingGrid * a_Grid, cCraftingRecipe * a_Recipe)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_POST_CRAFTING);
	ASSERT(FnName != NULL);
	if (!m_LuaState.PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, (void *)a_Player, "cPlayer");
	tolua_pushusertype(m_LuaState, (void *)a_Grid,   "cCraftingGrid");
	tolua_pushusertype(m_LuaState, (void *)a_Recipe, "cCraftingRecipe");

	if (!m_LuaState.CallFunction(3, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}





bool cPlugin_NewLua::OnPreCrafting(const cPlayer * a_Player, const cCraftingGrid * a_Grid, cCraftingRecipe * a_Recipe)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_PRE_CRAFTING);
	ASSERT(FnName != NULL);
	if (!m_LuaState.PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, (void *)a_Player, "cPlayer");
	tolua_pushusertype(m_LuaState, (void *)a_Grid,   "cCraftingGrid");
	tolua_pushusertype(m_LuaState, (void *)a_Recipe, "cCraftingRecipe");

	if (!m_LuaState.CallFunction(3, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}





bool cPlugin_NewLua::OnTakeDamage(cEntity & a_Receiver, TakeDamageInfo & a_TDI)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_TAKE_DAMAGE);
	ASSERT(FnName != NULL);
	if (!m_LuaState.PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, &a_Receiver, "cEntity");
	tolua_pushusertype(m_LuaState, &a_TDI,      "TakeDamageInfo");

	if (!m_LuaState.CallFunction(2, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) != 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}





bool cPlugin_NewLua::OnUpdatedSign(
	cWorld * a_World, 
	int a_BlockX, int a_BlockY, int a_BlockZ, 
	const AString & a_Line1, const AString & a_Line2, const AString & a_Line3, const AString & a_Line4,
	cPlayer * a_Player
)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_UPDATED_SIGN);
	ASSERT(FnName != NULL);
	if (!m_LuaState.PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, (void *)a_World, "cWorld");
	tolua_pushnumber  (m_LuaState, a_BlockX);
	tolua_pushnumber  (m_LuaState, a_BlockY);
	tolua_pushnumber  (m_LuaState, a_BlockZ);
	tolua_pushstring  (m_LuaState, a_Line1.c_str());
	tolua_pushstring  (m_LuaState, a_Line2.c_str());
	tolua_pushstring  (m_LuaState, a_Line3.c_str());
	tolua_pushstring  (m_LuaState, a_Line4.c_str());
	tolua_pushusertype(m_LuaState, (void *)a_Player, "cPlayer");

	if (!m_LuaState.CallFunction(9, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}





bool cPlugin_NewLua::OnUpdatingSign(
	cWorld * a_World, 
	int a_BlockX, int a_BlockY, int a_BlockZ, 
	AString & a_Line1, AString & a_Line2, AString & a_Line3, AString & a_Line4,
	cPlayer * a_Player
)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_UPDATING_SIGN);
	ASSERT(FnName != NULL);
	if (!m_LuaState.PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, (void *)a_World, "cWorld");
	tolua_pushnumber  (m_LuaState, a_BlockX);
	tolua_pushnumber  (m_LuaState, a_BlockY);
	tolua_pushnumber  (m_LuaState, a_BlockZ);
	tolua_pushstring  (m_LuaState, a_Line1.c_str());
	tolua_pushstring  (m_LuaState, a_Line2.c_str());
	tolua_pushstring  (m_LuaState, a_Line3.c_str());
	tolua_pushstring  (m_LuaState, a_Line4.c_str());
	tolua_pushusertype(m_LuaState, (void *)a_Player, "cPlayer");

	if (!m_LuaState.CallFunction(9, 5, "OnUpdatingSign"))
	{
		return false;
	}

	
	bool bRetVal = (tolua_toboolean(m_LuaState, -5, 0) > 0);
	if (lua_isstring(m_LuaState, -4))
	{
		a_Line1 = tolua_tostring(m_LuaState, -4, "");
	}
	if (lua_isstring(m_LuaState, -3))
	{
		a_Line2 = tolua_tostring(m_LuaState, -3, "");
	}
	if (lua_isstring(m_LuaState, -2))
	{
		a_Line3 = tolua_tostring(m_LuaState, -2, "");
	}
	if (lua_isstring(m_LuaState, -1))
	{
		a_Line4 = tolua_tostring(m_LuaState, -1, "");
	}
	return bRetVal;
}





bool cPlugin_NewLua::OnWeatherChanged(cWorld & a_World)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_WEATHER_CHANGED);
	ASSERT(FnName != NULL);
	if (!m_LuaState.PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, &a_World, "cWorld");

	if (!m_LuaState.CallFunction(1, 1, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}





bool cPlugin_NewLua::OnWeatherChanging(cWorld & a_World, eWeather & a_NewWeather)
{
	cCSLock Lock(m_CriticalSection);
	const char * FnName = GetHookFnName(cPluginManager::HOOK_WEATHER_CHANGED);
	ASSERT(FnName != NULL);
	if (!m_LuaState.PushFunction(FnName))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, &a_World, "cWorld");
	tolua_pushnumber  (m_LuaState, a_NewWeather);

	if (!m_LuaState.CallFunction(2, 2, FnName))
	{
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	if (lua_isnumber(m_LuaState, -2))
	{
		a_NewWeather = (eWeather)lua_tointeger(m_LuaState, -2);
	}
	lua_pop(m_LuaState, 1);
	return bRetVal;
}





bool cPlugin_NewLua::HandleCommand(const AStringVector & a_Split, cPlayer * a_Player)
{
	ASSERT(!a_Split.empty());
	CommandMap::iterator cmd = m_Commands.find(a_Split[0]);
	if (cmd == m_Commands.end())
	{
		LOGWARNING("Command handler is registered in cPluginManager but not in cPlugin, wtf? Command \"%s\".", a_Split[0].c_str());
		return false;
	}
	
	cCSLock Lock(m_CriticalSection);
	
	// Push the function to be called:
	if (!m_LuaState.PushFunctionFromRegistry(cmd->second))
	{
		LOGWARNING("Command handler function for \"%s\" is invalid", cmd->first.c_str());
		return false;
	}
	
	// Push the split:
	m_LuaState.PushStringVector(a_Split);
	
	// Push player:
	tolua_pushusertype(m_LuaState, a_Player, "cPlayer");
	
	// Call function:
	if (!m_LuaState.CallFunction(2, 1, __FUNCTION__))
	{
		LOGWARNING("LUA error in %s. Stack size: %i", __FUNCTION__, lua_gettop(m_LuaState));
		return false;
	}
	
	// Handle return value:
	bool RetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
	lua_pop(m_LuaState, 1);  // Pop return value
	
	return RetVal;
}





bool cPlugin_NewLua::HandleConsoleCommand(const AStringVector & a_Split, cCommandOutputCallback & a_Output)
{
	ASSERT(!a_Split.empty());
	CommandMap::iterator cmd = m_ConsoleCommands.find(a_Split[0]);
	if (cmd == m_ConsoleCommands.end())
	{
		LOGWARNING("Console command handler is registered in cPluginManager but not in cPlugin, wtf? Console command \"%s\", plugin \"%s\".",
			a_Split[0].c_str(), GetName().c_str()
		);
		return false;
	}
	
	cCSLock Lock(m_CriticalSection);
	
	// Push the function to be called:
	lua_rawgeti(m_LuaState, LUA_REGISTRYINDEX, cmd->second);  // same as lua_getref()
	
	// Push the split:
	lua_createtable(m_LuaState, a_Split.size(), 0);
	int newTable = lua_gettop(m_LuaState);
	int index = 1;
	std::vector<std::string>::const_iterator iter = a_Split.begin(), end = a_Split.end();
	while(iter != end)
	{
		tolua_pushstring(m_LuaState, (*iter).c_str());
		lua_rawseti(m_LuaState, newTable, index);
		++iter;
		++index;
	}
	
	// Call function:
	if (!m_LuaState.CallFunction(1, 2, __FUNCTION__))
	{
		LOGWARNING("Lua error in %s. Stack size: %i", __FUNCTION__, lua_gettop(m_LuaState));
		return false;
	}
	
	// Handle return values:
	if (lua_isstring(m_LuaState, -1))
	{
		AString str = tolua_tocppstring(m_LuaState, -1, "");
		a_Output.Out(str);
	}
	bool RetVal = (tolua_toboolean(m_LuaState, -2, 0) > 0);
	lua_pop(m_LuaState, 2);  // Pop return values
	
	return RetVal;
}





void cPlugin_NewLua::ClearCommands(void)
{
	cCSLock Lock(m_CriticalSection);

	// Unreference the bound functions so that Lua can GC them
	if (m_LuaState != NULL)
	{
		for (CommandMap::iterator itr = m_Commands.begin(), end = m_Commands.end(); itr != end; ++itr)
		{
			luaL_unref(m_LuaState, LUA_REGISTRYINDEX, itr->second);
		}
	}
	m_Commands.clear();
}





void cPlugin_NewLua::ClearConsoleCommands(void)
{
	cCSLock Lock(m_CriticalSection);

	// Unreference the bound functions so that Lua can GC them
	if (m_LuaState != NULL)
	{
		for (CommandMap::iterator itr = m_ConsoleCommands.begin(), end = m_ConsoleCommands.end(); itr != end; ++itr)
		{
			luaL_unref(m_LuaState, LUA_REGISTRYINDEX, itr->second);
		}
	}
	m_ConsoleCommands.clear();
}





bool cPlugin_NewLua::CanAddHook(cPluginManager::PluginHook a_Hook)
{
	const char * FnName = GetHookFnName(a_Hook);
	if (FnName == NULL)
	{
		// Unknown hook ID
		LOGWARNING("Plugin %s wants to add an unknown hook ID (%d). The plugin need not work properly.", GetName().c_str(), a_Hook);
		return false;
	}
	
	// Check if the function is available
	lua_getglobal(m_LuaState, FnName);
	bool res = lua_isfunction(m_LuaState, -1);
	lua_pop(m_LuaState, 1);
	
	if (res)
	{
		return true;
	}
	
	LOGWARNING("Plugin %s wants to add a hook (%d), but it doesn't provide the callback function \"%s\" for it. The plugin need not work properly.", 
		GetName().c_str(), a_Hook, FnName
	);
	
	// Lua stacktrace:
	LOGWARNING("Stack trace:");
	lua_Debug entry;
	int depth = 0; 
	while (lua_getstack(m_LuaState, depth, &entry))
	{
		int status = lua_getinfo(m_LuaState, "Sln", &entry);
		assert(status);

		LOGWARNING("  %s(%d): %s", entry.short_src, entry.currentline, entry.name ? entry.name : "?");
		depth++;
	}
	LOGWARNING("Stack trace end");

	return false;
}





const char * cPlugin_NewLua::GetHookFnName(cPluginManager::PluginHook a_Hook)
{
	switch (a_Hook)
	{
		case cPluginManager::HOOK_BLOCK_TO_PICKUPS:             return "OnBlockToPickups";
		case cPluginManager::HOOK_CHAT:                         return "OnChat";
		case cPluginManager::HOOK_CHUNK_AVAILABLE:              return "OnChunkAvailable";
		case cPluginManager::HOOK_CHUNK_GENERATED:              return "OnChunkGenerated";
		case cPluginManager::HOOK_CHUNK_GENERATING:             return "OnChunkGenerating";
		case cPluginManager::HOOK_CHUNK_UNLOADED:               return "OnChunkUnloaded";
		case cPluginManager::HOOK_CHUNK_UNLOADING:              return "OnChunkUnloading";
		case cPluginManager::HOOK_COLLECTING_PICKUP:            return "OnCollectingPickup";
		case cPluginManager::HOOK_CRAFTING_NO_RECIPE:           return "OnCraftingNoRecipe";
		case cPluginManager::HOOK_DISCONNECT:                   return "OnDisconnect";
		case cPluginManager::HOOK_EXECUTE_COMMAND:              return "OnExecuteCommand";
		case cPluginManager::HOOK_HANDSHAKE:                    return "OnHandshake";
		case cPluginManager::HOOK_KILLING:                      return "OnKilling";
		case cPluginManager::HOOK_LOGIN:                        return "OnLogin";
		case cPluginManager::HOOK_PLAYER_BREAKING_BLOCK:        return "OnPlayerBreakingBlock";
		case cPluginManager::HOOK_PLAYER_BROKEN_BLOCK:          return "OnPlayerBrokenBlock";
		case cPluginManager::HOOK_PLAYER_EATING:                return "OnPlayerEating";
		case cPluginManager::HOOK_PLAYER_JOINED:                return "OnPlayerJoined";
		case cPluginManager::HOOK_PLAYER_LEFT_CLICK:            return "OnPlayerLeftClick";
		case cPluginManager::HOOK_PLAYER_MOVING:                return "OnPlayerMoving";
		case cPluginManager::HOOK_PLAYER_PLACED_BLOCK:          return "OnPlayerPlacedBlock";
		case cPluginManager::HOOK_PLAYER_PLACING_BLOCK:         return "OnPlayerPlacingBlock";
		case cPluginManager::HOOK_PLAYER_RIGHT_CLICK:           return "OnPlayerRightClick";
		case cPluginManager::HOOK_PLAYER_RIGHT_CLICKING_ENTITY: return "OnPlayerRightClickingEntity";
		case cPluginManager::HOOK_PLAYER_SHOOTING:              return "OnPlayerShooting";
		case cPluginManager::HOOK_PLAYER_SPAWNED:               return "OnPlayerSpawned";
		case cPluginManager::HOOK_PLAYER_TOSSING_ITEM:          return "OnPlayerTossingItem";
		case cPluginManager::HOOK_PLAYER_USED_BLOCK:            return "OnPlayerUsedBlock";
		case cPluginManager::HOOK_PLAYER_USED_ITEM:             return "OnPlayerUsedItem";
		case cPluginManager::HOOK_PLAYER_USING_BLOCK:           return "OnPlayerUsingBlock";
		case cPluginManager::HOOK_PLAYER_USING_ITEM:            return "OnPlayerUsingItem";
		case cPluginManager::HOOK_POST_CRAFTING:                return "OnPostCrafting";
		case cPluginManager::HOOK_PRE_CRAFTING:                 return "OnPreCrafting";
		case cPluginManager::HOOK_TAKE_DAMAGE:                  return "OnTakeDamage";
		case cPluginManager::HOOK_TICK:                         return "OnTick";
		case cPluginManager::HOOK_UPDATED_SIGN:                 return "OnUpdatedSign";
		case cPluginManager::HOOK_UPDATING_SIGN:                return "OnUpdatingSign";
		case cPluginManager::HOOK_WEATHER_CHANGED:              return "OnWeatherChanged";
		case cPluginManager::HOOK_WEATHER_CHANGING:             return "OnWeatherChanging";
		default: return NULL;
	}  // switch (a_Hook)
}





AString cPlugin_NewLua::HandleWebRequest(const HTTPRequest * a_Request )
{
	cCSLock Lock(m_CriticalSection);
	std::string RetVal = "";

	std::pair< std::string, std::string > TabName = GetTabNameForRequest(a_Request);
	std::string SafeTabName = TabName.second;
	if( SafeTabName.empty() )
		return "";

	sWebPluginTab* Tab = 0;
	for (TabList::iterator itr = GetTabs().begin(); itr != GetTabs().end(); ++itr)
	{
		if ((*itr)->SafeTitle.compare(SafeTabName) == 0) // This is the one! Rawr
		{
			Tab = *itr;
			break;
		}
	}

	if( Tab )
	{
		// LOGINFO("1. Stack size: %i", lua_gettop(m_LuaState) );
		lua_rawgeti( m_LuaState, LUA_REGISTRYINDEX, Tab->UserData); // same as lua_getref()

		// LOGINFO("2. Stack size: %i", lua_gettop(m_LuaState) );
		// Push HTTPRequest
		tolua_pushusertype( m_LuaState, (void*)a_Request, "const HTTPRequest" );
		// LOGINFO("Calling bound function! :D");
		int s = lua_pcall( m_LuaState, 1, 1, 0);

		if ( s != 0 )
		{
			std::string err = lua_tostring(m_LuaState, -1);
			LOGERROR("-- %s", err.c_str() );
			lua_pop(m_LuaState, 1);
			LOGINFO("error. Stack size: %i", lua_gettop(m_LuaState) );
			return err;	// Show the error message in the web page, looks cool
		}

		if( !lua_isstring( m_LuaState, -1 ) )
		{
			LOGWARN("WARNING: WebPlugin tab '%s' did not return a string!", Tab->Title.c_str() );
			lua_pop(m_LuaState, 1); // Pop return value
			return std::string("WARNING: WebPlugin tab '") + Tab->Title + std::string("' did not return a string!");
		}

		RetVal += tolua_tostring(m_LuaState, -1, 0);
		lua_pop(m_LuaState, 1); // Pop return value
		// LOGINFO("ok. Stack size: %i", lua_gettop(m_LuaState) );
	}

	return RetVal;
}





bool cPlugin_NewLua::AddWebTab(const AString & a_Title, lua_State * a_LuaState, int a_FunctionReference)
{
	cCSLock Lock(m_CriticalSection);
	if (a_LuaState != m_LuaState)
	{
		LOGERROR("Only allowed to add a tab to a WebPlugin of your own Plugin!");
		return false;
	}
	sWebPluginTab * Tab = new sWebPluginTab();
	Tab->Title = a_Title;
	Tab->SafeTitle = SafeString(a_Title);

	Tab->UserData = a_FunctionReference;

	GetTabs().push_back(Tab);
	return true;
}





void cPlugin_NewLua::BindCommand(const AString & a_Command, int a_FnRef)
{
	ASSERT(m_Commands.find(a_Command) == m_Commands.end());
	m_Commands[a_Command] = a_FnRef;
}





void cPlugin_NewLua::BindConsoleCommand(const AString & a_Command, int a_FnRef)
{
	ASSERT(m_ConsoleCommands.find(a_Command) == m_ConsoleCommands.end());
	m_ConsoleCommands[a_Command] = a_FnRef;
}





void cPlugin_NewLua::Unreference(int a_LuaRef)
{
	cCSLock Lock(m_CriticalSection);
	luaL_unref(m_LuaState, LUA_REGISTRYINDEX, a_LuaRef);
}





bool cPlugin_NewLua::CallbackWindowClosing(int a_FnRef, cWindow & a_Window, cPlayer & a_Player, bool a_CanRefuse)
{
	ASSERT(a_FnRef != LUA_REFNIL);
	
	cCSLock Lock(m_CriticalSection);
	lua_rawgeti(m_LuaState, LUA_REGISTRYINDEX, a_FnRef);  // Push the function to be called
	tolua_pushusertype(m_LuaState, &a_Window, "cWindow");
	tolua_pushusertype(m_LuaState, &a_Player, "cPlayer");
	tolua_pushboolean (m_LuaState, a_CanRefuse ? 1 : 0);

	// Call function:
	if (!m_LuaState.CallFunction(3, 1, __FUNCTION__))
	{
		LOGWARNING("LUA error in %s. Stack size: %i", __FUNCTION__, lua_gettop(m_LuaState));
		return false;
	}

	bool bRetVal = (tolua_toboolean(m_LuaState, -1, false) > 0);
	lua_pop(m_LuaState, 1);
	return bRetVal;
}





void cPlugin_NewLua::CallbackWindowSlotChanged(int a_FnRef, cWindow & a_Window, int a_SlotNum)
{
	ASSERT(a_FnRef != LUA_REFNIL);
	
	cCSLock Lock(m_CriticalSection);
	lua_rawgeti(m_LuaState, LUA_REGISTRYINDEX, a_FnRef);  // Push the function to be called
	tolua_pushusertype(m_LuaState, &a_Window, "cWindow");
	tolua_pushnumber  (m_LuaState, a_SlotNum);

	// Call function:
	if (!m_LuaState.CallFunction(2, 0, __FUNCTION__))
	{
		LOGWARNING("LUA error in %s. Stack size: %i", __FUNCTION__, lua_gettop(m_LuaState));
	}
}