Skip to content

Commit

Permalink
0.4, collect chest items, magic storage integration
Browse files Browse the repository at this point in the history
  • Loading branch information
JavidPack committed Aug 5, 2018
1 parent 1109970 commit 5661e04
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 7 deletions.
8 changes: 8 additions & 0 deletions ItemChecklist.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@

namespace ItemChecklist
{
// TODO: is ItemChecklistPlayer.foundItems a waste of memory? investigate and trim it down if needed.
// TODO: World Checklist? MP shared checklist?
// Has this item ever been seen on this world? - easy. Maintain separate bool array, on change, notify server, relay to clients.
// send bool array as byte array?
// query magic storage?
// WHY? I want to know everything we can craft yet
public class ItemChecklist : Mod
{
static internal ItemChecklist instance;
Expand All @@ -29,6 +35,7 @@ public override void Load()
{
instance = this;
ToggleChecklistHotKey = RegisterHotKey("Toggle Item Checklist", "I");
MagicStorageIntegration.Load();
}

public override void Unload()
Expand All @@ -37,6 +44,7 @@ public override void Unload()
instance = null;
ToggleChecklistHotKey = null;
ItemChecklistInterface = null;
MagicStorageIntegration.Unload();
}

public override void AddRecipes()
Expand Down
6 changes: 5 additions & 1 deletion ItemChecklist.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
<StartupObject />
</PropertyGroup>
<ItemGroup>
<Reference Include="MagicStorage_v0.4.3.1">
<HintPath>lib\MagicStorage_v0.4.3.1.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Xna.Framework">
<HintPath>..\..\solutiondlls\Microsoft.Xna.Framework.dll</HintPath>
</Reference>
Expand Down Expand Up @@ -69,6 +72,7 @@
<Compile Include="ItemChecklistPlayer.cs" />
<Compile Include="ItemChecklistUI.cs" />
<Compile Include="ItemSlot.cs" />
<Compile Include="MagicStorageIntegration.cs" />
<Compile Include="UICheckbox.cs" />
<Compile Include="UIGrid.cs" />
<Compile Include="UIHoverImageButton.cs" />
Expand All @@ -89,7 +93,7 @@
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>"C:\Program Files (x86)\Steam\steamapps\common\Terraria\Terraria.exe" -build "$(ProjectDir)\" -eac "$(TargetPath)"</PostBuildEvent>
<PostBuildEvent>"C:\Program Files (x86)\Steam\steamapps\common\Terraria\tModLoaderServer.exe" -build "$(ProjectDir)\" -eac "$(TargetPath)"</PostBuildEvent>
</PropertyGroup>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
50 changes: 45 additions & 5 deletions ItemChecklistPlayer.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using ItemChecklist.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Terraria;
using Terraria.DataStructures;
using Terraria.GameInput;
using Terraria.ID;
using Terraria.ModLoader;
Expand Down Expand Up @@ -29,6 +32,7 @@ class ItemChecklistPlayer : ModPlayer
// Because of save, these values inherit the last used setting while loading
internal SortModes sortModePreference = SortModes.TerrariaSort;
internal bool announcePreference;
internal bool findChestItemsPreference = true;
internal int showCompletedPreference;

public override void ProcessTriggers(TriggersSet triggersSet)
Expand All @@ -48,6 +52,7 @@ public override void OnEnterWorld(Player player)
var itemChecklistPlayer = Main.LocalPlayer.GetModPlayer<ItemChecklistPlayer>(mod);
ItemChecklistUI.visible = false;
ItemChecklistUI.announce = announcePreference;
ItemChecklistUI.collectChestItems = findChestItemsPreference;
ItemChecklistUI.sortMode = sortModePreference;
ItemChecklistUI.showCompleted = showCompletedPreference;
ItemChecklist.instance.ItemChecklistUI.RefreshPreferences();
Expand All @@ -72,23 +77,55 @@ public override void Initialize()
}

announcePreference = false;
findChestItemsPreference = true;
sortModePreference = SortModes.TerrariaSort;
showCompletedPreference = 0;
}
}

public override void UpdateAutopause()
{
ChestCheck();
}

public override void PreUpdate()
{
if (!Main.dedServ)
ChestCheck();
}

