Advertisement
Xyberviri

AutoDoorCloser.cs

Mar 29th, 2015
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.17 KB | None | 0 0
  1. using System.Reflection;
  2. using System.Collections.Generic;
  3. using Oxide.Core;
  4.  
  5. namespace Oxide.Plugins
  6. {
  7.     [Info("AutoDoorCloser", "Bombardir", "2.0.0", ResourceId = 800)]
  8.     class AutoDoorCloser : RustPlugin
  9.     {
  10.         const string DataFile = "AutoDoorCloserData";
  11.         static readonly MethodInfo UpdateLayerMethod = typeof(BuildingBlock).GetMethod("UpdateLayer", (BindingFlags.Instance | BindingFlags.NonPublic));
  12.  
  13.         static Dictionary<Door, Timer> ActiveTimers = new Dictionary<Door, Timer>();
  14.         static Dictionary<ulong, float> PlayersData;
  15.  
  16.         #region Logic
  17.  
  18.         void OnDoorOpened(Door door, BaseEntity.RPCMessage rpc)
  19.         {
  20.             float time;
  21.             if (!PlayersData.TryGetValue(rpc.player.userID, out time))
  22.                 time = DefaultTime;
  23.  
  24.             if (time <= 0)
  25.                 return;
  26.  
  27.             Timer ActiveTimer;
  28.             if (ActiveTimers.TryGetValue(door, out ActiveTimer))
  29.             {
  30.                 ActiveTimer.Destroy();
  31.                 ActiveTimers.Remove(door);
  32.             }
  33.  
  34.             ActiveTimers[door] = timer.Once(time, () =>
  35.             {
  36.                 ActiveTimers.Remove(door);
  37.                 if (door == null || !door.IsOpen())
  38.                     return;
  39.  
  40.                 door.SetFlag(BaseEntity.Flags.Open, false);
  41.                 UpdateLayerMethod.Invoke(door, null);
  42.                 door.SendNetworkUpdateImmediate(false);
  43.             });
  44.         }
  45.  
  46.         #endregion
  47.  
  48.         #region Chat cmds
  49.  
  50.         [ChatCommand("ad")]
  51.         void ad(BasePlayer player, string command, string[] args)
  52.         {
  53.             if (args.Length == 0)
  54.             {
  55.                 player.ChatMessage(Syntax);
  56.                 return;
  57.             }
  58.  
  59.             string arg = args[0];
  60.             float time = 0;
  61.  
  62.             if (arg != "off")
  63.             {
  64.                 if (!float.TryParse(arg, out time))
  65.                 {
  66.                     player.ChatMessage(Number);
  67.                     return;
  68.                 }
  69.  
  70.                 if (time > MaxTime || time < MinTime)
  71.                 {
  72.                     player.ChatMessage(Time);
  73.                     return;
  74.                 }
  75.  
  76.                 player.ChatMessage(string.Format(Succes, time));
  77.             }
  78.             else
  79.                 player.ChatMessage(SuccesOff);
  80.  
  81.             PlayersData[player.userID] = time;
  82.             Interface.Oxide.DataFileSystem.WriteObject(DataFile, PlayersData);
  83.         }
  84.  
  85.         #endregion
  86.  
  87.         #region Config | Data load
  88.  
  89.         #region Config Vars
  90.  
  91.         static float DefaultTime = 3;
  92.         static float MaxTime = 10;
  93.         static float MinTime = 0.01f;
  94.  
  95.         static string Syntax = "Error! Syntax: /ad [time|off]";
  96.         static string Number = "Error! Incorrect number!";
  97.         static string Time = "Error! Your time should be between {0} and {1}!";
  98.         static string Succes = "Your doors will close automatically after {0} sec.";
  99.         static string SuccesOff = "You turn off the automatic closing doors!!";
  100.  
  101.         #endregion
  102.  
  103.         void LoadDefaultConfig() { }
  104.  
  105.         void Init()
  106.         {
  107.             CheckCfg<float>("Default close time", ref DefaultTime);
  108.             CheckCfg<float>("Max close time", ref MaxTime);
  109.             CheckCfg<float>("Min close time", ref MinTime);
  110.             if (MinTime < 0)
  111.                 MinTime = 0.01f;
  112.  
  113.             CheckCfg<string>("Syntax error", ref Syntax);
  114.             CheckCfg<string>("Number error", ref Number);
  115.             CheckCfg<string>("Time error", ref Time);
  116.  
  117.             CheckCfg<string>("Succes msg", ref Succes);
  118.             CheckCfg<string>("Succes off msg", ref SuccesOff);
  119.  
  120.             Time = string.Format(Time, MinTime, MaxTime);
  121.  
  122.             try { PlayersData = Interface.Oxide.DataFileSystem.ReadObject<Dictionary<ulong, float>>(DataFile); } catch { }
  123.             if (PlayersData == null) PlayersData = new Dictionary<ulong, float>();
  124.         }
  125.  
  126.         void CheckCfg<T>(string Key, ref T var)
  127.         {
  128.             if (Config[Key] is T)
  129.                 var = (T)Config[Key];
  130.             else
  131.                 Config[Key] = var;
  132.         }
  133.  
  134.         #endregion
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement