Compare commits

...

5 Commits

2 changed files with 66 additions and 5 deletions

50
Main.cs
View File

@@ -1109,6 +1109,56 @@ namespace KCM
}
}
}
[HarmonyPatch(typeof(Villager), "Update")]
public class VillagerMovementSync
{
private struct MovementSnapshot
{
public Vector3 position;
public float timestamp;
}
private static readonly Dictionary<Guid, MovementSnapshot> lastSnapshots = new Dictionary<Guid, MovementSnapshot>();
private const float MovementThreshold = 0.3f;
private const float ForceSendInterval = 0.5f;
public static void Postfix(Villager __instance)
{
if (!KCServer.IsRunning || KCServer.server == null)
return;
if (__instance == null)
return;
Guid guid = __instance.guid;
Component villagerComponent = (Component)(object)__instance;
if (villagerComponent == null)
return;
Vector3 currentPosition = villagerComponent.transform.position;
float now = Time.time;
if (lastSnapshots.TryGetValue(guid, out MovementSnapshot snapshot))
{
float distance = Vector3.Distance(snapshot.position, currentPosition);
if (distance < MovementThreshold && (now - snapshot.timestamp) < ForceSendInterval)
return;
}
lastSnapshots[guid] = new MovementSnapshot
{
position = currentPosition,
timestamp = now
};
new VillagerTeleportTo()
{
guid = guid,
pos = currentPosition
}.SendToAll();
}
}
#endregion
#region "Job Hooks"

View File

@@ -1,5 +1,4 @@
using System;
using System.Linq;
using System.Reflection;
using UnityEngine;
@@ -12,15 +11,27 @@ namespace KCM
private static MethodInfo isPrivateMethod;
private static MethodInfo addResourceStorageMethod;
private static readonly Assembly[] candidateAssemblies = new[]
{
typeof(Player).Assembly,
typeof(World).Assembly,
typeof(FreeResourceManager).Assembly,
};
private static void EnsureInitialized()
{
if (resourceStorageType != null)
return;
resourceStorageType = AppDomain.CurrentDomain
.GetAssemblies()
.Select(a => a.GetType("Assets.Interface.IResourceStorage", false))
.FirstOrDefault(t => t != null);
foreach (var assembly in candidateAssemblies)
{
if (assembly == null)
continue;
resourceStorageType = assembly.GetType("Assets.Interface.IResourceStorage", false);
if (resourceStorageType != null)
break;
}
if (resourceStorageType == null)
return;