Skip to content

Commit

Permalink
Keybind reset & jumping bug fix
Browse files Browse the repository at this point in the history
Both bugs were actually the same thing!

The mod's config was only being loaded after `PostModsInit()`, and it wasn't being filled in with saved keybinds at all unless the Jolly Co-op menu was opened, since that's usually done *during* `PostModsInit()`.

The fix was to make `JollyRebindConfig` bind everything in its constructor which is called in `OnModsInit()` (which it should have been doing anyway really), and if it needs to be changed in `PostModsInit()`, recreating it and calling `Reload()` in order to manually fill it with any saved keybinds.
  • Loading branch information
SabreML committed Jul 14, 2024
1 parent cdb3911 commit 564ec5e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 23 deletions.
2 changes: 1 addition & 1 deletion JollyRebind/modinfo.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "sabreml.jollyrebind",
"name": "Jolly Rebind",
"version": "1.2.3",
"version": "1.2.4",
"target_game_version": "v1.9.15",
"authors": "SabreML",
"description": "Adds a customisable input to change the Jolly Co-op pointing button.",
Expand Down
11 changes: 1 addition & 10 deletions src/JollyRebindConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,16 @@ public class JollyRebindConfig : OptionInterface
{
public static Configurable<KeyCode>[] PlayerPointInputs = new Configurable<KeyCode>[4];

private static ConfigHolder configHolder;


// Cache `this.config` in a static field so that I can be a bit lazy with it in `CreateInputConfigs()`.
public JollyRebindConfig()
{
configHolder = config;
}

// Bind each player's `Configurable<KeyCode>` to the `ConfigHolder`, with Spacebar as the default key.
public static void CreateInputConfigs()
{
for (int i = 0; i < PlayerPointInputs.Length; i++)
{
PlayerPointInputs[i] = configHolder.Bind($"PlayerPointInputP{i}", KeyCode.Space);
PlayerPointInputs[i] = config.Bind($"PlayerPointInputP{i}", KeyCode.Space);
}
}


// Hook and override these methods to manually stop this interface being opened.
// This is needed because there aren't actually any configurable settings, and it'll be confusing to see a blank config page.
// (I can't find a better way to do this unfortunately, so this works for now.)
Expand Down
22 changes: 10 additions & 12 deletions src/JollyRebindMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,9 @@

namespace JollyRebind
{
[BepInPlugin("sabreml.jollyrebind", "JollyRebind", "1.2.3")]
[BepInPlugin("sabreml.jollyrebind", "JollyRebind", "1.2.4")]
public class JollyRebindMod : BaseUnityPlugin
{
// The maximum number of co-op players. (Default: 4)
// The 'Myriad of Slugcats' mod can increase this up to 16.
public static int MaxPlayerCount { private set; get; }


// A `HashSet` of previously logged controller element exceptions.
// These are tracked because `JollyInputUpdate` happens once every frame, and this could very quickly spam the log file otherwise.
// (`HashSet`s don't allow duplicate entries.)
Expand All @@ -41,24 +36,27 @@ private void Init(On.RainWorld.orig_OnModsInit orig, RainWorld self)
MachineConnector.SetRegisteredOI(Info.Metadata.GUID, new JollyRebindConfig());
}

// 'Myriad of Slugcats' compatibility.
// Just for compatibility with the 'Myriad of Slugcats' mod.
private void PostInit(On.RainWorld.orig_PostModsInit orig, RainWorld self)
{
orig(self);

// The `PlayerObjectBodyColors` field seems to be the most reliable way to get a max player count.
MaxPlayerCount = RainWorld.PlayerObjectBodyColors.Length;
int MaxPlayerCount = RainWorld.PlayerObjectBodyColors.Length;
if (MaxPlayerCount > 4)
{
// Remake these arrays with a bigger size.
JollyMenuKeybinds.KeybindWrappers = new Menu.Remix.UIelementWrapper[MaxPlayerCount];
JollyRebindConfig.PlayerPointInputs = new Configurable<KeyCode>[MaxPlayerCount];
}

// Whether it's modified or not, finish setting up the input configs.
JollyRebindConfig.CreateInputConfigs();
// Re-register the mod's config with the new array size.
JollyRebindConfig modConfig = new JollyRebindConfig();
MachineConnector.SetRegisteredOI(Info.Metadata.GUID, modConfig);
// And reload any saved values from file into it.
modConfig.config.Reload();
}
}


// If the player has a custom keybind set (AKA: Not the map key), this method sets `jollyButtonDown` to true if the key is being held down.
private void JollyInputUpdateHK(On.Player.orig_JollyInputUpdate orig, Player self)
{
Expand Down

0 comments on commit 564ec5e

Please sign in to comment.