using Terraria.ID; using Terraria.Localization; using Terraria.ModLoader; namespace CalamityMod { public partial class CalamityUtils { /// The language key. This will have "Mods.CalamityMod." appended behind it. /// /// A instance found using the provided key with "Mods.CalamityMod." appended behind it. /// NOTE: Modded translations are not loaded until after PostSetupContent.Caching the result is suggested. /// public static LocalizedText GetText(string key) { return Language.GetOrRegister("Mods.CalamityMod." + key); } /// The language key. This will have "Mods.CalamityMod." appended behind it. /// /// A instance found using the provided key with "Mods.CalamityMod." appended behind it. /// NOTE: Modded translations are not loaded until after PostSetupContent.Caching the result is suggested. /// public static string GetTextValue(string key) { return Language.GetTextValue("Mods.CalamityMod." + key); } /// The item's ID. /// /// A instance for an item's name. /// NOTE: Modded translations are not loaded until after PostSetupContent.Caching the result is suggested. /// public static LocalizedText GetItemName(int itemID) { if (itemID < ItemID.Count) { return Language.GetText("ItemName." + ItemID.Search.GetName(itemID)); } return GetTextFromModItem(itemID, "DisplayName"); } /// /// A instance which will have the item's translated name. /// NOTE: Modded translations are not loaded until after PostSetupContent.Caching the result is suggested. /// public static LocalizedText GetItemName() where T : ModItem => GetTextFromModItem(ModContent.ItemType(), "DisplayName"); /// The item's ID. /// The desired suffix. /// /// A instance for the given item and suffix /// NOTE: Modded translations are not loaded until after PostSetupContent.Caching the result is suggested. /// public static LocalizedText GetTextFromModItem(int itemID, string suffix) { var modItem = ItemLoader.GetItem(itemID); return modItem.GetLocalization(suffix); } /// The desired suffix. /// /// A instance for the given item and suffix /// NOTE: Modded translations are not loaded until after PostSetupContent.Caching the result is suggested. /// public static LocalizedText GetTextFromModItem(string suffix) where T : ModItem => GetTextFromModItem(ModContent.ItemType(), suffix); /// The item's ID. /// The desired suffix. /// /// A instance for the given item and suffix /// NOTE: Modded translations are not loaded until after PostSetupContent.Caching the result is suggested. /// public static string GetTextValueFromModItem(int itemID, string suffix) => GetTextFromModItem(itemID, suffix).ToString(); /// The desired suffix. /// /// A instance for the given item and suffix /// NOTE: Modded translations are not loaded until after PostSetupContent.Caching the result is suggested. /// public static string GetTextValueFromModItem(string suffix) where T : ModItem => GetTextFromModItem(ModContent.ItemType(), suffix).ToString(); #region Tooltip Format Helper public static string FramesToSeconds(this int frame) => Round(frame / 60f, "N2"); public static string ToMph(this float velocity) => Round(velocity * 216000f / 42240f, "N0"); public static string ToMphps(this float velocity) => Round(velocity * 60f * 216000f / 42240f, "N2"); public static string ToTiles(this float pixel) => Round(pixel / 16f); public static string ToRegenPerSecond(this int regen) => Round(regen * 0.5f, "N2"); public static string ToJumpSpeedPercent(this float boost) => Round(boost * 20f, "N2"); public static string ToPercent(this float percent, string precision = "N1") => Round(percent * 100f, precision); public static string ToPercent(this double percent, string precision = "N1") => Round(percent * 100D, precision); // Double-rounded for proper digit cutoffs public static string Round(this float number, string precision = "N4") => float.Parse((number).ToString(precision)).ToString(); public static string Round(this double number, string precision = "N4") => float.Parse((number).ToString(precision)).ToString(); #endregion } }