Use same logic for all GetByNameOrId

NPC, Item, Prefix, Buff
This commit is contained in:
SGKoishi 2022-11-02 14:31:00 -07:00
parent d38046d74b
commit 1ee8058776
No known key found for this signature in database
GPG key ID: 8FFC399070653828
2 changed files with 132 additions and 73 deletions

View file

@ -16,7 +16,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using Terraria;
@ -35,6 +35,8 @@ namespace TShockAPI.Localization
private static readonly Dictionary<int, string> Prefixs = new Dictionary<int, string>();
private static readonly Dictionary<int, string> Buffs = new Dictionary<int, string>();
internal static void Initialize()
{
var culture = Language.ActiveCulture;
@ -58,10 +60,15 @@ namespace TShockAPI.Localization
NpcNames.Add(i, Lang.GetNPCNameValue(i));
}
for (var i = -17; i < Main.maxBuffTypes; i++)
{
Buffs.Add(i, Lang.GetBuffName(i));
}
foreach (var field in typeof(Main).Assembly.GetType("Terraria.ID.PrefixID")
.GetFields().Where(f => !f.Name.Equals("Count", StringComparison.Ordinal)))
{
Prefixs.Add((int) field.GetValue(null), field.Name);
Prefixs.Add((int)field.GetValue(null), field.Name);
}
}
finally
@ -114,5 +121,19 @@ namespace TShockAPI.Localization
return null;
}
/// <summary>
/// Get buff name in English
/// </summary>
/// <param name="id">Buff Id</param>
/// <returns>Buff name in English</returns>
public static string GetBuffNameById(int id)
{
string buff;
if (Buffs.TryGetValue(id, out buff))
return buff;
return null;
}
}
}