-
-
Notifications
You must be signed in to change notification settings - Fork 380
/
ModEntry.cs
280 lines (243 loc) · 9.55 KB
/
ModEntry.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Pathoschild.Stardew.Common;
using Pathoschild.Stardew.Common.Patching;
using Pathoschild.Stardew.HorseFluteAnywhere.Framework;
using Pathoschild.Stardew.HorseFluteAnywhere.Patches;
using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewModdingAPI.Utilities;
using StardewValley;
using StardewValley.Buildings;
using StardewValley.Characters;
using SObject = StardewValley.Object;
namespace Pathoschild.Stardew.HorseFluteAnywhere;
/// <summary>The mod entry point.</summary>
internal class ModEntry : Mod
{
/*********
** Fields
*********/
/// <summary>The unique item ID for a horse flute.</summary>
private const string HorseFluteId = "911";
/// <summary>The horse flute to play when the summon key is pressed.</summary>
private readonly Lazy<SObject> HorseFlute = new(() => ItemRegistry.Create<SObject>("(O)" + ModEntry.HorseFluteId));
/// <summary>The mod configuration.</summary>
private ModConfig Config = null!; // set in Entry
/// <summary>The summon key binding.</summary>
private KeybindList SummonKey => this.Config.SummonHorseKey;
/*********
** Public methods
*********/
/// <inheritdoc />
public override void Entry(IModHelper helper)
{
I18n.Init(helper.Translation);
CommonHelper.RemoveObsoleteFiles(this, "HorseFluteAnywhere.pdb"); // removed in 1.1.19
// load config
this.UpdateConfig();
// add patches
HarmonyPatcher.Apply(this,
new UtilityPatcher(this.Monitor)
);
// hook events
helper.Events.GameLoop.GameLaunched += this.OnGameLaunched;
helper.Events.Input.ButtonsChanged += this.OnButtonsChanged;
helper.Events.Player.Warped += this.OnWarped;
helper.Events.World.LocationListChanged += this.OnLocationListChanged;
// hook commands
helper.ConsoleCommands.Add("reset_horses", "Reset the name and ownership for every horse in the game, so you can rename or reclaim a broken horse.", (_, _) => this.ResetHorsesCommand());
}
/*********
** Private methods
*********/
/// <inheritdoc cref="IGameLoopEvents.GameLaunched" />
private void OnGameLaunched(object? sender, GameLaunchedEventArgs e)
{
// add Generic Mod Config Menu integration
new GenericModConfigMenuIntegrationForHorseFluteAnywhere(
getConfig: () => this.Config,
reset: () =>
{
this.Config = new ModConfig();
this.Helper.WriteConfig(this.Config);
this.UpdateConfig();
},
saveAndApply: () =>
{
this.Helper.WriteConfig(this.Config);
this.UpdateConfig();
},
modRegistry: this.Helper.ModRegistry,
monitor: this.Monitor,
manifest: this.ModManifest
).Register();
}
/// <inheritdoc cref="IInputEvents.ButtonsChanged" />
private void OnButtonsChanged(object? sender, ButtonsChangedEventArgs e)
{
if (this.SummonKey.JustPressed() && this.TryUseHorseFlute())
this.Helper.Input.SuppressActiveKeybinds(this.SummonKey);
}
/// <inheritdoc cref="IWorldEvents.LocationListChanged" />
private void OnLocationListChanged(object? sender, LocationListChangedEventArgs e)
{
// rescue lost horses
if (Context.IsMainPlayer)
{
foreach (GameLocation location in e.Removed)
{
foreach (Horse horse in this.GetHorsesIn(location).ToArray())
this.WarpHome(horse);
}
}
}
/// <inheritdoc cref="IPlayerEvents.Warped" />
private void OnWarped(object? sender, WarpedEventArgs e)
{
if (!e.IsLocalPlayer || !this.IsRidingHorse(Game1.player))
return;
// fix: warping onto a magic warp while mounted causes an infinite warp loop
Vector2 tile = CommonHelper.GetPlayerTile(Game1.player);
string touchAction = Game1.player.currentLocation.doesTileHaveProperty((int)tile.X, (int)tile.Y, "TouchAction", "Back");
if (touchAction != null && touchAction.StartsWith("MagicWarp "))
Game1.currentLocation.lastTouchActionLocation = tile;
// fix: warping into an event may break the event (e.g. Mr Qi's event on mine level event for the 'Cryptic Note' quest)
if (Game1.CurrentEvent != null)
Game1.player.mount.dismount();
}
/// <summary>Use the horse flute, if allowed in the current context.</summary>
/// <returns>Returns whether the horse flute was used.</returns>
private bool TryUseHorseFlute()
{
if (!this.CanPlayFlute(Game1.player))
return false;
this.HorseFlute.Value.performUseAction(Game1.currentLocation);
return true;
}
/// <summary>Reset all horse names and ownership, and log details to the SMAPI console.</summary>
private void ResetHorsesCommand()
{
// validate
if (!Context.IsWorldReady)
{
this.Monitor.Log("You must load a save to use this command.", LogLevel.Error);
return;
}
if (!Context.IsMainPlayer)
{
this.Monitor.Log("You must be the main player to use this command.", LogLevel.Error);
return;
}
// reset horse instances
Farm farm = Game1.getFarm();
bool anyChanged = false;
foreach (Stable stable in farm.buildings.OfType<Stable>())
{
bool curChanged = false;
// fetch info
Horse horse = stable.getStableHorse();
bool isTractor = this.IsTractor(horse);
bool hadName = !string.IsNullOrEmpty(horse?.Name);
// fix stable
if (stable.owner.Value != 0)
{
stable.owner.Value = 0;
curChanged = true;
}
// fix horse
if (horse != null)
{
if (horse.ownerId.Value != 0)
{
horse.ownerId.Value = 0;
curChanged = true;
}
if (!isTractor && hadName)
{
horse.Name = "";
curChanged = true;
}
}
// log
if (curChanged)
{
string message = isTractor
? $"Reset tractor {(hadName ? $"'{horse!.Name}'" : "with no name")}."
: $"Reset horse '{(hadName ? $"'{horse!.Name}'" : "with no name")}'. The next player who interacts with it will become the owner.";
this.Monitor.Log(message, LogLevel.Info);
}
anyChanged |= curChanged;
}
// reset player horse names
foreach (Farmer farmer in Game1.getAllFarmers())
{
if (!string.IsNullOrEmpty(farmer.horseName.Value))
{
farmer.horseName.Value = null;
anyChanged = true;
this.Monitor.Log($"Reset horse link for player '{farmer.Name}'.", LogLevel.Info);
}
}
this.Monitor.Log(anyChanged ? "Done!" : "No horses found to reset.", LogLevel.Info);
}
/// <summary>Update the mod configuration.</summary>
private void UpdateConfig()
{
this.Config = this.Helper.ReadConfig<ModConfig>();
}
/// <summary>Get all horses in the given location.</summary>
/// <param name="location">The location to scan.</param>
private IEnumerable<Horse> GetHorsesIn(GameLocation location)
{
return location.characters
.OfType<Horse>()
.Where(p => !this.ShouldIgnore(p));
}
/// <summary>Warp a horse back to its home.</summary>
/// <param name="horse">The horse to warp.</param>
private void WarpHome(Horse horse)
{
Farm farm = Game1.getFarm();
Stable? stable = farm.buildings.OfType<Stable>().FirstOrDefault(p => p.HorseId == horse.HorseId);
Game1.warpCharacter(horse, farm, Vector2.Zero);
stable?.grabHorse();
}
/// <summary>Get whether the player can play the flute.</summary>
/// <param name="player">The player to check.</param>
private bool CanPlayFlute(Farmer player)
{
const string id = $"{ItemRegistry.type_object}{ModEntry.HorseFluteId}";
return
Context.IsPlayerFree
&& (
!this.Config.RequireHorseFlute
|| player.Items.Any(p => p?.QualifiedItemId == id)
);
}
/// <summary>Get whether a player is riding a non-ignored horse.</summary>
/// <param name="player">The player to check.</param>
private bool IsRidingHorse(Farmer player)
{
return
player.mount != null
&& !this.ShouldIgnore(player.mount);
}
/// <summary>Get whether a horse should be ignored by the main horse logic.</summary>
/// <param name="horse">The horse to check.</param>
private bool ShouldIgnore(Horse? horse)
{
return
horse == null
|| this.IsTractor(horse) // Tractor Mod tractor
|| horse.GetType().FullName?.StartsWith("DeepWoodsMod.") == true; // Deep Woods unicorn
}
/// <summary>Get whether a horse is a tractor added by Tractor Mod, which manages the edge cases for tractor summoning automatically.</summary>
/// <param name="horse">The horse to check.</param>
private bool IsTractor(Horse horse)
{
return horse.modData?.TryGetValue("Pathoschild.TractorMod", out _) == true;
}
}