Skip to content

Commit

Permalink
Added integration with AndroLib
Browse files Browse the repository at this point in the history
  • Loading branch information
Aurocosh committed Mar 20, 2024
1 parent 55c710b commit 30b1765
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public bool CanConsume(Player player, float costModifier)
if (realCost <= 0)
return true;

var allItems = player.GetInventoryItems().Concat(statPlayer.ReagentItems);
var allItems = statPlayer.ReagentItems.Concat(player.GetInventoryItems()).Concat(player.IterateAllVacuumBagItems());
if (!UtilInventory.HasItems(allItems, itemType, realCost))
return false;
}
Expand Down Expand Up @@ -90,7 +90,7 @@ public override bool Consume(Player player, int playerLevel, SpellData spellData
if (realCost <= 0)
return true;

var allItems = player.GetInventoryItems().Concat(statPlayer.ReagentItems.OrderBy(x => x.stack));
var allItems = statPlayer.ReagentItems.Concat(player.GetInventoryItems()).Concat(player.IterateAllVacuumBagItems());
UtilInventory.ConsumeItems(allItems, itemType, realCost);
}

Expand Down
3 changes: 1 addition & 2 deletions Content/Spells/Base/SpellCosts/Reagent/ReagentSpellCost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ public override bool Consume(Player player, int playerLevel, SpellData spellData
return true;

var statPlayer = player.GetModPlayer<SpellwrightStatPlayer>();

var allItems = player.GetInventoryItems().Concat(statPlayer.ReagentItems.OrderBy(x => x.stack));
var allItems = statPlayer.ReagentItems.Concat(player.GetInventoryItems()).Concat(player.IterateAllVacuumBagItems());
if (!UtilInventory.ConsumeItems(allItems, ItemType, realCost))
{
var itemName = Lang.GetItemNameValue(ItemType);
Expand Down
9 changes: 9 additions & 0 deletions Content/Spells/Storage/Base/StorageSpell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,18 @@ private bool PushItems(Player player, int maxStorageSize, List<Item> storage)
}
}

storage.Sort(ItemSortOrder);
return storedAtLeastOne;
}

private static int ItemSortOrder(Item item1, Item item2)
{
int result = item1.type.CompareTo(item2.type);
if (result != 0)
return result;
return item1.stack.CompareTo(item2.stack);
}

public override bool ProcessExtraData(Player player, SpellStructure structure, out object extraData)
{
StorageAction action = StorageAction.Invalid;
Expand Down
2 changes: 0 additions & 2 deletions Core/Links/VoidStoragePageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ private StorageSpell GetStorageSpell(VoidStorageType storageType)
}
case VoidStorageType.Reagent:
{

_reagentVoidSpell ??= SpellLibrary.GetSpellByType<ReagentVoidSpell>();
return _reagentVoidSpell;
}
Expand All @@ -162,7 +161,6 @@ private static void DrinkPotion(Player player, List<Item> storage, int potionTyp
var potions =
from item in storage
where item.type == potionType && item.stack > 0
orderby item.stack ascending
select item;

var firstItem = potions.FirstOrDefault();
Expand Down
13 changes: 13 additions & 0 deletions Extensions/ItemExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Terraria;
using Terraria.ID;

namespace Spellwright.Extensions
{
Expand All @@ -10,5 +11,17 @@ public static bool IsTheSameAs(this Item item, Item compareItem)
return item.type == compareItem.type;
return false;
}

public static bool IsValidItem(this Item item)
{
return item.type != ItemID.None && item.stack > 0;
}

public static void Consume(this Item item, int amount)
{
item.stack -= amount;
if (item.stack <= 0)
item.TurnToAir();
}
}
}
18 changes: 18 additions & 0 deletions Extensions/PlayerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,24 @@ public static int CountItems(this Player player, Func<Item, bool> filter, Invent
return UtilInventory.CountItems(items, filter);
}

public static IEnumerable<Item> IterateAllVacuumBagItems(this Player player)
{
if (!Spellwright.Instance.IntegAndroLib.IsEnabled)
yield break;

foreach (Item item in player.GetInventoryItems(InventoryArea.MainSlots | InventoryArea.Ammo))
{
if (item.IsValidItem() && Spellwright.Instance.IntegAndroLib.TryGetStorageByItemId(item.type, out Item[] itemStorage))
{
foreach (Item internalItem in itemStorage)
{
if (internalItem.IsValidItem())
yield return internalItem;
}
}
}
}

