-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModEntry.cs
192 lines (158 loc) · 6.17 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
using System.Threading.Tasks;
using System.Linq;
using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewValley;
using AutoTravel2.UI;
using System.Collections.Generic;
using AutoTravel2.Integration;
using Microsoft.Xna.Framework.Input;
using AutoTravel2.Commands;
using HarmonyLib;
namespace AutoTravel2;
public sealed class ModEntry : Mod
{
#pragma warning disable CS8618
public ModConfig Config;
public static ModEntry Instance;
public IScreenReader? ScreenReader;
private IModHelper helper;
public List<TravelLocation> Locations = new List<TravelLocation>();
#pragma warning restore CS8616
private static string UniqueIdForCurrentSave => $"{SaveGame.FilterFileName(Game1.GetSaveGameName(false))}_{Game1.uniqueIDForThisGame}";
public override void Entry(IModHelper helper)
{
this.helper = helper;
Instance = this;
Log.Init(this.Monitor);
helper.Events.GameLoop.GameLaunched += this.GameLoop_GameLaunched;
helper.Events.GameLoop.SaveLoaded += this.GameLoop_SaveLoaded;
helper.Events.Input.ButtonPressed += this.OnButtonPressed;
helper.Events.Input.MouseWheelScrolled += this.OnMouseScroll;
var harmony = new Harmony(this.ModManifest.UniqueID);
CommandManager.Register(helper, harmony);
Config = helper.ReadConfig<ModConfig>();
}
private void GameLoop_SaveLoaded(object? sender, SaveLoadedEventArgs e)
{
if (this.Helper.Data.ReadGlobalData<List<TravelLocation>>(UniqueIdForCurrentSave) is List<TravelLocation> previousLocations)
{
Locations = previousLocations;
}
}
public void SaveLocations()
{
this.Helper.Data.WriteGlobalData(UniqueIdForCurrentSave, Locations);
}
public void AddLocation(string name)
{
if (string.IsNullOrWhiteSpace(name)) return;
if (Locations.Any(location => location.name == name))
{
ScreenReader?.SayWithMenuChecker(Config.PhraseLocationExists.Replace("{name}", name), true);
return;
}
Locations.Add(new(name, Game1.player.Position, Game1.currentLocation.NameOrUniqueName, Game1.player.FacingDirection));
ScreenReader?.SayWithMenuChecker(Config.PhraseLocationCreated.Replace("{name}", name), true);
SaveLocations();
}
public void RemoveLocation(TravelLocation? location)
{
if (location is null) return;
Locations.Remove(location);
ScreenReader?.SayWithMenuChecker(Config.PhraseLocationDeleted.Replace("{name}", location.name), true);
Game1.playSound("shwip");
SaveLocations();
}
public void WarpPlayer(TravelLocation? location)
{
if (location is null) return;
Warp warp = new Warp(0, 0, location.region, (int)(location.position.X + 16f) / 64, (int)location.position.Y / 64, false, false);
Game1.player.warpFarmer(warp, location.facingDirection);
Task.Run(async delegate
{
while (Game1.isWarping)
{
await Task.Delay(500);
}
if (location.region == "FarmHouse")
{
Game1.player.warpFarmer(warp, location.facingDirection);
while (Game1.isWarping)
{
await Task.Delay(500);
}
}
ScreenReader?.SayWithMenuChecker(Config.PhraseFinishedTravel, true);
Game1.player.faceDirection(location.facingDirection);
});
}
private void GameLoop_GameLaunched(object? sender, GameLaunchedEventArgs e)
{
if (helper.ModRegistry.IsLoaded("shoaib.stardewaccess"))
{
ScreenReader = Helper.ModRegistry.GetApi<IScreenReader>("shoaib.stardewaccess");
ScreenReader?.RegisterCustomMenuAsAccessible(typeof(AutoTravelMenu).FullName);
}
}
private void OnMouseScroll(object? sender, MouseWheelScrolledEventArgs e)
{
if (!Config.EnableMouseMenuScroll || e.Delta == 0) return;
if (Game1.activeClickableMenu is AutoTravelMenu autoTravelMenu)
{
(e.Delta > 0 ? Config.MenuUpButtons[0] : Config.MenuDownButtons[0]).TryGetKeyboard(out Keys key);
autoTravelMenu.receiveKeyPress(key);
}
}
private void OnButtonPressed(object? sender, ButtonPressedEventArgs e)
{
if (Game1.activeClickableMenu != null) return;
if (!Context.IsPlayerFree) return;
if (e.Button == Config.OpenMenuButton)
{
AutoTravelMenu autoTravelMenu = new AutoTravelMenu();
Game1.activeClickableMenu = autoTravelMenu;
if (Locations.Count > 0)
{
TravelLocation[] locations = Locations.OrderByDescending(l => l.favorite).ThenBy(l => l.name).ToArray();
if (locations.Length == 0)
{
autoTravelMenu.SetChildMenu(new CustomNamingMenu());
}
else
{
autoTravelMenu.SelectedLocation = locations[0];
autoTravelMenu.SaySelectedLocation(true);
}
}
}
}
public TravelLocation? GetPreviousLocation(TravelLocation currentLocation)
{
TravelLocation[] locations = Locations.OrderByDescending(l => l.favorite).ThenBy(l => l.name).ToArray();
for (int i = 0; i < locations.Length; i++)
{
int last_index = i == 0 ? locations.Length - 1 : i - 1;
TravelLocation thisLocation = locations[i];
if (thisLocation.Equals(currentLocation))
{
return locations[last_index];
}
}
return null;
}
public TravelLocation? GetNextLocation(TravelLocation currentLocation)
{
TravelLocation[] locations = Locations.OrderByDescending(l => l.favorite).ThenBy(l => l.name).ToArray();
for (int i = 0; i < locations.Length; i++)
{
int next_index = i == locations.Length - 1 ? 0 : i + 1;
TravelLocation thisLocation = locations[i];
if (thisLocation.Equals(currentLocation))
{
return locations[next_index];
}
}
return null;
}
}