1
0

Added all current hooks to the new plugin structure.

Converted MagicCarpet to the new plugin structure
When you fall of the MagicCarpet you teleport back up :D

git-svn-id: http://mc-server.googlecode.com/svn/trunk@220 0a769ca7-a7f5-676a-18bf-c427514a06d6
This commit is contained in:
faketruth 2012-02-01 19:19:51 +00:00
parent 9d64191ee3
commit 28ff03fcfe
6 changed files with 892 additions and 211 deletions

View File

@ -92,78 +92,6 @@ function cCarpet:moveTo( NewPos )
end end
end end
function cCarpet:getY()
MagicCarpetPlugin = {} return self.Location.y
MagicCarpetPlugin.__index = MagicCarpetPlugin
function MagicCarpetPlugin:new()
local t = {}
setmetatable(t, MagicCarpetPlugin)
local w = Lua__cPlugin:new()
tolua.setpeer(w, t)
w:tolua__set_instance(w)
return w
end end
function MagicCarpetPlugin:Initialize()
self:SetName( "MagicCarpet" )
self:SetVersion( 1 )
PluginManager = cRoot:Get():GetPluginManager()
PluginManager:AddHook( self, cPluginManager.E_PLUGIN_PLAYER_MOVE)
PluginManager:AddHook( self, cPluginManager.E_PLUGIN_DISCONNECT)
self:AddCommand("/mc", " - Spawns a magical carpet!", "magiccarpet")
self:BindCommand( "/mc", "magiccarpet", HandleCarpetCommand )
self.Carpets = {}
Log( "Initialized " .. self:GetName() .. " v." .. self:GetVersion() )
return true
end
function MagicCarpetPlugin:OnDisable()
Log( self:GetName() .. " v." .. self:GetVersion() .. " is shutting down..." )
for i, Carpet in pairs( self.Carpets ) do
Carpet:remove()
end
end
function HandleCarpetCommand( Split, Player )
Carpet = self.Carpets[ Player ]
if( Carpet == nil ) then
self.Carpets[ Player ] = cCarpet:new()
Player:SendMessage("You're on a magic carpet!" )
else
Carpet:remove()
self.Carpets[ Player ] = nil
Player:SendMessage("The carpet vanished!" )
end
return true
end
function MagicCarpetPlugin:OnDisconnect( Reason, Player )
local Carpet = self.Carpets[ Player ]
if( Carpet ~= nil ) then
Carpet:remove()
end
self.Carpets[ Player ] = nil
end
function MagicCarpetPlugin:OnPlayerMove( Player )
local Carpet = self.Carpets[ Player ]
if( Carpet == nil ) then
return
end
if( Player:GetPitch() == 90 ) then
Carpet:moveTo( cLocation:new( Player:GetPosX(), Player:GetPosY()-1, Player:GetPosZ() ) )
else
Carpet:moveTo( cLocation:new( Player:GetPosX(), Player:GetPosY(), Player:GetPosZ() ) )
end
end
Plugin = MagicCarpetPlugin:new()
cRoot:Get():GetPluginManager():AddPlugin( Plugin )

View File

