Skip to content

Commit

Permalink
Chirr minions now lose items alongside their owner.
Browse files Browse the repository at this point in the history
Fixed a potential infinite loop related to Chirr inventory sharing.
  • Loading branch information
Moffein committed Dec 24, 2022
1 parent 20bd097 commit f17ac65
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Starstorm 2/Modules/Prefabs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ internal static SurvivorDef RegisterNewSurvivor(GameObject bodyPrefab, GameObjec
survivorDef.desiredSortPosition = sortPosition;
survivorDef.unlockableDef = unlockableDef;

survivorDef.cachedName = namePrefix;

survivorDefinitions.Add(survivorDef);
return survivorDef;
}
Expand Down
2 changes: 1 addition & 1 deletion Starstorm 2/StarstormPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class StarstormPlugin : BaseUnityPlugin
{
internal const string guid = "com.ChirrLover.Starstorm2Unofficial";
internal const string modName = "Starstorm 2 Unofficial";
internal const string version = "0.5.10";
internal const string version = "0.5.12";

public static StarstormPlugin instance;

Expand Down
18 changes: 17 additions & 1 deletion Starstorm 2/Survivors/Chirr/Components/ChirrFriendController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,27 @@ private void Awake()

trackerUpdateStopwatch = 0f;
RoR2.Inventory.onServerItemGiven += UpdateMinionInventory;//Is there a better way with onInventoryChangedGlobal?
On.RoR2.Inventory.RemoveItem_ItemIndex_int += UpdateMinionInventoryItemRemoved;
On.RoR2.PingerController.SetCurrentPing += MinionPingRetarget;

this.indicatorCannotBefriend = new Indicator(base.gameObject, indicatorCannotBefriendPrefab);
this.indicatorReadyToBefriend = new Indicator(base.gameObject, indicatorReadyToBefriendPrefab);
this.indicatorFriend = new Indicator(base.gameObject, indicatorFriendPrefab);
}

private void UpdateMinionInventoryItemRemoved(On.RoR2.Inventory.orig_RemoveItem_ItemIndex_int orig, Inventory self, ItemIndex itemIndex, int count)
{
orig(self, itemIndex, count);
if (NetworkServer.active && ownerMaster && this._hasFriend && this.targetMaster && this.targetMaster.inventory && ownerMaster != this.targetMaster) //last case prevents a recursive loop
{
CharacterMaster cm = self.GetComponent<CharacterMaster>();
if (cm == ownerMaster)
{
this.targetMaster.inventory.RemoveItem(itemIndex, count);
}
}
}

private void Start()
{
if (ownerBody) ownerMaster = ownerBody.master;
Expand Down Expand Up @@ -183,11 +198,12 @@ private void OnDestroy()
{
RoR2.Inventory.onServerItemGiven -= UpdateMinionInventory;
On.RoR2.PingerController.SetCurrentPing -= MinionPingRetarget;
On.RoR2.Inventory.RemoveItem_ItemIndex_int -= UpdateMinionInventoryItemRemoved;
}

private void UpdateMinionInventory(Inventory inventory, ItemIndex itemIndex, int count)
{
if (ownerMaster && this._hasFriend && this.targetMaster && this.targetMaster.inventory && !IsBlacklistedItem(itemIndex))
if (count > 0 && ownerMaster && this._hasFriend && this.targetMaster && this.targetMaster.inventory && !IsBlacklistedItem(itemIndex) && ownerMaster != this.targetMaster)
{
CharacterMaster cm = inventory.GetComponent<CharacterMaster>();
if (cm == ownerMaster)
Expand Down

0 comments on commit f17ac65

Please sign in to comment.