-
Notifications
You must be signed in to change notification settings - Fork 2
/
ModEntry.cs
185 lines (165 loc) · 6.37 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
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Reflection;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewModdingAPI.Utilities;
using StardewValley;
namespace LastDayToPlant
{
public class ModEntry : Mod
{
private const int DaysInAMonth = 28;
private IModHelper MyHelper;
private ModConfig MyConfig;
private readonly List<Crop> AllCrops = new();
internal static IMonitor InternalMonitor;
public override void Entry(IModHelper helper)
{
MyHelper = helper;
I18n.Init(helper.Translation);
MyConfig = MyHelper.ReadConfig<ModConfig>();
InternalMonitor = this.Monitor;
LoadBaseGameCrops();
LoadModCrops();
// Localize the messages
foreach(var crop in AllCrops)
{
var baseName = crop.Name;
crop.Localize(MyHelper, baseName);
}
MyHelper.Events.GameLoop.DayStarted += GameLoop_DayStarted;
}
private void GameLoop_DayStarted(object sender, DayStartedEventArgs e)
{
// Check if the farmer has the agriculturist profession
var farmers = Game1.getAllFarmers();
foreach(var farmer in farmers)
{
if(farmer.IsMainPlayer && farmer.getProfessionForSkill(Farmer.farmingSkill,10) == Farmer.agriculturist)
{
FarmingSkills.IsAgriculturist = true;
break;
}
}
// Show any crops that can't be planted after today and still be harvested
var currentDay = SDate.From(Game1.Date).Day;
var currentYear = SDate.From(Game1.Date).Year;
var currentSeason = SDate.From(Game1.Date).Season;
switch (currentSeason)
{
case StardewValley.Season.Spring:
ShowCrops(Season.spring, currentDay, currentYear);
break;
case StardewValley.Season.Summer:
ShowCrops(Season.summer, currentDay, currentYear);
break;
case StardewValley.Season.Fall:
ShowCrops(Season.fall, currentDay, currentYear);
break;
case StardewValley.Season.Winter:
ShowCrops(Season.winter, currentDay, currentYear);
break;
default:
return;
}
}
private void LoadBaseGameCrops()
{
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("LastDayToPlant.BaseGameCrops.Crops.json"))
using (var reader = new StreamReader(stream))
{
var json = JArray.Parse(reader.ReadToEnd());
var list = json.ToObject<List<Crop>>();
AllCrops.AddRange(list);
}
}
private void LoadModCrops()
{
var modsPath = Path.Combine(Constants.GamePath, "Mods");
var modsPathExists = Directory.Exists(modsPath);
if (modsPathExists)
{
FindAndLoadCrops(modsPath);
}
}
private void FindAndLoadCrops(string path)
{
var files = Directory.GetFiles(path, "crop.json", SearchOption.AllDirectories);
foreach(var file in files)
{
var info = new FileInfo(file);
if(info.Name == "crop.json")
{
AllCrops.Add(Crop.FromModFile(file));
}
}
}
private void ShowCrops(Season season, int day, int year)
{
// Some crops are only available in Year 2. We don't want to show them in Year 1
var filtered = AllCrops.Where(x => x.IsLastGrowSeason(season) && year >= x.AvailableYear);
if(MyConfig.ShowGingerIslandCrops == false)
{
filtered = filtered.Where(x => x.GingerIsland == false);
}
foreach (var crop in filtered)
{
// No Fertilizer
var daysToMatureBoosted = CalculateActualGrowRate(crop, Fertilizer.None);
if (daysToMatureBoosted + day == DaysInAMonth)
{
Game1.addHUDMessage(new HUDMessage(crop.Message, HUDMessage.newQuest_type));
}
// SpeedGro
if (MyConfig.ShowSpeedGro)
{
var DaysToMatureBoosted = CalculateActualGrowRate(crop, Fertilizer.SpeedGro);
if (DaysToMatureBoosted + day == DaysInAMonth)
{
Game1.addHUDMessage(new HUDMessage(crop.MessageSpeedGro, HUDMessage.newQuest_type));
}
}
// Deluxe SpeedGro
if (MyConfig.ShowDeluxeSpeedGro)
{
var DaysToMatureBoosted = CalculateActualGrowRate(crop, Fertilizer.DeluxeSpeedGro);
if (DaysToMatureBoosted + day == DaysInAMonth)
{
Game1.addHUDMessage(new HUDMessage(crop.MessageDelxueSpeedGro, HUDMessage.newQuest_type));
}
}
// Hyper SppedGro
if (MyConfig.ShowHyperSpeedGro)
{
var DaysToMatureBoosted = CalculateActualGrowRate(crop, Fertilizer.HyperSpeedGro);
if (DaysToMatureBoosted + day == DaysInAMonth)
{
Game1.addHUDMessage(new HUDMessage(crop.MessageHyperSpeedGro, HUDMessage.newQuest_type));
}
}
}
}
private int CalculateActualGrowRate(Crop crop, double factor)
{
int daysToGrow = crop.DaysToGrowIrrigated > 0 ? crop.DaysToGrowIrrigated : crop.DaysToGrow;
if (FarmingSkills.IsAgriculturist)
{
return (int)(daysToGrow - (daysToGrow * (factor + FarmingSkills.AgriculturistGrowthRate)));
}
return (int)(daysToGrow - (daysToGrow * factor));
}
}
public enum Season
{
spring,
summer,
fall,
winter
}
}