From 64ed424a6b38da091b40eb411c7ee6a0e38866a7 Mon Sep 17 00:00:00 2001 From: Evil Factory <36804725+evilfactory@users.noreply.github.com> Date: Mon, 9 Aug 2021 23:43:38 -0300 Subject: [PATCH] new hooks and item removing --- .../ServerSource/Lua/LuaClasses.cs | 5 ++ .../ServerSource/Lua/LuaSetup.cs | 13 ++++++ .../Characters/Health/CharacterHealth.cs | 11 ++++- .../SharedSource/Items/Item.cs | 46 +++++++++++++++++++ 4 files changed, 74 insertions(+), 1 deletion(-) diff --git a/Barotrauma/BarotraumaServer/ServerSource/Lua/LuaClasses.cs b/Barotrauma/BarotraumaServer/ServerSource/Lua/LuaClasses.cs index c2891e30c..3e0d46da4 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Lua/LuaClasses.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Lua/LuaClasses.cs @@ -231,6 +231,11 @@ namespace Barotrauma return error; } + public static void RemoveItem(Item item) + { + EntitySpawner.Spawner.AddToRemoveQueue(item); + } + public static Submarine GetRespawnSub() { return GameMain.Server.RespawnManager.RespawnShuttle; diff --git a/Barotrauma/BarotraumaServer/ServerSource/Lua/LuaSetup.cs b/Barotrauma/BarotraumaServer/ServerSource/Lua/LuaSetup.cs index d65fd456b..a83f4162e 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Lua/LuaSetup.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Lua/LuaSetup.cs @@ -82,6 +82,15 @@ namespace Barotrauma } + public static DynValue CreateUserDataSafe(object o) + { + if(o == null) + return DynValue.Nil; + + return UserData.Create(o); + + } + public LuaSetup() { PrintMessage("Lua!"); @@ -124,6 +133,10 @@ namespace Barotrauma UserData.RegisterType(); UserData.RegisterType(); UserData.RegisterType(); + UserData.RegisterType(); + UserData.RegisterType(); + UserData.RegisterType(); + UserData.RegisterType(); lua = new Script(CoreModules.Preset_SoftSandbox | CoreModules.LoadMethods); diff --git a/Barotrauma/BarotraumaShared/SharedSource/Characters/Health/CharacterHealth.cs b/Barotrauma/BarotraumaShared/SharedSource/Characters/Health/CharacterHealth.cs index 25c2385ef..6cef4619a 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Characters/Health/CharacterHealth.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Characters/Health/CharacterHealth.cs @@ -410,7 +410,7 @@ namespace Barotrauma if (targetLimb == null) { #if SERVER - var should = GameMain.Lua.hook.Call("afflictionApplied", new DynValue[] { UserData.Create(this), UserData.Create(affliction) }); + var should = GameMain.Lua.hook.Call("afflictionApplied", new DynValue[] { LuaSetup.CreateUserDataSafe(this), LuaSetup.CreateUserDataSafe(affliction) }); if (should != null && should.CastToBool()) { @@ -526,6 +526,15 @@ namespace Barotrauma return; } +#if SERVER + var should = GameMain.Lua.hook.Call("afflictionApplied", new DynValue[] { LuaSetup.CreateUserDataSafe(this), LuaSetup.CreateUserDataSafe(attackResult), LuaSetup.CreateUserDataSafe(hitLimb) }); + + if (should != null && should.CastToBool()) + { + return; + } +#endif + foreach (Affliction newAffliction in attackResult.Afflictions) { if (newAffliction.Prefab.LimbSpecific) diff --git a/Barotrauma/BarotraumaShared/SharedSource/Items/Item.cs b/Barotrauma/BarotraumaShared/SharedSource/Items/Item.cs index ffb0d38cf..e11d20cdc 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Items/Item.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Items/Item.cs @@ -11,6 +11,7 @@ using System.Linq; using System.Xml.Linq; using Barotrauma.Extensions; using Barotrauma.MapCreatures.Behavior; +using MoonSharp.Interpreter; #if CLIENT using Microsoft.Xna.Framework.Graphics; @@ -2258,6 +2259,15 @@ namespace Barotrauma { if (condition == 0.0f) { return; } +#if SERVER + var should = GameMain.Lua.hook.Call("itemSecondaryUse", new DynValue[] { LuaSetup.CreateUserDataSafe(this), LuaSetup.CreateUserDataSafe(character)}); + + if (should != null && should.CastToBool()) + { + return; + } +#endif + bool remove = false; foreach (ItemComponent ic in components) @@ -2289,6 +2299,15 @@ namespace Barotrauma public void ApplyTreatment(Character user, Character character, Limb targetLimb) { +#if SERVER + var should = GameMain.Lua.hook.Call("itemApplyTreatment", new DynValue[] { LuaSetup.CreateUserDataSafe(this), LuaSetup.CreateUserDataSafe(user), LuaSetup.CreateUserDataSafe(character), LuaSetup.CreateUserDataSafe(targetLimb) }); + + if (should != null && should.CastToBool()) + { + return; + } +#endif + //can't apply treatment to dead characters if (character.IsDead) return; if (!UseInHealthInterface) return; @@ -2345,6 +2364,15 @@ namespace Barotrauma public void Drop(Character dropper, bool createNetworkEvent = true) { +#if SERVER + var should = GameMain.Lua.hook.Call("itemDrop", new DynValue[] { LuaSetup.CreateUserDataSafe(this), LuaSetup.CreateUserDataSafe(dropper)}); + + if (should != null && should.CastToBool()) + { + return; + } +#endif + if (createNetworkEvent) { if (parentInventory != null && !parentInventory.Owner.Removed && !Removed && @@ -2395,6 +2423,15 @@ namespace Barotrauma public void Equip(Character character) { +#if SERVER + var should = GameMain.Lua.hook.Call("itemEquip", new DynValue[] { LuaSetup.CreateUserDataSafe(this), LuaSetup.CreateUserDataSafe(character)}); + + if (should != null && should.CastToBool()) + { + return; + } +#endif + if (Removed) { DebugConsole.ThrowError($"Tried to equip a removed item ({Name}).\n{Environment.StackTrace.CleanupStackTrace()}"); @@ -2406,6 +2443,15 @@ namespace Barotrauma public void Unequip(Character character) { +#if SERVER + var should = GameMain.Lua.hook.Call("itemUnequip", new DynValue[] { LuaSetup.CreateUserDataSafe(this), LuaSetup.CreateUserDataSafe(character)}); + + if (should != null && should.CastToBool()) + { + return; + } +#endif + foreach (ItemComponent ic in components) { ic.Unequip(character); } }