From 76a3b181790dbc8f03ec91539f4c8d9ca2a095d0 Mon Sep 17 00:00:00 2001 From: Wofsauge Date: Mon, 3 Jun 2024 22:57:38 +0200 Subject: [PATCH] Added EID:ReplaceVariableStr() to make text variable replacements easier --- eid_api.lua | 6 ++++++ eid_bagofcrafting.lua | 4 ++-- eid_modifiers.lua | 10 +++++----- eid_repentogon.lua | 4 ++-- eid_tmtrainer.lua | 8 ++++---- 5 files changed, 19 insertions(+), 13 deletions(-) diff --git a/eid_api.lua b/eid_api.lua index 0f953034..549bd313 100644 --- a/eid_api.lua +++ b/eid_api.lua @@ -2048,3 +2048,9 @@ function EID:UpdateAllPlayerPassiveItems() end return listUpdatedForPlayers end + +-- Replaces Variable placeholders in string with a given value +-- Example: "My {1} message" --> "My test message" +function EID:ReplaceVariableStr(str, varID, newString) + return str:gsub("{"..varID.."}", newString) +end diff --git a/eid_bagofcrafting.lua b/eid_bagofcrafting.lua index d7803820..2b2c9046 100644 --- a/eid_bagofcrafting.lua +++ b/eid_bagofcrafting.lua @@ -1172,7 +1172,7 @@ function EID:handleBagOfCraftingRendering(ignoreRefreshRate) --filteredRecipesList is a table of tables for each item, so we have to iterate over the table using sortedIDs if (bagOfCraftingOffset > 0) then - prevDesc = prevDesc .. prefix .. moreDesc:gsub("{1}", bagOfCraftingOffset) + prevDesc = prevDesc .. prefix .. EID:ReplaceVariableStr(moreDesc, 1, bagOfCraftingOffset) end local curOffset = 0 refreshPosition = -1 @@ -1207,7 +1207,7 @@ function EID:handleBagOfCraftingRendering(ignoreRefreshRate) end end if (bagOfCraftingOffset + EID.Config["BagOfCraftingResults"] < filteredNumResults) then - prevDesc = prevDesc .. prefix .. moreDesc:gsub("{1}",(filteredNumResults-EID.Config["BagOfCraftingResults"]-bagOfCraftingOffset)) + prevDesc = prevDesc .. prefix .. EID:ReplaceVariableStr(moreDesc, 1, (filteredNumResults-EID.Config["BagOfCraftingResults"]-bagOfCraftingOffset)) elseif lockedResults then prevDesc = prevDesc .. prefix end diff --git a/eid_modifiers.lua b/eid_modifiers.lua index 6178bf48..736c70d6 100644 --- a/eid_modifiers.lua +++ b/eid_modifiers.lua @@ -129,7 +129,7 @@ local function VoidCallback(descObj, isRune) if v > 0 then local statIncreaseStr = "↑ " .. voidStatIcons[i] .. " " .. voidNames[i] .. "#" local replaceCount = 0 - statIncreaseStr, replaceCount = string.gsub(statIncreaseStr, "{1}", "+" .. string.format("%.4g",v*voidStatUps[i])) + statIncreaseStr, replaceCount = EID:ReplaceVariableStr(statIncreaseStr, 1, "+" .. string.format("%.4g",v*voidStatUps[i])) if replaceCount == 0 then statIncreaseStr = "↑ " .. voidStatIcons[i] .. " +" .. string.format("%.4g",v*voidStatUps[i]) .. " " .. voidNames[i] .. "#" end EID:appendToDescription(descObj, statIncreaseStr) end @@ -284,8 +284,8 @@ local function BlackFeatherCallback(descObj) local dmgColor = (hasBox or isGolden) and "ColorGold" or "ColorLime" local description = EID:getDescriptionEntry("BlackFeatherInformation") - description, _ = string.gsub(description, "{1}", tostring(itemCounter)) - description, _ = string.gsub(description, "{2}", "{{"..dmgColor.."}}"..damageMultiplied.."{{CR}}") + description, _ = EID:ReplaceVariableStr(description, 1, tostring(itemCounter)) + description, _ = EID:ReplaceVariableStr(description, 2, "{{"..dmgColor.."}}"..damageMultiplied.."{{CR}}") EID:appendToDescription(descObj, "# "..description) return descObj @@ -441,7 +441,7 @@ if EID.isRepentance then for i,v in ipairs(statsToDisplay) do local statIncreaseStr = "↑ " .. voidStatIcons[v] .. " " .. voidNames[v] local replaceCount = 0 - statIncreaseStr, replaceCount = string.gsub(statIncreaseStr, "{1}", "+" .. string.format("%.4g",voidStatUps[v])) + statIncreaseStr, replaceCount = EID:ReplaceVariableStr(statIncreaseStr, 1, "+" .. string.format("%.4g",voidStatUps[v])) if replaceCount == 0 then statIncreaseStr = "↑ " .. voidStatIcons[v] .. " +" .. string.format("%.4g",voidStatUps[v]) .. " " .. voidNames[v] end newStr = newStr .. statIncreaseStr if #statsToDisplay > 1 then newStr = newStr .. "?" end @@ -506,7 +506,7 @@ if EID.isRepentance then end end if hasCarBattery then - EID:appendToDescription(descObj, " " .. EID:getDescriptionEntry("ResultsWithX"):gsub("{1}", "{{Collectible356}}")) + EID:appendToDescription(descObj, " " .. EID:ReplaceVariableStr(EID:getDescriptionEntry("ResultsWithX"), 1, "{{Collectible356}}")) end if firstID ~= 0 and EID.TabPreviewID == 0 then EID.TabPreviewID = firstID diff --git a/eid_repentogon.lua b/eid_repentogon.lua index 68686224..55baa207 100644 --- a/eid_repentogon.lua +++ b/eid_repentogon.lua @@ -184,8 +184,8 @@ local function DonationMachineCallback(descObj) end end - descObj.Description = string.gsub(descObj.Description, "{1}", coinsNeeded) - descObj.Description = string.gsub(descObj.Description, "{2}", coinsNeeded - totalDonations) + descObj.Description = EID:ReplaceVariableStr(descObj.Description, 1, coinsNeeded) + descObj.Description = EID:ReplaceVariableStr(descObj.Description, 2, coinsNeeded - totalDonations) descObj.Icon = EID.InlineIcons["DonationMachine"] return descObj end diff --git a/eid_tmtrainer.lua b/eid_tmtrainer.lua index 7641c335..cd8e7bc1 100644 --- a/eid_tmtrainer.lua +++ b/eid_tmtrainer.lua @@ -89,7 +89,7 @@ function EID:CheckGlitchedItemConfig(id) local s = string.format("%.4g",val) local prefix = "↑ " if val > 0 then s = "+" .. s else prefix = "↓ " end - attributes = attributes .. prefix .. string.gsub(localizedNames[v] or localizedNamesEnglish[v], "{1}", s) + attributes = attributes .. prefix .. EID:ReplaceVariableStr(localizedNames[v] or localizedNamesEnglish[v], 1, s) if val ~= 1 and val ~= -1 then attributes = attributes .. localizedNames["pluralize"] or localizedNamesEnglish["pluralize"] end attributes = attributes .. "#" end @@ -126,7 +126,7 @@ function EID:CheckGlitchedItemConfig(id) local s = string.format("%.2g",SimpleRound(val)) local prefix = "↑ " if val > 0 then s = "+" .. s else prefix = "↓ " end - attributes = attributes .. prefix .. string.gsub(voidNames[i], "{1}", s) .. "#" + attributes = attributes .. prefix .. EID:ReplaceVariableStr(voidNames[i], 1, s) .. "#" end end @@ -173,8 +173,8 @@ function EID:CheckGlitchedItemConfig(id) attributes = attributes .. "#" end attributes = attributes .. localizedNames[effectType] or localizedNamesEnglish[effectType] .. "#" - for k,v in ipairs(triggerReplacements) do attributes = string.gsub(attributes, "{T" .. k .. "}", v) end - for k,v in ipairs(replacements) do attributes = string.gsub(attributes, "{" .. k .. "}", v) end + for k,v in ipairs(triggerReplacements) do attributes = EID:ReplaceVariableStr(attributes, "T" .. k, v) end + for k,v in ipairs(replacements) do attributes = EID:ReplaceVariableStr(attributes, k, v) end if effectTrigger ~= "chain" then lastEffectTrigger = effectTrigger end end