generated from AriasCreations/vsmodtemplate
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:
parent
0f318a9efa
commit
5234415034
5 changed files with 83 additions and 3 deletions
|
@ -1,4 +1,5 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using Vintagestory.API.Client;
|
using Vintagestory.API.Client;
|
||||||
using Vintagestory.API.Common;
|
using Vintagestory.API.Common;
|
||||||
|
@ -15,6 +16,15 @@ namespace AriasServerUtils
|
||||||
public static ASUModConfig config = new ASUModConfig();
|
public static ASUModConfig config = new ASUModConfig();
|
||||||
private static ICoreServerAPI API;
|
private static ICoreServerAPI API;
|
||||||
private static bool bDirty = false;
|
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>
|
/// <summary>
|
||||||
/// Method to register all mod blocks
|
/// Method to register all mod blocks
|
||||||
|
@ -53,10 +63,45 @@ namespace AriasServerUtils
|
||||||
api.Event.ServerRunPhase(EnumServerRunPhase.GameReady, OnGameReady);
|
api.Event.ServerRunPhase(EnumServerRunPhase.GameReady, OnGameReady);
|
||||||
api.Event.ServerRunPhase(EnumServerRunPhase.Shutdown, OnShutdown);
|
api.Event.ServerRunPhase(EnumServerRunPhase.Shutdown, OnShutdown);
|
||||||
api.Event.Timer(OnCheckModDirty, 20);
|
api.Event.Timer(OnCheckModDirty, 20);
|
||||||
|
api.Event.PlayerDeath += OnPlayerDeath;
|
||||||
|
|
||||||
api.ChatCommands.Create("setspawn").RequiresPrivilege(Privilege.controlserver).HandleWith(Events.HandleSetSpawn);
|
api.ChatCommands.Create("setspawn").RequiresPrivilege(Privilege.controlserver).HandleWith(Events.HandleSetSpawn);
|
||||||
api.ChatCommands.Create("spawn").RequiresPrivilege(Privilege.chat).HandleWith(Events.HandleSpawn);
|
api.ChatCommands.Create("spawn").RequiresPrivilege(Privilege.chat).HandleWith(Events.HandleSpawn);
|
||||||
api.ChatCommands.Create("delspawn").RequiresPrivilege(Privilege.controlserver).HandleWith(Events.HandleClearSpawn);
|
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()
|
private void OnCheckModDirty()
|
||||||
|
@ -73,6 +118,7 @@ namespace AriasServerUtils
|
||||||
{
|
{
|
||||||
// Mod Shutdown //
|
// Mod Shutdown //
|
||||||
// Handle any remaining tasks before shutdown
|
// Handle any remaining tasks before shutdown
|
||||||
|
API.Logger.Notification(Lang.Get($"{MOD_ID}:halt"));
|
||||||
OnCheckModDirty();
|
OnCheckModDirty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,6 +15,29 @@ namespace AriasServerUtils
|
||||||
return TextCommandResult.Success(Lang.Get($"{ServerUtilities.MOD_ID}:rmspawn"));
|
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)
|
internal static TextCommandResult HandleSetSpawn(TextCommandCallingArgs args)
|
||||||
{
|
{
|
||||||
PlayerPosition pos = PlayerPosition.from(args.Caller.Entity);
|
PlayerPosition pos = PlayerPosition.from(args.Caller.Entity);
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using Vintagestory.API.Common;
|
||||||
using Vintagestory.API.Common.Entities;
|
using Vintagestory.API.Common.Entities;
|
||||||
using Vintagestory.API.MathTools;
|
using Vintagestory.API.MathTools;
|
||||||
|
using Vintagestory.GameContent;
|
||||||
|
|
||||||
namespace AriasServerUtils
|
namespace AriasServerUtils
|
||||||
{
|
{
|
||||||
|
@ -42,4 +45,9 @@ namespace AriasServerUtils
|
||||||
return pos;
|
return pos;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class PlayerInventory
|
||||||
|
{
|
||||||
|
public List<ItemStack> Items = new List<ItemStack>();
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -4,5 +4,8 @@
|
||||||
|
|
||||||
"spawn-not-set": "A world spawn location is not currently set",
|
"spawn-not-set": "A world spawn location is not currently set",
|
||||||
"tp-spawn": "Teleported to spawn",
|
"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.."
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,8 +3,8 @@
|
||||||
"modid": "ariasserverutils",
|
"modid": "ariasserverutils",
|
||||||
"name": "Aria's Server Utilities",
|
"name": "Aria's Server Utilities",
|
||||||
"authors": ["zontreck"],
|
"authors": ["zontreck"],
|
||||||
"description": "A collection of server utilities\n\nBuild Date: 01-18-2025 @ 02:58 AM",
|
"description": "A collection of server utilities\n\nBuild Date: 01-18-2025 @ 04:23 AM",
|
||||||
"version": "1.0.0-dev.2",
|
"version": "1.0.0-dev.3",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"game": ""
|
"game": ""
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue