C#:
using System.Linq;
using UnityEngine;
using System.Collections.Generic;
namespace Oxide.Plugins
{
[Info("ImprovedDoorClosers", "Death", "1.0.5")]
class ImprovedDoorClosers : RustPlugin
{
#region Declarations
ConfigFile options;
#endregion
#region Hooks
void Init()
{
LoadConfig();
}
void OnServerInitialized()
{
foreach (var closer in BaseNetworkable.serverEntities.OfType<DoorCloser>())
{
OnEntitySpawned(closer);
}
foreach (var entity in ItemManager.GetItemDefinitions())
{
if (options.DoorSettings.ContainsKey(entity.shortname))
{
continue;
}
if (entity.shortname.Contains("hinged") || entity.shortname.Contains("gates.external") || entity.shortname.Contains("ladder.hatch"))
{
options.DoorSettings.Add(entity.shortname, new Options { shutDelay = 3f, closerPlacement = Vector3.zero });
}
}
SaveConfig(options);
}
void OnEntitySpawned(DoorCloser closer)
{
var door = closer.GetDoor();
if (door == null || !options.DoorSettings.TryGetValue(door.ShortPrefabName, out var properties))
{
return;
}
closer.delay = properties.shutDelay;
closer.transform.localPosition = properties.closerPlacement;
closer.SendNetworkUpdateImmediate();
}
#endregion
#region Functions
#region Config
class ConfigFile
{
public Dictionary<string, Options> DoorSettings;
}
class Options
{
public float shutDelay = 3f;
public Vector3 closerPlacement = Vector3.zero;
}
void LoadDefaultConfig()
{
options = new ConfigFile();
PopulateDefaultData();
}
void LoadConfig()
{
options = Config.ReadObject<ConfigFile>();
if (options.DoorSettings == null)
{
PopulateDefaultData();
}
SaveConfig(options);
}
void PopulateDefaultData()
{
options.DoorSettings = new Dictionary<string, Options>()
{
{ "floor.ladder.hatch.toptier", new Options { shutDelay = 6f, closerPlacement = new Vector3(-1.1f, -0.07f, 0) }},
{ "floor.triangle.ladder.hatch.toptier", new Options { shutDelay = 6f, closerPlacement = new Vector3(-1.1f, -0.07f, 0) }},
{ "floor.ladder.hatch", new Options { shutDelay = 6f, closerPlacement = new Vector3(-1, -0.07f, 0) }},
{ "floor.triangle.ladder.hatch", new Options { shutDelay = 6f, closerPlacement = new Vector3(-1, -0.07f, 0) }},
{ "gates.external.high.wood", new Options { shutDelay = 8f, closerPlacement = new Vector3(0, 4.8f, 0) }},
{ "gates.external.high.stone", new Options { shutDelay = 8f, closerPlacement = new Vector3(0, 4.2f, 0) }},
{ "wall.frame.garagedoor", new Options { shutDelay = 6f, closerPlacement = new Vector3(-0.25f, 2.96f, 1.26f) }},
};
Puts("Default config has been added. Configure in /data/ImprovedDoorClosers.json");
}
void SaveConfig(ConfigFile config)
{
Config.WriteObject(config, true);
}
#endregion
#endregion
}
}