Skip to content

Commit

Permalink
Merge pull request #700 from Bannerlord-Coop-Team/Enter-exit-settleme…
Browse files Browse the repository at this point in the history
…nt-fix

Settlement Enter Spam & Leave Crash fix
  • Loading branch information
garrettluskey authored Dec 15, 2023
2 parents 5297049 + 1a2dbbb commit 88a061b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using TaleWorlds.CampaignSystem.Map;
using TaleWorlds.CampaignSystem.Party;
using TaleWorlds.CampaignSystem.Settlements;
using TaleWorlds.CampaignSystem.Settlements.Workshops;
using TaleWorlds.Library;

namespace GameInterface.Services.MobileParties.Patches;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ namespace GameInterface.Services.MobileParties.Patches;
/// <summary>
/// Patches for player encounters
/// </summary>

[HarmonyPatch(typeof(EncounterManager))]
internal class EncounterManagerPatches
{
private static bool inSettlement = false;
private static MethodInfo Start => typeof(PlayerEncounter).GetMethod(nameof(PlayerEncounter.Start));
private static MethodInfo Init => typeof(PlayerEncounter).GetMethod(
"Init",
Expand Down Expand Up @@ -62,9 +64,20 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi

private static void PlayerEncounterIntercept(PartyBase attackerParty, PartyBase defenderParty, Settlement settlement)
{
if (inSettlement) return;

var message = new StartSettlementEncounterAttempted(
attackerParty.MobileParty.StringId,
settlement.StringId);
MessageBroker.Instance.Publish(attackerParty, message);

inSettlement = true;
}

[HarmonyPostfix]
[HarmonyPatch(typeof(PlayerEncounter), nameof(PlayerEncounter.Finish))]
private static void PlayerEncounterFinishPatch(bool forcePlayerOutFromSettlement)
{
inSettlement = false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using GameInterface.Services.MobileParties.Patches;
using HarmonyLib;
using System;
using System.Collections.Generic;
using System.Text;
using TaleWorlds.CampaignSystem;
using TaleWorlds.CampaignSystem.CampaignBehaviors;
using TaleWorlds.CampaignSystem.Encounters;
using TaleWorlds.CampaignSystem.GameMenus;

namespace GameInterface.Services.Settlements.Patches
{
[HarmonyPatch(typeof(EncounterGameMenuBehavior))]
public class EncounterGameMenuBehaviorPatch
{
//These patches disables opening of these menus if EncounterSettlement is null as it seems to get called multiple times.
[HarmonyPrefix]
[HarmonyPatch("game_menu_town_outside_on_init")]
public static bool Prefix(MenuCallbackArgs args)
{
if (PlayerEncounter.EncounterSettlement != null) return true;

return false;
}

[HarmonyPrefix]
[HarmonyPatch("game_menu_town_town_besiege_on_condition")]
public static bool CheckFortificationEncounterSettlement(MenuCallbackArgs args)
{
if (PlayerEncounter.EncounterSettlement != null) return true;

return false;
}
}
}

0 comments on commit 88a061b

Please sign in to comment.