C#:
// Все права принадлежат дискорд сообществу https://discord.gg/VgNHPpNrz6
using System;
using System.Collections.Generic;
using Oxide.Core;
using Oxide.Core.Configuration;
namespace Oxide.Plugins
{
[Info("AutoDoors", "Wulf/lukespragg", "2.3.0", ResourceId = 800)]
[Description("Automatically closes doors behind players after X seconds.")]
class AutoDoors : RustPlugin
{
// Do NOT edit this file, instead edit AutoDoors.json in oxide/config and AutoDoors.en.json in oxide/lang,
// or create a language file for another language using the 'en' file as a default.#region Localization
void LoadDefaultMessages()
{
var messages = new Dictionary<string, string>
{
{"ChatCommand", "ad"},
{"CommandUsage", "Usage:\n /ad to disable automatic doors\n /ad # (a number between 5 and 30)"},
{"DelayDisabled", "Automatic door closing is now disabled"},
{"DelaySetTo", "Automatic door closing delay set to {time}s"}
};
lang.RegisterMessages(messages, this);
}
#endregion
#region Configuration
int DefaultDelay => GetConfig("DefaultDelay", 5);
int MaximumDelay => GetConfig("MaximumDelay", 30);
int MinimumDelay => GetConfig("MinimumDelay", 5);
protected override void LoadDefaultConfig()
{
Config["DefaultDelay"] = DefaultDelay;
Config["MaximumDelay"] = MaximumDelay;
Config["MinimumDelay"] = MinimumDelay;
SaveConfig();
}
#endregion
#region Initialization
readonly DynamicConfigFile dataFile = Interface.Oxide.DataFileSystem.GetFile("AutoDoors");
Dictionary<ulong, int> playerPrefs = new Dictionary<ulong, int>();
void Loaded()
{
#if !RUST
throw new NotSupportedException($"This plugin does not support {(covalence.Game ?? "this game")}");
#endif
LoadDefaultConfig();
LoadDefaultMessages();
playerPrefs = dataFile.ReadObject<Dictionary<ulong, int>>();
cmd.AddChatCommand(GetMessage("ChatCommand"), this, "AutoDoorChatCmd");
}
#endregion
#region Chat Command
#if RUST
void AutoDoorChatCmd(BasePlayer player, string command, string[] args)
{
int time;
if (args == null || args.Length != 1 || !int.TryParse(args[0], out time)) time = 0;
if (time > MaximumDelay || time < MinimumDelay && time != 0)
{
PrintToChat(player, GetMessage("CommandUsage", player.UserIDString));
return;
}
playerPrefs[player.userID] = time;
dataFile.WriteObject(playerPrefs);
PrintToChat(player, time == 0 ? GetMessage("DelayDisabled", player.UserIDString)
: GetMessage("DelaySetTo").Replace("{time}", time.ToString()));
}
#endif
#endregion
#region Door Closing
#if RUST
void OnDoorOpened(Door door, BasePlayer player)
{
if (door == null || !door.IsOpen() || door.LookupPrefabName().Contains("shutter")) return;
int time;
if (!playerPrefs.TryGetValue(player.userID, out time)) time = DefaultDelay;
if (time == 0) return;
timer.Once(time, () =>
{
if (!door || !door.IsOpen()) return;
door.SetFlag(BaseEntity.Flags.Open, false);
door.SendNetworkUpdateImmediate();
});
}
#endif
#endregion
#region Helper Methods
T GetConfig<T>(string name, T defaultValue)
{
if (Config[name] == null) return defaultValue;
return (T)Convert.ChangeType(Config[name], typeof(T));
}
string GetMessage(string key, string steamId = null) => lang.GetMessage(key, this, steamId);
#endregion
}
}