-
-
Notifications
You must be signed in to change notification settings - Fork 429
Expand file tree
/
Copy pathModEntry.cs
More file actions
93 lines (79 loc) · 3.29 KB
/
ModEntry.cs
File metadata and controls
93 lines (79 loc) · 3.29 KB
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
using Pathoschild.Stardew.Common;
using Pathoschild.Stardew.Common.Integrations.GenericModConfigMenu;
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
*********/
/// <inheritdoc />
public override void Entry(IModHelper helper)
{
CommonHelper.RemoveObsoleteFiles(this, "NoclipMode.pdb"); // removed in 1.3.8
// 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" />
private void OnGameLaunched(object? sender, GameLaunchedEventArgs e)
{
this.AddGenericModConfigMenu(
new GenericModConfigMenuIntegrationForNoclipMode(),
get: () => this.Config,
set: config => this.Config = config
);
}
/// <inheritdoc cref="IInputEvents.ButtonsChanged" />
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)
}
}