refactor: Remove BuildingRemoveHook to streamline building removal process

This commit is contained in:
2025-12-15 09:35:39 +01:00
parent df1def69e4
commit ca517be369

63
Main.cs
View File

@@ -1015,69 +1015,6 @@ namespace KCM
}
}
[HarmonyPatch(typeof(Building), "Remove")]
public class BuildingRemoveHook
{
// Store original Player.inst to restore after Remove
private static Player originalPlayerInst = null;
public static void Prefix(Building __instance)
{
try
{
// Skip if we're processing a remove packet (prevents infinite loop)
if (Packets.Game.GameBuilding.BuildingRemovePacket.isProcessingPacket)
return;
if (KCClient.client.IsConnected)
{
// Only send packet if this building belongs to a player
if (__instance.TeamID() >= 0)
{
Main.helper.Log($"Building {__instance.UniqueName} (guid: {__instance.guid}) being removed, sending packet");
new Packets.Game.GameBuilding.BuildingRemovePacket()
{
guid = __instance.guid
}.Send();
}
// Set Player.inst to the correct player for this building
// This ensures Remove() modifies the correct player's job lists
originalPlayerInst = Player.inst;
Player correctPlayer = GetPlayerByBuilding(__instance);
if (correctPlayer != null && correctPlayer != Player.inst)
{
Player.inst = correctPlayer;
}
}
}
catch (Exception e)
{
helper.Log($"Error in BuildingRemoveHook Prefix: {e.Message}");
helper.Log(e.StackTrace);
}
}
public static void Postfix(Building __instance)
{
try
{
// Restore original Player.inst after Remove completes
if (KCClient.client.IsConnected && originalPlayerInst != null)
{
Player.inst = originalPlayerInst;
originalPlayerInst = null;
}
}
catch (Exception e)
{
helper.Log($"Error in BuildingRemoveHook Postfix: {e.Message}");
helper.Log(e.StackTrace);
}
}
}
#endregion
#region "Time Hooks"