public static void ClearBuffs(this Player player, IEnumerable<int> buffTypes)
{
var buffHash = new HashSet<int>(buffTypes);
Expand Down
122 changes: 122 additions & 0 deletions Integration/AndroLibIntegration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
using System.Collections.Generic;
using Terraria;
using Terraria.ModLoader;

namespace Spellwright.Integration
{
internal class AndroLibIntegration
{
public const string LibName = "androLib";
public Mod ModObject { get; }
public bool IsEnabled { get; }

private readonly Dictionary<int, int> _itemIdToStorageMap = new();

public AndroLibIntegration()
{
IsEnabled = ModLoader.TryGetMod(LibName, out var androLib);
ModObject = androLib;

if (IsEnabled)
ReloadContainerIds();
}

public bool TryGetStorageByItemId(int itemId, out Item[] storageItems)
{
if (!_itemIdToStorageMap.TryGetValue(itemId, out int storageId))
{
storageItems = null;
return false;
}

storageItems = GetItems(storageId);
return true;
}

public void ReloadContainerIds()
{
if (!IsEnabled)
return;

_itemIdToStorageMap.Clear();
RegisterStorage("AmmoBag", "VacuumBags");
RegisterStorage("BannerBag", "VacuumBags");
RegisterStorage("BossBag", "VacuumBags");
RegisterStorage("BuildersBox", "VacuumBags");
RegisterStorage("ExquisitePotionFlask", "VacuumBags", "PotionFlask");
RegisterStorage("FishingBelt", "VacuumBags");
RegisterStorage("HerbSatchel", "VacuumBags");
RegisterStorage("JarOfDirt", "VacuumBags");
RegisterStorage("MechanicsToolbelt", "VacuumBags");
RegisterStorage("PaintBucket", "VacuumBags");
RegisterStorage("PortableStation", "VacuumBags");
RegisterStorage("PotionFlask", "VacuumBags");
RegisterStorage("SlayersSack", "VacuumBags");
RegisterStorage("TrashCan", "VacuumBags");
RegisterStorage("WallEr", "VacuumBags");

RegisterStorage("BagBlack", "VacuumBags");
RegisterStorage("BagBlue", "VacuumBags");
RegisterStorage("BagBrown", "VacuumBags");
RegisterStorage("BagGray", "VacuumBags");
RegisterStorage("BagGreen", "VacuumBags");
RegisterStorage("BagOrange", "VacuumBags");
RegisterStorage("BagPink", "VacuumBags");
RegisterStorage("BagPurple", "VacuumBags");
RegisterStorage("BagRed", "VacuumBags");
RegisterStorage("BagWhite", "VacuumBags");
RegisterStorage("BagYellow", "VacuumBags");

RegisterStorage("PackBlack", "VacuumBags", "BagBlack");
RegisterStorage("PackBlue", "VacuumBags", "BagBlue");
RegisterStorage("PackBrown", "VacuumBags", "BagBrown");
RegisterStorage("PackGray", "VacuumBags", "BagGray");
RegisterStorage("PackGreen", "VacuumBags", "BagGreen");
RegisterStorage("PackOrange", "VacuumBags", "BagOrange");
RegisterStorage("PackPink", "VacuumBags", "BagPink");
RegisterStorage("PackPurple", "VacuumBags", "BagPurple");
RegisterStorage("PackRed", "VacuumBags", "BagRed");
RegisterStorage("PackWhite", "VacuumBags", "BagWhite");
RegisterStorage("PackYellow", "VacuumBags", "BagYellow");

RegisterStorage("CalamitousCauldron", "VacuumBags");
RegisterStorage("EarthenPyramid", "VacuumBags");
RegisterStorage("EssenceOfGathering", "VacuumBags");
RegisterStorage("FargosMementos", "VacuumBags");
RegisterStorage("HoiPoiCapsule", "VacuumBags");
RegisterStorage("LokisTesseract", "VacuumBags");
RegisterStorage("SpookyGourd", "VacuumBags");

RegisterStorage("OreBag", "VacuumBags", "OreBag", "VacuumOreBag");
}

private void RegisterStorage(string itemName, string modName, string storageName = null, string storageModName = null)
{
storageName ??= itemName;
storageModName ??= modName;

if (ModContent.TryFind<ModItem>(modName, itemName, out var item))
{
int storageId = GetStorageId(storageName, storageModName);
if (storageId >= 0)
_itemIdToStorageMap.Add(item.Item.type, storageId);
else
Spellwright.Instance.Logger.Info($"Unable to get a storage id for: {modName} - {itemName}.");
}
else
{
Spellwright.Instance.Logger.Info($"Unable to find storage item: {modName} - {itemName}.");
}
}

private int GetStorageId(string itemName, string modName)
{
return (int)ModObject.Call("GetStorageID", modName, itemName);
}

private Item[] GetItems(int storageId)
{
return (Item[])ModObject.Call("GetItems", storageId);
}
}
}
12 changes: 8 additions & 4 deletions Spellwright.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.Xna.Framework;
using Spellwright.Content.Spells;
using Spellwright.Core.Spells;
using Spellwright.Integration;
using Spellwright.Network.NetworkActions;
using Spellwright.UI.Components;
using Spellwright.UI.States;
Expand All @@ -17,12 +18,13 @@ namespace Spellwright
{
public class Spellwright : Mod
{
internal static Spellwright Instance;
internal static Spellwright Instance { get; private set; }

internal UISpellInputState spellInputState;
internal UIMessageState uiMessageState;
internal UISpellInputState spellInputState { get; private set; }
internal UIMessageState uiMessageState { get; private set; }
internal UserInterface userInterface { get; private set; }

internal UserInterface userInterface;
internal AndroLibIntegration IntegAndroLib { get; private set; }

public Spellwright()
{
Expand All @@ -49,6 +51,7 @@ public override void PostSetupContent()
NetworkAction.Load(this);
SpellLibrary.Refresh();
SpellModifiersProcessor.Initialize();
IntegAndroLib = new AndroLibIntegration();
}

public override void Unload()
Expand All @@ -59,6 +62,7 @@ public override void Unload()
spellInputState = null;
uiMessageState = null;
userInterface = null;
IntegAndroLib = null;

NetworkAction.Unload();
SpellLoader.Unload();
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.9
version = 0.8.3.0
dllReferences = NetSerializer
languageVersion = 7
buildIgnore = *.csproj, *.sln, *.user, *.md, obj\*, bin\*, .vs\*, .git\*, .gitignore
Expand Down

0 comments on commit 30b1765

Please sign in to comment.