From 7c5eb929db1b601787c978dbf008c4ec2fc6ebd2 Mon Sep 17 00:00:00 2001 From: Chev <11602755+chev2@users.noreply.github.com> Date: Tue, 20 Apr 2021 21:36:21 -0700 Subject: [PATCH] Artifact of Sequencing v1.0.2 --- .../ArtifactOfSequencing/changelog.txt | 4 + .../ArtifactOfSequencing/manifest.json | 4 +- ArtifactOfSequencing/ArtifactOfSequencing.cs | 80 +++++++++++-------- 3 files changed, 51 insertions(+), 37 deletions(-) diff --git a/.thunderstore/ArtifactOfSequencing/changelog.txt b/.thunderstore/ArtifactOfSequencing/changelog.txt index 61ea497..bd0f7cb 100644 --- a/.thunderstore/ArtifactOfSequencing/changelog.txt +++ b/.thunderstore/ArtifactOfSequencing/changelog.txt @@ -1,3 +1,7 @@ +1.0.2 -- +- Artifact should now apply to all connected players +- Fixed a bug where the artifact would still attempt to convert items when the artifact is disabled after a run + 1.0.1 -- - Fixed an error in RandomItem() preventing starting items from properly spawning diff --git a/.thunderstore/ArtifactOfSequencing/manifest.json b/.thunderstore/ArtifactOfSequencing/manifest.json index c5abf7b..499b4c2 100644 --- a/.thunderstore/ArtifactOfSequencing/manifest.json +++ b/.thunderstore/ArtifactOfSequencing/manifest.json @@ -1,10 +1,10 @@ { "name": "ArtifactOfSequencing", - "version_number": "1.0.1", + "version_number": "1.0.2", "website_url": "https://github.com/chev2/RoR2-Mods", "description": "Spawn with a starting item of every tier. Any picked up items will be converted to the starting item of the same tier.", "dependencies": [ "bbepis-BepInExPack-5.4.9", - "tristanmcpherson-R2API-3.0.25" + "tristanmcpherson-R2API-3.0.30" ] } diff --git a/ArtifactOfSequencing/ArtifactOfSequencing.cs b/ArtifactOfSequencing/ArtifactOfSequencing.cs index 5a273ad..2d469cd 100644 --- a/ArtifactOfSequencing/ArtifactOfSequencing.cs +++ b/ArtifactOfSequencing/ArtifactOfSequencing.cs @@ -10,7 +10,7 @@ using UnityEngine; namespace Chev { [BepInDependency("com.bepis.r2api")] - [BepInPlugin("com.Chev.ArtifactOfSequencing", "Artifact of Sequencing", "1.0.1")] + [BepInPlugin("com.Chev.ArtifactOfSequencing", "Artifact of Sequencing", "1.0.2")] [NetworkCompatibility(CompatibilityLevel.EveryoneMustHaveMod, VersionStrictness.EveryoneNeedSameModVersion)] [R2APISubmoduleDependency(nameof(ArtifactAPI))] public class ArtifactOfSequencingMod : BaseUnityPlugin @@ -56,8 +56,6 @@ namespace Chev // On run start event // This is when we add our items Run.onRunStartGlobal += AddBeginningItems; - - Logger.LogMessage("Loaded mod com.Chev.ArtifactOfSequencing"); } /// @@ -110,45 +108,57 @@ namespace Chev } } + /// + /// Converts any potential items to their corresponding starter item + /// + private void ConvertPotentialItems(Inventory inv, ItemIndex itemIndex, int count) + { + ItemDef starterItemToCompare = ItemFromTier(ItemCatalog.GetItemDef(itemIndex).tier); + + // If they are different items + if (starterItemToCompare != null && starterItemToCompare.itemIndex != itemIndex) + { + int itemCount = inv.GetItemCount(itemIndex); + + inv.RemoveItem(itemIndex, itemCount); + inv.GiveItem(starterItemToCompare, itemCount); + } + } + public void AddBeginningItems(Run run) { - // If our artifact is not enabled - if (!RunArtifactManager.instance.IsArtifactEnabled(Artifact.artifactIndex)) - return; - - CharacterMaster master = PlayerCharacterMasterController.instances[0].master; - - if (master) + // If our artifact is enabled + if (RunArtifactManager.instance.IsArtifactEnabled(Artifact.artifactIndex)) { - // Set starting items - CommonItem = RandomItem(ItemTier.Tier1); - UncommonItem = RandomItem(ItemTier.Tier2); - LegendaryItem = RandomItem(ItemTier.Tier3); - BossItem = RandomItem(ItemTier.Boss); - LunarItem = RandomItem(ItemTier.Lunar); - - // Give the starting items - master.inventory.GiveItem(CommonItem, CommonItemCount.Value); - master.inventory.GiveItem(UncommonItem, UncommonItemCount.Value); - master.inventory.GiveItem(LegendaryItem, LegendaryItemCount.Value); - master.inventory.GiveItem(BossItem, BossItemCount.Value); - master.inventory.GiveItem(LunarItem, LunarItemCount.Value); - // Called every time an item is given // If the item is not a starter item, remove it and give the starter item - Inventory.onServerItemGiven += (inv, itemIndex, count) => - { - ItemDef starterItemToCompare = ItemFromTier(ItemCatalog.GetItemDef(itemIndex).tier); + Inventory.onServerItemGiven += ConvertPotentialItems; + } + // Else, if our artifact isn't enabled + else + { + // Unsubscribe from the event if the artifact is disabled + Inventory.onServerItemGiven -= ConvertPotentialItems; + return; + } - // If they are different items - if (starterItemToCompare != null && starterItemToCompare.itemIndex != itemIndex) - { - int itemCount = inv.GetItemCount(itemIndex); + // Set starting items + CommonItem = RandomItem(ItemTier.Tier1); + UncommonItem = RandomItem(ItemTier.Tier2); + LegendaryItem = RandomItem(ItemTier.Tier3); + BossItem = RandomItem(ItemTier.Boss); + LunarItem = RandomItem(ItemTier.Lunar); - inv.RemoveItem(itemIndex, itemCount); - inv.GiveItem(starterItemToCompare, itemCount); - } - }; + // Give the starting items to every palyer + for (int i = 0; i < CharacterMaster.readOnlyInstancesList.Count; i++) + { + CharacterMaster character = CharacterMaster.readOnlyInstancesList[i]; + + character.inventory.GiveItem(CommonItem, CommonItemCount.Value); + character.inventory.GiveItem(UncommonItem, UncommonItemCount.Value); + character.inventory.GiveItem(LegendaryItem, LegendaryItemCount.Value); + character.inventory.GiveItem(BossItem, BossItemCount.Value); + character.inventory.GiveItem(LunarItem, LunarItemCount.Value); } } }