@ -0,0 +1,65 @@
local PLUGIN = {}
local Carpets = {}
function Initialize( Plugin )
PLUGIN = Plugin
Plugin:SetName( "MagicCarpet" )
Plugin:SetVersion( 1 )
PluginManager = cRoot:Get():GetPluginManager()
PluginManager:AddHook( Plugin, cPluginManager.E_PLUGIN_PLAYER_MOVE)
PluginManager:AddHook( Plugin, cPluginManager.E_PLUGIN_DISCONNECT)
Plugin:AddCommand("/mc", " - Spawns a magical carpet!", "magiccarpet")
Plugin:BindCommand( "/mc", "magiccarpet", HandleCarpetCommand )
Log( "Initialized " .. Plugin:GetName() .. " v." .. Plugin:GetVersion() )
return true
end
function OnDisable()
Log( PLUGIN:GetName() .. " v." .. PLUGIN:GetVersion() .. " is shutting down..." )
for i, Carpet in pairs( Carpets ) do
Carpet:remove()
end
end
function HandleCarpetCommand( Split, Player )
Carpet = Carpets[ Player ]
if( Carpet == nil ) then
Carpets[ Player ] = cCarpet:new()
Player:SendMessage("You're on a magic carpet!" )
else
Carpet:remove()
Carpets[ Player ] = nil
Player:SendMessage("The carpet vanished!" )
end
return true
end
function OnDisconnect( Reason, Player )
local Carpet = Carpets[ Player ]
if( Carpet ~= nil ) then
Carpet:remove()
end
Carpets[ Player ] = nil
end
function OnPlayerMove( Player )
local Carpet = Carpets[ Player ]
if( Carpet == nil ) then
return
end
if( Player:GetPitch() == 90 ) then
Carpet:moveTo( cLocation:new( Player:GetPosX(), Player:GetPosY()-1, Player:GetPosZ() ) )
else
if( Player:GetPosY() < Carpet:getY() ) then
LOGINFO("Fell tru mc!")
Player:TeleportTo( Player:GetPosX(), Carpet:getY(), Player:GetPosZ() )
end
Carpet:moveTo( cLocation:new( Player:GetPosX(), Player:GetPosY(), Player:GetPosZ() ) )
end
end

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
/* /*
** Lua binding: AllToLua ** Lua binding: AllToLua
** Generated automatically by tolua++-1.0.92 on 02/01/12 00:50:40. ** Generated automatically by tolua++-1.0.92 on 02/01/12 20:13:33.
*/ */
/* Exported function */ /* Exported function */

View File

