-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModEntry.cs
166 lines (134 loc) · 5.82 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
using System;
using FishingProgression.framework;
using FishingProgression.reflection;
using HarmonyLib;
using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewModdingAPI.Utilities;
using StardewValley;
using StardewValley.Menus;
using StardewValley.Tools;
namespace FishingProgression
{
internal class ModEntry : Mod
{
private readonly PerScreen<Bobber> bobber = new();
/// <summary>Whether the player is in the fishing minigame.</summary>
private readonly PerScreen<bool> BeganFishingGame = new();
/// <summary>The number of ticks since the player opened the fishing minigame.</summary>
private readonly PerScreen<int> UpdateIndex = new();
public override void Entry(IModHelper helper)
{
Globals.InitializeGlobals(this);
Globals.InitializeConfig();
helper.Events.GameLoop.UpdateTicked += OnUpdateTicked;
helper.Events.Display.MenuChanged += OnMenuChanged;
helper.Events.GameLoop.GameLaunched += OnGameLaunched;
var harmony = new Harmony(Globals.Manifest.UniqueID);
// example patch, you'll need to edit this for your patch
harmony.Patch(
original: AccessTools.Method(typeof(StardewValley.Tools.FishingRod), nameof(StardewValley.Tools.FishingRod.pullFishFromWater)),
prefix: new HarmonyMethod(typeof(PullFishFromWater), nameof(PullFishFromWater.pullFishFromWater_Prefix))
);
harmony.Patch(
original: AccessTools.Method(typeof(StardewValley.Tools.FishingRod), "calculateTimeUntilFishingBite"),
postfix: new HarmonyMethod(typeof(CalculateTimeUntilFishingBite), nameof(CalculateTimeUntilFishingBite.calculateTimeUntilFishingBite_PostFix))
);
}
private void OnGameLaunched(object sender, EventArgs e)
{
Globals.RegisterConfigMenu();
}
private void OnMenuChanged(object sender, MenuChangedEventArgs e)
{
this.bobber.Value = e.NewMenu is BobberBar menu ? new Bobber(menu, this.Helper.Reflection) : null;
}
private void OnUpdateTicked(object sender, EventArgs e)
{
// Checks to see if the world is ready.
if (!Context.IsWorldReady)
return;
// Gets the player
Farmer player = Game1.player;
// Checks is the player exists or is not the local player.
if (player == null || !player.IsLocalPlayer)
{
return;
}
// Checks if the menu is the fishing minigame and that we have received the minigame instance
if (Game1.activeClickableMenu is BobberBar && this.bobber.Value !=null)
{
// Allows us to interact with the minigame instance
Bobber newBobber = this.bobber.Value;
// Checks if we have applied our buffs or not, and makes sure this is delayed enough so that the minigame has definitely started
if(!this.BeganFishingGame.Value && this.UpdateIndex.Value > 15)
{
if (Globals.Config.EnableDifficultyModifier)
{
ReduceDifficulty(newBobber, player);
}
if (Globals.Config.EnableAutoTreasureChest && player.FishingLevel >= Globals.Config.AutoTreasureChestRequirement)
{
TreasureCaught(newBobber);
}
if (player.CurrentTool is FishingRod rod)
{
RestoreTackle(rod, player);
}
// Internal stopper to make sure we only apply the buff once per minigame
this.BeganFishingGame.Value = true;
}
// The timer to make sure that the buffs applied are delayed
if (this.UpdateIndex.Value < 20)
{
this.UpdateIndex.Value++;
}
} else {
// Resets internal indicators
this.BeganFishingGame.Value = false;
this.UpdateIndex.Value = 0;
}
if (player.CurrentTool is FishingRod fishingRod)
{
if(Globals.Config.EnableAutoHook)
{
AutoHook(fishingRod, player);
}
}
}
private void AutoHook(FishingRod rod, Farmer player)
{
if (player.FishingLevel < Globals.Config.AutoHookRequirement)
return;
if (rod.isNibbling && rod.isFishing && !rod.isReeling && !rod.pullingOutOfWater && !rod.hit)
{
Farmer.useTool(player);
}
}
private void ReduceDifficulty(Bobber bar, Farmer player)
{
// Added roughly 1/4 of a second after it updates, once per fishing game
// Difficulty modifier is calculated based on the player's level
float difficultyModifier = Math.Clamp(1 - (player.FishingLevel * (Globals.Config.DifficultyModifier / 100)), 0, 0.75f);
// Apply the buffs
bar.Difficulty *= difficultyModifier;
}
private void RestoreTackle(FishingRod rod, Farmer player)
{
if (rod.attachments[1] == null)
return;
var tackle = rod.attachments[1];
if (tackle.uses.Value >= 0 && Game1.random.Next(0, 100) <= Math.Clamp(player.FishingLevel * Globals.Config.TackleRestorationChance, 1, 100))
{
rod.attachments[1].uses.Value -= 1;
}
}
private void TreasureCaught(Bobber bar)
{
if(bar.Treasure)
{
bar.TreasureCaught = true;
}
}
}
}