Skip to content

Commit

Permalink
Added item grouping and storage size to void storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Aurocosh committed Mar 17, 2024
1 parent 65aaa77 commit 6504d19
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 23 deletions.
3 changes: 3 additions & 0 deletions Common/Players/SpellwrightStatPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class SpellwrightStatPlayer : ModPlayer
public List<Item> ReagentItems = new();
public int MetaBoostCount { get; set; } = 0;
public bool AutoTorches { get; set; } = false;
public bool GroupStorageByType { get; set; } = false;

public override void PostUpdate()
{
Expand All @@ -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());
Expand All @@ -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<TagCompound>("PotionItems").Select(ItemIO.Load).ToList();
StoredItems = tag.GetList<TagCompound>("StoredItems").Select(ItemIO.Load).ToList();
ReagentItems = tag.GetList<TagCompound>("ReagentItems").Select(ItemIO.Load).ToList();
Expand Down
2 changes: 1 addition & 1 deletion Content/Spells/Storage/Base/StorageSpell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal abstract class StorageSpell : ModSpell
{
protected abstract List<Item> 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)
Expand Down
2 changes: 1 addition & 1 deletion Content/Spells/Storage/ItemVoidSpell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected override List<Item> GetStorage(Player player)
return statPlayer.StoredItems;
}

protected override int StorageSize(int playerLevel)
public override int StorageSize(int playerLevel)
{
return 50 * playerLevel;
}
Expand Down
2 changes: 1 addition & 1 deletion Content/Spells/Storage/PotionVoidSpell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected override List<Item> GetStorage(Player player)
return statPlayer.PotionItems;
}

protected override int StorageSize(int playerLevel)
public override int StorageSize(int playerLevel)
{
return playerLevel * 6;
}
Expand Down
3 changes: 2 additions & 1 deletion Content/Spells/Storage/ReagentVoidSpell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ protected override List<Item> 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;
Expand Down
95 changes: 77 additions & 18 deletions Core/Links/VoidStoragePageHandler.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -9,6 +12,7 @@
using System.Text;
using Terraria;
using Terraria.ID;
using Terraria.Localization;

namespace Spellwright.Core.Links
{
Expand All @@ -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<SpellwrightPlayer>();
var statPlayer = player.GetModPlayer<SpellwrightStatPlayer>();
List<Item> storage;
if (storageType == VoidStorageType.Item)
storage = statPlayer.StoredItems;
else if (storageType == VoidStorageType.Potion)
storage = statPlayer.PotionItems;
else
storage = statPlayer.ReagentItems;
List<Item> storage = GetPlayerStorage(storageType, statPlayer);
StorageSpell storageSpell = GetStorageSpell(storageType);
int storageSize = storageSpell?.StorageSize(spellwrightPlayer.PlayerLevel) ?? 0;

var stringBuilder = new StringBuilder();

Expand All @@ -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);
Expand All @@ -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<Item> 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<ItemVoidSpell>();
return _itemVoidSpell;
}
case VoidStorageType.Potion:
{
_potionVoidSpell ??= SpellLibrary.GetSpellByType<PotionVoidSpell>();
return _potionVoidSpell;
}
case VoidStorageType.Reagent:
{

_reagentVoidSpell ??= SpellLibrary.GetSpellByType<ReagentVoidSpell>();
return _reagentVoidSpell;
}
}
return null;
}

private static void DrinkPotion(Player player, List<Item> storage, int potionType)
{
if (potionType <= 0)
Expand Down
15 changes: 15 additions & 0 deletions Core/Spells/SpellLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ internal class SpellLibrary
{
private static readonly List<ModSpell> registeredSpells = new();
private static readonly Dictionary<int, ModSpell> spellIdMap = new();
private static readonly Dictionary<Type, ModSpell> spellTypeMap = new();
private static readonly Dictionary<string, ModSpell> spellNameMap = new();
private static readonly Dictionary<string, ModSpell> spellIncantationMap = new();
private static readonly MultiValueDictionary<int, string> incantationListMap = new();
Expand All @@ -24,6 +25,7 @@ public static void Refresh()
{
spellIdMap.Clear();
spellNameMap.Clear();
spellTypeMap.Clear();
spellIncantationMap.Clear();
incantationListMap.Clear();

Expand All @@ -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());
Expand All @@ -58,6 +61,7 @@ public static void Unload()
{
spellIdMap.Clear();
spellNameMap.Clear();
spellTypeMap.Clear();
registeredSpells.Clear();
spellIncantationMap.Clear();
incantationListMap.Clear();
Expand All @@ -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<T>() 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)
Expand All @@ -88,6 +102,7 @@ public static ModSpell GetSpellByIncantation(string incantation)
return null;
return spell;
}

public static IReadOnlyList<string> GetSpellIncantationList(int id)
{
if (incantationListMap.TryGetValue(id, out var list))
Expand Down
5 changes: 5 additions & 0 deletions Localization/en-US.hjson
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}
}
Expand Down
2 changes: 1 addition & 1 deletion build.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 6504d19

Please sign in to comment.