forked from Pathoschild/StardewMods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModEntry.cs
99 lines (87 loc) · 3.97 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
using Pathoschild.Stardew.NoclipMode.Framework;
using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewModdingAPI.Utilities;
using StardewValley;
namespace Pathoschild.Stardew.NoclipMode
{
/// <summary>The mod entry point.</summary>
public class ModEntry : Mod
{
/*********
** Fields
*********/
/// <summary>The mod configuration.</summary>
private ModConfig Config = null!; // set in Entry
/// <summary>The keys which toggle noclip mode.</summary>
private KeybindList ToggleKey => this.Config.ToggleKey;
/// <summary>An arbitrary number which identifies messages from Noclip Mode.</summary>
private const int MessageID = 91871825;
/*********
** 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)
{
// init
I18n.Init(helper.Translation);
this.Config = helper.ReadConfig<ModConfig>();
// hook events
helper.Events.GameLoop.GameLaunched += this.OnGameLaunched;
helper.Events.Input.ButtonsChanged += this.OnButtonsChanged;
}
/*********
** Private methods
*********/
/// <inheritdoc cref="IGameLoopEvents.GameLaunched"/>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event data.</param>
private void OnGameLaunched(object? sender, GameLaunchedEventArgs e)
{
// add Generic Mod Config Menu integration
new GenericModConfigMenuIntegrationForNoclipMode(
getConfig: () => this.Config,
reset: () => this.Config = new ModConfig(),
saveAndApply: () => this.Helper.WriteConfig(this.Config),
modRegistry: this.Helper.ModRegistry,
monitor: this.Monitor,
manifest: this.ModManifest
).Register();
}
/// <inheritdoc cref="IInputEvents.ButtonsChanged"/>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event data.</param>
private void OnButtonsChanged(object? sender, ButtonsChangedEventArgs e)
{
if (this.CanToggle() && this.ToggleKey.JustPressed())
{
bool enabled = Game1.player.ignoreCollisions = !Game1.player.ignoreCollisions;
this.ShowConfirmationMessage(enabled, this.ToggleKey);
}
}
/// <summary>Show a confirmation message for the given noclip mode, if enabled.</summary>
/// <param name="noclipEnabled">Whether noclip was enabled; else noclip was disabled.</param>
/// <param name="keybind">The keybind that was pressed.</param>
private void ShowConfirmationMessage(bool noclipEnabled, KeybindList keybind)
{
// skip if message not enabled
if (noclipEnabled && !this.Config.ShowEnabledMessage)
return;
if (!noclipEnabled && !this.Config.ShowDisabledMessage)
return;
// show message
Game1.hudMessages.RemoveAll(p => p.number == ModEntry.MessageID);
string? keybindStr = keybind.GetKeybindCurrentlyDown()?.ToString();
string text = noclipEnabled ? I18n.EnabledMessage(keybindStr) : I18n.DisabledMessage(keybindStr);
Game1.addHUDMessage(new HUDMessage(text, HUDMessage.error_type) { noIcon = true, number = ModEntry.MessageID });
}
/// <summary>Get whether noclip mode can be toggled in the current context.</summary>
private bool CanToggle()
{
return
Context.IsPlayerFree // free to move
|| (Context.IsWorldReady && Game1.eventUp); // in a cutscene (so players can get unstuck if something blocks scripted movement)
}
}
}