private void ChestCheck()
{
if (!Main.dedServ && player.whoAmI == Main.myPlayer)
{
var itemChecklistPlayer = Main.LocalPlayer.GetModPlayer<ItemChecklistPlayer>(mod);
for (int i = 0; i < 59; i++)
{
if (!player.inventory[i].IsAir && !itemChecklistPlayer.foundItem[player.inventory[i].type] && itemChecklistPlayer.findableItems[player.inventory[i].type])
if (!player.inventory[i].IsAir && !foundItem[player.inventory[i].type] && findableItems[player.inventory[i].type])
{
mod.GetGlobalItem<ItemChecklistGlobalItem>().ItemReceived(player.inventory[i]); // TODO: Analyze performance impact? do every 60 frames only?
}
}
if (player.chest != -1 && (player.chest != player.lastChest || Main.autoPause && Main.gamePaused) && ItemChecklistUI.collectChestItems)
{
//Main.NewText(player.chest + " " + player.lastChest);
Item[] items;
if (player.chest == -2)
items = player.bank.item;
else if (player.chest == -3)
items = player.bank2.item;
else if (player.chest == -4)
items = player.bank3.item;
else
items = Main.chest[player.chest].item;
for (int i = 0; i < 40; i++)
{
((ItemChecklistGlobalItem)mod.GetGlobalItem("ItemChecklistGlobalItem")).ItemReceived(player.inventory[i]);
if (!items[i].IsAir && !foundItem[items[i].type] && findableItems[items[i].type])
{
mod.GetGlobalItem<ItemChecklistGlobalItem>().ItemReceived(items[i]);
}
}
}
if (ItemChecklistUI.collectChestItems && MagicStorageIntegration.Enabled)
MagicStorageIntegration.FindItemsInStorage();
}
}

Expand All @@ -99,7 +136,8 @@ public override TagCompound Save()
{
["FoundItems"] = foundItems.Select(ItemIO.Save).ToList(),
["SortMode"] = (int)ItemChecklistUI.sortMode,
["Announce"] = ItemChecklistUI.announce,
["Announce"] = ItemChecklistUI.announce, // Not saving default, saving last used....good thing?
["CollectChestItems"] = ItemChecklistUI.collectChestItems,
["ShowCompleted"] = ItemChecklistUI.showCompleted,
};
}
Expand All @@ -109,6 +147,8 @@ public override void Load(TagCompound tag)
foundItems = tag.GetList<TagCompound>("FoundItems").Select(ItemIO.Load).ToList();
sortModePreference = (SortModes)tag.GetInt("SortMode");
announcePreference = tag.GetBool("Announce");
if (tag.ContainsKey("CollectChestItems")) // Missing tags get defaultvalue, which would be false, which isn't what we want.
findChestItemsPreference = tag.GetBool("CollectChestItems");
showCompletedPreference = tag.GetInt("ShowCompleted");

foreach (var item in foundItems)
Expand Down
17 changes: 17 additions & 0 deletions ItemChecklistUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class ItemChecklistUI : UIState
public UIToggleHoverImageButton muteButton;
public UIHoverImageButton sortButton;
public UIHoverImageButton modFilterButton;
public UIToggleHoverImageButton collectChestItemsButton;
public UIPanel checklistPanel;
public UIGrid checklistGrid;
public static SortModes sortMode = SortModes.TerrariaSort;
Expand All @@ -26,6 +27,7 @@ class ItemChecklistUI : UIState
public static bool visible = false;
public static int showCompleted = 0; // 0: both, 1: unfound, 2: found
public static bool announce = true;
public static bool collectChestItems = true;
public static string hoverText = "";

ItemSlot[] itemSlots;
Expand All @@ -41,6 +43,7 @@ public override void OnInitialize()
// Is initialize called? (Yes it is called on reload) I want to reset nicely with new character or new loaded mods: visible = false;

announce = false; // overwritten by modplayer
collectChestItems = false;

checklistPanel = new UIPanel();
checklistPanel.SetPadding(10);
Expand Down Expand Up @@ -76,6 +79,12 @@ public override void OnInitialize()
modFilterButton.Top.Pixels = 4;
checklistPanel.Append(modFilterButton);

collectChestItemsButton = new UIToggleHoverImageButton(Main.cursorTextures[8], ItemChecklist.instance.GetTexture("closeButton"), "Toggle Collect Chest Items", collectChestItems);
collectChestItemsButton.OnClick += ToggleFindChestItemsButtonClicked;
collectChestItemsButton.Left.Pixels = spacing * 8 + 28 * 4;
collectChestItemsButton.Top.Pixels = 4;
checklistPanel.Append(collectChestItemsButton);

