Skip to content

Commit

Permalink
Good question
Browse files Browse the repository at this point in the history
  • Loading branch information
atravita-mods committed Mar 12, 2022
1 parent 637d777 commit 532d510
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 143 deletions.
34 changes: 15 additions & 19 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
[*.{cs,vb}]
dotnet_style_prefer_is_null_check_over_reference_equality_method=true:warning
dotnet_style_null_propagation=true:warning
dotnet_style_qualification_for_field=true:error
dotnet_style_qualification_for_property=true:error
dotnet_style_qualification_for_method=true:error
dotnet_style_qualification_for_event=true:error

#constants
dotnet_naming_symbols.all_constants.applicable_kinds = field
dotnet_naming_symbols.all_constants.applicable_accessibilities = *
dotnet_naming_symbols.all_constants.required_modifiers = const, readonly
# IDE0047: Remove unnecessary parentheses
dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity

dotnet_naming_style.all_upper_underscore.capitalization = all_upper
dotnet_naming_style.all_upper_underscore.word_separator = _
# IDE0047: Remove unnecessary parentheses
dotnet_style_parentheses_in_other_binary_operators = always_for_clarity

dotnet_naming_rule.constants_must_be_all_uppercase.symbols = all_constants
dotnet_naming_rule.constants_must_be_all_uppercase.style = all_upper_underscore
dotnet_naming_rule.constants_must_be_all_uppercase.severity = suggestion

# IDE0003: Remove qualification
dotnet_style_qualification_for_event =true:suggestion
dotnet_style_qualification_for_field=true:suggestion
dotnet_style_qualification_for_property=true:suggestion
dotnet_style_qualification_for_method=true:suggestion
dotnet_diagnostic.CA1304.severity=suggestion
# IDE0048: Add parentheses for clarity
dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity

[*.cs]
csharp_style_pattern_matching_over_is_with_cast_check=true:warning
csharp_style_pattern_matching_over_as_with_null_check=true:warning
insert_final_newline=true
csharp_style_var_for_built_in_types=false:suggestion
csharp_style_var_when_type_is_apparent=false:suggestion
csharp_style_var_elsewhere=false:suggestion
113 changes: 0 additions & 113 deletions .editorconfig.inferred

This file was deleted.

