C#:
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Oxide.Core;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("DecayEntity", "Sparkless", "0.0.1")]
public class DecayEntity : RustPlugin
{
public class DecaySettings
{
[JsonProperty("Decay Time Repeat")]
public int DecayTimeRepeat;
[JsonProperty("Damage Repeat")]
public int DecayDamage;
}
private ConfigData _config;
public class ConfigData
{
[JsonProperty("ShortPrefabName => Settings decay")]
public Dictionary<string, DecaySettings> DecaySettingses = new Dictionary<string, DecaySettings>();
public static ConfigData GetNewCong()
{
ConfigData newConfig = new ConfigData();
newConfig.DecaySettingses = new Dictionary<string, DecaySettings>()
{
["icewall"] = new DecaySettings()
{
DecayTimeRepeat = 1800,
DecayDamage = 5000,
},
["wall.external.high.ice"] = new DecaySettings()
{
DecayTimeRepeat = 1800,
DecayDamage = 5000
},
["wall.external.high.stone"] = new DecaySettings()
{
DecayTimeRepeat = 1800,
DecayDamage = 5000
},
["wall.external.high.wood"] = new DecaySettings()
{
DecayTimeRepeat = 1800,
DecayDamage = 5000
},
["barricade.cover.wood"] = new DecaySettings()
{
DecayTimeRepeat = 1800,
DecayDamage = 5000
},
};
return newConfig;
}
}
protected override void LoadConfig()
{
base.LoadConfig();
try
{
_config = Config.ReadObject<ConfigData>();
}
catch
{
LoadDefaultConfig();
}
NextTick(SaveConfig);
}
protected override void LoadDefaultConfig() => _config = ConfigData.GetNewCong();
protected override void SaveConfig() => Config.WriteObject(_config);
List<uint> HasHet = new List<uint>();
private static DecayEntity _ins;
void OnServerInitialized()
{
_ins = this;
try
{
HasHet = Interface.GetMod().DataFileSystem.ReadObject<List<uint>>(Name);
}
catch
{
HasHet = new List<uint>();
}
if (HasHet.Count > 0)
{
foreach (var ent in HasHet)
{
var BaseNetworkable = global::BaseNetworkable.serverEntities.Find(ent);
if (BaseNetworkable == null)
{
continue;
}
var combat = BaseNetworkable.GetComponent<BaseCombatEntity>();
if (combat != null && combat.GetComponent<DecayComponent>() == null)
{
combat.gameObject.AddComponent<DecayComponent>();
}
}
}
}
void Unload()
{
Interface.Oxide.DataFileSystem.WriteObject(Name, HasHet);
var obj = UnityEngine.Object.FindObjectsOfType<DecayComponent>();
for (int i = 0; i < obj.Length; i++)
{
UnityEngine.Object.Destroy(obj[i]);
}
}
void OnEntityBuilt(Planner plan, GameObject go)
{
BasePlayer player = plan.GetOwnerPlayer();
var entity = go.GetComponent<BaseCombatEntity>();
if (entity != null)
{
var find = _config.DecaySettingses.FirstOrDefault(p => p.Key == entity.ShortPrefabName);
if (find.Value != null && entity.GetComponent<DecayComponent>() == null)
{
entity.gameObject.AddComponent<DecayComponent>();
HasHet.Add(entity.net.ID);
}
}
}
public class DecayComponent : MonoBehaviour
{
public BaseCombatEntity entity;
private DecaySettings system;
private void Awake()
{
entity = GetComponent<BaseCombatEntity>();
GetList();
InvokeRepeating("UpdateHealth", system.DecayTimeRepeat, system.DecayTimeRepeat);
}
public void GetList()
{
var find = _ins._config.DecaySettingses.FirstOrDefault(p => p.Key == entity.ShortPrefabName);
system = find.Value;
}
void UpdateHealth()
{
if (entity.GetBuildingPrivilege()) return;
entity.health -= this.system.DecayDamage;
entity.SendNetworkUpdate();
entity.SendNetworkUpdateImmediate();
if (entity._health <= 0)
{
entity?.Kill();
}
if (entity.health <= 0)
{
entity?.Kill();
}
}
private void OnDestroy()
{
Destroy(this);
}
}
}
}