diff --git a/PkmnLib.Dynamic/Libraries/DataLoaders/AbilityDataLoader.cs b/PkmnLib.Dynamic/Libraries/DataLoaders/AbilityDataLoader.cs index 24ea9d1..095ed97 100644 --- a/PkmnLib.Dynamic/Libraries/DataLoaders/AbilityDataLoader.cs +++ b/PkmnLib.Dynamic/Libraries/DataLoaders/AbilityDataLoader.cs @@ -55,7 +55,7 @@ public static class AbilityDataLoader var effect = serialized.Effect; var parameters = serialized.Parameters.ToDictionary(x => (StringKey)x.Key, x => x.Value.ToParameter()); - StringKey? effectName = effect == null ? null! : new StringKey(effect); + var effectName = string.IsNullOrWhiteSpace(effect) ? (StringKey?)null : new StringKey(effect); var flags = serialized.Flags.Select(x => new StringKey(x)).ToImmutableHashSet(); diff --git a/PkmnLib.Static/Utils/StringKey.cs b/PkmnLib.Static/Utils/StringKey.cs index f962706..ea30cd4 100644 --- a/PkmnLib.Static/Utils/StringKey.cs +++ b/PkmnLib.Static/Utils/StringKey.cs @@ -1,3 +1,5 @@ +using System.Diagnostics.CodeAnalysis; + namespace PkmnLib.Static.Utils; /// @@ -26,8 +28,18 @@ public readonly record struct StringKey /// /// Converts a to a . /// + [return: NotNullIfNotNull("key")] + public static implicit operator StringKey?(string? key) => + string.IsNullOrWhiteSpace(key) ? null! : new StringKey(key); + + /// + /// Converts a to a . + /// Throws an if the key is null or whitespace. + /// public static implicit operator StringKey(string key) => - string.IsNullOrWhiteSpace(key) ? default : new StringKey(key); + string.IsNullOrWhiteSpace(key) + ? throw new ArgumentException("Key cannot be null or whitespace.", nameof(key)) + : new StringKey(key); /// public override string ToString() => _key.ToLowerInvariant(); diff --git a/Plugins/PkmnLib.Plugin.Gen7.Tests/DataTests/AbilityDataTests.cs b/Plugins/PkmnLib.Plugin.Gen7.Tests/DataTests/AbilityDataTests.cs index 7cca405..8d7760c 100644 --- a/Plugins/PkmnLib.Plugin.Gen7.Tests/DataTests/AbilityDataTests.cs +++ b/Plugins/PkmnLib.Plugin.Gen7.Tests/DataTests/AbilityDataTests.cs @@ -24,7 +24,7 @@ public class AbilityDataTests } } - [Test, MethodDataSource(nameof(AllAbilitiesHaveValidScriptsData)), Explicit] + [Test, MethodDataSource(nameof(AllAbilitiesHaveValidScriptsData))] public async Task AllAbilitiesEffectsHaveValidScripts(TestCaseData test) { var scriptName = test.Ability.Effect; @@ -39,7 +39,7 @@ public class AbilityDataTests catch (Exception e) { // Helper method to find the line number of the effect in the JSON file - var file = Path.GetFullPath("../../../../Plugins/PkmnLib.Plugin.Gen7/Data/Abilities.json"); + var file = Path.GetFullPath("../../../../Plugins/PkmnLib.Plugin.Gen7/Data/Abilities.jsonc"); var json = await File.ReadAllLinesAsync(file); var moveLineNumber = json.Select((line, index) => new { line, index }) .FirstOrDefault(x => x.line.Contains($"\"name\": \"{test.Ability.Effect}\""))?.index + 1; diff --git a/Plugins/PkmnLib.Plugin.Gen7.Tests/Scripts/Moves/MultiAttackTests.cs b/Plugins/PkmnLib.Plugin.Gen7.Tests/Scripts/Moves/MultiAttackTests.cs index dfdcc46..cefd80e 100644 --- a/Plugins/PkmnLib.Plugin.Gen7.Tests/Scripts/Moves/MultiAttackTests.cs +++ b/Plugins/PkmnLib.Plugin.Gen7.Tests/Scripts/Moves/MultiAttackTests.cs @@ -72,7 +72,8 @@ public class MultiAttackTests user.Library.Returns(dynamicLibrary); dynamicLibrary.StaticLibrary.Returns(staticLibrary); staticLibrary.Types.Returns(typeLibrary); - item.Name.Returns((StringKey)test.ItemName!); + if (test.ItemName != null) + item.Name.Returns(new StringKey(test.ItemName)); move.User.Returns(user); if (test.ItemName != null) diff --git a/Plugins/PkmnLib.Plugin.Gen7/Scripts/Abilities/PreventCritical.cs b/Plugins/PkmnLib.Plugin.Gen7/Scripts/Abilities/PreventCritical.cs index 8400d24..78e2f0f 100644 --- a/Plugins/PkmnLib.Plugin.Gen7/Scripts/Abilities/PreventCritical.cs +++ b/Plugins/PkmnLib.Plugin.Gen7/Scripts/Abilities/PreventCritical.cs @@ -1,5 +1,13 @@ namespace PkmnLib.Plugin.Gen7.Scripts.Abilities; +/// +/// PreventCritical is a generic ability that prevents the user from being hit by critical hits. +/// This ability completely blocks any incoming critical hits from opposing Pokémon. +/// This ability is used by abilities like Battle Armor and Shell Armor. +/// +/// Bulbapedia - Battle Armor +/// Bulbapedia - Shell Armor +/// [Script(ScriptCategory.Ability, "prevent_critical")] public class PreventCritical : Script {