From 6504d19d63b0dd7a8ca6d4d58560f9c18454e14a Mon Sep 17 00:00:00 2001 From: Aurocosh Date: Sun, 17 Mar 2024 11:06:02 +0700 Subject: [PATCH] Added item grouping and storage size to void storage --- Common/Players/SpellwrightStatPlayer.cs | 3 + Content/Spells/Storage/Base/StorageSpell.cs | 2 +- Content/Spells/Storage/ItemVoidSpell.cs | 2 +- Content/Spells/Storage/PotionVoidSpell.cs | 2 +- Content/Spells/Storage/ReagentVoidSpell.cs | 3 +- Core/Links/VoidStoragePageHandler.cs | 95 +++++++++++++++++---- Core/Spells/SpellLibrary.cs | 15 ++++ Localization/en-US.hjson | 5 ++ build.txt | 2 +- 9 files changed, 106 insertions(+), 23 deletions(-) diff --git a/Common/Players/SpellwrightStatPlayer.cs b/Common/Players/SpellwrightStatPlayer.cs index 82e2194..154afcc 100644 --- a/Common/Players/SpellwrightStatPlayer.cs +++ b/Common/Players/SpellwrightStatPlayer.cs @@ -17,6 +17,7 @@ public class SpellwrightStatPlayer : ModPlayer public List ReagentItems = new(); public int MetaBoostCount { get; set; } = 0; public bool AutoTorches { get; set; } = false; + public bool GroupStorageByType { get; set; } = false; public override void PostUpdate() { @@ -38,6 +39,7 @@ public override void SaveData(TagCompound tag) { tag.Add("MetaBoostCount", MetaBoostCount); tag.Add("AutoTorches", AutoTorches); + tag.Add("GroupStorageByType", GroupStorageByType); tag.Add("PotionItems", PotionItems.Select(ItemIO.Save).ToList()); tag.Add("StoredItems", StoredItems.Select(ItemIO.Save).ToList()); tag.Add("ReagentItems", ReagentItems.Select(ItemIO.Save).ToList()); @@ -47,6 +49,7 @@ public override void LoadData(TagCompound tag) { MetaBoostCount = tag.GetInt("MetaBoostCount"); AutoTorches = tag.GetBool("AutoTorches"); + GroupStorageByType = tag.GetBool("GroupStorageByType"); PotionItems = tag.GetList("PotionItems").Select(ItemIO.Load).ToList(); StoredItems = tag.GetList("StoredItems").Select(ItemIO.Load).ToList(); ReagentItems = tag.GetList("ReagentItems").Select(ItemIO.Load).ToList(); diff --git a/Content/Spells/Storage/Base/StorageSpell.cs b/Content/Spells/Storage/Base/StorageSpell.cs index e1133a8..a867b50 100644 --- a/Content/Spells/Storage/Base/StorageSpell.cs +++ b/Content/Spells/Storage/Base/StorageSpell.cs @@ -13,7 +13,7 @@ internal abstract class StorageSpell : ModSpell { protected abstract List GetStorage(Player player); protected abstract bool CanAccept(Item item); - protected abstract int StorageSize(int playerLevel); + public abstract int StorageSize(int playerLevel); protected abstract InventoryArea IncludedArea(); public override bool Cast(Player player, int playerLevel, SpellData spellData) diff --git a/Content/Spells/Storage/ItemVoidSpell.cs b/Content/Spells/Storage/ItemVoidSpell.cs index 17b07f8..c7645f6 100644 --- a/Content/Spells/Storage/ItemVoidSpell.cs +++ b/Content/Spells/Storage/ItemVoidSpell.cs @@ -37,7 +37,7 @@ protected override List GetStorage(Player player) return statPlayer.StoredItems; } - protected override int StorageSize(int playerLevel) + public override int StorageSize(int playerLevel) { return 50 * playerLevel; } diff --git a/Content/Spells/Storage/PotionVoidSpell.cs b/Content/Spells/Storage/PotionVoidSpell.cs index b301583..82d5ca0 100644 --- a/Content/Spells/Storage/PotionVoidSpell.cs +++ b/Content/Spells/Storage/PotionVoidSpell.cs @@ -37,7 +37,7 @@ protected override List GetStorage(Player player) return statPlayer.PotionItems; } - protected override int StorageSize(int playerLevel) + public override int StorageSize(int playerLevel) { return playerLevel * 6; } diff --git a/Content/Spells/Storage/ReagentVoidSpell.cs b/Content/Spells/Storage/ReagentVoidSpell.cs index cc63998..7854243 100644 --- a/Content/Spells/Storage/ReagentVoidSpell.cs +++ b/Content/Spells/Storage/ReagentVoidSpell.cs @@ -46,10 +46,11 @@ protected override List GetStorage(Player player) return statPlayer.ReagentItems; } - protected override int StorageSize(int playerLevel) + public override int StorageSize(int playerLevel) { return playerLevel; } + protected override InventoryArea IncludedArea() { return InventoryArea.All; diff --git a/Core/Links/VoidStoragePageHandler.cs b/Core/Links/VoidStoragePageHandler.cs index ae15c05..a962757 100644 --- a/Core/Links/VoidStoragePageHandler.cs +++ b/Core/Links/VoidStoragePageHandler.cs @@ -1,6 +1,9 @@ using Microsoft.Xna.Framework; using Spellwright.Common.Players; +using Spellwright.Content.Spells.Storage; +using Spellwright.Content.Spells.Storage.Base; using Spellwright.Core.Links.Base; +using Spellwright.Core.Spells; using Spellwright.Extensions; using Spellwright.UI.Components.TextBox.Text; using Spellwright.Util; @@ -9,6 +12,7 @@ using System.Text; using Terraria; using Terraria.ID; +using Terraria.Localization; namespace Spellwright.Core.Links { @@ -25,18 +29,19 @@ public VoidStoragePageHandler() { } + private ItemVoidSpell _itemVoidSpell = null; + private PotionVoidSpell _potionVoidSpell = null; + private ReagentVoidSpell _reagentVoidSpell = null; + public override string ProcessLink(ref LinkData linkData, Player player) { VoidStorageType storageType = linkData.GetId(VoidStorageType.Item); + var spellwrightPlayer = player.GetModPlayer(); var statPlayer = player.GetModPlayer(); - List storage; - if (storageType == VoidStorageType.Item) - storage = statPlayer.StoredItems; - else if (storageType == VoidStorageType.Potion) - storage = statPlayer.PotionItems; - else - storage = statPlayer.ReagentItems; + List storage = GetPlayerStorage(storageType, statPlayer); + StorageSpell storageSpell = GetStorageSpell(storageType); + int storageSize = storageSpell?.StorageSize(spellwrightPlayer.PlayerLevel) ?? 0; var stringBuilder = new StringBuilder(); @@ -60,8 +65,20 @@ public override string ProcessLink(ref LinkData linkData, Player player) } } + if (linkData.HasParameter("group")) + { + statPlayer.GroupStorageByType = !statPlayer.GroupStorageByType; + linkData.RemoveParameter("group"); + } + var countTitle = GetTranslation("ItemsInStorage").Format(storage.Count).AsFormText().WithColor(Color.Gray); stringBuilder.AppendLine(countTitle.ToString()); + var storageSizeTitle = GetTranslation("StorageSize").Format(storageSize).AsFormText().WithColor(Color.Gray); + stringBuilder.AppendLine(storageSizeTitle.ToString()); + + string groupTrKey = statPlayer.GroupStorageByType ? "ItemsGrouped" : "ItemsNotGrouped"; + var groupTitle = GetFormText(groupTrKey).WithLink("VoidStorage", storageType).WithParam("group").WithColor(Color.Orange).ToString(); + stringBuilder.AppendLine(groupTitle.ToString()); stringBuilder.AppendLine(); var sortedStorage = storage.OrderBy(x => x.Name).ThenByDescending(x => x.stack); @@ -75,25 +92,67 @@ group item by item.type into itemGroup foreach (var value in displayedItems) { - var item = value.First(); - int count = value.Sum(x => x.stack); + var firstItem = value.First(); + var totalCount = value.Sum(x => x.stack); + string name = Lang.GetItemNameValue(firstItem.type); - string name = Lang.GetItemNameValue(item.type); - if (storageType == VoidStorageType.Potion) + var itemsToDisplay = value.AsEnumerable(); + if (statPlayer.GroupStorageByType) + itemsToDisplay = Enumerable.Repeat(firstItem, 1); + + foreach (var nextItem in itemsToDisplay) { - string statusText = item.favorited ? "Unlocked" : "Locked"; - Color color = item.favorited ? Color.DarkOrange : Color.DarkGray; - string lockText = new FormattedText(statusText, color).WithLink("VoidStorage", storageType).WithParam("fav", item.type).ToString(); - string drinkLink = new FormattedText("Drink", Color.Gray).WithLink("VoidStorage", storageType).WithParam("drink", item.type).ToString(); - stringBuilder.Append($" ({lockText}, {drinkLink}) - "); + int count = statPlayer.GroupStorageByType ? totalCount : nextItem.stack; + if (storageType == VoidStorageType.Potion) + { + string statusText = firstItem.favorited ? "Unlocked" : "Locked"; + Color color = firstItem.favorited ? Color.DarkOrange : Color.DarkGray; + string lockText = new FormattedText(statusText, color).WithLink("VoidStorage", storageType).WithParam("fav", firstItem.type).ToString(); + string drinkLink = new FormattedText("Drink", Color.Gray).WithLink("VoidStorage", storageType).WithParam("drink", firstItem.type).ToString(); + stringBuilder.Append($" ({lockText}, {drinkLink}) - "); + } + + stringBuilder.Append($"{name} ({count})"); + stringBuilder.AppendLine(); } - stringBuilder.Append($"{name} ({count})"); - stringBuilder.AppendLine(); } return stringBuilder.ToString(); } + private static List GetPlayerStorage(VoidStorageType storageType, SpellwrightStatPlayer statPlayer) + { + if (storageType == VoidStorageType.Item) + return statPlayer.StoredItems; + else if (storageType == VoidStorageType.Potion) + return statPlayer.PotionItems; + else + return statPlayer.ReagentItems; + } + + private StorageSpell GetStorageSpell(VoidStorageType storageType) + { + switch (storageType) { + case VoidStorageType.Item: + { + _itemVoidSpell ??= SpellLibrary.GetSpellByType(); + return _itemVoidSpell; + } + case VoidStorageType.Potion: + { + _potionVoidSpell ??= SpellLibrary.GetSpellByType(); + return _potionVoidSpell; + } + case VoidStorageType.Reagent: + { + + _reagentVoidSpell ??= SpellLibrary.GetSpellByType(); + return _reagentVoidSpell; + } + } + return null; + } + private static void DrinkPotion(Player player, List storage, int potionType) { if (potionType <= 0) diff --git a/Core/Spells/SpellLibrary.cs b/Core/Spells/SpellLibrary.cs index 3ebff1a..5b2bac0 100644 --- a/Core/Spells/SpellLibrary.cs +++ b/Core/Spells/SpellLibrary.cs @@ -11,6 +11,7 @@ internal class SpellLibrary { private static readonly List registeredSpells = new(); private static readonly Dictionary spellIdMap = new(); + private static readonly Dictionary spellTypeMap = new(); private static readonly Dictionary spellNameMap = new(); private static readonly Dictionary spellIncantationMap = new(); private static readonly MultiValueDictionary incantationListMap = new(); @@ -24,6 +25,7 @@ public static void Refresh() { spellIdMap.Clear(); spellNameMap.Clear(); + spellTypeMap.Clear(); spellIncantationMap.Clear(); incantationListMap.Clear(); @@ -32,6 +34,7 @@ public static void Refresh() { spellIdMap.Add(modSpell.Type, modSpell); spellNameMap.Add(modSpell.Name, modSpell); + spellTypeMap.Add(modSpell.GetType(), modSpell); var defaultIncantation = GetDefaultIncantation(modSpell); incantations.Add(defaultIncantation.ToLower()); @@ -58,6 +61,7 @@ public static void Unload() { spellIdMap.Clear(); spellNameMap.Clear(); + spellTypeMap.Clear(); registeredSpells.Clear(); spellIncantationMap.Clear(); incantationListMap.Clear(); @@ -74,12 +78,22 @@ public static ModSpell GetSpellById(int id) return modSpell; return null; } + public static ModSpell GetSpellByName(string name) { if (spellNameMap.TryGetValue(name, out var modSpell)) return modSpell; return null; } + + public static T GetSpellByType() where T: ModSpell + { + Type type = typeof(T); + if (spellTypeMap.TryGetValue(type, out var modSpell)) + return (T)modSpell; + return null; + } + public static ModSpell GetSpellByIncantation(string incantation) { if (incantation == null) @@ -88,6 +102,7 @@ public static ModSpell GetSpellByIncantation(string incantation) return null; return spell; } + public static IReadOnlyList GetSpellIncantationList(int id) { if (incantationListMap.TryGetValue(id, out var list)) diff --git a/Localization/en-US.hjson b/Localization/en-US.hjson index cf7b0b2..b7086aa 100644 --- a/Localization/en-US.hjson +++ b/Localization/en-US.hjson @@ -43,6 +43,8 @@ Mods: { Or: Or And: And Level: Level + True: True + False: False StoredItems: Stored items SpellsLearned: Spells learned: {0} SpellLevelTooLow: My spellcasting level is too low @@ -327,6 +329,9 @@ Mods: { ReagentVoid: Reagent Void Locked: Locked Unlocked: Unlocked + ItemsNotGrouped: Items are not grouped + ItemsGrouped: Items are grouped by type + StorageSize: Storage size: {0} StorageStatus: Storage status: {0} ItemsInStorage: Items in storage: {0} } diff --git a/build.txt b/build.txt index 89ebef3..f83cc37 100644 --- a/build.txt +++ b/build.txt @@ -1,6 +1,6 @@ displayName = Spellwright author = Aurocosh -version = 0.8.2.4 +version = 0.8.2.5 dllReferences = NetSerializer languageVersion = 7 buildIgnore = *.csproj, *.sln, *.user, *.md, obj\*, bin\*, .vs\*, .git\*, .gitignore