Begin to add a death inventory backup system

TODO: only take the backpack slots not the backpack contents; also exclude the character's clothing
This commit is contained in:
zontreck 2025-01-18 04:25:10 -07:00
parent 0f318a9efa
commit 5234415034
5 changed files with 83 additions and 3 deletions

View file

@ -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<string, PlayerInventory> backupInventory = new Dictionary<string, PlayerInventory>();
internal static string[] saveInvTypes = new string[] {
GlobalConstants.hotBarInvClassName,
GlobalConstants.backpackInvClassName,
GlobalConstants.craftingInvClassName,
GlobalConstants.mousecursorInvClassName,
GlobalConstants.characterInvClassName
};
/// <summary>
/// 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();
}

View file

@ -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);

View file

@ -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<ItemStack> Items = new List<ItemStack>();
}
}

View file

@ -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.."
}

View file

@ -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": ""
}