From d5dc0d58923dc1eb460342d7c595703a2617857f Mon Sep 17 00:00:00 2001 From: zontreck Date: Sat, 18 Jan 2025 14:11:00 -0700 Subject: [PATCH] Adds homes --- AriasServerUtils/ASUModSystem.cs | 50 +++++++++++++ AriasServerUtils/EventHandler.cs | 74 +++++++++++++++++++ AriasServerUtils/PlayerData.cs | 51 ++++++++++++- .../assets/ariasserverutils/lang/en.json | 9 ++- AriasServerUtils/modinfo.json | 4 +- 5 files changed, 184 insertions(+), 4 deletions(-) diff --git a/AriasServerUtils/ASUModSystem.cs b/AriasServerUtils/ASUModSystem.cs index 531f551..74ba68e 100644 --- a/AriasServerUtils/ASUModSystem.cs +++ b/AriasServerUtils/ASUModSystem.cs @@ -18,6 +18,8 @@ namespace AriasServerUtils private static bool bDirty = false; internal static Dictionary backupInventory = new Dictionary(); + internal static Dictionary mPlayerData = new Dictionary(); + internal static string[] saveInvTypes = new string[] { GlobalConstants.hotBarInvClassName, GlobalConstants.backpackInvClassName, @@ -64,6 +66,11 @@ namespace AriasServerUtils api.Event.ServerRunPhase(EnumServerRunPhase.Shutdown, OnShutdown); api.Event.Timer(OnCheckModDirty, 20); api.Event.PlayerDeath += OnPlayerDeath; + api.Event.PlayerJoin += OnPlayerJoin; + api.Event.PlayerDisconnect += OnPlayerDC; + api.Event.PlayerLeave += OnPlayerDC; + + api.ChatCommands.Create("setspawn").RequiresPrivilege(Privilege.controlserver).HandleWith(Events.HandleSetSpawn); api.ChatCommands.Create("spawn").RequiresPrivilege(Privilege.chat).HandleWith(Events.HandleSpawn); @@ -73,6 +80,39 @@ namespace AriasServerUtils 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); + + 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); + } + + private void OnPlayerDC(IServerPlayer byPlayer) + { + OnCheckModDirty(); + + mPlayerData.Remove(byPlayer.PlayerName); + } + + private void OnPlayerJoin(IServerPlayer byPlayer) + { + API.Logger.Notification($"[ASU] {Lang.Get($"{MOD_ID}:playerjoin")}"); + + PlayerStorage data = API.LoadModConfig(GetConfigurationFile(byPlayer.PlayerName, ModConfigType.World)); + if (data == null) data = new PlayerStorage(); + + mPlayerData[byPlayer.PlayerName] = data; + } + + public static PlayerStorage GetPlayerData(IServerPlayer player) + { + if (mPlayerData.ContainsKey(player.PlayerName)) + { + return mPlayerData[player.PlayerName]; + } + else + { + return new PlayerStorage(); + } } private TextCommandResult TestDeath(TextCommandCallingArgs args) @@ -129,6 +169,16 @@ namespace AriasServerUtils //API.Logger.Notification(Lang.Get($"{MOD_ID}:timer")); bDirty = false; SaveGlobalConfig(); + + SavePlayerData(); + } + } + + private void SavePlayerData() + { + foreach (var data in mPlayerData) + { + API.StoreModConfig(data.Value, GetConfigurationFile(data.Key, ModConfigType.World)); } } diff --git a/AriasServerUtils/EventHandler.cs b/AriasServerUtils/EventHandler.cs index 495b3b9..cdb02f6 100644 --- a/AriasServerUtils/EventHandler.cs +++ b/AriasServerUtils/EventHandler.cs @@ -15,6 +15,59 @@ namespace AriasServerUtils return TextCommandResult.Success(Lang.Get($"{ServerUtilities.MOD_ID}:rmspawn")); } + internal static TextCommandResult HandleDelHome(TextCommandCallingArgs args) + { + string homeName = "default"; + if (args.ArgCount > 0) + { + homeName = args[0] as string; + } + + if (args.Caller.Player is IServerPlayer isp) + { + PlayerStorage data = ServerUtilities.GetPlayerData(isp); + if (data.Homes.ContainsKey(homeName)) + { + data.Homes.Remove(homeName); + + ServerUtilities.SendMessageTo(isp, Lang.Get($"{ServerUtilities.MOD_ID}:home-del")); + ServerUtilities.MarkDirty(); + } + else + { + ServerUtilities.SendMessageTo(isp, Lang.Get($"{ServerUtilities.MOD_ID}:home-no")); + } + } + + return TextCommandResult.Success(); + } + + internal static TextCommandResult HandleGoHome(TextCommandCallingArgs args) + { + string homeName = "default"; + if (args.ArgCount > 0) + { + homeName = args[0] as string; + } + + if (args.Caller.Player is IServerPlayer isp) + { + PlayerStorage data = ServerUtilities.GetPlayerData(isp); + if (data.Homes.ContainsKey(homeName)) + { + data.Homes[homeName].Location.Merge(isp.Entity); + + ServerUtilities.SendMessageTo(isp, Lang.Get($"{ServerUtilities.MOD_ID}:home-tp")); + } + else + { + ServerUtilities.SendMessageTo(isp, Lang.Get($"{ServerUtilities.MOD_ID}:home-no")); + } + } + + return TextCommandResult.Success(); + } + internal static TextCommandResult HandleReturnItems(TextCommandCallingArgs args) { IPlayer player = args[0] as IPlayer; @@ -38,6 +91,27 @@ namespace AriasServerUtils return TextCommandResult.Success(); } + internal static TextCommandResult HandleSetHome(TextCommandCallingArgs args) + { + string homeName = "default"; + if (args.ArgCount > 0) + { + homeName = args[0] as string; + } + + if (args.Caller.Player is IServerPlayer isp) + { + + var data = ServerUtilities.GetPlayerData(isp); + data.Homes[homeName] = Home.MakeHome(args.Caller.Player.Entity, homeName); + + ServerUtilities.SendMessageTo(isp, Lang.Get($"{ServerUtilities.MOD_ID}:home-set")); + ServerUtilities.MarkDirty(); + } + + 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 9a98c40..5a04966 100644 --- a/AriasServerUtils/PlayerData.cs +++ b/AriasServerUtils/PlayerData.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Runtime.ConstrainedExecution; using Vintagestory.API.Common; using Vintagestory.API.Common.Entities; using Vintagestory.API.MathTools; @@ -8,7 +9,7 @@ using Vintagestory.GameContent; namespace AriasServerUtils { [Serializable] - public class PlayerPosition + public class PlayerPosition : ICloneable { public int X { get; set; } = 0; public int Y { get; set; } = 0; @@ -16,12 +17,27 @@ namespace AriasServerUtils public int Dimension { get; set; } = 0; public float Yaw { get; set; } = 0.0f; + public float Pitch { get; set; } = 0.0f; + + public readonly PlayerPosition ZERO = new(0, 0, 0, 0, 0.0f, 0.0f); + + public PlayerPosition() { } + internal PlayerPosition(int x, int y, int z, int dim, float yaw, float pitch) + { + this.X = x; + this.Y = y; + this.Z = z; + this.Dimension = dim; + this.Yaw = yaw; + this.Pitch = pitch; + } public void Merge(Entity entity) { entity.TeleportTo(new BlockPos(X, Y, Z, Dimension)); entity.Pos.SetYaw(Yaw); + entity.Pos.Pitch = Pitch; } @@ -41,13 +57,46 @@ namespace AriasServerUtils pos.Dimension = playerPos.dimension; pos.Yaw = entity.Pos.Yaw; + pos.Pitch = entity.Pos.Pitch; return pos; } + + public object Clone() + { + return new PlayerPosition(this.X, this.Y, this.Z, this.Dimension, this.Yaw, this.Pitch); + } } public class PlayerInventory { public List Items = new List(); } + + /// + /// This class creates a simple storage for all memory within this mod + /// + [Serializable] + public class PlayerStorage + { + public Dictionary Homes = new Dictionary(); + } + + /// + /// This class represents a single home object + /// + [Serializable] + public class Home + { + public PlayerPosition Location { get; set; } + + public static Home MakeHome(Entity player, string homeName) + { + Home home = new Home(); + home.Location = PlayerPosition.from(player); + + return home; + + } + } } \ No newline at end of file diff --git a/AriasServerUtils/assets/ariasserverutils/lang/en.json b/AriasServerUtils/assets/ariasserverutils/lang/en.json index a5aeea8..a8c6a12 100644 --- a/AriasServerUtils/assets/ariasserverutils/lang/en.json +++ b/AriasServerUtils/assets/ariasserverutils/lang/en.json @@ -7,5 +7,12 @@ "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.." + "nobackup": "No backup inventory exists, aborting restore..", + + "playerjoin": "A player has joined the server, their data is being loaded, or created", + + "home-tp": "You have teleported home", + "home-no": "No such home exists", + "home-max": "You have reached your max number of homes", + "home-set": "Home saved" } diff --git a/AriasServerUtils/modinfo.json b/AriasServerUtils/modinfo.json index c91723e..2054eec 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 @ 01:00 PM", - "version": "1.0.0-dev.4", + "description": "A collection of server utilities\n\nBuild Date: 01-18-2025 @ 02:06 PM", + "version": "1.0.0-dev.5", "dependencies": { "game": "" }