From fcf1ffac76fba4e7b1736fc03a3f1dab76c5b082 Mon Sep 17 00:00:00 2001 From: b3ni15 Date: Mon, 15 Dec 2025 09:19:42 +0100 Subject: [PATCH] feat: Add BuildingRemovePacket to handle building removal requests in-game --- Enums/Packets.cs | 6 +- .../Game/GameBuilding/BuildingRemovePacket.cs | 64 +++++++++++++++++++ 2 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 Packets/Game/GameBuilding/BuildingRemovePacket.cs diff --git a/Enums/Packets.cs b/Enums/Packets.cs index f7da401..a9baf00 100644 --- a/Enums/Packets.cs +++ b/Enums/Packets.cs @@ -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 } } diff --git a/Packets/Game/GameBuilding/BuildingRemovePacket.cs b/Packets/Game/GameBuilding/BuildingRemovePacket.cs new file mode 100644 index 0000000..1020332 --- /dev/null +++ b/Packets/Game/GameBuilding/BuildingRemovePacket.cs @@ -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); + } + } +} + +