Fix: Add position sync for villagers and duplicate check

- Add position property to AddVillagerPacket
- Teleport villager to correct position on client
- Add duplicate guid check to prevent double villager creation
- Send position from Main.cs hook

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2025-12-14 10:52:39 +01:00
parent 7d6c915b49
commit eab7931f52
2 changed files with 17 additions and 0 deletions

View File

@@ -780,6 +780,7 @@ namespace KCM
new AddVillagerPacket()
{
guid = __result.guid,
position = pos, // Include villager spawn position
}.Send();
}
catch (Exception e)

View File

@@ -12,6 +12,7 @@ namespace KCM.Packets.Game.GamePlayer
public override ushort packetId => (ushort)Enums.Packets.AddVillager;
public Guid guid { get; set; }
public Vector3 position { get; set; }
public override void HandlePacketClient()
{
@@ -19,11 +20,25 @@ namespace KCM.Packets.Game.GamePlayer
{
if (KCClient.client.Id == clientId) return;
// Check for duplicate villager by guid
var existingVillager = player.inst.Workers.data.FirstOrDefault(v => v != null && v.guid == guid);
if (existingVillager != null)
{
Main.helper.Log($"Villager with guid {guid} already exists, skipping duplicate");
return;
}
Main.helper.Log("Received add villager packet from " + player.name + $"({player.id})");
Villager v = Villager.CreateVillager();
v.guid = guid;
// Set villager position
if (position != Vector3.zero)
{
v.TeleportTo(position);
}
player.inst.Workers.Add(v);
player.inst.Homeless.Add(v);
@@ -31,6 +46,7 @@ namespace KCM.Packets.Game.GamePlayer
catch (Exception e)
{
Main.helper.Log("Error handling add villager packet: " + e.Message);
Main.helper.Log(e.StackTrace);
}
}