checklistGrid = new UIGrid(5);
checklistGrid.Top.Pixels = 32f + spacing;
checklistGrid.Width.Set(-25f, 1f);
Expand Down Expand Up @@ -160,12 +169,20 @@ private void ToggleModFilterButtonClicked(UIMouseEvent evt, UIElement listeningE
UpdateNeeded();
}

private void ToggleFindChestItemsButtonClicked(UIMouseEvent evt, UIElement listeningElement)
{
collectChestItems = !collectChestItems;
Main.PlaySound(collectChestItems ? SoundID.MenuOpen : SoundID.MenuClose);
collectChestItemsButton.SetEnabled(collectChestItems);
}

internal void RefreshPreferences()
{
foundFilterButton.hoverText = "Cycle Found Filter: " + foundFilterStrings[showCompleted];
sortButton.hoverText = "Cycle Sort Method: " + sortMode.ToFriendlyString();
modFilterButton.hoverText = "Cycle Mod Filter: " + modnames[currentMod];
muteButton.SetEnabled(announce);
collectChestItemsButton.SetEnabled(collectChestItems);
UpdateNeeded();
}

Expand Down
56 changes: 56 additions & 0 deletions MagicStorageIntegration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using MagicStorage;
using MagicStorage.Components;
using Terraria;
using Terraria.DataStructures;
using Terraria.ModLoader;

namespace ItemChecklist
{
// autopause?
static class MagicStorageIntegration
{
static Mod MagicStorage;
public static bool Enabled => MagicStorage != null;
static Point16 previousStorageAccess = new Point16(-1, -1);
static StorageAccess tile;
//static StorageAccess tile = null; //throws TypeInitializationException

public static void Load()
{
MagicStorage = ModLoader.GetMod("MagicStorage");
}

public static void Unload()
{
MagicStorage = null;
}

internal static void FindItemsInStorage()
{
var storagePlayer = Main.LocalPlayer.GetModPlayer<StoragePlayer>();
Point16 storageAccess = storagePlayer.ViewingStorage();
if (storageAccess == previousStorageAccess)
return;
previousStorageAccess = storageAccess;
if (!Main.playerInventory || storageAccess.X < 0 || storageAccess.Y < 0)
return;
ModTile modTile = TileLoader.GetTile(Main.tile[storageAccess.X, storageAccess.Y].type);
if (modTile == null || !(modTile is StorageAccess))
{
return;
}
TEStorageHeart heart = ((StorageAccess)modTile).GetHeart(storageAccess.X, storageAccess.Y);
if (heart == null)
{
return;
}
var items = heart.GetStoredItems();
// Will 1000 items crash the chat?
foreach (var item in items)
{
ItemChecklist.instance.GetGlobalItem<ItemChecklistGlobalItem>().ItemReceived(item);
}
}
}
}
3 changes: 2 additions & 1 deletion build.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
author = jopojelly
version = 0.2.2.4
version = 0.4
displayName = Item Checklist
homepage = https://forums.terraria.org/index.php?threads/item-checklist-in-game-100-item-collection-checklist.52786/
hideCode = false
Expand All @@ -9,3 +9,4 @@ languageVersion = 6
includePDB = true
notworkingside = Client
buildIgnore = .vs\*, Properties\*, *.csproj, *.user, obj\*, bin\*, *.config, lib\*, .gitignore, .git\*
weakReferences = [email protected]
10 changes: 10 additions & 0 deletions description.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
Item Checklist lets you view a checklist for picking up or crafting 100% of the items in the game

Toggle the checklist with the hotkey assigned to it in the settings.

The checklist has several buttons at the top.

Cycle Found Filter: Filters the list to show All, only Found, or only missing items.
Cycle Sort Method: Sorts the list by ID, Value, Alphabetical, Rarity, or the chest auto-sorting algorithm.
Cycle Mod Filter: Lets you filter by all items, only vanilla items, or individual mods.
Toggle Messages: Toggles the announcement chat text that happens when you pick up a new item.
Toggle Collect Chest Items: You can toggle the behavior of counting items seen in chests as "collected".

If you use Magic Storage and have the Toggle Collect Chest Items enabled, you will also collect all items in storage if you access it.
Binary file added lib/MagicStorage_v0.4.3.1.dll
Binary file not shown.

0 comments on commit 5661e04

Please sign in to comment.