From 7354f59533d30e599f2548044d748e23ca6dd37f Mon Sep 17 00:00:00 2001 From: WhiteFang Date: Thu, 4 Aug 2022 19:12:11 +0200 Subject: [PATCH] Refactored the Command System so it makes use of the VModCharacter --- BloodRefill/Systems/BloodRefillSystem.cs | 12 ++++---- .../Systems/PvPLeaderboardSystem.cs | 10 +++---- PvPPunishment/Systems/PvPPunishmentSystem.cs | 8 +++--- Shared/CommandSystem/Command.cs | 13 +++------ Shared/CommandSystem/CommandExtensions.cs | 11 ++------ Shared/CommandSystem/CommandSystem.cs | 28 +++++++++---------- .../HighestGearScoreSystem.cs | 6 ++-- Shared/VModCharacter.cs | 12 ++++++++ Shared/VModStorage.cs | 2 +- 9 files changed, 52 insertions(+), 50 deletions(-) diff --git a/BloodRefill/Systems/BloodRefillSystem.cs b/BloodRefill/Systems/BloodRefillSystem.cs index 0e7ff69..ac6cf30 100644 --- a/BloodRefill/Systems/BloodRefillSystem.cs +++ b/BloodRefill/Systems/BloodRefillSystem.cs @@ -185,7 +185,7 @@ private static void ChangeBloodType(User user, BloodType bloodType, float qualit [Command("setblood", "setblood []", "Sets your blood type to the specified blood-type and blood-quality, and optionally adds a given amount of blood (in Litres).", true)] private static void OnSetBloodCommand(Command command) { - var user = command.User; + var vmodCharacter = command.VModCharacter; var argCount = command.Args.Length; if(argCount >= 2) { @@ -206,7 +206,7 @@ private static void OnSetBloodCommand(Command command) } else { - user.SendSystemMessage($"Invalid gain-amount '{searchBloodQuality}'. Should be between -10 and 10"); + vmodCharacter.SendSystemMessage($"Invalid gain-amount '{searchBloodQuality}'. Should be between -10 and 10"); } } else @@ -216,18 +216,18 @@ private static void OnSetBloodCommand(Command command) if(addBloodAmount.HasValue) { - ChangeBloodType(user, bloodType, bloodQuality, (int)(addBloodAmount.Value * 10f)); - user.SendSystemMessage($"Changed blood type to {bloodQuality}% {searchBloodType} and added {addBloodAmount.Value}L"); + ChangeBloodType(vmodCharacter.User, bloodType, bloodQuality, (int)(addBloodAmount.Value * 10f)); + vmodCharacter.SendSystemMessage($"Changed blood type to {bloodQuality}% {searchBloodType} and added {addBloodAmount.Value}L"); } } else { - user.SendSystemMessage($"Invalid blood-quality '{searchBloodQuality}'. Should be between 1 and 100"); + vmodCharacter.SendSystemMessage($"Invalid blood-quality '{searchBloodQuality}'. Should be between 1 and 100"); } } else { - user.SendSystemMessage($"Invalid blood-type '{searchBloodType}'. Options are: {string.Join(", ", validBloodTypes.Select(x => x.ToString()))}"); + vmodCharacter.SendSystemMessage($"Invalid blood-type '{searchBloodType}'. Options are: {string.Join(", ", validBloodTypes.Select(x => x.ToString()))}"); } } else diff --git a/PvPLeaderboard/Systems/PvPLeaderboardSystem.cs b/PvPLeaderboard/Systems/PvPLeaderboardSystem.cs index f962595..470f6c9 100644 --- a/PvPLeaderboard/Systems/PvPLeaderboardSystem.cs +++ b/PvPLeaderboard/Systems/PvPLeaderboardSystem.cs @@ -134,7 +134,7 @@ private static void OnPvPStatsCommand(Command command) pvpStats = new PvPStats(); _pvpStats[user.PlatformId] = pvpStats; } - command.User.SendSystemMessage($"{searchUsername} K/D: {pvpStats.KDRatio} [{pvpStats.Kills}/{pvpStats.Deaths}] - Rank {PvPLeaderboard.ToList().FindIndex(x => x.Key == user.PlatformId) + 1}"); + command.VModCharacter.SendSystemMessage($"{searchUsername} K/D: {pvpStats.KDRatio} [{pvpStats.Kills}/{pvpStats.Deaths}] - Rank {PvPLeaderboard.ToList().FindIndex(x => x.Key == user.PlatformId) + 1}"); } command.Use(); } @@ -153,17 +153,17 @@ private static void OnPvPLeaderboardCommand(Command command) var maxPage = (int)Math.Ceiling(_pvpStats.Count / (double)recordsPerPage); page = Math.Min(maxPage - 1, page); - var user = command.User; + var vmodCharacter = command.VModCharacter; var entityManager = VWorld.Server.EntityManager; var leaderboard = PvPLeaderboard.Skip(page * recordsPerPage).Take(recordsPerPage); - user.SendSystemMessage("========== PvP Leaderboard =========="); + vmodCharacter.SendSystemMessage("========== PvP Leaderboard =========="); int rank = (page * recordsPerPage) + 1; foreach((var platformId, var pvpStats) in leaderboard) { - user.SendSystemMessage($"{rank}. {Utils.GetCharacterName(platformId, entityManager)} : {pvpStats.KDRatio} [{pvpStats.Kills}/{pvpStats.Deaths}]"); + vmodCharacter.SendSystemMessage($"{rank}. {Utils.GetCharacterName(platformId, entityManager)} : {pvpStats.KDRatio} [{pvpStats.Kills}/{pvpStats.Deaths}]"); rank++; } - user.SendSystemMessage($"=============== {page + 1}/{maxPage} ==============="); + vmodCharacter.SendSystemMessage($"=============== {page + 1}/{maxPage} ==============="); command.Use(); } diff --git a/PvPPunishment/Systems/PvPPunishmentSystem.cs b/PvPPunishment/Systems/PvPPunishmentSystem.cs index c3d04c7..b1ded0b 100644 --- a/PvPPunishment/Systems/PvPPunishmentSystem.cs +++ b/PvPPunishment/Systems/PvPPunishmentSystem.cs @@ -231,11 +231,11 @@ private static void OnIsPunishedPlayerCommand(Command command) { if(vmodCharacter.Value.HasBuff(Utils.SevereGarlicDebuff, entityManager)) { - command.User.SendSystemMessage($"Vampire {searchUsername} is currently punished."); + command.VModCharacter.SendSystemMessage($"Vampire {searchUsername} is currently punished."); } else { - command.User.SendSystemMessage($"Vampire {searchUsername} isn't punished."); + command.VModCharacter.SendSystemMessage($"Vampire {searchUsername} isn't punished."); } } command.Use(); @@ -250,7 +250,7 @@ private static void OnPunishPlayerCommand(Command command) if(vmodCharacter.HasValue) { vmodCharacter.Value.ApplyBuff(Utils.SevereGarlicDebuff); - command.User.SendSystemMessage($"Vampire {searchUsername} has been punished."); + command.VModCharacter.SendSystemMessage($"Vampire {searchUsername} has been punished."); } command.Use(); } @@ -264,7 +264,7 @@ private static void OnUnPunishPlayerCommand(Command command) if(vmodCharacter.HasValue) { vmodCharacter.Value.RemoveBuff(Utils.SevereGarlicDebuff); - command.User.SendSystemMessage($"Vampire {searchUsername} has been un-punished."); + command.VModCharacter.SendSystemMessage($"Vampire {searchUsername} has been un-punished."); } command.Use(); } diff --git a/Shared/CommandSystem/Command.cs b/Shared/CommandSystem/Command.cs index 87a89b6..c876af5 100644 --- a/Shared/CommandSystem/Command.cs +++ b/Shared/CommandSystem/Command.cs @@ -1,7 +1,4 @@ -using ProjectM.Network; -using Unity.Entities; - -namespace VMods.Shared +namespace VMods.Shared { public class Command { @@ -10,9 +7,7 @@ public class Command public string Name { get; } public string[] Args { get; } - public User User { get; } - public Entity SenderUserEntity { get; } - public Entity SenderCharEntity { get; } + public VModCharacter VModCharacter { get; } public bool Used { get; private set; } @@ -20,8 +15,8 @@ public class Command #region Lifecycle - public Command(User user, Entity senderUserEntity, Entity senderCharEntity, string name, params string[] args) - => (User, SenderUserEntity, SenderCharEntity, Name, Args) = (user, senderUserEntity, senderCharEntity, name, args); + public Command(VModCharacter vmodCharacter, string name, params string[] args) + => (VModCharacter, Name, Args) = (vmodCharacter, name, args); #endregion diff --git a/Shared/CommandSystem/CommandExtensions.cs b/Shared/CommandSystem/CommandExtensions.cs index 54c4c60..f514502 100644 --- a/Shared/CommandSystem/CommandExtensions.cs +++ b/Shared/CommandSystem/CommandExtensions.cs @@ -20,20 +20,15 @@ public static (string searchUsername, VModCharacter? vmodCharacter) FindVModChar } else { - searchUsername = command.User.CharacterName.ToString(); - fromCharacter = command.ToVModCharacter(entityManager); + searchUsername = command.VModCharacter.User.CharacterName.ToString(); + fromCharacter = command.VModCharacter; } if(sendCannotBeFoundMessage && !fromCharacter.HasValue) { - command.User.SendSystemMessage($"[{Utils.PluginName}] Vampire {searchUsername} couldn't be found."); + command.VModCharacter.SendSystemMessage($"[{Utils.PluginName}] Vampire {searchUsername} couldn't be found."); } return (searchUsername, fromCharacter); } - - public static VModCharacter ToVModCharacter(this Command command, EntityManager? entityManager = null) - { - return new VModCharacter(command.SenderUserEntity, command.SenderCharEntity, entityManager); - } } } diff --git a/Shared/CommandSystem/CommandSystem.cs b/Shared/CommandSystem/CommandSystem.cs index afb708b..53f9adb 100644 --- a/Shared/CommandSystem/CommandSystem.cs +++ b/Shared/CommandSystem/CommandSystem.cs @@ -59,12 +59,12 @@ public static void UnregisterCommand(Action commandMethod, CommandAttri public static void SendInvalidCommandMessage(Command command, bool invalidArgument = false) { - SendInvalidCommandMessage(command.User, invalidArgument); + SendInvalidCommandMessage(command.VModCharacter, invalidArgument); } - public static void SendInvalidCommandMessage(User user, bool invalidArgument = false) + public static void SendInvalidCommandMessage(VModCharacter vmodCharacter, bool invalidArgument = false) { - user.SendSystemMessage($"Invalid command{(invalidArgument ? " argument" : string.Empty)}. Check {CommandSystemConfig.CommandSystemPrefix.Value}help [] for more information."); + vmodCharacter.SendSystemMessage($"Invalid command{(invalidArgument ? " argument" : string.Empty)}. Check {CommandSystemConfig.CommandSystemPrefix.Value}help [] for more information."); } #endregion @@ -115,7 +115,7 @@ private static void OnChatMessage(VChatEvent chatEvent) if(timeDiff.TotalSeconds < CommandSystemConfig.CommandSystemCommandCooldown.Value) { int waitTime = (int)Math.Ceiling(CommandSystemConfig.CommandSystemCommandCooldown.Value - timeDiff.TotalSeconds); - chatEvent.User.SendSystemMessage($"Please wait for {waitTime} second(s) before sending another command."); + user.SendSystemMessage($"Please wait for {waitTime} second(s) before sending another command."); chatEvent.Cancel(); return; } @@ -124,7 +124,7 @@ private static void OnChatMessage(VChatEvent chatEvent) } // Fire the command (so an event handler can actually handle/execute it) - Command command = new(user, chatEvent.SenderUserEntity, chatEvent.SenderCharacterEntity, name, args); + Command command = new(new VModCharacter(chatEvent.SenderUserEntity, chatEvent.SenderCharacterEntity), name, args); foreach((var method, var attribute) in _commandMethods) { @@ -196,19 +196,19 @@ private static void PruneCommandTimes() private static void OnHelpCommand(Command command) { var commandPrefix = CommandSystemConfig.CommandSystemPrefix.Value; - var user = command.User; + var vmodCharacter = command.VModCharacter; switch(command.Args.Length) { case 0: { - user.SendSystemMessage($"List of {Utils.PluginName} commands:"); + vmodCharacter.SendSystemMessage($"List of {Utils.PluginName} commands:"); _commandMethods.ForEach(x => SendCommandInfo(x.attribute)); _commandReflectionMethods.ForEach(x => SendCommandInfo(x.attribute)); // Nested Method(s) void SendCommandInfo(CommandAttribute attribute) { - if(attribute.ReqAdmin && !user.IsAdmin) + if(attribute.ReqAdmin && !vmodCharacter.IsAdmin) { return; } @@ -218,7 +218,7 @@ void SendCommandInfo(CommandAttribute attribute) message += " - [ADMIN]"; } message += $" - {attribute.Description}"; - user.SendSystemMessage(message); + vmodCharacter.SendSystemMessage(message); } } break; @@ -239,18 +239,18 @@ void SendCommandInfo(CommandAttribute attribute) } // Check the found info - if(attribute == null || attribute.ReqAdmin && !user.IsAdmin) + if(attribute == null || attribute.ReqAdmin && !vmodCharacter.IsAdmin) { return; } - user.SendSystemMessage($"Help for {commandPrefix}{attribute.Names[0]}"); + vmodCharacter.SendSystemMessage($"Help for {commandPrefix}{attribute.Names[0]}"); if(attribute.Names.Count > 1) { - user.SendSystemMessage($"Aliases: {string.Join(", ", attribute.Names.Skip(1).Select(x => $"{commandPrefix}{x}"))}"); + vmodCharacter.SendSystemMessage($"Aliases: {string.Join(", ", attribute.Names.Skip(1).Select(x => $"{commandPrefix}{x}"))}"); } - user.SendSystemMessage($"Description: {attribute.Description}"); - user.SendSystemMessage($"Usage: {commandPrefix}{attribute.Usage}"); + vmodCharacter.SendSystemMessage($"Description: {attribute.Description}"); + vmodCharacter.SendSystemMessage($"Usage: {commandPrefix}{attribute.Usage}"); } return; diff --git a/Shared/HighestGearScoreSystem/HighestGearScoreSystem.cs b/Shared/HighestGearScoreSystem/HighestGearScoreSystem.cs index 8914242..1913346 100644 --- a/Shared/HighestGearScoreSystem/HighestGearScoreSystem.cs +++ b/Shared/HighestGearScoreSystem/HighestGearScoreSystem.cs @@ -152,11 +152,11 @@ private static void OnHighestGearScoreCommand(Command command) if(_gearScoreData.TryGetValue(user.PlatformId, out var gearScoreData)) { TimeSpan diff = DateTime.UtcNow.Subtract(gearScoreData.LastUpdated); - command.User.SendSystemMessage($"[{Utils.PluginName}] The Highest Gear Score for {searchUsername} (Lv: {GetCurrentGearScore(vmodCharacter.Value, entityManager)}) was {gearScoreData.HighestGearScore} (Last updated {diff.ToAgoString()} ago)."); + command.VModCharacter.SendSystemMessage($"[{Utils.PluginName}] The Highest Gear Score for {searchUsername} (Lv: {GetCurrentGearScore(vmodCharacter.Value, entityManager)}) was {gearScoreData.HighestGearScore} (Last updated {diff.ToAgoString()} ago)."); } else { - command.User.SendSystemMessage($"[{Utils.PluginName}] No Highest Gear Score is recorded for {searchUsername} (Lv: {GetCurrentGearScore(vmodCharacter.Value, entityManager)})."); + command.VModCharacter.SendSystemMessage($"[{Utils.PluginName}] No Highest Gear Score is recorded for {searchUsername} (Lv: {GetCurrentGearScore(vmodCharacter.Value, entityManager)})."); } } } @@ -171,7 +171,7 @@ private static void OnResetHighestGearScoreCommand(Command command) { var user = vmodCharacter.Value.User; _gearScoreData.Remove(user.PlatformId); - command.User.SendSystemMessage($"[{Utils.PluginName}] Removed the Highest Gear Score record for {searchUsername}."); + command.VModCharacter.SendSystemMessage($"[{Utils.PluginName}] Removed the Highest Gear Score record for {searchUsername}."); } } diff --git a/Shared/VModCharacter.cs b/Shared/VModCharacter.cs index e8ab8ec..2e4f36d 100644 --- a/Shared/VModCharacter.cs +++ b/Shared/VModCharacter.cs @@ -2,6 +2,7 @@ using ProjectM.Network; using Unity.Collections; using Unity.Entities; +using Wetstone.API; namespace VMods.Shared { @@ -15,6 +16,12 @@ public readonly struct VModCharacter #endregion + #region Properties + + public bool IsAdmin => User.IsAdmin; + + #endregion + #region Lifecycle public VModCharacter(User user, PlayerCharacter character) => (User, Character, FromCharacter) = (user, character, FromCharacter = new FromCharacter() @@ -91,6 +98,11 @@ public bool HasBuff(PrefabGUID buffGUID, EntityManager? entityManager = null) return BuffUtility.HasBuff(entityManager.Value, FromCharacter.Character, buffGUID); } + public void SendSystemMessage(string message) + { + User.SendSystemMessage(message); + } + #endregion } } diff --git a/Shared/VModStorage.cs b/Shared/VModStorage.cs index 853142c..cd8ecd5 100644 --- a/Shared/VModStorage.cs +++ b/Shared/VModStorage.cs @@ -77,7 +77,7 @@ public static T Load(string filename, Func getDefaultValue) private static void OnSaveAllCommand(Command command) { SaveAll(); - command.User.SendSystemMessage($"VMod Plugin '{Utils.PluginName}' saved successfully."); + command.VModCharacter.SendSystemMessage($"VMod Plugin '{Utils.PluginName}' saved successfully."); } #endregion