u1-liquid

autoSkipNightを有効にしてるとほぼ暗くならないTime Of Day

Jun 26th, 2022 (edited)
1,255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.38 KB | None | 0 0
  1. using Oxide.Core.Plugins;
  2. using Oxide.Core;
  3. using System;
  4. using System.Text;
  5. using System.Collections.Generic;
  6. using System.Globalization;
  7. using UnityEngine;
  8.  
  9. namespace Oxide.Plugins
  10. {
  11.     [Info("TimeOfDay", "FuJiCuRa", "2.3.4")]
  12.     [Description("Does alter day and night duration.")]
  13.     public class TimeOfDay : RustPlugin
  14.     {
  15.         bool Changed;
  16.         bool Initialized;
  17.         int componentSearchAttempts;
  18.         TOD_Time timeComponent;
  19.         bool activatedDay;
  20.  
  21.         int authLevelCmds;
  22.         int authLevelFreeze;
  23.         int dayLength;
  24.         int nightLength;
  25.         int presetDay;
  26.         int presetMonth;
  27.         int presetYear;
  28.         bool setPresetDate;
  29.         bool freezeDate;
  30.         bool autoSkipNight;
  31.         bool autoSkipDay;
  32.         bool logAutoSkipConsole;
  33.         bool freezeTimeOnload;
  34.         float timeToFreeze;
  35.        
  36.         object GetConfig(string menu, string datavalue, object defaultValue)
  37.         {
  38.             var data = Config[menu] as Dictionary<string, object>;
  39.             if (data == null)
  40.             {
  41.                 data = new Dictionary<string, object>();
  42.                 Config[menu] = data;
  43.                 Changed = true;
  44.             }
  45.             object value;
  46.             if (!data.TryGetValue(datavalue, out value))
  47.             {
  48.                 value = defaultValue;
  49.                 data[datavalue] = value;
  50.                 Changed = true;
  51.             }
  52.             return value;
  53.         }
  54.  
  55.         void LoadVariables()
  56.         {
  57.             dayLength =  System.Convert.ToInt32(GetConfig("Settings", "dayLength", 30));
  58.             nightLength =  System.Convert.ToInt32(GetConfig("Settings", "nightLength", 30));
  59.             freezeDate = System.Convert.ToBoolean(GetConfig("Settings", "freezeDate", false));
  60.             authLevelCmds = System.Convert.ToInt32(GetConfig("Settings", "authLevelCmds", 1));
  61.             authLevelFreeze = System.Convert.ToInt32(GetConfig("Settings", "authLevelFreeze", 2));
  62.             autoSkipNight = System.Convert.ToBoolean(GetConfig("Settings", "autoSkipNight", false));
  63.             autoSkipDay = System.Convert.ToBoolean(GetConfig("Settings", "autoSkipDay", false));
  64.             logAutoSkipConsole = System.Convert.ToBoolean(GetConfig("Settings", "logAutoSkipConsole", true));
  65.            
  66.             presetDay =  System.Convert.ToInt32(GetConfig("DatePreset", "presetDay", 1));
  67.             presetMonth =  System.Convert.ToInt32(GetConfig("DatePreset", "presetMonth", 1));
  68.             presetYear =  System.Convert.ToInt32(GetConfig("DatePreset", "presetYear", 2020));
  69.             setPresetDate = System.Convert.ToBoolean(GetConfig("DatePreset", "setPresetDate", false));
  70.            
  71.             freezeTimeOnload = System.Convert.ToBoolean(GetConfig("TimeFreeze", "freezeTimeOnload", false));
  72.             timeToFreeze = System.Convert.ToSingle(GetConfig("TimeFreeze", "timeToFreeze", 12.0));
  73.  
  74.             if (!Changed) return;
  75.             SaveConfig();
  76.             Changed = false;
  77.         }
  78.  
  79.         protected override void LoadDefaultConfig()
  80.         {
  81.             Config.Clear();
  82.             LoadVariables();
  83.         }
  84.  
  85.         void Loaded()
  86.         {
  87.             LoadVariables();
  88.             Initialized = false;
  89.         }
  90.  
  91.         void Unload()
  92.         {
  93.             if (timeComponent == null || !Initialized) return;
  94.             timeComponent.OnSunrise -= OnSunrise;
  95.             timeComponent.OnSunset -= OnSunset;
  96.             timeComponent.OnDay -= OnDay;
  97.             timeComponent.OnHour -= OnHour;
  98.         }
  99.  
  100.         void OnServerInitialized()
  101.         {
  102.             if (TOD_Sky.Instance == null)
  103.             {
  104.                 componentSearchAttempts++;
  105.                 if (componentSearchAttempts < 10)
  106.                     timer.Once(1, OnServerInitialized);
  107.                 else
  108.                     PrintWarning("Could not find required component after 10 attempts. Plugin disabled");
  109.                 return;
  110.             }
  111.             timeComponent = TOD_Sky.Instance.Components.Time;
  112.             if (timeComponent == null)
  113.             {
  114.                 PrintWarning("Could not fetch time component. Plugin disabled");
  115.                 return;
  116.             }
  117.             if (setPresetDate)
  118.             {
  119.                 TOD_Sky.Instance.Cycle.Day = presetDay;
  120.                 TOD_Sky.Instance.Cycle.Month = presetMonth;
  121.                 TOD_Sky.Instance.Cycle.Year = presetYear;
  122.             }
  123.             SetTimeComponent();
  124.             if (freezeTimeOnload)
  125.                 StartupFreeze();
  126.         }
  127.  
  128.         void SetTimeComponent()
  129.         {
  130.             timeComponent.ProgressTime = true;
  131.             timeComponent.UseTimeCurve = false;
  132.             timeComponent.OnSunrise += OnSunrise;
  133.             timeComponent.OnSunset += OnSunset;
  134.             timeComponent.OnDay += OnDay;
  135.             timeComponent.OnHour += OnHour;
  136.             Initialized = true;
  137.             if (TOD_Sky.Instance.Cycle.Hour > TOD_Sky.Instance.SunriseTime && TOD_Sky.Instance.Cycle.Hour < TOD_Sky.Instance.SunsetTime)
  138.                 OnSunrise();
  139.             else
  140.                 OnSunset();
  141.         }
  142.        
  143.         void StartupFreeze()
  144.         {
  145.             if (!Initialized) return;
  146.             timeComponent.ProgressTime = false;
  147.             ConVar.Env.time = timeToFreeze;
  148.         }
  149.  
  150.         void OnDay()
  151.         {
  152.             if (Initialized && freezeDate)
  153.                 --TOD_Sky.Instance.Cycle.Day;
  154.         }
  155.  
  156.         void OnHour()
  157.         {
  158.             if (!Initialized) return;
  159.             if (TOD_Sky.Instance.Cycle.Hour > TOD_Sky.Instance.SunriseTime && TOD_Sky.Instance.Cycle.Hour + 3.0f < TOD_Sky.Instance.SunsetTime && !activatedDay)
  160.             {
  161.                 OnSunrise();
  162.                 return;
  163.             }
  164.             if ((TOD_Sky.Instance.Cycle.Hour + 3.0f > TOD_Sky.Instance.SunsetTime || TOD_Sky.Instance.Cycle.Hour < TOD_Sky.Instance.SunriseTime) && activatedDay)
  165.             {
  166.                 OnSunset();
  167.                 return;
  168.             }
  169.         }
  170.  
  171.         void OnSunrise()
  172.         {
  173.             if (!Initialized) return;
  174.             if (autoSkipDay && !autoSkipNight)
  175.             {
  176.                 TOD_Sky.Instance.Cycle.Hour = TOD_Sky.Instance.SunsetTime;
  177.                 if (logAutoSkipConsole)
  178.                     Puts("Daytime autoskipped");
  179.                 OnSunset();
  180.                 return;
  181.             }
  182.             timeComponent.DayLengthInMinutes = dayLength * (24.0f / (TOD_Sky.Instance.SunsetTime - TOD_Sky.Instance.SunriseTime));
  183.             if (!activatedDay)
  184.                 Interface.CallHook("OnTimeSunrise");
  185.             activatedDay = true;
  186.         }
  187.  
  188.         void OnSunset()
  189.         {
  190.             if (!Initialized) return;
  191.             if (autoSkipNight)
  192.             {
  193.                 float timeToAdd = (24 - TOD_Sky.Instance.Cycle.Hour) + TOD_Sky.Instance.SunriseTime + 3.0f;
  194.                 TOD_Sky.Instance.Cycle.Hour += timeToAdd;
  195.                 if (logAutoSkipConsole)
  196.                     Puts("Nighttime autoskipped");
  197.                 OnSunrise();
  198.                 return;
  199.             }
  200.             timeComponent.DayLengthInMinutes = nightLength * (24.0f / (24.0f - (TOD_Sky.Instance.SunsetTime - TOD_Sky.Instance.SunriseTime)));
  201.             if (activatedDay)
  202.                 Interface.CallHook("OnTimeSunset");
  203.             activatedDay = false;
  204.         }
  205.        
  206.         [ConsoleCommand("tod.daylength")]
  207.         void ConsoleDayLength(ConsoleSystem.Arg arg)
  208.         {
  209.             if (!Initialized) return;
  210.             if (arg.Connection != null && arg.Connection.authLevel < authLevelCmds) return;
  211.             if (arg.Args == null || arg.Args.Length < 1)
  212.             {
  213.                 SendReply(arg, $"Current 'dayLength' setting is '{dayLength}'");
  214.                 return;
  215.             }
  216.            
  217.             int newDaylength = 0;
  218.             if (int.TryParse(arg.Args[0], out newDaylength))
  219.             {
  220.                 if (newDaylength < 1)
  221.                 {
  222.                     SendReply(arg, $"The new daylength must be greater zero");
  223.                     return;
  224.                 }
  225.             }
  226.             dayLength = newDaylength;
  227.             SendReply(arg, $"The 'dayLength' has been set to '{dayLength}'");
  228.            
  229.             if (TOD_Sky.Instance.IsDay)
  230.                 OnSunrise();
  231.             else
  232.                 OnSunset();
  233.             Config["Settings", "dayLength"] = dayLength;
  234.             SaveConfig();
  235.         }
  236.        
  237.         [ConsoleCommand("tod.nightlength")]
  238.         void ConsoleNightLength(ConsoleSystem.Arg arg)
  239.         {
  240.             if (!Initialized) return;
  241.             if (arg.Connection != null && arg.Connection.authLevel < authLevelCmds) return;
  242.             if (arg.Args == null || arg.Args.Length < 1)
  243.             {
  244.                 SendReply(arg, $"Current 'nightLength' setting is '{nightLength}'");
  245.                 return;
  246.             }
  247.            
  248.             int newNightlength = 0;
  249.             if (int.TryParse(arg.Args[0], out newNightlength))
  250.             {
  251.                 if (newNightlength < 1)
  252.                 {
  253.                     SendReply(arg, $"The new nightlength must be greater zero");
  254.                     return;
  255.                 }
  256.             }
  257.             nightLength = newNightlength;
  258.             SendReply(arg, $"The 'nightLength' has been set to '{nightLength}'");
  259.             if (TOD_Sky.Instance.IsDay)
  260.                 OnSunrise();
  261.             else
  262.                 OnSunset();
  263.             Config["Settings", "nightLength"] = nightLength;
  264.             SaveConfig();
  265.         }
  266.        
  267.         [ConsoleCommand("tod.freezetime")]
  268.         void ConsoleFreezeTime(ConsoleSystem.Arg arg)
  269.         {
  270.             if (!Initialized) return;
  271.             if (arg.Connection != null && arg.Connection.authLevel < authLevelFreeze) return;
  272.  
  273.             timeComponent.ProgressTime = !timeComponent.ProgressTime;
  274.            
  275.             if (timeComponent.ProgressTime)
  276.                 SendReply(arg, $"The game time was unfreezed");
  277.             else
  278.                 SendReply(arg, $"The game time was freezed");
  279.         }
  280.  
  281.         [ConsoleCommand("tod.skipday")]
  282.         void ConsoleSkipDay(ConsoleSystem.Arg arg)
  283.         {
  284.             if (!Initialized) return;
  285.             if (arg.Connection != null && arg.Connection.authLevel < authLevelCmds) return;
  286.             if (TOD_Sky.Instance.IsNight)
  287.             {
  288.                 SendReply(arg, $"Night is already active");
  289.                 return;
  290.             }
  291.             OnSunset();
  292.             TOD_Sky.Instance.Cycle.Hour = TOD_Sky.Instance.SunsetTime;
  293.             SendReply(arg, $"Current daytime skipped");
  294.         }
  295.  
  296.         [ConsoleCommand("tod.skipnight")]
  297.         void ConsoleSkipNight(ConsoleSystem.Arg arg)
  298.         {
  299.             if (!Initialized) return;
  300.             if (arg.Connection != null && arg.Connection.authLevel < authLevelCmds) return;
  301.             if (TOD_Sky.Instance.IsDay)
  302.             {
  303.                 SendReply(arg, $"Day is already active");
  304.                 return;
  305.             }
  306.             OnSunrise();
  307.             TOD_Sky.Instance.Cycle.Hour = TOD_Sky.Instance.SunriseTime;
  308.             SendReply(arg, $"Current nighttime skipped");
  309.         }          
  310.  
  311.         [ChatCommand("tod")]
  312.         private void TodCommand(BasePlayer player, string command, string[] args)
  313.         {
  314.             if (!Initialized)
  315.                 return;
  316.             TimeSpan ts1= TimeSpan.FromHours(TOD_Sky.Instance.SunriseTime);
  317.             TimeSpan ts2= TimeSpan.FromHours(TOD_Sky.Instance.SunsetTime);
  318.            
  319.             StringBuilder stringBuilder = new StringBuilder();
  320.             stringBuilder.AppendLine(FormNeutralMessage("-------- Settings --------"));
  321.             stringBuilder.AppendLine("Current Time".PadRight(15) + TOD_Sky.Instance.Cycle.DateTime.ToString("HH:mm:ss"));
  322.             stringBuilder.AppendLine("Sunrise Hour".PadRight(15) + string.Format("{0}:{1}", System.Math.Truncate(ts1.TotalHours).ToString(), ts1.Minutes.ToString()));
  323.             stringBuilder.AppendLine("Sunset Hour".PadRight(15) + string.Format("{0}:{1}", System.Math.Truncate(ts2.TotalHours).ToString(), ts2.Minutes.ToString()));
  324.             stringBuilder.AppendLine("Daylength".PadRight(15) + dayLength.ToString() + " minutes");
  325.             stringBuilder.Append("Nightlength".PadRight(15) + nightLength.ToString() + " minutes");
  326.             PrintPluginMessageToChat(player, stringBuilder.ToString().TrimEnd());
  327.         }
  328.  
  329.         [HookMethod("SendHelpText")]
  330.         private void SendHelpText(BasePlayer player)
  331.         {
  332.             if (!Initialized) return;
  333.             StringBuilder stringBuilder = new StringBuilder();
  334.             stringBuilder.Append(FormNeutralMessage("-------------------- Available Commands --------------------\n"));
  335.             stringBuilder.Append(FormNeutralMessage("/tod") + " - Shows current Time Of Day.\n");
  336.             PrintPluginMessageToChat(player, stringBuilder.ToString());
  337.         }
  338.  
  339.         private void PrintPluginMessageToChat(BasePlayer player, string message)
  340.         {
  341.             PrintToChat(player, "<b><size=16>[<color=#ffa500ff>" + this.Name + "</color>]</size></b>\n" + message);
  342.         }
  343.  
  344.         private void PrintPluginMessageToChat(string message)
  345.         {
  346.             PrintToChat("<b><size=16>[<color=#ffa500ff>" + this.Name + "</color>]</size></b>\n" + message);
  347.         }
  348.  
  349.         private string FormNeutralMessage(string message)
  350.         {
  351.             return "<color=#c0c0c0ff>" + message + "</color>";
  352.         }
  353.  
  354.     }
  355. }
  356.  
Add Comment
Please, Sign In to add comment