internal static void Wear(this Pawn_ApparelTracker _this, Apparel newApparel, bool dropReplacedApparel = true) { SlotGroupUtility.Notify_TakingThing(newApparel); if (newApparel.Spawned) { newApparel.DeSpawn(); } if (!ApparelUtility.HasPartsToWear(_this.pawn, newApparel.def)) { Log.Warning(string.Concat(new object[] { _this.pawn, " tried to wear ", newApparel, " but he has no body parts required to wear it." })); return; } for (int i = _this.WornApparel.Count - 1; i >= 0; i--) { Apparel apparel = _this.WornApparel[i]; if (!ApparelUtility.CanWearTogether(newApparel.def, apparel.def)) { bool forbid = _this.pawn.Faction.HostileTo(Faction.OfPlayer); if (dropReplacedApparel) { Apparel apparel2; if (!_this.TryDrop(apparel, out apparel2, _this.pawn.Position, forbid)) { Log.Error(_this.pawn + " could not drop " + apparel); return; } } else { _this.WornApparel.Remove(apparel); } } } _this.WornApparel.Add(newApparel); newApparel.wearer = _this.pawn; _this.SortWornApparelIntoDrawOrder(); _this.ApparelChanged(); Utility.TryUpdateInventory(_this.pawn); // Apparel was added, update inventory MethodInfo methodInfo = typeof(Pawn_ApparelTracker).GetMethod("SortWornApparelIntoDrawOrder", BindingFlags.Instance | BindingFlags.NonPublic); methodInfo.Invoke(_this, new object[] { }); LongEventHandler.ExecuteWhenFinished(new Action(_this.ApparelChanged)); }
internal static void AddEquipment(this Pawn_EquipmentTracker _this, ThingWithComps newEq) { SlotGroupUtility.Notify_TakingThing(newEq); // Fetch private fields Pawn pawn = (Pawn)pawnFieldInfo.GetValue(_this); ThingWithComps primaryInt = (ThingWithComps)primaryIntFieldInfo.GetValue(_this); if ((from eq in _this.AllEquipment where eq.def == newEq.def select eq).Any <ThingWithComps>()) { Log.Error(string.Concat(new object[] { "Pawn ", pawn.LabelCap, " got equipment ", newEq, " while already having it." })); return; } if (newEq.def.equipmentType == EquipmentType.Primary && _this.Primary != null) { Log.Error(string.Concat(new object[] { "Pawn ", pawn.LabelCap, " got primaryInt equipment ", newEq, " while already having primaryInt equipment ", _this.Primary })); return; } if (newEq.def.equipmentType == EquipmentType.Primary) { primaryPropertyInfo.SetValue(_this, newEq, null); // Changed assignment to SetValue() since we're fetching a private variable through reflection } else { Log.Error("Tried to equip " + newEq + " but it's not Primary. Secondary weapons are not supported."); } foreach (Verb current in newEq.GetComp <CompEquippable>().AllVerbs) { current.caster = pawn; current.Notify_PickedUp(); } CE_Utility.TryUpdateInventory(pawn); // Added equipment, update inventory }
internal static void AddEquipment(this Pawn_EquipmentTracker _this, ThingWithComps newEq) { SlotGroupUtility.Notify_TakingThing(newEq); // Fetch private fields Pawn pawn = (Pawn)pawnFieldInfo.GetValue(_this); ThingWithComps primaryInt = (ThingWithComps)primaryIntFieldInfo.GetValue(_this); SlotGroupUtility.Notify_TakingThing(newEq); if (_this.AllEquipment.Where(eq => eq.def == newEq.def).Any <ThingWithComps>()) { Log.Error(string.Concat(new object[] { "Pawn ", pawn.LabelCap, " got equipment ", newEq, " while already having it." })); return; } if (newEq.def.equipmentType == EquipmentType.Primary && primaryInt != null) { Log.Error(string.Concat(new object[] { "Pawn ", pawn.LabelCap, " got primaryInt equipment ", newEq, " while already having primaryInt equipment ", primaryInt })); return; } if (newEq.def.equipmentType == EquipmentType.Primary) { primaryIntFieldInfo.SetValue(_this, newEq); // Changed assignment to SetValue() since we're fetching a private variable through reflection } foreach (Verb current in newEq.GetComp <CompEquippable>().AllVerbs) { current.caster = pawn; current.Notify_PickedUp(); } Utility.TryUpdateInventory(pawn); // Added equipment, update inventory }
internal static bool TryAdd(this ThingContainer _this, Thing item, bool canMergeWithExistingStacks = true) { if (item == null) { Log.Warning("Tried to add null item to ThingContainer."); return(false); } if (_this.Contains(item)) { Log.Warning("Tried to add " + item + " to ThingContainer but this item is already here."); return(false); } if (item.stackCount > _this.AvailableStackSpace) { return(_this.TryAdd(item, _this.AvailableStackSpace) > 0); } List <Thing> innerList = (List <Thing>)innerListFieldInfo.GetValue(_this); // Fetch innerList through reflection SlotGroupUtility.Notify_TakingThing(item); if (canMergeWithExistingStacks && item.def.stackLimit > 1) { for (int i = 0; i < innerList.Count; i++) { if (innerList[i].def == item.def) { int num = item.stackCount; if (num > _this.AvailableStackSpace) { num = _this.AvailableStackSpace; } Thing other = item.SplitOff(num); if (!innerList[i].TryAbsorbStack(other, false)) { Log.Error("ThingContainer did TryAbsorbStack " + item + " but could not absorb stack."); } } if (item.Destroyed) { //CR PART!!! CR_Utility.TryUpdateInventory(_this.owner as Pawn_InventoryTracker); // Item has been added, notify CompInventory return(true); } } } int maxStacks = (int)maxStacksFieldInfo.GetValue(_this); // Fetch maxStacks through reflection if (innerList.Count >= maxStacks) { return(false); } if (item.Spawned) { item.DeSpawn(); } if (item.HasAttachment(ThingDefOf.Fire)) { item.GetAttachment(ThingDefOf.Fire).Destroy(DestroyMode.Vanish); } item.holdingContainer = _this; innerList.Add(item); CR_Utility.TryUpdateInventory(_this.owner as Pawn_InventoryTracker); // Item has been added, notify CompInventory return(true); }
internal static bool TryAdd(this ThingContainer _this, Thing item) { if (item.stackCount > _this.AvailableStackSpace) { Log.Error(string.Concat(new object[] { "Add item with stackCount=", item.stackCount, " with only ", _this.AvailableStackSpace, " in container. Splitting and adding..." })); return(_this.TryAdd(item, _this.AvailableStackSpace)); } List <Thing> innerList = (List <Thing>)innerListFieldInfo.GetValue(_this); // Fetch innerList through reflection SlotGroupUtility.Notify_TakingThing(item); if (item.def.stackLimit > 1) { for (int i = 0; i < innerList.Count; i++) { if (innerList[i].def == item.def) { int num = item.stackCount; if (num > _this.AvailableStackSpace) { num = _this.AvailableStackSpace; } Thing other = item.SplitOff(num); if (!innerList[i].TryAbsorbStack(other, false)) { Log.Error("ThingContainer did TryAbsorbStack " + item + " but could not absorb stack."); } } if (item.Destroyed) { Utility.TryUpdateInventory(_this.owner as Pawn_InventoryTracker); // Item has been added, notify CompInventory return(true); } } } int maxStacks = (int)maxStacksFieldInfo.GetValue(_this); // Fetch maxStacks through reflection if (innerList.Count >= maxStacks) { return(false); } if (item.Spawned) { item.DeSpawn(); } if (item.HasAttachment(ThingDefOf.Fire)) { item.GetAttachment(ThingDefOf.Fire).Destroy(DestroyMode.Vanish); } item.holder = _this; innerList.Add(item); Utility.TryUpdateInventory(_this.owner as Pawn_InventoryTracker); // Item has been added, notify CompInventory return(true); }