Adds several convenience features

This commit is contained in:
2024-09-30 14:20:45 +02:00
parent 257c04c98b
commit a39c77745d
9 changed files with 72 additions and 13 deletions

View File

@@ -7,12 +7,13 @@ namespace PkmnLib.Static.Libraries;
/// <summary>
/// The library for all species in the game.
/// </summary>
public interface IReadOnlySpeciesLibrary
public interface IReadOnlySpeciesLibrary : IEnumerable<ISpecies>
{
/// <summary>
/// Tries to get a species from the library. Returns false if the species is not found.
/// </summary>
bool TryGet(StringKey key, [MaybeNullWhen(false)] out ISpecies value);
bool TryGetById(int id, [MaybeNullWhen(false)] out ISpecies value);
/// <summary>
/// Gets a random species from the library.
@@ -31,4 +32,13 @@ public interface IReadOnlySpeciesLibrary
}
/// <inheritdoc cref="IReadOnlySpeciesLibrary"/>
public class SpeciesLibrary : DataLibrary<ISpecies>, IReadOnlySpeciesLibrary;
public class SpeciesLibrary : DataLibrary<ISpecies>, IReadOnlySpeciesLibrary
{
/// <inheritdoc />
public bool TryGetById(int id, [MaybeNullWhen(false)] out ISpecies value)
{
return this.FirstOrDefault(s => s.Id == id) is { } species
? (value = species) != null
: (value = default) != null;
}
}