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:
1
Main.cs
1
Main.cs
@@ -780,6 +780,7 @@ namespace KCM
|
||||
new AddVillagerPacket()
|
||||
{
|
||||
guid = __result.guid,
|
||||
position = pos, // Include villager spawn position
|
||||
}.Send();
|
||||
}
|
||||
catch (Exception e)
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user