From cf500081d1af1dc16bbaa75e17480f7d75fb8bd6 Mon Sep 17 00:00:00 2001 From: Evil Factory <36804725+evilfactory@users.noreply.github.com> Date: Tue, 17 May 2022 12:24:52 -0300 Subject: [PATCH] renamed hooks (with compatibility), added new hook: character.giveJobItems (because of how often its used) and updated hook docs --- .../ServerSource/Characters/Character.cs | 2 +- .../ServerSource/Networking/GameServer.cs | 4 +- .../BarotraumaShared/Lua/CompatibilityLib.lua | 16 ++++++++ .../AI/Objectives/AIObjectiveManager.cs | 2 +- .../SharedSource/Characters/Character.cs | 4 +- docs/lua/Hooks.lua | 39 +++++++++++++------ 6 files changed, 51 insertions(+), 16 deletions(-) diff --git a/Barotrauma/BarotraumaServer/ServerSource/Characters/Character.cs b/Barotrauma/BarotraumaServer/ServerSource/Characters/Character.cs index 84c50b8c6..785836584 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Characters/Character.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Characters/Character.cs @@ -25,7 +25,7 @@ namespace Barotrauma GameServer.Log(GameServer.CharacterLogName(this) + " has died (Cause of death: " + causeOfDeath + ")", ServerLog.MessageType.Attack); } } - GameMain.LuaCs.Hook.Call("characterDeath", this,causeOfDeathAffliction); + GameMain.LuaCs.Hook.Call("character.death", this,causeOfDeathAffliction); if (HasAbilityFlag(AbilityFlags.RetainExperienceForNewCharacter)) { diff --git a/Barotrauma/BarotraumaServer/ServerSource/Networking/GameServer.cs b/Barotrauma/BarotraumaServer/ServerSource/Networking/GameServer.cs index de81f0364..3e515684a 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Networking/GameServer.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Networking/GameServer.cs @@ -293,7 +293,7 @@ namespace Barotrauma.Networking SendConsoleMessage("Granted all permissions to " + newClient.Name + ".", newClient); } - GameMain.LuaCs.Hook.Call("clientConnected", newClient); + GameMain.LuaCs.Hook.Call("client.connected", newClient); SendChatMessage($"ServerMessage.JoinedServer~[client]={clName}", ChatMessageType.Server, null, changeType: PlayerConnectionChangeType.Joined); @@ -2899,7 +2899,7 @@ namespace Barotrauma.Networking { if (client == null) return; - GameMain.LuaCs.Hook.Call("clientDisconnected", client); + GameMain.LuaCs.Hook.Call("client.disconnected", client); if (gameStarted && client.Character != null) { diff --git a/Barotrauma/BarotraumaShared/Lua/CompatibilityLib.lua b/Barotrauma/BarotraumaShared/Lua/CompatibilityLib.lua index c5a9dd100..47cb30631 100644 --- a/Barotrauma/BarotraumaShared/Lua/CompatibilityLib.lua +++ b/Barotrauma/BarotraumaShared/Lua/CompatibilityLib.lua @@ -76,4 +76,20 @@ end compatibilityLib["Player"] = luaPlayer +Hook.Add("character.created", "compatibility.character.created", function (character) + Hook.Call("characterCreated", character) +end) + +Hook.Add("character.death", "compatibility.character.death", function (character, causeOfDeathAffliction) + Hook.Call("characterDeath", character, causeOfDeathAffliction) +end) + +Hook.Add("client.connected", "compatibility.client.connected", function (client) + Hook.Call("clientConnected", client) +end) + +Hook.Add("client.disconnected", "compatibility.client.disconnected", function (client) + Hook.Call("clientDisconnected", client) +end) + return compatibilityLib \ No newline at end of file diff --git a/Barotrauma/BarotraumaShared/SharedSource/Characters/AI/Objectives/AIObjectiveManager.cs b/Barotrauma/BarotraumaShared/SharedSource/Characters/AI/Objectives/AIObjectiveManager.cs index d476ba3fb..2f365ebaa 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Characters/AI/Objectives/AIObjectiveManager.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Characters/AI/Objectives/AIObjectiveManager.cs @@ -80,7 +80,7 @@ namespace Barotrauma public void AddObjective(T objective) where T : AIObjective { - var result = GameMain.LuaCs.Hook.Call("AI.AddObjective", this, objective); + var result = GameMain.LuaCs.Hook.Call("AI.addObjective", this, objective); if (result != null && result.Value) return; diff --git a/Barotrauma/BarotraumaShared/SharedSource/Characters/Character.cs b/Barotrauma/BarotraumaShared/SharedSource/Characters/Character.cs index 47c6469c8..650b8e82d 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Characters/Character.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Characters/Character.cs @@ -1043,7 +1043,7 @@ namespace Barotrauma } #endif - GameMain.LuaCs.Hook.Call("characterCreated", new object[] { newCharacter }); + GameMain.LuaCs.Hook.Call("character.created", new object[] { newCharacter }); return newCharacter; } @@ -1441,6 +1441,8 @@ namespace Barotrauma { if (info?.Job == null) { return; } info.Job.GiveJobItems(this, spawnPoint); + + GameMain.LuaCs.Hook.Call("character.giveJobItems", this, spawnPoint); } public void GiveIdCardTags(WayPoint spawnPoint, bool createNetworkEvent = false) diff --git a/docs/lua/Hooks.lua b/docs/lua/Hooks.lua index 3d99e6262..79e5709b8 100644 --- a/docs/lua/Hooks.lua +++ b/docs/lua/Hooks.lua @@ -25,7 +25,7 @@ function Hook.Add(eventName, hookName, func) end -- @tparam string hookname hook name -- @realm shared -- @usage --- Hook.Remove("characterDeath", "characterDeathExample") +-- Hook.Remove("character.death", "characterDeathExample") function Hook.Remove(eventName, hookName) end --- Calls a hook. @@ -34,7 +34,7 @@ function Hook.Remove(eventName, hookName) end -- @realm shared -- @usage -- Hook.Add("think", "happyDebuggingSuckers", function() --- Hook.Call("characterDead", {}) -- ruin someone's day +-- Hook.Call("character.death", {}) -- ruin someone's day -- end) function Hook.Call(eventName, parameters) end @@ -62,12 +62,13 @@ function chatMessage(message, sender) end --- Called when a client connects -- @tparam client connectedClient -- @realm shared -function clientConnected(connectedClient) end +function client.connected(connectedClient) end --- Called when a client disconnects -- @tparam client disconnectedClient -- @realm shared -function clientDisconnected(disconnectedClient) end +function client.disconnected(disconnectedClient) end + --- Called on round start -- @realm shared @@ -77,23 +78,35 @@ function roundStart() end -- @realm shared function roundEnd() end ---- Gets callled everytime a character is created. +--- Gets called everytime a character is created. -- @tparam character createdCharacter -- @realm shared -function characterCreated(createdCharacter) end +function character.created(createdCharacter) end + +--- Gets called after the character is given the job items, useful if your script only wants to check for newly created characters in campaign gamemode. +-- @tparam character character +-- @tparam WayPoint waypoint +-- @realm shared +function character.giveJobItems(character, waypoint) end --- Gets called everytime a Character dies. -- @tparam Character character A dead Character. -- @realm shared -- @usage --- Hook.Add("characterDeath", "characterDeathExample", function(character) +-- Hook.Add("character.death", "characterDeathExample", function(character) -- print(character) -- end) -function characterDeath(character) end +function character.death(character) end ---- Gets called every time an affliction is applied. +--- Gets called every time an affliction is applied to a character. -- @realm shared -function afflictionApplied(affliction, characterHealth, limb) end +function character.applyAffliction(character, limbHealth, newAffliction, allowStacking) end + +--- Gets gets called every time an attack damage. +-- @realm shared +function character.applyDamage(character, attackResult, hitLimb, allowStacking) end + + --- Gets called every time an affliction updates. -- @realm shared function afflictionUpdate(affliction, characterHealth, limb) end @@ -125,7 +138,11 @@ function item.interact(item, characterPicker, ignoreRequiredItemsBool, forceSele --- Gets called every time two items are combined, eg: drag an half empty magazine to another half empty magazine to combine -- @realm shared -function item.combine(item, otherItem, userCharacter) end +function item.combine(item, deconstructor, characterUser, allowRemove) end + +--- Gets called every time an item is deconstructed. Return true to prevent item from being removed. +-- @realm shared +function item.deconstructed(item, otherItem, userCharacter) end --- Gets called every time an item is moved from one inventory slot to another, return true to cancel -- @realm shared