feat: Add BuildingRemovePacket to handle building removal requests in-game

This commit is contained in:
2025-12-15 09:19:42 +01:00
parent 40369ffe4b
commit fcf1ffac76
2 changed files with 66 additions and 4 deletions

View File

@@ -18,11 +18,8 @@ namespace KCM.Enums
KingdomName = 32,
StartGame = 33,
WorldSeed = 34,
Building = 50,
BuildingOnPlacement = 51,
World = 70,
WorldPlace = 71,
FellTree = 72,
@@ -44,6 +41,7 @@ namespace KCM.Enums
AddVillager = 88,
SetupInitialWorkers = 89,
VillagerTeleportTo = 90,
PlaceKeepRandomly = 91
PlaceKeepRandomly = 91,
BuildingRemove = 92
}
}

View File

@@ -0,0 +1,64 @@
using System;
namespace KCM.Packets.Game.GameBuilding
{
public class BuildingRemovePacket : Packet
{
public override ushort packetId => (ushort)Enums.Packets.BuildingRemove;
// Flag to prevent infinite loop when removing buildings from packet
public static bool isProcessingPacket = false;
public Guid guid { get; set; }
public override void HandlePacketClient()
{
if (clientId == KCClient.client.Id) return;
Main.helper.Log($"Received building remove packet for guid {guid} from {player.name}");
// Try to find the building in the player who owns it
Building building = player.inst.GetBuilding(guid);
if (building == null)
{
// Try to find it in any player's buildings
foreach (var kcp in Main.kCPlayers.Values)
{
building = kcp.inst.GetBuilding(guid);
if (building != null) break;
}
}
if (building == null)
{
Main.helper.Log($"Building with guid {guid} not found on client, may already be removed.");
return;
}
try
{
Main.helper.Log($"Removing building {building.UniqueName} at {building.transform.position}");
// Set flag to prevent sending packet back
isProcessingPacket = true;
building.Remove();
isProcessingPacket = false;
}
catch (Exception e)
{
isProcessingPacket = false;
Main.helper.Log($"Error removing building: {e.Message}");
Main.helper.Log(e.StackTrace);
}
}
public override void HandlePacketServer()
{
// Forward the remove packet to all other clients
SendToAll(clientId);
}
}
}