From fb5ef9080a6cec7b12d6a8764422b9e4f1856e36 Mon Sep 17 00:00:00 2001 From: Wofsauge Date: Fri, 4 Jun 2021 23:29:27 +0200 Subject: [PATCH] Improved code readability, descObj variable names and added a function to append to descriptions --- eid_api.lua | 44 ++++++++++++++++++++++++++---------------- eid_bagOfCrafting.lua | 6 +++--- eid_modifiers.lua | 44 ++++++++++++++++++++++-------------------- main.lua | 45 ++++++++++++++++++++++--------------------- 4 files changed, 76 insertions(+), 63 deletions(-) diff --git a/eid_api.lua b/eid_api.lua index e7cd8e9b..c41119ca 100644 --- a/eid_api.lua +++ b/eid_api.lua @@ -259,7 +259,7 @@ end -- returns the current text position function EID:getTextPosition() - local posVector = Vector(EID.UsedPosition.X,EID.UsedPosition.Y) + local posVector = Vector(EID.UsedPosition.X, EID.UsedPosition.Y) for a,modifier in pairs(EID.PositionModifiers) do posVector = posVector + modifier end @@ -289,20 +289,25 @@ function EID:getLastDescribedEntity() return EID.lastDescriptionEntity end +-- Appends a given string to the description of a given Description object +function EID:appendToDescription(descObj, appendString) + descObj.Description = descObj.Description..appendString +end + -- returns the description object of the specified entity -- falls back to english if the objID isnt available function EID:getDescriptionObj(Type, Variant, SubType) local description = {} - description.ItemType = Type - description.ItemVariant = Variant - description.ID = SubType - description.fullItemString = Type.."."..Variant.."."..description.ID - description.Name = EID:getObjectName(Type, Variant, description.ID) + description.ObjType = Type + description.ObjVariant = Variant + description.ObjSubType = SubType + description.fullItemString = Type.."."..Variant.."."..description.ObjSubType + description.Name = EID:getObjectName(Type, Variant, description.ObjSubType) - local tableEntry = EID:getDescriptionData(Type, Variant, description.ID) - description.Description = tableEntry and tableEntry[3] or EID:getXMLDescription(Type, Variant, description.ID) + local tableEntry = EID:getDescriptionData(Type, Variant, description.ObjSubType) + description.Description = tableEntry and tableEntry[3] or EID:getXMLDescription(Type, Variant, description.ObjSubType) - description.Transformation = EID:getTransformation(Type, Variant, description.ID) + description.Transformation = EID:getTransformation(Type, Variant, description.ObjSubType) for k,modifier in pairs(EID.DescModifiers) do if modifier.condition(description) then @@ -332,20 +337,24 @@ end -- returns the specified object table in the current language. -- falls back to english if it doesnt exist -function EID:getDescriptionTable(objTable) - return EID.descriptions[EID.Config["Language"]][objTable] or EID.descriptions["en_us"][objTable] +function EID:getDescriptionEntry(objTable, objID) + if not objID then + return EID.descriptions[EID.Config["Language"]][objTable] or EID.descriptions["en_us"][objTable] + else + local translatedTable = EID.descriptions[EID.Config["Language"]][objTable] + return (translatedTable and translatedTable[objID]) or (EID.descriptions["en_us"][objTable] and EID.descriptions["en_us"][objTable][objID]) + end end -- returns the description data table related to a given id, variant and subtype -- falls back to english if it doesnt exist function EID:getDescriptionData(Type, Variant, SubType) local fullString = Type.."."..Variant - local moddedDesc = EID.descriptions[EID.Config["Language"]].custom[fullString.."."..SubType] or - EID.descriptions["en_us"].custom[fullString.."."..SubType] or nil + local moddedDesc = EID:getDescriptionEntry("custom", fullString.."."..SubType) local tableName = EID:getTableName(Type, Variant, SubType) local adjustedID = EID:getAdjustedSubtype(Type, Variant, SubType) local legacyModdedDescription = EID:getLegacyModDescription(Type, Variant, adjustedID) - local defaultDesc = EID.descriptions[EID.Config["Language"]][tableName][adjustedID] or EID.descriptions["en_us"][tableName][adjustedID] or nil + local defaultDesc = EID:getDescriptionEntry(tableName, adjustedID) return moddedDesc or legacyModdedDescription or defaultDesc end @@ -406,7 +415,7 @@ function EID:getTransformationName(id) end return id end - return EID:getDescriptionTable("transformations")[tonumber(id) + 1] or str + return EID:getDescriptionEntry("transformations")[tonumber(id) + 1] or str end -- tries to get the ingame name of an item based on its ID @@ -430,9 +439,9 @@ function EID:getObjectName(Type, Variant, SubType) name = name or EID.itemConfig:GetPillEffect(adjustedSubtype - 1).Name return string.gsub(name,"I'm Excited!!!","I'm Excited!!") -- prevent markup trigger elseif tableName == "sacrifice" then - return EID:getDescriptionTable("sacrificeHeader") + return EID:getDescriptionEntry("sacrificeHeader") elseif tableName == "dice" then - return EID:getDescriptionTable("diceHeader") + return EID:getDescriptionEntry("diceHeader") elseif tableName == "custom" then return name or Type.."."..Variant.."."..SubType end @@ -452,6 +461,7 @@ function EID:getXMLDescription(Type, Variant, SubType) end return desc or "(No Description available)" end + -- check if an entity is part of the describable entities function EID:hasDescription(entity) local isAllowed = false diff --git a/eid_bagOfCrafting.lua b/eid_bagOfCrafting.lua index bb3d12a5..ad2adde1 100644 --- a/eid_bagOfCrafting.lua +++ b/eid_bagOfCrafting.lua @@ -571,9 +571,9 @@ function EID:handleBagOfCraftingRendering() end local customDescObj = EID:getDescriptionObj(5, 100, 710) - local roomDesc = EID.descriptions[EID.Config["Language"]].CraftingRoomContent or EID.descriptions["en_us"].CraftingRoomContent - local bagContentDesc = EID.descriptions[EID.Config["Language"]].CraftingBagContent or EID.descriptions["en_us"].CraftingBagContent - local resultDesc = EID.descriptions[EID.Config["Language"]].CraftingResults or EID.descriptions["en_us"].CraftingResults + local roomDesc = EID:getDescriptionEntry("CraftingRoomContent") + local bagContentDesc = EID:getDescriptionEntry("CraftingBagContent") + local resultDesc = EID:getDescriptionEntry("CraftingResults") local bagContentResult = "" if #EID.BagItems >= 8 then local recipe = EID:calculateBagOfCrafting(EID.BagItems) diff --git a/eid_modifiers.lua b/eid_modifiers.lua index 670a5b39..177e4fde 100644 --- a/eid_modifiers.lua +++ b/eid_modifiers.lua @@ -3,8 +3,9 @@ local game = Game() -- Handle Birthright local function BirthrightCondition(descObj) - return descObj.ItemType == 5 and descObj.ItemVariant == PickupVariant.PICKUP_COLLECTIBLE and descObj.ID == 619 + return descObj.ObjType == 5 and descObj.ObjVariant == PickupVariant.PICKUP_COLLECTIBLE and descObj.ObjSubType == 619 end + local function BirthrightCallback(descObj) descObj.Description = "" local describedPlayerTypes = {} @@ -13,11 +14,10 @@ local game = Game() local playerID = player:GetPlayerType() if not player:IsSubPlayer() and player:GetMainTwin( ):GetPlayerType() == playerID and not describedPlayerTypes[playerID] then describedPlayerTypes[playerID] = true - local translatedDesc = EID.descriptions[EID.Config["Language"]]["birthright"] - local birthrightDesc = (translatedDesc and translatedDesc[playerID+1]) or EID.descriptions["en_us"]["birthright"][playerID+1] or nil + local birthrightDesc = EID:getDescriptionEntry("birthright", playerID+1) if birthrightDesc ~=nil then local playerName = birthrightDesc[1] or player:GetName() - descObj.Description = descObj.Description.."{{CustomTransformation}} {{ColorGray}}"..playerName.."{{CR}}#"..birthrightDesc[3].."#" + EID:appendToDescription(descObj, "{{CustomTransformation}} {{ColorGray}}"..playerName.."{{CR}}#"..birthrightDesc[3].."#") end end end @@ -29,7 +29,7 @@ local game = Game() -- Handle Bingeeater description addition local function BingeeaterCondition(descObj) - if descObj.ItemType ~= 5 or descObj.ItemVariant ~= PickupVariant.PICKUP_COLLECTIBLE then + if descObj.ObjType ~= 5 or descObj.ObjVariant ~= PickupVariant.PICKUP_COLLECTIBLE then return false end for i = 0,game:GetNumPlayers() - 1 do @@ -40,12 +40,12 @@ local game = Game() end return false end + local function BingeeaterCallback(descObj) - local translatedDesc = EID.descriptions[EID.Config["Language"]]["bingeEaterBuffs"] - local bingeBuff = (translatedDesc and translatedDesc[descObj.ID]) or EID.descriptions["en_us"]["bingeEaterBuffs"][descObj.ID] or nil + local bingeBuff = EID:getDescriptionEntry("bingeEaterBuffs", descObj.ObjSubType) if bingeBuff ~= nil then - local bingeStr = "#{{Collectible664}} " - descObj.Description = descObj.Description..bingeStr..bingeBuff[3]:gsub("#",bingeStr) + local iconStr = "#{{Collectible664}} " + EID:appendToDescription(descObj, iconStr..bingeBuff[3]:gsub("#",iconStr)) end return descObj end @@ -55,7 +55,7 @@ local game = Game() -- Handle Spindown Dice description addition local function SpindownDiceCondition(descObj) - if descObj.ItemType ~= 5 or descObj.ItemVariant ~= PickupVariant.PICKUP_COLLECTIBLE then + if descObj.ObjType ~= 5 or descObj.ObjVariant ~= PickupVariant.PICKUP_COLLECTIBLE then return false end for i = 0,game:GetNumPlayers() - 1 do @@ -66,20 +66,21 @@ local game = Game() end return false end + local function SpindownDiceCallback(descObj) - descObj.Description = descObj.Description.."#{{Collectible723}} :" - local refID = descObj.ID + EID:appendToDescription(descObj, "#{{Collectible723}} :") + local refID = descObj.ObjSubType for i = 1,EID.Config["SpindownDiceResults"] do local spinnedID = EID:getSpindownResult(refID) refID = spinnedID if spinnedID > 0 then - descObj.Description = descObj.Description.."{{Collectible"..spinnedID.."}}" + EID:appendToDescription(descObj, "{{Collectible"..spinnedID.."}}") if i ~= EID.Config["SpindownDiceResults"] then - descObj.Description = descObj.Description.." ->" + EID:appendToDescription(descObj, " ->") end else - local errorMsg = EID.descriptions[EID.Config["Language"]]["spindownError"] or EID.descriptions["en_us"]["spindownError"] or nil - descObj.Description = descObj.Description..errorMsg + local errorMsg = EID:getDescriptionEntry("spindownError") or "" + EID:appendToDescription(descObj, errorMsg) break end end @@ -88,9 +89,10 @@ local game = Game() EID:addDescriptionModifier("Spindown Dice", SpindownDiceCondition, SpindownDiceCallback) + -- Handle Tarot Cloth description addition local function TarotClothCondition(descObj) - if descObj.ItemType ~= 5 or descObj.ItemVariant ~= PickupVariant.PICKUP_TAROTCARD then + if descObj.ObjType ~= 5 or descObj.ObjVariant ~= PickupVariant.PICKUP_TAROTCARD then return false end for i = 0,game:GetNumPlayers() - 1 do @@ -101,12 +103,12 @@ local game = Game() end return false end + local function TarotClothCallback(descObj) - local translatedDesc = EID.descriptions[EID.Config["Language"]]["tarotClothBuffs"] or EID.descriptions["en_us"]["tarotClothBuffs"] - local clothBuff = (translatedDesc and translatedDesc[descObj.ID]) or ( translatedDesc and translatedDesc[descObj.ID]) or nil + local clothBuff = EID:getDescriptionEntry("tarotClothBuffs", descObj.ObjSubType) if clothBuff ~= nil then - local bingeStr = "#{{Collectible451}} " - descObj.Description = descObj.Description..bingeStr..clothBuff[3]:gsub("#",bingeStr) + local iconStr = "#{{Collectible451}} " + EID:appendToDescription(descObj, iconStr..clothBuff[3]:gsub("#",iconStr)) end return descObj end diff --git a/main.lua b/main.lua index 96d268a4..12e91a30 100644 --- a/main.lua +++ b/main.lua @@ -211,27 +211,24 @@ end function EID:printDescription(desc) local textScale = Vector(EID.Config["Scale"], EID.Config["Scale"]) local renderPos = EID:getTextPosition() - local itemType = -1 - if tonumber(desc.ID) ~= nil and desc.ItemType == 5 and desc.ItemVariant == 100 then - itemType = EID.itemConfig:GetCollectible(tonumber(desc.ID)).Type or -1 - end local offsetX = 0 if EID.Config["ShowItemIcon"] then local iconType = nil - local renderID = desc.ID - if desc.ItemType == 5 and desc.ItemVariant == 100 then - iconType = "Collectible" - elseif desc.ItemType == 5 and desc.ItemVariant == 350 then - iconType = "Trinket" - elseif desc.ItemType == 5 and desc.ItemVariant == 300 then - iconType = "Card" - elseif desc.ItemType == 5 and desc.ItemVariant == 70 then - iconType = "Pill" + if desc.ObjType == 5 then + if desc.ObjVariant == 100 then + iconType = "Collectible" + elseif desc.ObjVariant == 350 then + iconType = "Trinket" + elseif desc.ObjVariant == 300 then + iconType = "Card" + elseif desc.ObjVariant == 70 then + iconType = "Pill" + end end if iconType ~= nil then offsetX = offsetX + 14 EID:renderString( - "{{" .. iconType .. renderID .. "}}", + "{{" .. iconType .. desc.ObjSubType .. "}}", renderPos + (Vector(-3, -4) * EID.Config["Scale"]), textScale, EID:getNameColor() @@ -239,6 +236,10 @@ function EID:printDescription(desc) end end --Display ItemType / Charge + local itemType = -1 + if tonumber(desc.ObjSubType) ~= nil and desc.ObjType == 5 and desc.ObjVariant == 100 then + itemType = EID.itemConfig:GetCollectible(tonumber(desc.ObjSubType)).Type or -1 + end if EID.Config["ShowItemType"] and (itemType == 3 or itemType == 4) then local offsetY = 2 if EID.Config["Scale"] < 1 then @@ -249,11 +250,11 @@ function EID:printDescription(desc) if itemType == 3 then -- Display Charge offsetX = offsetX + 1 - local curItemConfig = EID.itemConfig:GetCollectible(desc.ID) + local curItemConfig = EID.itemConfig:GetCollectible(desc.ObjSubType) if REPENTANCE and curItemConfig.ChargeType == ItemConfig.CHARGE_TIMED then EID.InlineIconSprite2:SetFrame("pickups", 10) -- Timer Icon - elseif REPENTANCE and (curItemConfig.ChargeType == ItemConfig.CHARGE_SPECIAL or desc.ID == CollectibleType.COLLECTIBLE_BLANK_CARD or desc.ID == CollectibleType.COLLECTIBLE_PLACEBO or - desc.ID == CollectibleType.COLLECTIBLE_CLEAR_RUNE or desc.ID == CollectibleType.COLLECTIBLE_D_INFINITY) then + elseif REPENTANCE and (curItemConfig.ChargeType == ItemConfig.CHARGE_SPECIAL or desc.ObjSubType == CollectibleType.COLLECTIBLE_BLANK_CARD or desc.ObjSubType == CollectibleType.COLLECTIBLE_PLACEBO or + desc.ObjSubType == CollectibleType.COLLECTIBLE_CLEAR_RUNE or desc.ObjSubType == CollectibleType.COLLECTIBLE_D_INFINITY) then EID.InlineIconSprite2:SetFrame("numbers", 13) else EID.InlineIconSprite2:SetFrame("numbers", curItemConfig.MaxCharges) @@ -274,7 +275,7 @@ function EID:printDescription(desc) if EID.Config["TranslateItemName"] ~= 2 then local curLanguage = EID.Config["Language"] EID.Config["Language"] = "en_us" - local englishName = EID:getObjectName(desc.ItemType, desc.ItemVariant, desc.ID) + local englishName = EID:getObjectName(desc.ObjType, desc.ObjVariant, desc.ObjSubType) EID.Config["Language"] = curLanguage if EID.Config["TranslateItemName"] == 1 then curName = englishName @@ -282,8 +283,8 @@ function EID:printDescription(desc) curName = curName.." ("..englishName..")" end end - if REPENTANCE and EID.Config["ShowQuality"] and desc.ItemVariant == PickupVariant.PICKUP_COLLECTIBLE then - local quality = tonumber(EID.itemConfig:GetCollectible(tonumber(desc.ID)).Quality) + if REPENTANCE and EID.Config["ShowQuality"] and desc.ObjVariant == PickupVariant.PICKUP_COLLECTIBLE then + local quality = tonumber(EID.itemConfig:GetCollectible(tonumber(desc.ObjSubType)).Quality) curName = curName.." - {{Quality"..quality.."}}" end EID:renderString( @@ -564,7 +565,7 @@ local function onRender(t) end local descriptionObj = EID:getDescriptionObj(closest.Type, closest.Variant, trinketID) if isGoldenTrinket then - local goldenDesc = EID.descriptions[EID.Config["Language"]].goldenTrinket or EID.descriptions["en_us"].goldenTrinket or "" + local goldenDesc = EID:getDescriptionEntry("goldenTrinket") or "" descriptionObj.Description = "{{ColorGold}}"..goldenDesc.."#"..descriptionObj.Description end EID:printDescription(descriptionObj) @@ -615,7 +616,7 @@ local function onRender(t) EID:printDescription(descEntry) else EID:renderString( - "{{Pill"..pillColor.."}} "..EID:getDescriptionTable("unidentifiedPill"), + "{{Pill"..pillColor.."}} "..EID:getDescriptionEntry("unidentifiedPill"), EID:getTextPosition(), Vector(EID.Config["Scale"], EID.Config["Scale"]), EID:getErrorColor()