Skip to content

Commit 85608b4

Browse files
authored
MEEP: Work around vanilla bug regarding farmhouse animal deletion. (#72)
MEEP: Migrate animal spawn logic to morning "fluff" function as workaround for vanilla bug.
1 parent 171d27e commit 85608b4

File tree

4 files changed

+45
-11
lines changed

4 files changed

+45
-11
lines changed

MappingExtensionsAndExtraProperties/MappingExtensionsAndExtraProperties.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<Import Project="..\DecidedlyShared\DecidedlyShared.projitems" Label="Shared" />
33

44
<PropertyGroup>
55
<Name>Mapping Extensions and Extra Properties (MEEP)</Name>
66
<Description>Extra map features and tile/map properties to spice up your custom maps.</Description>
77
<MinimumApiVersion>4.0.0</MinimumApiVersion>
88
<UpdateKeys>Nexus:14493</UpdateKeys>
9-
<Version>2.5.1</Version>
9+
<Version>2.5.2</Version>
1010
</PropertyGroup>
1111

1212
<PropertyGroup>

MappingExtensionsAndExtraProperties/src/Features/FarmAnimalSpawnsFeature.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ public override void Enable()
7979
}
8080

8181
this.Enabled = true;
82+
83+
// This is necessary in order to have animals spawn on the first day, since we otherwise
84+
// add this on day end.
85+
Game1.addMorningFluffFunction(this.DayStartAction);
8286
}
8387

8488
public override void Disable()
@@ -88,7 +92,6 @@ public override void Disable()
8892

8993
public override void RegisterCallbacks()
9094
{
91-
FeatureManager.OnDayStartCallback += this.OnDayStart;
9295
FeatureManager.EarlyDayEndingCallback += this.OnEarlyDayEnding;
9396
FeatureManager.OnDisplayRenderedCallback += this.OnDisplayRenderedCallback;
9497
}
@@ -139,9 +142,12 @@ private void OnEarlyDayEnding(object? sender, EventArgs e)
139142
});
140143

141144
if (this.animalsRemoved != this.animalsSpawned)
142-
logger.Log("MEEP didn't remove as many animals as were spawned. There will likely be a warning about duplicates after this. Please upload this log to https://smapi.io and report this.", LogLevel.Warn);
145+
logger.Log("MEEP didn't remove as many animals as were spawned. There will likely be a warning about duplicates after this. Please upload this log to https://smapi.io and report this if you notice any problems.", LogLevel.Trace);
143146

144147
this.animalsRemoved = 0;
148+
149+
// This is a workaround for a vanilla bug.
150+
Game1.addMorningFluffFunction(this.DayStartAction);
145151
}
146152

147153
private void RemoveFarmAnimal(FarmAnimal animal)
@@ -174,19 +180,17 @@ private void RemoveFarmAnimal(FarmAnimal animal)
174180
}
175181
catch (Exception e)
176182
{
177-
logger.Log($"Ran into a problem removing animal {animal.Name}");
183+
logger.Log($"Ran into a problem removing animal {animal.Name}", LogLevel.Warn);
178184
}
179185
}
180186

181-
private void OnDayStart(object? sender, EventArgs e)
187+
private void DayStartAction()
182188
{
183189
if (!Context.IsWorldReady || !Context.IsMainPlayer || !this.Enabled)
184190
return;
185191

186192
if (FarmAnimalSpawnsFeature.quickSaveApi is not null)
187193
{
188-
logger.Log("Quick Save's API was loaded properly.", LogLevel.Trace);
189-
190194
if (FarmAnimalSpawnsFeature.quickSaveApi.IsLoading)
191195
{
192196
logger.Log("Quick Save indicated it was loading. Skipping this DayStart.", LogLevel.Trace);
@@ -267,7 +271,7 @@ private void OnDayStart(object? sender, EventArgs e)
267271
{
268272
logger.Error(
269273
$"Animal {babbyAnimal.Name} already exists with MEEP id {id} in {targetLocation.Name}. This means removal failed to happen for some reason. Attempting to fix it automatically.");
270-
glitchedAnimals.Add(existingAnimal);
274+
glitchedAnimals.Add(existingAnimal);
271275
}
272276
else if (string.IsNullOrWhiteSpace(id))
273277
{
@@ -306,7 +310,7 @@ private void OnDayStart(object? sender, EventArgs e)
306310
}
307311
catch (Exception ex)
308312
{
309-
logger.Log($"Caught an exception spawning {animal.Value.AnimalId} spawned in {animal.Value.LocationId}. Skipping!");
313+
logger.Log($"Caught an exception spawning {animal.Value.AnimalId} spawned in {animal.Value.LocationId}. Skipping!", LogLevel.Error);
310314
logger.Exception(ex);
311315
}
312316
}

MappingExtensionsAndExtraProperties/src/ModEntry.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using DecidedlyShared.APIs;
45
using DecidedlyShared.Logging;
56
using DecidedlyShared.Utilities;
@@ -66,6 +67,35 @@ public override void Entry(IModHelper helper)
6667

6768
helper.Events.Player.Warped += this.PlayerOnWarped;
6869
helper.Events.GameLoop.DayStarted += this.OnDayStarted;
70+
71+
this.AddDebugKeybinds();
72+
}
73+
74+
[Conditional("DEBUG")]
75+
private void AddDebugKeybinds()
76+
{
77+
this.Helper.Events.Input.ButtonPressed += this.OnDebugButtonPressed;
78+
}
79+
80+
private void OnDebugButtonPressed(object? sender, ButtonPressedEventArgs e)
81+
{
82+
// Welcome to hot reload city. Population: me.
83+
84+
if (e.Button == SButton.OemSemicolon)
85+
{
86+
this.logger.Log($"Printing info of all animals in {Game1.currentLocation.Name}.", LogLevel.Info);
87+
88+
foreach (FarmAnimal animal in Game1.currentLocation.animals.Values)
89+
{
90+
this.logger.Log($"{animal.Name} X: {animal.Position.X}.", LogLevel.Info);
91+
this.logger.Log($"{animal.Name} Y: {animal.Position.Y}.", LogLevel.Info);
92+
93+
foreach (string s in animal.modData.Values)
94+
{
95+
this.logger.Log($"{animal.Name} modData: {s}", LogLevel.Info);
96+
}
97+
}
98+
}
6999
}
70100

71101
private void LoadContentPacks()

MappingExtensionsAndExtraProperties/test-pack/TilePropertyMod/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"Name": "MEEP Tile Property Test Mod",
33
"Author": "DecidedlyHuman",
4-
"Version": "2.5.1",
4+
"Version": "2.5.2",
55
"Description": "Tile properties for testing.",
66
"UniqueID": "meep.test",
77
"UpdateKeys": [],

0 commit comments

Comments
 (0)