-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathModEntry.cs
92 lines (80 loc) · 3.52 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
using System;
using Harmony;
using SafeLightning.CommandParsing;
using StardewModdingAPI;
using System.Reflection;
using Microsoft.Xna.Framework;
using StardewValley;
using StardewValley.Locations;
using StardewValley.TerrainFeatures;
namespace SafeLightning
{
/// <summary>The mod entry point.</summary>
public class ModEntry : Mod
{
/*********
** Public methods
*********/
/// <summary>The mod entry point, called after the mod is first loaded.</summary>
/// <param name="helper">Provides simplified APIs for writing mods.</param>
public override void Entry(IModHelper helper)
{
CommandParser commandParser = new CommandParser(this.Monitor, this.Helper.ConsoleCommands);
commandParser.RegisterCommands();
HarmonyInstance instance = HarmonyInstance.Create(this.Helper.ModRegistry.ModID);
instance.PatchAll(Assembly.GetExecutingAssembly());
}
/*********
** Mod API
**********/
/// <summary>Delegate for the method the API will call when it is notified about a new strike.</summary>
/// <param name="position">The position to be hit</param>
/// <param name="effects">Whether to display visual/sound effects or not</param>
public delegate void StrikeLightningDelegate(Vector2 position, bool effects);
/// <summary>Get an API that other mods can access. This is always called after <see cref="Entry" />.</summary>
public override object GetApi()
{
return new API(this.ModWantsToStrikeLightningAt);
}
/// <summary>When told that another mod will cause a lightning strike, save the state of that <see cref="TerrainFeature" /> so it can be restored.</summary>
/// <param name="position">The position that will be hit</param>
/// <param name="effects">Whether visual/sound effects should be created</param>
private void ModWantsToStrikeLightningAt(Vector2 position, bool effects)
{
if (!Context.IsWorldReady)
return;
StrikeLightningSafelyAt(position, effects);
}
/// <summary>Safely strikes lightning.</summary>
/// <param name="position">The position to strike lightning</param>
/// <param name="effects">Whether visual/sound effects should be created</param>
internal static void StrikeLightningSafelyAt(Vector2 position, bool effects)
{
Farm farm = Game1.getFarm();
Farm.LightningStrikeEvent lightningStrikeEvent;
if (farm.objects.TryGetValue(position, out StardewValley.Object obj) && obj.bigCraftable.Value && obj.ParentSheetIndex == 9 && obj.heldObject.Value == null)
{
obj.heldObject.Value = new StardewValley.Object(787, 1, false, -1, 0);
obj.MinutesUntilReady = Utility.CalculateMinutesUntilMorning(Game1.timeOfDay);
obj.shakeTimer = 1000;
lightningStrikeEvent = new Farm.LightningStrikeEvent
{
bigFlash = true,
createBolt = true,
boltPosition = position * 64f + new Vector2(32f, 0.0f)
};
}
else
{
lightningStrikeEvent = new Farm.LightningStrikeEvent()
{
smallFlash = true
};
}
if (effects)
{
farm.lightningStrikeEvent.Fire(lightningStrikeEvent);
}
}
}
}