From 4e8128b9d738d1744cc883deaa9c294f43ba8242 Mon Sep 17 00:00:00 2001 From: faketruth Date: Thu, 26 Jan 2012 23:10:49 +0000 Subject: Converted all of the Core plugin to the new plugin structure, except for the WebAdmin part git-svn-id: http://mc-server.googlecode.com/svn/trunk@175 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- Plugins/NewTest/main.lua | 118 ++++++++++++++++++++++++++++++++------- Plugins/NewTest/onblockplace.lua | 60 ++++++++++++++++++++ Plugins/NewTest/onkilled.lua | 24 ++++++++ Plugins/NewTest/onlogin.lua | 20 +++++++ Plugins/NewTest/onplayerjoin.lua | 4 ++ source/cPlugin_NewLua.cpp | 102 +++++++++++++++++++++++++++------ source/cPlugin_NewLua.h | 17 ++++-- 7 files changed, 303 insertions(+), 42 deletions(-) create mode 100644 Plugins/NewTest/onblockplace.lua create mode 100644 Plugins/NewTest/onkilled.lua create mode 100644 Plugins/NewTest/onlogin.lua create mode 100644 Plugins/NewTest/onplayerjoin.lua diff --git a/Plugins/NewTest/main.lua b/Plugins/NewTest/main.lua index fedd430e5..3dfa3f2f1 100644 --- a/Plugins/NewTest/main.lua +++ b/Plugins/NewTest/main.lua @@ -1,12 +1,19 @@ -LOGINFO("main.lua!!") -LOOLOL = "12345" -PLUGIN = {} +---- Some settings ----- +SHOW_PLUGIN_NAMES = true -- If true, plugin name will be shown before commands + -- This is overwritten in the Initialize() function +------------------------ + +-- Global variables +PLUGIN = {} -- Reference to own plugin object +BannedPlayersIni = {} +WhiteListIni = {} +ItemsTable = {} function Initialize( Plugin ) - LOGINFO("Initialize in main.lua") PLUGIN = Plugin - Plugin:SetName("LOLOL new plugin!") + Plugin:SetName( "NewCore" ) + Plugin:SetVersion( 8 ) PluginManager = cRoot:Get():GetPluginManager() PluginManager:AddHook( Plugin, cPluginManager.E_PLUGIN_PLAYER_JOIN ) @@ -14,43 +21,112 @@ function Initialize( Plugin ) PluginManager:AddHook( Plugin, cPluginManager.E_PLUGIN_BLOCK_PLACE ) PluginManager:AddHook( Plugin, cPluginManager.E_PLUGIN_KILLED ) - Plugin:AddCommand("/help", " - [Page] Show this message", "core.help") + Plugin:AddCommand("/help", " - [Page] Show this message", "core.help") Plugin:AddCommand("/pluginlist", " - Show list of plugins", "core.pluginlist") Plugin:AddCommand("/tp", " - [Player] - Teleport yourself to a player", "core.teleport") Plugin:AddCommand("/item", " - [ItemID/Name] - Spawn an item for yourself", "core.item") Plugin:AddCommand("/list", " - Shows list of connected players", "core.playerlist") Plugin:AddCommand("/motd", " - Show message of the day", "core.motd") - Plugin:AddCommand("/reload", " - Reload all plugins", "core.reload") + Plugin:AddCommand("/reload", " - Reload all plugins", "core.reload") Plugin:AddCommand("/time", " - [Day/Night] - Sets the time of day", "core.time") - Plugin:AddCommand("/spawn", " - Return to the spawn", "core.spawn") - Plugin:AddCommand("/kick", " - [Player] - Kick a player", "core.kick") + Plugin:AddCommand("/spawn", " - Return to the spawn", "core.spawn") + Plugin:AddCommand("/kick", " - [Player] - Kick a player", "core.kick") Plugin:AddCommand("/ban", " - [Player] - Ban a player", "core.ban") Plugin:AddCommand("/unban", " - [Player] - Unban a player", "core.unban") - Plugin:AddCommand("/top", " - Teleport yourself to the top most block", "core.top") - Plugin:AddCommand("/gm", " - [Gamemode (0|1)] - Change your gamemode", "core.changegm") + Plugin:AddCommand("/top", " - Teleport yourself to the top most block", "core.top") + Plugin:AddCommand("/gm", " - [Gamemode (0|1)] - Change your gamemode", "core.changegm") Plugin:AddCommand("/gotoworld", " - Move to a different world!", "core.gotoworld") Plugin:BindCommand( "/help", "core.help", HandleHelpCommand ) - Plugin:BindCommand( "/pluginlist","core.pluginlist", HandlePluginListCommand ) - Plugin:BindCommand( "/tp", "core.teleport", HandleTPCommand ) + Plugin:BindCommand( "/pluginlist", "core.pluginlist", HandlePluginListCommand ) + Plugin:BindCommand( "/tp", "core.teleport", HandleTPCommand ) Plugin:BindCommand( "/item", "core.item", HandleItemCommand ) - Plugin:BindCommand( "/i", "core.item", HandleItemCommand ) + Plugin:BindCommand( "/i", "core.item", HandleItemCommand ) Plugin:BindCommand( "/list", "core.playerlist", HandlePlayerListCommand ) Plugin:BindCommand( "/who", "core.playerlist", HandlePlayerListCommand ) - Plugin:BindCommand( "/playerlist","core.playerlist", HandlePlayerListCommand ) + Plugin:BindCommand( "/playerlist", "core.playerlist", HandlePlayerListCommand ) Plugin:BindCommand( "/motd", "core.motd", HandleMOTDCommand ) - Plugin:BindCommand( "/reload", "core.reload", HandleReloadCommand ) + Plugin:BindCommand( "/reload", "core.reload", HandleReloadCommand ) Plugin:BindCommand( "/time", "core.time", HandleTimeCommand ) - Plugin:BindCommand( "/spawn", "core.spawn", HandleSpawnCommand ) + Plugin:BindCommand( "/spawn", "core.spawn", HandleSpawnCommand ) Plugin:BindCommand( "/home", "core.spawn", HandleSpawnCommand ) Plugin:BindCommand( "/kick", "core.kick", HandleKickCommand ) Plugin:BindCommand( "/ban", "core.ban", HandleBanCommand ) - Plugin:BindCommand( "/unban", "core.unban", HandleUnbanCommand ) - Plugin:BindCommand( "/top", "core.top", HandleTopCommand ) - Plugin:BindCommand( "/gm", "core.changegm", HandleChangeGMCommand ) + Plugin:BindCommand( "/unban", "core.unban", HandleUnbanCommand ) + Plugin:BindCommand( "/top", "core.top", HandleTopCommand ) + Plugin:BindCommand( "/gm", "core.changegm", HandleChangeGMCommand ) Plugin:BindCommand( "/gotoworld", "core.gotoworld", HandleGotoWorldCommand ) - LOGINFO("Plugin name is: " .. Plugin:GetName() ) + + local IniFile = cIniFile("settings.ini") + if ( IniFile:ReadFile() == true ) then + SHOW_PLUGIN_NAMES = IniFile:GetValueB("HelpPlugin", "ShowPluginNames", true ) + end + + local itemsINI = cIniFile("items.ini") + if ( itemsINI:ReadFile() == true ) then + local KeyID = itemsINI:FindKey('Items') + + LOGINFO("Core: loaded " .. itemsINI:GetNumValues( KeyID ) .. " item names.") + + for i = 0, itemsINI:GetNumValues('Items') do + local ItemName = itemsINI:GetValueName( KeyID, i ) + local ItemSyntax = itemsINI:GetValue(KeyID, i, "0") + + local ItemData = StringSplit(ItemSyntax, ":") -- [1] = ID, [2] = perhaps meta/dmg + --LOGINFO( "#ItemData: " .. #ItemData ) + if( #ItemData > 0 ) then + --LOGINFO("ItemData[0]: "..ItemData[1]) + local ItemID = tonumber( ItemData[1] ) + if( ItemID > 0 ) then + local ItemMeta = 0 + if( #ItemData > 1 ) then + ItemMeta = tonumber( ItemData[2] ) + end + ItemsTable[ ItemName ] = cItem( ItemID, 1, ItemMeta ) + LOGINFO("Got item: " .. ItemName .. "-> " .. ItemsTable[ ItemName ].m_ItemID ..":" .. ItemsTable[ ItemName ].m_ItemHealth ) + end + end + end + + HAVE_ITEM_NAMES = true + end + + -- Load whitelist, and add default values and stuff + WhiteListIni = cIniFile("whitelist.ini") + if ( WhiteListIni:ReadFile() == true ) then + if( WhiteListIni:GetValueB("WhiteListSettings", "WhiteListOn", false) == true ) then + if( WhiteListIni:GetNumValues("WhiteList") > 0 ) then + LOGINFO("Core: loaded " .. WhiteListIni:GetNumValues('WhiteList') .. " whitelisted players.") + else + LOGWARN("WARNING: WhiteList is on, but there are no people in the whitelist!") + end + end + else + WhiteListIni:SetValueB("WhiteListSettings", "WhiteListOn", false ) + WhiteListIni:SetValue("WhiteList", "", "") -- So it adds an empty header + WhiteListIni:DeleteValue("WhiteList", "") -- And remove the value + WhiteListIni:KeyComment("WhiteList", "PlayerName=1") + if( WhiteListIni:WriteFile() == false ) then + LOGWARN("WARNING: Could not write to whitelist.ini") + end + end + + -- Load banned players, and add default values and stuff + BannedPlayersIni = cIniFile("banned.ini") + if ( BannedPlayersIni:ReadFile() == true ) then + if( BannedPlayersIni:GetNumValues("Banned") > 0 ) then + LOGINFO("Core: loaded " .. BannedPlayersIni:GetNumValues("Banned") .. " banned players.") + end + else + BannedPlayersIni:SetValue("Banned", "", "") -- So it adds an empty header + BannedPlayersIni:DeleteValue("Banned", "") -- And remove the value + BannedPlayersIni:KeyComment("Banned", "PlayerName=1") + if( BannedPlayersIni:WriteFile() == false ) then + LOGWARN("WARNING: Could not write to banned.ini") + end + end + LOG( "Initialized " .. Plugin:GetName() .. " v." .. Plugin:GetVersion() ) return true end \ No newline at end of file diff --git a/Plugins/NewTest/onblockplace.lua b/Plugins/NewTest/onblockplace.lua new file mode 100644 index 000000000..ba75bf5c2 --- /dev/null +++ b/Plugins/NewTest/onblockplace.lua @@ -0,0 +1,60 @@ +function OnBlockPlace( Block, Player ) + + -- dont check if the direction is in the air + if Block.m_Direction ~= -1 then + + local X = Block.m_PosX + local Y = Block.m_PosY + local Z = Block.m_PosZ + X, Y, Z = AddDirection( X, Y, Z, Block.m_Direction ) + if( Y >= 128 or Y < 0 ) then + return true + end + + local collision = false + local World = Player:GetWorld() + local PlayerList = World:GetAllPlayers() + + -- check if a player occupies the placement location + for i, Player in ipairs( PlayerList ) do + + -- drop the decimals, we only care about the full block X,Y,Z + local PlayerX = math.floor(Player:GetPosX(), 0) + local PlayerY = math.floor(Player:GetPosY(), 0) + local PlayerZ = math.floor(Player:GetPosZ(), 0) + + local BlockX = Block.m_PosX + local BlockY = Block.m_PosY + local BlockZ = Block.m_PosZ + + -- player height is 2 blocks, so we check the position and then offset it up one + -- so they can't place a block on there face + + if Block.m_Direction == 0 then if PlayerY == BlockY-2 and PlayerX == BlockX and PlayerZ == BlockZ then collision = true end end + if Block.m_Direction == 1 then if PlayerY == BlockY+1 and PlayerX == BlockX and PlayerZ == BlockZ then collision = true end end + + if Block.m_Direction == 2 then if PlayerY == BlockY and PlayerX == BlockX and PlayerZ == BlockZ-1 then collision = true end end + if Block.m_Direction == 2 then if PlayerY+1 == BlockY and PlayerX == BlockX and PlayerZ == BlockZ-1 then collision = true end end + + if Block.m_Direction == 3 then if PlayerY == BlockY and PlayerX == BlockX and PlayerZ == BlockZ+1 then collision = true end end + if Block.m_Direction == 3 then if PlayerY+1 == BlockY and PlayerX == BlockX and PlayerZ == BlockZ+1 then collision = true end end + + if Block.m_Direction == 4 then if PlayerY == BlockY and PlayerX == BlockX-1 and PlayerZ == BlockZ then collision = true end end + if Block.m_Direction == 4 then if PlayerY+1 == BlockY and PlayerX == BlockX-1 and PlayerZ == BlockZ then collision = true end end + + if Block.m_Direction == 5 then if PlayerY == BlockY and PlayerX == BlockX+1 and PlayerZ == BlockZ then collision = true end end + if Block.m_Direction == 5 then if PlayerY+1 == BlockY and PlayerX == BlockX+1 and PlayerZ == BlockZ then collision = true end end + + end + + if collision then + return true + else + return false + end + + end + + return false + +end \ No newline at end of file diff --git a/Plugins/NewTest/onkilled.lua b/Plugins/NewTest/onkilled.lua new file mode 100644 index 000000000..a8a92f667 --- /dev/null +++ b/Plugins/NewTest/onkilled.lua @@ -0,0 +1,24 @@ +function OnKilled( Killed, Killer ) + if( Killer == nil ) then + local KilledPlayer = tolua.cast( Killed, "cPlayer") + if( not KilledPlayer:IsA("cPlayer") or KilledPlayer == nil ) then + return false + end + + local Server = cRoot:Get():GetServer() + Server:SendMessage( cChatColor.Red .. KilledPlayer:GetName() .. " died" ) + else + local KilledPlayer = tolua.cast( Killed, "cPlayer") + if( not KilledPlayer:IsA("cPlayer") or KilledPlayer == nil ) then + return false + end + local KillerPlayer = tolua.cast( Killer, "cPlayer") + if( not KillerPlayer:IsA("cPlayer") or KillerPlayer == nil ) then + return false + end + + local Server = cRoot:Get():GetServer() + Server:SendMessage( cChatColor.Red .. KilledPlayer:GetName() .. " was killed by " .. KillerPlayer:GetName() .. "!" ) + end + return false +end \ No newline at end of file diff --git a/Plugins/NewTest/onlogin.lua b/Plugins/NewTest/onlogin.lua new file mode 100644 index 000000000..a706f8024 --- /dev/null +++ b/Plugins/NewTest/onlogin.lua @@ -0,0 +1,20 @@ +function OnLogin( PacketData ) + if( PacketData.m_Username ~= "" ) then + if( BannedPlayersIni:GetValueB("Banned", PacketData.m_Username, false) == true ) then + local Server = cRoot:Get():GetServer() + Server:SendMessage( PacketData.m_Username .. " tried to join, but is banned!" ) + LOGINFO( PacketData.m_Username .. " tried to join, but is banned!") + return true -- Player is banned, return true to deny access + end + if( WhiteListIni:GetValueB("WhiteListSettings", "WhiteListOn", false ) == true ) then + if( WhiteListIni:GetValueB("WhiteList", PacketData.m_Username, false ) == false ) then -- not on whitelist + local Server = cRoot:Get():GetServer() + Server:SendMessage( PacketData.m_Username .. " tried to join, but is not on the whitelist." ) + LOGINFO( PacketData.m_Username .. " tried to join, but is not on the whitelist." ) + return true -- Deny access to the server + end + end + end + + return false +end \ No newline at end of file diff --git a/Plugins/NewTest/onplayerjoin.lua b/Plugins/NewTest/onplayerjoin.lua new file mode 100644 index 000000000..e8263f608 --- /dev/null +++ b/Plugins/NewTest/onplayerjoin.lua @@ -0,0 +1,4 @@ +function OnPlayerJoin( Player ) + ShowMOTDTo( Player ) + return false +end \ No newline at end of file diff --git a/source/cPlugin_NewLua.cpp b/source/cPlugin_NewLua.cpp index f81f2e95e..81e629ce4 100644 --- a/source/cPlugin_NewLua.cpp +++ b/source/cPlugin_NewLua.cpp @@ -80,11 +80,8 @@ bool cPlugin_NewLua::Initialize() // Call intialize function - lua_getglobal(m_LuaState, "Initialize"); - if(!lua_isfunction(m_LuaState,-1)) + if( !PushFunction("Initialize") ) { - LOGWARN("Error in plugin %s: Could not find function Initialize()", m_Directory.c_str() ); - lua_pop(m_LuaState,1); lua_close( m_LuaState ); m_LuaState = 0; return false; @@ -92,11 +89,9 @@ bool cPlugin_NewLua::Initialize() tolua_pushusertype(m_LuaState, this, "cPlugin_NewLua"); - // do the call (1 arguments, 1 result) - int s = lua_pcall(m_LuaState, 1, 1, 0); - if( report_errors( m_LuaState, s ) ) + + if( !CallFunction(1, 1, "Initialize") ) { - LOGWARN("Error in plugin %s calling function Initialize()", m_Directory.c_str() ); lua_close( m_LuaState ); m_LuaState = 0; return false; @@ -116,20 +111,95 @@ bool cPlugin_NewLua::Initialize() void cPlugin_NewLua::Tick(float a_Dt) { - lua_getglobal(m_LuaState, "Tick"); + if( !PushFunction("Tick") ) + return; + + tolua_pushnumber( m_LuaState, a_Dt ); + + CallFunction(1, 0, "Tick"); +} + +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; +} + +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; +} + +bool cPlugin_NewLua::OnBlockPlace( cPacket_BlockPlace* a_PacketData, cPlayer* a_Player ) +{ + if( !PushFunction("OnBlockPlace") ) + return false; + + tolua_pushusertype(m_LuaState, a_PacketData, "cPacket_BlockPlace"); + tolua_pushusertype(m_LuaState, a_Player, "cPlayer"); + + if( !CallFunction(2, 1, "OnBlockPlace") ) + return false; + + bool bRetVal = (tolua_toboolean( m_LuaState, -1, 0) > 0); + return bRetVal; +} + +bool cPlugin_NewLua::OnKilled( cPawn* a_Killed, cEntity* a_Killer ) +{ + if( !PushFunction("OnKilled") ) + return false; + + tolua_pushusertype(m_LuaState, a_Killed, "cPawn"); + tolua_pushusertype(m_LuaState, a_Killer, "cEntity"); + + if( !CallFunction(2, 1, "OnKilled") ) + return false; + + bool bRetVal = (tolua_toboolean( m_LuaState, -1, 0) > 0); + return bRetVal; +} + + + + +// Helper functions +bool cPlugin_NewLua::PushFunction( const char* a_FunctionName ) +{ + lua_getglobal(m_LuaState, a_FunctionName); if(!lua_isfunction(m_LuaState,-1)) { - LOGWARN("Error in plugin %s: Could not find function Tick()", m_Directory.c_str() ); + LOGWARN("Error in plugin %s: Could not find function %s()", m_Directory.c_str(), a_FunctionName ); lua_pop(m_LuaState,1); - return; + return false; } + return true; +} - tolua_pushnumber( m_LuaState, a_Dt ); - - // do the call (1 arguments, 0 result)/ - int s = lua_pcall(m_LuaState, 1, 0, 0); +bool cPlugin_NewLua::CallFunction( int a_NumArgs, int a_NumResults, const char* a_FunctionName ) +{ + int s = lua_pcall(m_LuaState, a_NumArgs, a_NumResults, 0); if( report_errors( m_LuaState, s ) ) { - LOGWARN("Error in plugin %s calling function Tick()", m_Directory.c_str() ); + LOGWARN("Error in plugin %s calling function %s()", m_Directory.c_str(), a_FunctionName ); + return false; } + return true; } \ No newline at end of file diff --git a/source/cPlugin_NewLua.h b/source/cPlugin_NewLua.h index 418865879..bdd7f1f7e 100644 --- a/source/cPlugin_NewLua.h +++ b/source/cPlugin_NewLua.h @@ -5,16 +5,23 @@ typedef struct lua_State lua_State; -class cPlugin_NewLua : public cPlugin //tolua_export -{ //tolua_export -public: //tolua_export +class cPlugin_NewLua : public cPlugin //tolua_export +{ //tolua_export +public: //tolua_export cPlugin_NewLua( const char* a_PluginName ); ~cPlugin_NewLua(); - virtual bool Initialize(); //tolua_export - virtual void Tick(float a_Dt); //tolua_export + virtual bool Initialize(); //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 + virtual bool OnBlockPlace( cPacket_BlockPlace* a_PacketData, cPlayer* a_Player ); // tolua_export + virtual bool OnKilled( cPawn* a_Killed, cEntity* a_Killer ); //tolua_export private: + bool PushFunction( const char* a_FunctionName ); + bool CallFunction( int a_NumArgs, int a_NumResults, const char* a_FunctionName ); // a_FunctionName is only used for error messages, nothing else + std::string m_Directory; lua_State* m_LuaState; };//tolua_export \ No newline at end of file -- cgit v1.2.3