From aeab6d9e4536e75d71b3c23a1519511fb596e65d Mon Sep 17 00:00:00 2001 From: stevenh Date: Tue, 21 Feb 2012 00:19:50 +0000 Subject: [PATCH] Fix for getting Mythical prefix by name fixes #374 Also optimised processing so it only does one pass instead of two as well as minimising string manipulation functions --- TShockAPI/Utils.cs | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/TShockAPI/Utils.cs b/TShockAPI/Utils.cs index b5048f67..ca28491f 100644 --- a/TShockAPI/Utils.cs +++ b/TShockAPI/Utils.cs @@ -29,6 +29,8 @@ namespace TShockAPI { public class Utils { + private readonly static int firstItemPrefix = 1; + private readonly static int lastItemPrefix = 83; // Utils is a Singleton private static readonly Utils instance = new Utils(); private Utils() {} @@ -442,22 +444,32 @@ namespace TShockAPI { Item item = new Item(); item.SetDefaults(0); - for (int i = 1; i < 83; i++) - { - item.prefix = (byte) i; - if (item.AffixName().Trim() == name) - return new List {i}; - } + string lowerName = name.ToLower(); var found = new List(); - for (int i = 1; i < 83; i++) + for (int i = firstItemPrefix; i <= lastItemPrefix; i++) { try { - item.prefix = (byte) i; - if (item.AffixName().Trim().ToLower() == name.ToLower()) - return new List {i}; - if (item.AffixName().Trim().ToLower().StartsWith(name.ToLower())) + item.prefix = (byte)i; + string trimmed = item.AffixName().Trim(); + if (trimmed == name) + { + // Exact match found.Add(i); + return found; + } + else + { + string trimmedLower = trimmed.ToLower(); + if (trimmedLower == lowerName) + { + // Exact match (caseinsensitive) + found.Add(i); + return found; + } + else if (trimmedLower.StartsWith(lowerName)) // Partial match + found.Add(i); + } } catch { @@ -474,7 +486,7 @@ namespace TShockAPI public List GetPrefixByIdOrName(string idOrName) { int type = -1; - if (int.TryParse(idOrName, out type) && type > 0 && type < 84) + if (int.TryParse(idOrName, out type) && type >= firstItemPrefix && type <= lastItemPrefix) { return new List {type}; }