i made it work. Delete all the existing code in Building Health and paste this code and it should work. Set to 1.0.4 to avoid version conflicts.
using System;
using System.Collections.Generic;
using Oxide.Core;
namespace Oxide.Plugins
{
[Info("Entity Data", "Orange", "1.0.4")]
[Description("Saving specified information about player-owned entities")]
public class EntityData : RustPlugin
{
#region Vars
private class OEntity
{
public ulong ownerID;
public double spawnTime;
}
#endregion
#region Oxide Hooks
private void OnServerInitialized()
{
LoadData();
}
private void Unload()
{
SaveData();
}
private void OnNewSave()
{
SaveData();
}
private void OnEntitySpawned(BaseEntity entity)
{
CheckEntity(entity);
}
private void OnEntityKill(BaseEntity entity)
{
CheckEntity(entity);
}
#endregion
#region Helpers
private void CheckEntity(BaseEntity entity)
{
var owner = entity.OwnerID;
if (!owner.IsSteamId())
{
return;
}
var id = entity.net.ID.Value;
if (entities.ContainsKey(id))
{
entities.Remove(id);
}
else
{
entities.Add(id, new OEntity{ownerID = entity.OwnerID, spawnTime = Now()});
}
}
private double Now()
{
return DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds;
}
private int Passed(double a)
{
return Convert.ToInt32(Now() - a);
}
#endregion
#region Data
private Dictionary<ulong, OEntity> entities = new Dictionary<ulong, OEntity>();
private const string filename = "EntityData";
private void LoadData()
{
try
{
entities = Interface.Oxide.DataFileSystem.ReadObject<Dictionary<ulong, OEntity>>(filename);
}
catch (Exception e)
{
PrintWarning(e.Message);
}
SaveData();
timer.Every(150f, SaveData);
}
private void SaveData()
{
Interface.Oxide.DataFileSystem.WriteObject(filename, entities);
}
#endregion
#region API
private int API_GetLifeDuration(ulong netID)
{
if (!entities.ContainsKey(netID))
{
return 0;
}
var spawnTime = entities[netID].spawnTime;
return Passed(spawnTime);
}
private double API_GetSpawnTime(ulong netID)
{
if (!entities.ContainsKey(netID))
{
return 0;
}
return entities[netID].spawnTime;
}
private ulong API_GetOwner(ulong netID)
{
if (!entities.ContainsKey(netID))
{
return 0;
}
return entities[netID].ownerID;
}
#endregion
}
}