diff --git a/AriasServerUtils/ASUModSystem.cs b/AriasServerUtils/ASUModSystem.cs index c92644c..6ffab7c 100644 --- a/AriasServerUtils/ASUModSystem.cs +++ b/AriasServerUtils/ASUModSystem.cs @@ -20,6 +20,8 @@ namespace AriasServerUtils internal static Dictionary mPlayerData = new Dictionary(); + internal static BackCaches backCaches = new BackCaches(); + internal static Warps serverWarps = new Warps(); @@ -87,7 +89,7 @@ namespace AriasServerUtils api.ChatCommands.Create("sethome").RequiresPlayer().WithArgs(parsers.OptionalWord("name")).WithDescription("Creates a home").RequiresPrivilege(Privilege.chat).HandleWith(Events.HandleSetHome); api.ChatCommands.Create("home").RequiresPlayer().WithArgs(parsers.OptionalWord("name")).WithDescription("Teleports you to home").RequiresPrivilege(Privilege.chat).HandleWith(Events.HandleGoHome); api.ChatCommands.Create("delhome").RequiresPlayer().WithArgs(parsers.OptionalWord("name")).WithDescription("Deletes a home entry").RequiresPrivilege(Privilege.chat).HandleWith(Events.HandleDelHome); - api.ChatCommands.Create("homes").RequiresPlayer().WithDescription("Lists your homes").RequiresPrivilege(Privilege.controlserver).HandleWith(Events.HandleListHomes); + api.ChatCommands.Create("homes").RequiresPlayer().WithDescription("Lists your homes").RequiresPrivilege(Privilege.chat).HandleWith(Events.HandleListHomes); api.ChatCommands.Create("asu") .RequiresPrivilege(Privilege.chat) @@ -118,6 +120,14 @@ namespace AriasServerUtils .WithDescription("DANGER: This updates the flag allowing anybody to create warps, even non-admins. It is recommended to leave this alone. The default is true/on/yes") .HandleWith(Events.HandleUpdateASUMgrWarps) .EndSubCommand() + .BeginSubCommand("maxback") + .RequiresPrivilege(Privilege.controlserver) + .WithArgs( + parsers.OptionalInt("max") + ) + .WithDescription("Max number of back positions cached for players") + .HandleWith(Events.HandleUpdateASUMaxBack) + .EndSubCommand() .EndSubCommand() .BeginSubCommand("help") .RequiresPrivilege(Privilege.chat) @@ -129,6 +139,13 @@ namespace AriasServerUtils api.ChatCommands.Create("warp").RequiresPlayer().RequiresPrivilege(Privilege.chat).WithDescription("Warp to the specified server warp").WithArgs(parsers.OptionalWord("name")).HandleWith(Events.HandleWarp); api.ChatCommands.Create("delwarp").RequiresPlayer().RequiresPrivilege(Privilege.chat).WithDescription("Deletes the specified warp").WithArgs(parsers.OptionalWord("name")).HandleWith(Events.HandleWarpDelete); api.ChatCommands.Create("warps").RequiresPlayer().RequiresPrivilege(Privilege.chat).WithDescription("Lists all server warps").HandleWith(Events.HandleWarpList); + + api.ChatCommands.Create("back").RequiresPlayer().RequiresPrivilege(Privilege.chat).WithDescription("Returns you to the last location you were at").HandleWith(Events.HandleBack); + } + + public static void NewBackCacheForPlayer(IServerPlayer player) + { + backCaches.AddPosition(player.PlayerName, PlayerPosition.from(player.Entity)); } private void OnPlayerDC(IServerPlayer byPlayer) @@ -172,6 +189,8 @@ namespace AriasServerUtils PlayerInventory inv = new PlayerInventory(); var invMgr = player.InventoryManager; + NewBackCacheForPlayer(player); + var iBackpackSlotNum = 0; foreach (var type in saveInvTypes) diff --git a/AriasServerUtils/EventHandler.cs b/AriasServerUtils/EventHandler.cs index 01f300f..1e75408 100644 --- a/AriasServerUtils/EventHandler.cs +++ b/AriasServerUtils/EventHandler.cs @@ -12,7 +12,23 @@ namespace AriasServerUtils { internal static TextCommandResult HandleASU(TextCommandCallingArgs args) { - return TextCommandResult.Success(Lang.Get($"{ServerUtilities.MOD_ID}:help", ServerUtilities.config.MaxHomes, ServerUtilities.config.AdminsBypassMaxHomes, string.Join(", ", new string[] { "setspawn", "spawn", "delspawn", "sethome", "home", "delhome", "homes", "restoreinv", "asu", "warp", "setwarp", "delwarp", "warps" }))); + return TextCommandResult.Success(Lang.Get($"{ServerUtilities.MOD_ID}:help", ServerUtilities.config.MaxHomes, ServerUtilities.config.AdminsBypassMaxHomes, ServerUtilities.config.MaxBackCache, string.Join(", ", new string[] { "setspawn", "spawn", "delspawn", "sethome", "home", "delhome", "homes", "restoreinv", "asu", "warp", "setwarp", "delwarp", "warps", "back" }))); + } + + internal static TextCommandResult HandleBack(TextCommandCallingArgs args) + { + PlayerPosition pos = ServerUtilities.backCaches.ReadAndPopNewestPosition(args.Caller.Player.PlayerName); + if (pos == null) + { + return TextCommandResult.Success(Lang.Get($"{ServerUtilities.MOD_ID}:back-no")); + } + else + { + // Go back to old position + pos.Merge(args.Caller.Player.Entity); + return TextCommandResult.Success(Lang.Get($"{ServerUtilities.MOD_ID}:back")); + } + } internal static TextCommandResult HandleClearSpawn(TextCommandCallingArgs args) @@ -63,6 +79,7 @@ namespace AriasServerUtils PlayerStorage data = ServerUtilities.GetPlayerData(isp); if (data.Homes.ContainsKey(homeName)) { + ServerUtilities.NewBackCacheForPlayer(isp); data.Homes[homeName].Location.Merge(isp.Entity); ServerUtilities.SendMessageTo(isp, Lang.Get($"{ServerUtilities.MOD_ID}:home-tp")); @@ -177,6 +194,7 @@ namespace AriasServerUtils if (args.Caller.Player is IServerPlayer isp) { ServerUtilities.SendMessageTo(isp, Lang.Get($"{ServerUtilities.MOD_ID}:tp-spawn")); + ServerUtilities.NewBackCacheForPlayer(isp); ServerUtilities.config.Spawn.Merge(args.Caller.Player.Entity); } @@ -188,7 +206,7 @@ namespace AriasServerUtils internal static TextCommandResult HandleUpdateASUBypass(TextCommandCallingArgs args) { - if (args[1] is bool bypass) + if (args[0] is bool bypass) { ServerUtilities.config.AdminsBypassMaxHomes = bypass; ServerUtilities.MarkDirty(); @@ -199,6 +217,19 @@ namespace AriasServerUtils } + internal static TextCommandResult HandleUpdateASUMaxBack(TextCommandCallingArgs args) + { + if (args[0] is int max) + { + ServerUtilities.config.MaxBackCache = max; + ServerUtilities.MarkDirty(); + + return TextCommandResult.Success(Lang.Get($"{ServerUtilities.MOD_ID}:updatedconfig")); + } + + return TextCommandResult.Success(); + } + internal static TextCommandResult HandleUpdateASUMaxHomes(TextCommandCallingArgs args) { if (args[0] is int maxHomes) @@ -231,7 +262,7 @@ namespace AriasServerUtils { if (ServerUtilities.serverWarps.warps.ContainsKey(name)) { - + ServerUtilities.NewBackCacheForPlayer(isp); ServerUtilities.serverWarps.warps[name].Location.Merge(isp.Entity); ServerUtilities.SendMessageTo(isp, Lang.Get($"{ServerUtilities.MOD_ID}:warp-set", name)); diff --git a/AriasServerUtils/Globals.cs b/AriasServerUtils/Globals.cs index 8d8f85e..df5a653 100644 --- a/AriasServerUtils/Globals.cs +++ b/AriasServerUtils/Globals.cs @@ -16,6 +16,7 @@ namespace AriasServerUtils public int MaxHomes { get; set; } = 20; public bool AdminsBypassMaxHomes { get; set; } = true; public bool onlyAdminsCreateWarps { get; set; } = true; + public int MaxBackCache { get; set; } = 10; public PlayerPosition Spawn { get; set; } @@ -45,4 +46,39 @@ namespace AriasServerUtils { public Dictionary warps = new Dictionary(); } + + + public class BackCaches + { + private const int MaxCacheSize = 10; + private readonly Dictionary> playerCache = new(); + + public void AddPosition(string playerName, PlayerPosition position) + { + if (!playerCache.TryGetValue(playerName, out var positions)) + { + positions = new LinkedList(); + playerCache[playerName] = positions; + } + + if (positions.Count >= MaxCacheSize) + { + positions.RemoveFirst(); // Remove the oldest position + } + + positions.AddLast(position.Clone()); // Add the new position + } + + public PlayerPosition ReadAndPopNewestPosition(string playerName) + { + if (playerCache.TryGetValue(playerName, out var positions) && positions.Count > 0) + { + var newestPosition = positions.Last.Value; + positions.RemoveLast(); // Remove the newest position + return newestPosition; + } + + return null; // Return null if no positions are available + } + } } \ No newline at end of file diff --git a/AriasServerUtils/assets/ariasserverutils/lang/en.json b/AriasServerUtils/assets/ariasserverutils/lang/en.json index 49bdb1d..7116583 100644 --- a/AriasServerUtils/assets/ariasserverutils/lang/en.json +++ b/AriasServerUtils/assets/ariasserverutils/lang/en.json @@ -18,7 +18,7 @@ "home-del": "Home deleted", "home-list": "You have [{0}] home(s)\n\n{1}", - "help": "All Aria's Server Utilities Commands: \n\nMax Homes: {0}; Admins can bypass max homes: {1}\n{2}", + "help": "All Aria's Server Utilities Commands: \n\nMax Homes: {0}; \nAdmins can bypass max homes: {1}\nMax back positions: {2}\n\n{3}", "updatedconfig": "[ASU] server config updated", @@ -27,5 +27,8 @@ "warp-no": "You lack permissions to manage a warp", "warp-fail": "Warp [{0}] does not exist", "warp-del": "Warp [{0}] deleted", - "warp-list": "There are [{0}] total warps\n\n{1}" + "warp-list": "There are [{0}] total warps\n\n{1}", + + "back-no": "There's no position to go back to", + "back": "You've been taken back to your last position" } diff --git a/AriasServerUtils/modinfo.json b/AriasServerUtils/modinfo.json index 5821fc5..ef9e6f3 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 @ 04:14 PM", - "version": "1.0.2", + "description": "A collection of server utilities\n\nBuild Date: 01-18-2025 @ 09:22 PM", + "version": "1.0.3", "dependencies": { "game": "" }