diff --git a/AriasServerUtils/ASUModSystem.cs b/AriasServerUtils/ASUModSystem.cs index dd3405b..e5f5662 100644 --- a/AriasServerUtils/ASUModSystem.cs +++ b/AriasServerUtils/ASUModSystem.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.IO; using Vintagestory.API.Client; using Vintagestory.API.Common; @@ -15,6 +16,15 @@ namespace AriasServerUtils public static ASUModConfig config = new ASUModConfig(); private static ICoreServerAPI API; private static bool bDirty = false; + internal static Dictionary backupInventory = new Dictionary(); + + internal static string[] saveInvTypes = new string[] { + GlobalConstants.hotBarInvClassName, + GlobalConstants.backpackInvClassName, + GlobalConstants.craftingInvClassName, + GlobalConstants.mousecursorInvClassName, + GlobalConstants.characterInvClassName + }; /// /// Method to register all mod blocks @@ -53,10 +63,45 @@ namespace AriasServerUtils api.Event.ServerRunPhase(EnumServerRunPhase.GameReady, OnGameReady); api.Event.ServerRunPhase(EnumServerRunPhase.Shutdown, OnShutdown); api.Event.Timer(OnCheckModDirty, 20); + api.Event.PlayerDeath += OnPlayerDeath; api.ChatCommands.Create("setspawn").RequiresPrivilege(Privilege.controlserver).HandleWith(Events.HandleSetSpawn); api.ChatCommands.Create("spawn").RequiresPrivilege(Privilege.chat).HandleWith(Events.HandleSpawn); api.ChatCommands.Create("delspawn").RequiresPrivilege(Privilege.controlserver).HandleWith(Events.HandleClearSpawn); + + + api.ChatCommands.Create("test_death").RequiresPlayer().RequiresPrivilege(Privilege.controlserver).HandleWith(TestDeath); + var parsers = api.ChatCommands.Parsers; + api.ChatCommands.Create("restoreinv").RequiresPlayer().WithArgs(parsers.OnlinePlayer("player")).HandleWith(Events.HandleReturnItems).WithDescription("Returns items to a player in the event of a problem").RequiresPrivilege(Privilege.controlserver); + } + + private TextCommandResult TestDeath(TextCommandCallingArgs args) + { + if (args.Caller.Player is IServerPlayer isp) OnPlayerDeath(isp, null); + + return TextCommandResult.Success(); + } + + private void OnPlayerDeath(IServerPlayer player, DamageSource damageSource) + { + PlayerInventory inv = new PlayerInventory(); + var invMgr = player.InventoryManager; + + + + foreach (var type in saveInvTypes) + { + foreach (var stack in invMgr.GetOwnInventory(type)) + { + if (stack.Empty) continue; + inv.Items.Add(stack.Itemstack.Clone()); + + API.Logger.Notification($"SAVED STORAGE ITEM TYPE: {stack.Itemstack}"); + } + } + + backupInventory[player.PlayerName] = inv; + } private void OnCheckModDirty() @@ -73,6 +118,7 @@ namespace AriasServerUtils { // Mod Shutdown // // Handle any remaining tasks before shutdown + API.Logger.Notification(Lang.Get($"{MOD_ID}:halt")); OnCheckModDirty(); } diff --git a/AriasServerUtils/EventHandler.cs b/AriasServerUtils/EventHandler.cs index 9b07682..495b3b9 100644 --- a/AriasServerUtils/EventHandler.cs +++ b/AriasServerUtils/EventHandler.cs @@ -15,6 +15,29 @@ namespace AriasServerUtils return TextCommandResult.Success(Lang.Get($"{ServerUtilities.MOD_ID}:rmspawn")); } + internal static TextCommandResult HandleReturnItems(TextCommandCallingArgs args) + { + IPlayer player = args[0] as IPlayer; + if (player is IServerPlayer isp) + { + + if (ServerUtilities.backupInventory.ContainsKey(player.PlayerName)) + isp.InventoryManager.DiscardAll(); + else + { + ServerUtilities.SendMessageTo(isp, Lang.Get($"{ServerUtilities.MOD_ID}:nobackup")); + return TextCommandResult.Success(); + } + + foreach (var stack in ServerUtilities.backupInventory[player.PlayerName].Items) + { + isp.InventoryManager.TryGiveItemstack(stack.Clone()); + } + } + + return TextCommandResult.Success(); + } + internal static TextCommandResult HandleSetSpawn(TextCommandCallingArgs args) { PlayerPosition pos = PlayerPosition.from(args.Caller.Entity); diff --git a/AriasServerUtils/PlayerData.cs b/AriasServerUtils/PlayerData.cs index 4bc8108..9a98c40 100644 --- a/AriasServerUtils/PlayerData.cs +++ b/AriasServerUtils/PlayerData.cs @@ -1,6 +1,9 @@ using System; +using System.Collections.Generic; +using Vintagestory.API.Common; using Vintagestory.API.Common.Entities; using Vintagestory.API.MathTools; +using Vintagestory.GameContent; namespace AriasServerUtils { @@ -42,4 +45,9 @@ namespace AriasServerUtils return pos; } } + + public class PlayerInventory + { + public List Items = new List(); + } } \ No newline at end of file diff --git a/AriasServerUtils/assets/ariasserverutils/lang/en.json b/AriasServerUtils/assets/ariasserverutils/lang/en.json index 9152f36..a5aeea8 100644 --- a/AriasServerUtils/assets/ariasserverutils/lang/en.json +++ b/AriasServerUtils/assets/ariasserverutils/lang/en.json @@ -4,5 +4,8 @@ "spawn-not-set": "A world spawn location is not currently set", "tp-spawn": "Teleported to spawn", - "rmspawn": "World Spawn cleared. The /spawn command will now be non-functional until a new spawn is set" + "rmspawn": "World Spawn cleared. The /spawn command will now be non-functional until a new spawn is set", + + "halt": "Aria's Server Utils is shutting down, performing any last minute pending tasks...", + "nobackup": "No backup inventory exists, aborting restore.." } diff --git a/AriasServerUtils/modinfo.json b/AriasServerUtils/modinfo.json index 1b97d54..78d77da 100644 --- a/AriasServerUtils/modinfo.json +++ b/AriasServerUtils/modinfo.json @@ -3,8 +3,8 @@ "modid": "ariasserverutils", "name": "Aria's Server Utilities", "authors": ["zontreck"], - "description": "A collection of server utilities\n\nBuild Date: 01-18-2025 @ 02:58 AM", - "version": "1.0.0-dev.2", + "description": "A collection of server utilities\n\nBuild Date: 01-18-2025 @ 04:23 AM", + "version": "1.0.0-dev.3", "dependencies": { "game": "" }