simplify with newer pattern features

This commit is contained in:
Jesse Plamondon-Willard 2022-04-06 18:25:00 -04:00
parent b6c8cfc28b
commit 0539bb8f37
No known key found for this signature in database
GPG Key ID: CF8B1456B3E29F49
16 changed files with 21 additions and 25 deletions

View File

@ -64,7 +64,7 @@ namespace StardewModdingAPI.ModBuildConfig.Analyzer
}
// conditional access
if (node is ConditionalAccessExpressionSyntax conditionalAccess && conditionalAccess.WhenNotNull is MemberBindingExpressionSyntax conditionalBinding)
if (node is ConditionalAccessExpressionSyntax { WhenNotNull: MemberBindingExpressionSyntax conditionalBinding } conditionalAccess)
{
declaringType = semanticModel.GetTypeInfo(conditionalAccess.Expression).Type;
memberType = semanticModel.GetTypeInfo(node);

View File

@ -94,9 +94,8 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.World
this.RemoveObjects(location, obj =>
obj is not Chest
&& (
obj.Name == "Weeds"
|| obj.Name == "Stone"
|| (obj.ParentSheetIndex == 294 || obj.ParentSheetIndex == 295)
obj.Name is "Weeds" or "Stone"
|| obj.ParentSheetIndex is 294 or 295
)
)
+ this.RemoveResourceClumps(location, clump => this.DebrisClumps.Contains(clump.parentSheetIndex.Value));

View File

@ -106,7 +106,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework
{
foreach (int id in this.TryLoad<int, string>("Data\\weapons").Keys)
{
yield return this.TryCreate(ItemType.Weapon, id, p => (p.ID >= 32 && p.ID <= 34)
yield return this.TryCreate(ItemType.Weapon, id, p => p.ID is >= 32 and <= 34
? (Item)new Slingshot(p.ID)
: new MeleeWeapon(p.ID)
);

View File

@ -137,7 +137,7 @@ namespace StardewModdingAPI.Toolkit.Framework.ModScanning
return new ModFolder(root, searchFolder, ModType.Xnb, null, ModParseError.XnbMod, "it's not a SMAPI mod (see https://smapi.io/xnb for info).");
// SMAPI installer
if (relevantFiles.Any(p => p.Name == "install on Linux.sh" || p.Name == "install on macOS.command" || p.Name == "install on Windows.bat"))
if (relevantFiles.Any(p => p.Name is "install on Linux.sh" or "install on macOS.command" or "install on Windows.bat"))
return new ModFolder(root, searchFolder, ModType.Invalid, null, ModParseError.ManifestMissing, "the SMAPI installer isn't a mod (you can delete this folder after running the installer file).");
// not a mod?

View File

@ -48,7 +48,7 @@ namespace StardewModdingAPI.Toolkit.Serialization
{
json = File.ReadAllText(fullPath);
}
catch (Exception ex) when (ex is DirectoryNotFoundException || ex is FileNotFoundException)
catch (Exception ex) when (ex is DirectoryNotFoundException or FileNotFoundException)
{
result = default(TModel);
return false;

View File

@ -58,7 +58,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.Chucklefish
.GetAsync(string.Format(this.ModPageUrlFormat, parsedId))
.AsString();
}
catch (ApiException ex) when (ex.Status == HttpStatusCode.NotFound || ex.Status == HttpStatusCode.Forbidden)
catch (ApiException ex) when (ex.Status is HttpStatusCode.NotFound or HttpStatusCode.Forbidden)
{
return page.SetError(RemoteModStatus.DoesNotExist, "Found no Chucklefish mod with this ID.");
}

View File

@ -75,7 +75,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.Nexus
mod = await this.GetModFromApiAsync(parsedId);
// page doesn't exist
if (mod == null || mod.Status == NexusModStatus.Hidden || mod.Status == NexusModStatus.NotPublished)
if (mod == null || mod.Status is NexusModStatus.Hidden or NexusModStatus.NotPublished)
return page.SetError(RemoteModStatus.DoesNotExist, "Found no Nexus mod with this ID.");
// return info

View File

@ -23,7 +23,7 @@ namespace StardewModdingAPI
internal static bool IsSaveLoaded => Game1.hasLoadedGame && Game1.activeClickableMenu is not TitleMenu;
/// <summary>Whether the game is currently writing to the save file.</summary>
internal static bool IsSaving => Game1.activeClickableMenu is SaveGameMenu || Game1.activeClickableMenu is ShippingMenu; // saving is performed by SaveGameMenu, but it's wrapped by ShippingMenu on days when the player shipping something
internal static bool IsSaving => Game1.activeClickableMenu is SaveGameMenu or ShippingMenu; // saving is performed by SaveGameMenu, but it's wrapped by ShippingMenu on days when the player shipping something
/// <summary>The active split-screen instance IDs.</summary>
internal static readonly ISet<int> ActiveScreenIds = new HashSet<int>();
@ -39,7 +39,7 @@ namespace StardewModdingAPI
}
/// <summary>Whether the in-game world is completely unloaded and not in the process of being loaded. The world may still exist in memory at this point, but should be ignored.</summary>
internal static bool IsWorldFullyUnloaded => Context.LoadStage == LoadStage.ReturningToTitle || Context.LoadStage == LoadStage.None;
internal static bool IsWorldFullyUnloaded => Context.LoadStage is LoadStage.ReturningToTitle or LoadStage.None;
/*********

View File

@ -47,9 +47,9 @@ namespace StardewModdingAPI.Framework.Content
/// <inheritdoc />
public TData GetData<TData>()
{
if (this.Data is not TData)
if (this.Data is not TData data)
throw new InvalidCastException($"The content data of type {this.Data.GetType().FullName} can't be converted to the requested {typeof(TData).FullName}.");
return (TData)this.Data;
return data;
}
}
}

View File

@ -234,7 +234,7 @@ namespace StardewModdingAPI.Framework.Input
isDown: pressed.Contains(button)
);
if (button == SButton.MouseLeft || button == SButton.MouseMiddle || button == SButton.MouseRight || button == SButton.MouseX1 || button == SButton.MouseX2)
if (button is SButton.MouseLeft or SButton.MouseMiddle or SButton.MouseRight or SButton.MouseX1 or SButton.MouseX2)
mouseOverrides[button] = newState;
else if (button.TryGetKeyboard(out Keys _))
keyboardOverrides[button] = newState;

View File

@ -32,11 +32,8 @@ namespace StardewModdingAPI.Framework.ModLoading
ModDataRecordVersionedFields dataRecord = modDatabase.Get(manifest?.UniqueID)?.GetVersionedFields(manifest);
// apply defaults
if (manifest != null && dataRecord != null)
{
if (dataRecord.UpdateKey != null)
manifest.UpdateKeys = new[] { dataRecord.UpdateKey };
}
if (manifest != null && dataRecord?.UpdateKey is not null)
manifest.UpdateKeys = new[] { dataRecord.UpdateKey };
// build metadata
bool shouldIgnore = folder.Type == ModType.Ignored;

View File

@ -37,7 +37,7 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters
// skip if not broken
FieldDefinition fieldDefinition = fieldRef.Resolve();
if (fieldDefinition != null && !fieldDefinition.HasConstant)
if (fieldDefinition?.HasConstant == false)
return false;
// rewrite if possible

View File

@ -674,7 +674,7 @@ namespace StardewModdingAPI
}
// mouse
if (input == SButton.MouseLeft || input == SButton.MouseRight)
if (input is SButton.MouseLeft or SButton.MouseRight)
{
button = new InputButton(mouseLeft: input == SButton.MouseLeft);
return true;

View File

@ -23,7 +23,7 @@ namespace StardewModdingAPI
/// <param name="state">The button state.</param>
public static bool IsDown(this SButtonState state)
{
return state == SButtonState.Held || state == SButtonState.Pressed;
return state is SButtonState.Held or SButtonState.Pressed;
}
}
}

View File

@ -118,11 +118,11 @@ namespace StardewModdingAPI.Utilities
return SButtonState.None;
// mix of held + pressed => pressed
if (states.All(p => p == SButtonState.Pressed || p == SButtonState.Held))
if (states.All(p => p is SButtonState.Pressed or SButtonState.Held))
return SButtonState.Pressed;
// mix of held + released => released
if (states.All(p => p == SButtonState.Held || p == SButtonState.Released))
if (states.All(p => p is SButtonState.Held or SButtonState.Released))
return SButtonState.Released;
// not down last tick or now

View File

@ -139,7 +139,7 @@ namespace StardewModdingAPI.Utilities
public bool IsDown()
{
SButtonState state = this.GetState();
return state == SButtonState.Pressed || state == SButtonState.Held;
return state is SButtonState.Pressed or SButtonState.Held;
}
/// <summary>Get whether the input binding was just pressed this tick.</summary>