1 change: 1 addition & 0 deletions FarmCaveSpawn/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
[assembly: SuppressMessage("StyleCop.CSharp.LayoutRules", "SA1515:Single-line comment should be preceded by blank line", Justification = "Preference")]
[assembly: SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:Field names should not contain underscore", Justification = "Preference")]
[assembly: SuppressMessage("StyleCop.CSharp.NamingRules", "SA1306:Field names should begin with lower-case letter", Justification = "Preference")]
[assembly: SuppressMessage("StyleCop.CSharp.SpacingRules", "SA1011:Closing square brackets should be spaced correctly", Justification = "Stylecop doesn't understand nullable apparently")]
62 changes: 51 additions & 11 deletions FarmCaveSpawn/ModEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Text.RegularExpressions;
using AtraBase.Toolkit.Extensions;
using AtraShared.Integrations;
using AtraShared.MigrationManager;
using AtraShared.Utils.Extensions;
using Microsoft.Xna.Framework;
using StardewModdingAPI.Events;
Expand Down Expand Up @@ -37,6 +38,8 @@ public class ModEntry : Mod
/// </summary>
private List<int> TreeFruit = new();

private MigrationManager? migrator;

// The config is set by the Entry method, so it should never realistically be null
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
private ModConfig config;
Expand Down Expand Up @@ -67,6 +70,13 @@ internal Random Random
/// </summary>
internal bool SpawnedFruitToday { get; private set; }

/// <summary>
/// Formats a float as a percent value.
/// </summary>
/// <param name="val">Value to format.</param>
/// <returns>Formatted string.</returns>
public static string FormatPercentValue(float val) => $"{val:f0}%";

/// <inheritdoc />
public override void Entry(IModHelper helper)
{
Expand All @@ -87,6 +97,8 @@ public override void Entry(IModHelper helper)
helper.Events.GameLoop.DayStarted += this.SpawnFruit;
helper.Events.GameLoop.GameLaunched += this.SetUpConfig;
helper.Events.GameLoop.OneSecondUpdateTicking += this.BellsAndWhistles;
helper.Events.GameLoop.SaveLoaded += this.SaveLoaded;

helper.ConsoleCommands.Add(
name: "av.fcs.list_fruits",
documentation: I18n.ListFruits_Description(),
Expand Down Expand Up @@ -144,7 +156,8 @@ private void SetUpConfig(object? sender, GameLaunchedEventArgs e)
property: property,
getConfig: () => this.config,
min: 0f,
max: 100f);
max: 100f,
formatValue: FormatPercentValue);
}
else if (property.PropertyType.Equals(typeof(SeasonalBehavior)))
{
Expand Down Expand Up @@ -309,7 +322,7 @@ private void SpawnFruit(object? sender, DayStartedEventArgs e)
/// <param name="tile">Tile to place fruit on.</param>
private void PlaceFruit(GameLocation location, Vector2 tile)
{
int fruitToPlace = Utility.GetRandom(this.Random.NextDouble() < (this.config.TreeFruitChance / 100f) && this.TreeFruit.Count > 0 ? this.TreeFruit : this.BASE_FRUIT, this.Random);
int fruitToPlace = Utility.GetRandom(this.TreeFruit.Count > 0 && this.Random.NextDouble() < (this.config.TreeFruitChance / 100f) ? this.TreeFruit : this.BASE_FRUIT, this.Random);
location.setObject(tile, new SObject(fruitToPlace, 1)
{
IsSpawnedObject = true,
Expand Down Expand Up @@ -347,7 +360,6 @@ private IEnumerable<Vector2> IterateTiles(GameLocation location, int xstart = 1,
/// </summary>
/// <param name="command">Name of command.</param>
/// <param name="args">Arguments for command.</param>
[SuppressMessage("Style", "IDE0060:Remove unused parameter", Justification = "Console command format.")]
private void ListFruits(string command, string[] args)
{
if (!Context.IsWorldReady)
Expand Down Expand Up @@ -422,17 +434,12 @@ private List<int> GetTreeFruits()
if ((!this.config.AllowAnyTreeProduct && fruit.Category != SObject.FruitsCategory)
|| (this.config.EdiblesOnly && fruit.Edibility < 0)
|| fruit.Price > this.config.PriceCap
|| denylist.Contains(fruit.Name))
|| denylist.Contains(fruit.Name)
|| (this.config.NoBananasBeforeShrine && fruit.Name.Equals("Banana", StringComparison.OrdinalIgnoreCase)
&& !Context.IsWorldReady && Game1.getLocationFromName("IslandEast") is IslandEast islandeast && !islandeast.bananaShrineComplete.Value))
{
continue;
}
if (this.config.NoBananasBeforeShrine && fruit.Name.Equals("Banana"))
{
if (!Context.IsWorldReady && Game1.getLocationFromName("IslandEast") is IslandEast islandeast && !islandeast.bananaShrineComplete.Value)
{
continue;
}
}
treeFruits.Add(objectIndex);
}
catch (Exception ex)
Expand Down Expand Up @@ -494,4 +501,37 @@ private void BellsAndWhistles(object? sender, OneSecondUpdateTickingEventArgs e)
}
}
}

/// <summary>
/// Raised when save is loaded.
/// </summary>
/// <param name="sender">Unknown, used by SMAPI.</param>
/// <param name="e">Parameters.</param>
/// <remarks>Used to load in this mod's data models.</remarks>
private void SaveLoaded(object? sender, SaveLoadedEventArgs e)
{
if (Context.IsSplitScreen && Context.ScreenId != 0)
{
return;
}
this.migrator = new(this.ModManifest, this.Helper, this.Monitor);
this.migrator.ReadVersionInfo();

this.Helper.Events.GameLoop.Saved += this.WriteMigrationData;
}

/// <summary>
/// Writes migration data then detaches the migrator.
/// </summary>
/// <param name="sender">Smapi thing.</param>
/// <param name="e">Arguments for just-before-saving.</param>
private void WriteMigrationData(object? sender, SavedEventArgs e)
{
if (this.migrator is not null)
{
this.migrator.SaveVersionInfo();
this.migrator = null;
}
this.Helper.Events.GameLoop.Saved -= this.WriteMigrationData;
}
}

0 comments on commit 532d510

Please sign in to comment.