@ -120,6 +120,14 @@ bool cPlugin_NewLua::Initialize()
return bSuccess; return bSuccess;
} }
void cPlugin_NewLua::OnDisable()
{
if( !PushFunction("OnDisable", false) ) // false = don't log error if not found
return;
CallFunction(0, 0, "OnDisable");
}
void cPlugin_NewLua::Tick(float a_Dt) void cPlugin_NewLua::Tick(float a_Dt)
{ {
if( !PushFunction("Tick") ) if( !PushFunction("Tick") )
@ -130,28 +138,30 @@ void cPlugin_NewLua::Tick(float a_Dt)
CallFunction(1, 0, "Tick"); CallFunction(1, 0, "Tick");
} }
bool cPlugin_NewLua::OnPlayerJoin( cPlayer* a_Player ) bool cPlugin_NewLua::OnCollectItem( cPickup* a_Pickup, cPlayer* a_Player )
{ {
if( !PushFunction("OnPlayerJoin") ) if( !PushFunction("OnCollectItem") )
return false; return false;
tolua_pushusertype(m_LuaState, a_Pickup, "cPickup");
tolua_pushusertype(m_LuaState, a_Player, "cPlayer"); tolua_pushusertype(m_LuaState, a_Player, "cPlayer");
if( !CallFunction(1, 1, "OnPlayerJoin") ) if( !CallFunction(2, 1, "OnCollectItem") )
return false; return false;
bool bRetVal = (tolua_toboolean( m_LuaState, -1, 0) > 0); bool bRetVal = (tolua_toboolean( m_LuaState, -1, 0) > 0);
return bRetVal; return bRetVal;
} }
bool cPlugin_NewLua::OnLogin( cPacket_Login* a_PacketData ) bool cPlugin_NewLua::OnDisconnect( std::string a_Reason, cPlayer* a_Player )
{ {
if( !PushFunction("OnLogin") ) if( !PushFunction("OnDisconnect") )
return false; return false;
tolua_pushusertype(m_LuaState, a_PacketData, "cPacket_Login"); tolua_pushstring( m_LuaState, a_Reason.c_str() );
tolua_pushusertype(m_LuaState, a_Player, "cPlayer");
if( !CallFunction(1, 1, "OnLogin") ) if( !CallFunction(2, 1, "OnDisconnect") )
return false; return false;
bool bRetVal = (tolua_toboolean( m_LuaState, -1, 0) > 0); bool bRetVal = (tolua_toboolean( m_LuaState, -1, 0) > 0);
@ -173,6 +183,96 @@ bool cPlugin_NewLua::OnBlockPlace( cPacket_BlockPlace* a_PacketData, cPlayer* a_
return bRetVal; return bRetVal;
} }
bool cPlugin_NewLua::OnBlockDig( cPacket_BlockDig* a_PacketData, cPlayer* a_Player, cItem* a_PickupItem )
{
if( !PushFunction("OnBlockDig") )
return false;
tolua_pushusertype(m_LuaState, a_PacketData, "cPacket_BlockDig");
tolua_pushusertype(m_LuaState, a_Player, "cPlayer");
tolua_pushusertype(m_LuaState, a_PickupItem, "cItem");
if( !CallFunction(3, 1, "OnBlockDig") )
return false;
bool bRetVal = (tolua_toboolean( m_LuaState, -1, 0) > 0);
return bRetVal;
}
bool cPlugin_NewLua::OnChat( const char* a_Chat, cPlayer* a_Player )
{
if( !PushFunction("OnChat") )
return false;
tolua_pushstring( m_LuaState, a_Chat );
tolua_pushusertype(m_LuaState, a_Player, "cPlayer");
if( !CallFunction(2, 1, "OnChat") )
return false;
bool bRetVal = (tolua_toboolean( m_LuaState, -1, 0) > 0);
return bRetVal;
}
bool cPlugin_NewLua::OnLogin( cPacket_Login* a_PacketData )
{
if( !PushFunction("OnLogin") )
return false;
tolua_pushusertype(m_LuaState, a_PacketData, "cPacket_Login");
if( !CallFunction(1, 1, "OnLogin") )
return false;
bool bRetVal = (tolua_toboolean( m_LuaState, -1, 0) > 0);
return bRetVal;
}
void cPlugin_NewLua::OnPlayerSpawn( cPlayer* a_Player )
{
if( !PushFunction("OnPlayerSpawn") )
return;
tolua_pushusertype(m_LuaState, a_Player, "cPlayer");
CallFunction(1, 0, "OnPlayerSpawn");
}
bool cPlugin_NewLua::OnPlayerJoin( cPlayer* a_Player )
{
if( !PushFunction("OnPlayerJoin") )
return false;
tolua_pushusertype(m_LuaState, a_Player, "cPlayer");
if( !CallFunction(1, 1, "OnPlayerJoin") )
return false;
bool bRetVal = (tolua_toboolean( m_LuaState, -1, 0) > 0);
return bRetVal;
}
void cPlugin_NewLua::OnPlayerMove( cPlayer* a_Player )
{
if( !PushFunction("OnPlayerMove") )
return;
tolua_pushusertype(m_LuaState, a_Player, "cPlayer");
CallFunction(1, 0, "OnPlayerMove");
}
void cPlugin_NewLua::OnTakeDamage( cPawn* a_Pawn, TakeDamageInfo* a_TakeDamageInfo )
{
if( !PushFunction("OnTakeDamage") )
return;
tolua_pushusertype(m_LuaState, a_Pawn, "cPawn");
tolua_pushusertype(m_LuaState, a_TakeDamageInfo, "TakeDamageInfo");
CallFunction(2, 0, "OnTakeDamage");
}
bool cPlugin_NewLua::OnKilled( cPawn* a_Killed, cEntity* a_Killer ) bool cPlugin_NewLua::OnKilled( cPawn* a_Killed, cEntity* a_Killer )
{ {
if( !PushFunction("OnKilled") ) if( !PushFunction("OnKilled") )
@ -188,6 +288,7 @@ bool cPlugin_NewLua::OnKilled( cPawn* a_Killed, cEntity* a_Killer )
return bRetVal; return bRetVal;
} }
cWebPlugin_Lua* cPlugin_NewLua::CreateWebPlugin(lua_State* a_LuaState) cWebPlugin_Lua* cPlugin_NewLua::CreateWebPlugin(lua_State* a_LuaState)
{ {
if( a_LuaState != m_LuaState ) if( a_LuaState != m_LuaState )
@ -204,12 +305,15 @@ cWebPlugin_Lua* cPlugin_NewLua::CreateWebPlugin(lua_State* a_LuaState)
// Helper functions // Helper functions
bool cPlugin_NewLua::PushFunction( const char* a_FunctionName ) bool cPlugin_NewLua::PushFunction( const char* a_FunctionName, bool a_bLogError /* = true */ )
{ {
lua_getglobal(m_LuaState, a_FunctionName); lua_getglobal(m_LuaState, a_FunctionName);
if(!lua_isfunction(m_LuaState,-1)) if(!lua_isfunction(m_LuaState,-1))
{
if( a_bLogError )
{ {
LOGWARN("Error in plugin %s: Could not find function %s()", m_Directory.c_str(), a_FunctionName ); LOGWARN("Error in plugin %s: Could not find function %s()", m_Directory.c_str(), a_FunctionName );
}
lua_pop(m_LuaState,1); lua_pop(m_LuaState,1);
return false; return false;
} }

View File

@ -13,18 +13,30 @@ public: //tolua_export
cPlugin_NewLua( const char* a_PluginName ); cPlugin_NewLua( const char* a_PluginName );
~cPlugin_NewLua(); ~cPlugin_NewLua();
virtual void OnDisable(); //tolua_export
virtual bool Initialize(); //tolua_export virtual bool Initialize(); //tolua_export
virtual void Tick(float a_Dt); //tolua_export virtual void Tick(float a_Dt); //tolua_export
virtual bool OnPlayerJoin( cPlayer* a_Player ); //tolua_export
virtual bool OnLogin( cPacket_Login* a_PacketData ); //tolua_export //tolua_begin
virtual bool OnBlockPlace( cPacket_BlockPlace* a_PacketData, cPlayer* a_Player ); // tolua_export virtual bool OnCollectItem( cPickup* a_Pickup, cPlayer* a_Player );
virtual bool OnKilled( cPawn* a_Killed, cEntity* a_Killer ); //tolua_export virtual bool OnDisconnect( std::string a_Reason, cPlayer* a_Player );
virtual bool OnBlockPlace( cPacket_BlockPlace* a_PacketData, cPlayer* a_Player );
virtual bool OnBlockDig( cPacket_BlockDig* a_PacketData, cPlayer* a_Player, cItem* a_PickupItem );
virtual bool OnChat( const char* a_Chat, cPlayer* a_Player );
virtual bool OnLogin( cPacket_Login* a_PacketData );
virtual void OnPlayerSpawn( cPlayer* a_Player );
virtual bool OnPlayerJoin( cPlayer* a_Player );
virtual void OnPlayerMove( cPlayer* a_Player );
virtual void OnTakeDamage( cPawn* a_Pawn, TakeDamageInfo* a_TakeDamageInfo );
virtual bool OnKilled( cPawn* a_Killed, cEntity* a_Killer );
//tolua_end
lua_State* GetLuaState() { return m_LuaState; } lua_State* GetLuaState() { return m_LuaState; }
cWebPlugin_Lua* CreateWebPlugin(lua_State* a_LuaState); //tolua_export cWebPlugin_Lua* CreateWebPlugin(lua_State* a_LuaState); //tolua_export
private: private:
bool PushFunction( const char* a_FunctionName ); bool PushFunction( const char* a_FunctionName, bool a_bLogError = true );
bool CallFunction( int a_NumArgs, int a_NumResults, const char* a_FunctionName ); // a_FunctionName is only used for error messages, nothing else bool CallFunction( int a_NumArgs, int a_NumResults, const char* a_FunctionName ); // a_FunctionName is only used for error messages, nothing else
typedef std::list< cWebPlugin_Lua* > WebPluginList; typedef std::list< cWebPlugin_Lua* > WebPluginList;