feat: Add BuildingRemovePacket to handle building removal requests in-game
This commit is contained in:
@@ -18,11 +18,8 @@ namespace KCM.Enums
|
|||||||
KingdomName = 32,
|
KingdomName = 32,
|
||||||
StartGame = 33,
|
StartGame = 33,
|
||||||
WorldSeed = 34,
|
WorldSeed = 34,
|
||||||
|
|
||||||
|
|
||||||
Building = 50,
|
Building = 50,
|
||||||
BuildingOnPlacement = 51,
|
BuildingOnPlacement = 51,
|
||||||
|
|
||||||
World = 70,
|
World = 70,
|
||||||
WorldPlace = 71,
|
WorldPlace = 71,
|
||||||
FellTree = 72,
|
FellTree = 72,
|
||||||
@@ -44,6 +41,7 @@ namespace KCM.Enums
|
|||||||
AddVillager = 88,
|
AddVillager = 88,
|
||||||
SetupInitialWorkers = 89,
|
SetupInitialWorkers = 89,
|
||||||
VillagerTeleportTo = 90,
|
VillagerTeleportTo = 90,
|
||||||
PlaceKeepRandomly = 91
|
PlaceKeepRandomly = 91,
|
||||||
|
BuildingRemove = 92
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
64
Packets/Game/GameBuilding/BuildingRemovePacket.cs
Normal file
64
Packets/Game/GameBuilding/BuildingRemovePacket.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user