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

FREE EntityTriggers : 1.0.0 1.0.0

Нет прав для скачивания
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;

namespace Oxide.Plugins
{
    [Info("Entity Triggers", "Beast_", "1.0.0")]
    [Description("Triggers commands based on the server entity counts.")]
    public class EntityTriggers : RustPlugin
    {
        #region Fields
        private Timer checkTimer;
        private Configuration config;
        private Dictionary<int, bool> triggerStates = new Dictionary<int, bool>();

        public enum TriggerType
        {
            Equal,
            EqualOrGreater
        }
        #endregion

        #region Configuration
        private class Configuration
        {
            [JsonProperty("Check Interval In Seconds")]
            public int CheckInterval = 60;

            [JsonProperty("Execute One Trigger Per Check")]
            public bool ExecuteOneTriggerPerCheck = true;

            [JsonProperty("Triggers", ObjectCreationHandling = ObjectCreationHandling.Replace)]
            public Dictionary<int, TriggerObject> Triggers = new Dictionary<int, TriggerObject>
            {
                [100000] = new TriggerObject
                {
                    Enabled = true,
                    Priority = 1,
                    Type = TriggerType.EqualOrGreater,
                    RunOnce = false,
                    Commands = new List<string> { "say Server has reached 100,000 entities!" }
                },
                [120000] = new TriggerObject
                {
                    Enabled = true,
                    Priority = 2,
                    Type = TriggerType.EqualOrGreater,
                    RunOnce = false,
                    Commands = new List<string> { "say Server has reached 120,000 entities!" }
                },
                [135000] = new TriggerObject
                {
                    Enabled = true,
                    Priority = 3,
                    Type = TriggerType.Equal,
                    RunOnce = false,
                    Commands = new List<string> { "say Server has reached 135,000 entities!" }
                }
            };

            #region Trigger Object
            public class TriggerObject
            {
                [JsonProperty("Enabled")]
                public bool Enabled { get; set; }

                [JsonProperty("Priority (The lower the higher)")]
                public int Priority { get; set; }

                [JsonProperty("Type (0 = Equal, 1 = Equal Or Greater)")]
                public TriggerType Type { get; set; }

                [JsonProperty("Run Once")]
                public bool RunOnce { get; set; }

                [JsonProperty("Commands")]
                public List<string> Commands { get; set; }
            }
            #endregion

            public string ToJson() => JsonConvert.SerializeObject(this);
            public Dictionary<string, object> ToDictionary() => JsonConvert.DeserializeObject<Dictionary<string, object>>(ToJson());
        }

        protected override void LoadDefaultConfig() => config = new Configuration();

        protected override void LoadConfig()
        {
            base.LoadConfig();
            try
            {
                config = Config.ReadObject<Configuration>();
                if (config == null) throw new JsonException();

                SaveConfig();
            }
            catch
            {
                PrintWarning($"Configuration file {Name}.json is invalid; using defaults");
                LoadDefaultConfig();
            }
        }

        protected override void SaveConfig() => Config.WriteObject(config);
        #endregion

        #region Hooks
        private void OnServerInitialized()
        {
            checkTimer = timer.Every(config.CheckInterval, () =>
            {
                int entityCount = BaseNetworkable.serverEntities.Count;
                foreach (var trigger in config.Triggers.OrderBy(t => t.Value.Priority))
                {
                    if (!trigger.Value.Enabled) continue;
                    if (triggerStates.ContainsKey(trigger.Key) && triggerStates[trigger.Key] && trigger.Value.RunOnce)
                        continue;

                    bool shouldTrigger = false;
                    switch (trigger.Value.Type)
                    {
                        case TriggerType.Equal:
                            shouldTrigger = entityCount == trigger.Key;
                            break;
                        case TriggerType.EqualOrGreater:
                            shouldTrigger = entityCount >= trigger.Key;
                            break;
                    }

                    if (shouldTrigger)
                    {
                        triggerStates[trigger.Key] = true;
                        foreach (var command in trigger.Value.Commands)
                        {
                            ConsoleSystem.Run(ConsoleSystem.Option.Server, command);
                        }

                        if (config.ExecuteOneTriggerPerCheck) break;
                    }
                }
            });
        }

        private void Unload() => checkTimer?.Destroy();
        #endregion
    }
}

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