requested fixes + uses Debug.Assert instead of throwing an error

This commit is contained in:
Doze-Zoze authored and Cameron Ewell committed 2026-07-07 21:05:24 -04:00
1 parent 5fa395e07b
commit 1737c3f4cf
2 files changed
+55 -51

No files matched your search

+54 -50
View File
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using CalamityMod.Systems.Collections;
using Microsoft.Xna.Framework;
@@ -32,6 +34,14 @@ namespace CalamityMod
public static ModKeybind[] AccessoryKeybinds = [];
public static List<int> RentedKeybinds = [];
/// <summary>
/// We store the keybind then move it to the main list at the end of the frame
/// This ensures that between frames has keybinds in the values needed for drawing
/// This does means there's a 1 frame delay when switching slots for item tooltips to update, but that's never important
/// </summary>
public static List<int> KeybindRentalQueue = [];
public override void Load()
{
// Register keybinds
@@ -87,6 +97,24 @@ namespace CalamityMod
AccessoryKeybinds = null;
}
public override void PostUpdateEverything()
{
RentedKeybinds = KeybindRentalQueue.ToList();
KeybindRentalQueue.Clear();
}
}
public class AccessoryKeybindManager : GlobalItem
{
public override bool InstancePerEntity => true;
internal int KeybindId { get; set; }
public override void UpdateEquip(Item item, Player player)
{
if (CalamityItemSets.HasAccessoryKeybind[item.type])
CalamityKeybinds.KeybindRentalQueue.Add(item.type);
}
}
public static class CalamityKeybindsExtensions
@@ -104,20 +132,16 @@ namespace CalamityMod
return CalamityKeybinds.HeldItem.JustPressed;
if (!CalamityItemSets.HasAccessoryKeybind[item.type])
throw new InvalidOperationException("Item is not marked as having a Calamity keybind in the HasAccessoryKeybind item set");
Debug.Assert(false, "Item is not marked as having a Calamity keybind in the HasAccessoryKeybind item set, but is requesting an accessory keybind");
int keybindToUse = 0;
for (var i = 0; i < Main.LocalPlayer.armor.Length; i++)
if (CalamityKeybinds.RentedKeybinds.Contains(item.type))
{
if (Main.LocalPlayer.armor[i].type == item.type)
break;
if (CalamityItemSets.HasAccessoryKeybind[Main.LocalPlayer.armor[i].type])
keybindToUse++;
int index = CalamityKeybinds.RentedKeybinds.IndexOf(item.type);
CalamityKeybinds.AccessoryKeybinds.IndexInRange(index);
return CalamityKeybinds.AccessoryKeybinds[index].JustPressed;
}
if (keybindToUse > 6)
return false;
return CalamityKeybinds.AccessoryKeybinds[keybindToUse].JustPressed;
return false;
}
/// <summary>
@@ -134,21 +158,16 @@ namespace CalamityMod
return CalamityKeybinds.HeldItem.JustReleased;
if (!CalamityItemSets.HasAccessoryKeybind[item.type])
throw new InvalidOperationException("Item is not marked as having a Calamity keybind in the HasAccessoryKeybind item set");
Debug.Assert(false, "Item is not marked as having a Calamity keybind in the HasAccessoryKeybind item set, but is requesting an accessory keybind");
int keybindToUse = 0;
for (var i = 0; i < Main.LocalPlayer.armor.Length; i++)
if (CalamityKeybinds.RentedKeybinds.Contains(item.type))
{
if (Main.LocalPlayer.armor[i].type == item.type)
break;
if (CalamityItemSets.HasAccessoryKeybind[Main.LocalPlayer.armor[i].type])
keybindToUse++;
int index = CalamityKeybinds.RentedKeybinds.IndexOf(item.type);
CalamityKeybinds.AccessoryKeybinds.IndexInRange(index);
return CalamityKeybinds.AccessoryKeybinds[index].JustReleased;
}
if (keybindToUse > 6)
return false;
return CalamityKeybinds.AccessoryKeybinds[keybindToUse].JustReleased;
return false;
}
@@ -165,21 +184,16 @@ namespace CalamityMod
return CalamityKeybinds.HeldItem.Current;
if (!CalamityItemSets.HasAccessoryKeybind[item.type])
throw new InvalidOperationException("Item is not marked as having a Calamity keybind in the HasAccessoryKeybind item set");
Debug.Assert(false, "Item is not marked as having a Calamity keybind in the HasAccessoryKeybind item set, but is requesting an accessory keybind");
int keybindToUse = 0;
for (var i = 0; i < Main.LocalPlayer.armor.Length; i++)
if (CalamityKeybinds.RentedKeybinds.Contains(item.type))
{
if (Main.LocalPlayer.armor[i].type == item.type)
break;
if (CalamityItemSets.HasAccessoryKeybind[Main.LocalPlayer.armor[i].type])
keybindToUse++;
int index = CalamityKeybinds.RentedKeybinds.IndexOf(item.type);
CalamityKeybinds.AccessoryKeybinds.IndexInRange(index);
return CalamityKeybinds.AccessoryKeybinds[index].Current;
}
if (keybindToUse > 6)
return false;
return CalamityKeybinds.AccessoryKeybinds[keybindToUse].Current;
return false;
}
@@ -196,30 +210,20 @@ namespace CalamityMod
if (!CalamityItemSets.HasAccessoryKeybind[item.type])
return CalamityKeybinds.HeldItem;
if (!Main.LocalPlayer.armor.Any(x => x.type == item.type))
return null;
int keybindToUse = 0;
for (var i = 0; i < Main.LocalPlayer.armor.Length; i++)
if (CalamityKeybinds.RentedKeybinds.Contains(item.type))
{
if (Main.LocalPlayer.armor[i].type == item.type)
break;
if (CalamityItemSets.HasAccessoryKeybind[Main.LocalPlayer.armor[i].type])
keybindToUse++;
int index = CalamityKeybinds.RentedKeybinds.IndexOf(item.type);
CalamityKeybinds.AccessoryKeybinds.IndexInRange(index);
return CalamityKeybinds.AccessoryKeybinds[index];
}
if (keybindToUse > 6)
return null;
return CalamityKeybinds.AccessoryKeybinds[keybindToUse];
return null;
}
public static bool HasBoundDynamicHotkey(this Item item)
{
var h = item.GetDynamicModHotkey();
if (h == null) return false;
if (h?.GetAssignedKeysOrEmpty(PlayerInput.CurrentInputMode).Count() == 0)
if (h?.GetAssignedKeysOrEmpty(PlayerInput.CurrentInputMode).Count == 0)
return false;
return true;
}
+1 -1
View File
@@ -172,7 +172,7 @@ namespace CalamityMod.Systems.Collections
/// <summary>
/// If <see langword="true"/> for an item type, this item is an accessory which uses a keybind for an active ability.<br/>
/// Used for assigning it one of the dedicated keybind slots based on slot order with other accessories that use keybinds.<br/>
/// Defaults to <see langword="false"/>. Note that an error will be thrown if attempting to give an accessory a keybind without assigning it to this set!
/// Defaults to <see langword="false"/>.
/// </summary>
public static bool[] HasAccessoryKeybind = Factory.CreateNamedSet("HasAccessoryKeybind")
.Description("Counts this item as having an accessory keybind. Used for assigning which keybind goes to which accessory.")