Skip to content

Commit

Permalink
fixed canweartogether issue with other mods
Browse files Browse the repository at this point in the history
  • Loading branch information
rheirman committed May 30, 2019
1 parent e26b3ea commit a8cf6c4
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 4 deletions.
Binary file modified Assemblies/WhatTheHack.dll
Binary file not shown.
2 changes: 2 additions & 0 deletions Source/WhatTheHack/Harmony/ApparelUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ static void Postfix(Pawn p, ThingDef apparel, ref bool __result)

//Can never wear two belts together.

/*
[HarmonyPatch(typeof(ApparelUtility), "CanWearTogether")]
class ApparelUtility_CanWearTogether
{
Expand All @@ -35,5 +36,6 @@ static bool Prefix(ThingDef A, ThingDef B, BodyDef body, ref bool __result)
return true;
}
}
*/

}
52 changes: 52 additions & 0 deletions Source/WhatTheHack/Harmony/JobDriver_Wear.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using Harmony;
using RimWorld;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using Verse;
using Verse.AI;

namespace WhatTheHack.Harmony
{
//Don't allow multiple belts for mechs with belt modules. Mechs often don't have a waist, and vanilla CanWearTogether therefore always returns true for belt items.
//Almost literal copy of JobDriver_Wear_TryUnequipSomething, but applies for hacked mechs only, and disallows multiple belts.
[HarmonyPatch(typeof(JobDriver_Wear), "TryUnequipSomething")]
class JobDriver_Wear_TryUnequipSomething
{
static bool Prefix(ref JobDriver_Wear __instance)
{
if (__instance.pawn.IsHacked())
{
Apparel apparel = Traverse.Create(__instance).Property("Apparel").GetValue<Apparel>();
List<Apparel> wornApparel = __instance.pawn.apparel.WornApparel;
foreach (Apparel wornApp in wornApparel)
{
if (BothBelts(apparel.def, wornApp.def))
{
Log.Message("drop belt!");
bool forbid = __instance.pawn.Faction != null && __instance.pawn.Faction.HostileTo(Faction.OfPlayer);
Apparel apparel2;
if (!__instance.pawn.apparel.TryDrop(wornApp, out apparel2, __instance.pawn.PositionHeld, forbid))
{
Log.Error(__instance.pawn + " could not drop " + wornApp.ToStringSafe<Apparel>(), false);
__instance.EndJobWith(JobCondition.Errored);
return false;
}
break;
}
}
}
return true;
}
private static bool BothBelts(ThingDef A, ThingDef B)
{
if (Utilities.IsBelt(A.apparel) && Utilities.IsBelt(B.apparel))
{
return true;
}
return false;
}
}
}
10 changes: 6 additions & 4 deletions Source/WhatTheHack/Harmony/RestUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,18 @@ class RestUtility_CurrentBed
{
static bool Prefix(Pawn p, ref Building_Bed __result)
{
if (!p.RaceProps.IsMechanoid || p.Map == null)
if (p.jobs == null || p.CurJob == null)
{
return true;
}

if (p.jobs.curDriver != null && p.CurJob != null && p.CurJob.def != WTH_DefOf.WTH_Mechanoid_Rest)
if (!(p.RaceProps != null && p.RaceProps.IsMechanoid) || p.Map == null)
{
return true;
}
if (p.CurJob.def != WTH_DefOf.WTH_Mechanoid_Rest)
{
return true;
}

List<Thing> thingList = p.Position.GetThingList(p.Map);
foreach (Thing thing in thingList)
{
Expand Down
1 change: 1 addition & 0 deletions Source/WhatTheHack/WhatTheHack.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
<Compile Include="Comps\CompDataLevel.cs" />
<Compile Include="Comps\CompProperties_Overlays.cs" />
<Compile Include="Harmony\HediffStatsUtility.cs" />
<Compile Include="Harmony\JobDriver_Wear.cs" />
<Compile Include="Harmony\PawnUIOverlay.cs" />
<Compile Include="Harmony\Pawn_CallTracker.cs" />
<Compile Include="Harmony\Pawn_SkillTracker.cs" />
Expand Down

0 comments on commit a8cf6c4

Please sign in to comment.