Updated
private void DoUpdate()
{
var saveInfo = new BaseNetworkable.SaveInfo
{
forDisk = false
};
using (saveInfo.msg = Facepunch.Pool.Get<ProtoBuf.Entity>())
{
_timeEntity.Save(saveInfo);
var initialDateTime = DateTime.FromBinary(saveInfo.msg.environment.dateTime);
for (var i = 0; i < BasePlayer.activePlayerList.Count; i++)
{
var connection = BasePlayer.activePlayerList[i].net?.connection;
if (connection == null )
{
continue;
}
NetWrite nw = Net.sv.StartWrite();
saveInfo.forConnection = connection;
connection.validate.entityUpdates += 1u;
nw.PacketID(Message.Type.Entities);
nw.UInt32(connection.validate.entityUpdates);
TimeSpan offset;
string zoneId;
if (PluginInstance._playerZones.TryGetValue(BasePlayer.activePlayerList[i].UserIDString,
out zoneId) && PluginInstance._config.ZoneTime.TryGetValue(zoneId, out offset))
{
saveInfo.msg.environment.dateTime = (initialDateTime.Date + offset).ToBinary();
}
else
{
saveInfo.msg.environment.dateTime = initialDateTime.ToBinary();
}
saveInfo.msg.ToProto(nw);
nw.Send(new SendInfo(connection));
}
}
}
Merged postI acutally made a few changes. Not sure how efficent they are as I run a small server. The above gets the plugin working again. Below is what I run.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Network;
using Newtonsoft.Json;
using Oxide.Core.Plugins;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("Zone Manager Time", "misticos", "1.0.1")]
[Description("Set specific time for your zones")]
class ZoneManagerTime : CovalencePlugin
{
#region Variables
[PluginReference]
// ReSharper disable once InconsistentNaming
private Plugin ZoneManager = null;
private Dictionary<string, string> _playerZones = new Dictionary<string, string>();
#endregion
#region Configuration
private Configuration _config;
private class Configuration
{
[JsonProperty(PropertyName = "Zone ID Time", ObjectCreationHandling = ObjectCreationHandling.Replace)]
public Dictionary<string, TimeSpan> ZoneTime = new Dictionary<string, TimeSpan> {{"Zone ID", new TimeSpan(10, 5, 37)}};
[JsonProperty(PropertyName = "Update Frequency")]
public float UpdateFrequency = 5f;
}
protected override void LoadConfig()
{
base.LoadConfig();
try
{
_config = Config.ReadObject<Configuration>();
if (_config == null) throw new Exception();
SaveConfig();
}
catch
{
PrintError("Your configuration file contains an error. Using default configuration values.");
LoadDefaultConfig();
}
}
protected override void SaveConfig() => Config.WriteObject(_config);
protected override void LoadDefaultConfig() => _config = new Configuration();
#endregion
#region Hooks
private void OnServerInitialized()
{
new GameObject().AddComponent<DateController>().PluginInstance = this;
}
private void Unload()
{
UnityEngine.Object.DestroyImmediate(DateController.Instance.gameObject);
}
private TimeSpan scanZones(BasePlayer player)
{
TimeSpan ret = TimeSpan.Zero;
if (ZoneManager != null && ZoneManager.IsLoaded)
{
var zones = ZoneManager.Call<string[]>("GetPlayerZoneIDs", player);
if (zones != null && zones.Length != 0)
{
var zone = zones[zones.Length - 1];
if (_config.ZoneTime.ContainsKey(zone))
{
ret = _config.ZoneTime[zone];
}
}
}
return ret;
}
private void message(string data)
{
Puts(data);
}
#endregion
#region Controller
public class DateController : SingletonComponent<DateController>
{
public ZoneManagerTime PluginInstance = null;
private EnvSync _timeEntity;
private void Start()
{
_timeEntity = FindObjectOfType<EnvSync>();
_timeEntity.limitNetworking = true;
InvokeRepeating(DoUpdate, PluginInstance._config.UpdateFrequency, PluginInstance._config.UpdateFrequency);
PluginInstance.message("Starting");
}
protected override void OnDestroy()
{
base.OnDestroy();
_timeEntity.limitNetworking = false;
}
private void DoUpdate()
{
var saveInfo = new BaseNetworkable.SaveInfo
{
forDisk = false
};
using (saveInfo.msg = Facepunch.Pool.Get<ProtoBuf.Entity>())
{
_timeEntity.Save(saveInfo);
var initialDateTime = DateTime.FromBinary(saveInfo.msg.environment.dateTime);
for (var i = 0; i < BasePlayer.activePlayerList.Count; i++)
{
var connection = BasePlayer.activePlayerList[i].net?.connection;
if (connection == null)
continue;
NetWrite nw = Net.sv.StartWrite();
saveInfo.forConnection = connection;
connection.validate.entityUpdates += 1u;
nw.PacketID(Message.Type.Entities);
nw.UInt32(connection.validate.entityUpdates);
TimeSpan offset = PluginInstance.scanZones(BasePlayer.activePlayerList[i]);
if (offset != TimeSpan.Zero)
{
saveInfo.msg.environment.dateTime = (initialDateTime.Date + offset).ToBinary();
}
else
{
saveInfo.msg.environment.dateTime = initialDateTime.ToBinary();
}
saveInfo.msg.ToProto(nw);
nw.Send(new SendInfo(connection));
}
}
}
}
#endregion
}
}