Иконка ресурса

FREE FNoDecayUpkeep : 1.0.0 1.0.0

Нет прав для скачивания
C#:
using HarmonyLib;

namespace Oxide.Plugins;

[Info("FNoDecayUpkeep", "fufeldev", "1.0.0")]
public class FNoDecayUpkeep : RustPlugin
{
    private const float ProtectedSeconds = 2592000f;
    private const float ProtectedMinutes = 43200f;
    private Harmony _harmony;
    private Timer _timer;

    private void Init()
    {
        _harmony = new Harmony(Name);
        _harmony.PatchAll();
    }

    private void Unload()
    {
        _timer?.Destroy();
        _harmony?.UnpatchAll(Name);
    }

    private void OnServerInitialized()
    {
        ProtectAll();
        _timer = timer.Every(300f, ProtectAll);
    }

    private void OnEntitySpawned(BuildingPrivlidge priv)
    {
        NextTick(() => Protect(priv));
    }

    private void OnEntitySaved(BaseNetworkable entity, BaseNetworkable.SaveInfo info)
    {
        BuildingPrivlidge priv = entity as BuildingPrivlidge;
        if (priv == null || info.msg == null || info.msg.buildingPrivilege == null) return;
        info.msg.buildingPrivilege.protectedMinutes = ProtectedMinutes;
    }

    private static void Protect(BuildingPrivlidge priv)
    {
        if (priv == null || priv.IsDestroyed) return;
        priv.cachedProtectedMinutes = ProtectedMinutes;
        priv.MarkProtectedMinutesDirty();
        BuildingManager.Building building = priv.GetBuilding();
        if (building != null && building.HasDecayEntities())
        {
            foreach (DecayEntity entity in building.decayEntities)
            {
                if (entity == null || entity.IsDestroyed) continue;
                float left = ProtectedSeconds - entity.GetProtectedSeconds();
                if (left > 0f) entity.AddUpkeepTime(left);
            }
        }
        priv.SendNetworkUpdate();
    }

    private void ProtectAll()
    {
        foreach (BaseNetworkable entity in BaseNetworkable.serverEntities)
        {
            BuildingPrivlidge priv = entity as BuildingPrivlidge;
            if (priv != null) Protect(priv);
        }
    }

    [HarmonyPatch(typeof(BuildingPrivlidge), nameof(BuildingPrivlidge.GetProtectedMinutes))]
    private static class GetProtectedMinutesPatch
    {
        private static bool Prefix(BuildingPrivlidge __instance, ref float __result)
        {
            float minutes = (double)ConVar.Decay.scale > 0.01 ? ProtectedMinutes * ConVar.Decay.scale : ProtectedMinutes;
            if (__instance != null) __instance.cachedProtectedMinutes = minutes;
            __result = minutes;
            return false;
        }
    }

    [HarmonyPatch(typeof(BuildingPrivlidge), nameof(BuildingPrivlidge.PurchaseUpkeepTime), typeof(DecayEntity), typeof(float))]
    private static class PurchaseUpkeepTimePatch
    {
        private static bool Prefix(DecayEntity entity, float deltaTime, ref float __result)
        {
            if (entity != null && entity.GetProtectedSeconds() < ProtectedSeconds) entity.AddUpkeepTime(ProtectedSeconds - entity.GetProtectedSeconds());
            __result = 0f;
            return false;
        }
    }
}

Автор
15FPS
Скачиваний
0
Просмотры
1
Первый выпуск
Обновление
Оценка
0.00 звёзд 0 оценок
Сверху