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

FREE MonumentNames : 1.2.7 1.2.7

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

namespace Oxide.Plugins
{
    [Info("Monument Names", "Rustonauts", "1.2.7")]
    [Description("A plugin returning various monument details when given its short name or prefab")]
    class MonumentNames : RustPlugin
    {
        private Configuration _config;
        private List<DefaultMonument> _defaultMonuments = new List<DefaultMonument>();
    

        #region OxideHooks


        #endregion oxidehooks


        #region Init

        private void OnServerInitialized()
        {
            
            LoadConfig();
        }


        #endregion Init


        #region API 

        private string API_GetDisplayName(string _shortName)
        {
            foreach(Monument _monument in _config.Monuments)
            {
                if(_monument.shortName == _shortName) return _monument.displayName;
            }

            return _shortName;
        }

        private string API_GetDisplayNameFromPrefab(string _prefabName)
        {
            foreach(Monument _monument in _config.Monuments) {
                if(_monument.prefabName == _prefabName) return _monument.displayName;
            }

            return _prefabName;
        }

        private string API_GetShortNameFromPrefab(string _prefabName)
        {
            foreach(Monument _monument in _config.Monuments) {
                if(_monument.prefabName == _prefabName) return _monument.shortName;
            }

            return null;
        }

        private string API_GetGridPos(string _shortName)
        {
            foreach(Monument _monument in _config.Monuments) {
                if(_shortName == _monument.shortName) return _monument.gridPos;
            }

            return null;
        }

        private string API_GetPos(string _shortName)
        {
            foreach(Monument _monument in _config.Monuments) {
                if(_shortName == _monument.shortName) return _monument.v3Pos;
            }

            return null;
        }

        private float API_DistanceFrom(string _shortName, Vector3 _pos)
        {
            Vector3 monument_pos;
            float distance=1000;
            
            foreach(Monument _monument in _config.Monuments) {
                if(_shortName == _monument.shortName) {
                    
                    monument_pos = GetVector3(_monument.v3Pos);
                           
                    distance = Vector3.Distance(monument_pos, _pos);                                                  
                }
            }

            return distance; 
        }   

        private string API_ClosestMonument(Vector3 _pos)
        {            
            string closestMonument = "none";
            float closestDistance = 500;
            float currentDistance = 500;
            Vector3 monumentPos;

            foreach(Monument _monument in _config.Monuments) {                
                monumentPos = GetVector3(_monument.v3Pos);
                currentDistance = Vector3.Distance(monumentPos, _pos);
                
                if(currentDistance < closestDistance) {
                    closestMonument = _monument.shortName.ToString();
                    closestDistance = currentDistance;
                }
            }

            return closestMonument;
        }

        
        #endregion API


        #region Helpers


        private string GetGridPosition(Vector3 position) => MapHelper.PositionToString(position);

        private Vector3 GetVector3(string pos)
        {
            string[] coords = pos.Trim(new Char[]{'(', ')'}).Split(", ");
            float x, y, z;
            
            x = float.Parse(coords[0]);
            y = float.Parse(coords[1]);
            z = float.Parse(coords[2]);  

            return new Vector3(x, y, z);
        }

        private string GetShortName(string prefab_name) => prefab_name.ToString().Split("/").Last().Split(".prefab").First();   

        private string GetDisplayName(string shortName)
        {
            foreach(DefaultMonument _monument in _defaultMonuments) {
                if(shortName == _monument.shortName) return _monument.displayName;
            }

            return shortName;
        }


        #endregion


        #region Config

        private class DefaultMonument
        {
            [JsonProperty(PropertyName = "Monument Display Name")]
            public string displayName;

            [JsonProperty(PropertyName = "Monument Short Name")]
            public string shortName;

            public DefaultMonument(string _shortName, string _displayName)
            {
                displayName = _displayName;
                shortName = _shortName;
            }
        }


        private class Monument
        {
            [JsonProperty(PropertyName = "Monument ID")]
            public string id;

            [JsonProperty(PropertyName = "Monument Display Name")]
            public string displayName;

            [JsonProperty(PropertyName = "Monument Short Name")]
            public string shortName;

            [JsonProperty(PropertyName = "Monument Prefab Name")]
            public string prefabName;

            [JsonProperty(PropertyName = "Monument Grid Position")]
            public string gridPos;

            [JsonProperty(PropertyName = "Monument Vector 3 Position")]
            public string v3Pos;

            [JsonProperty(PropertyName = "Is Active")]
            public bool is_active;          


            public Monument()
            {

            }


            public Monument(string _id, bool _is_active, string _prefabName, string _shortName, string _displayName, string _gridPos, string _v3Pos)
            {
                id = _id;
                is_active = _is_active;
                prefabName = _prefabName;
                shortName = _shortName;
                displayName = _displayName;
                gridPos = _gridPos;
                v3Pos = _v3Pos;
            }  
        }


        private class Configuration
        {
            [JsonProperty(PropertyName = "Monuments", ObjectCreationHandling = ObjectCreationHandling.Replace)]
            public List<Monument> Monuments = new List<Monument>{};

            public List<string> BlackList = new List<string>
            {                
                "cave_large_hard",
                "cave_large_medium",
                "cave_large_sewers_hard",
                "cave_medium_easy",
                "cave_medium_hard",
                "cave_medium_medium",
                "cave_small_easy",
                "cave_small_hard",
                "cave_small_medium",
                "entrance_bunker_a",
                "entrance_bunker_b",
                "entrance_bunker_c",
                "entrance_bunker_d",                             
                "ice_lake_1",
                "ice_lake_2",
                "ice_lake_3",
                "ice_lake_4",
                "swamp_a",
                "swamp_b",
                "swamp_c",
                "water_well_a",
                "water_well_b",
                "water_well_c",
                "water_well_d",
                "water_well_e",
                "power_sub_big_1",
                "power_sub_big_2",
                "power_sub_small_1",
                "power_sub_small_2",                
            };
        }



        protected override void LoadConfig()
        {           
            base.LoadConfig();
            
            try
            {
                _config = Config.ReadObject<Configuration>();                
                 if (_config == null) throw new Exception();
            }
            catch
            {
                NewConfig();
            }

            SyncMonuments();            
        }

       
        protected void NewConfig()
        {
            _config = new Configuration();

            SyncMonuments();
        }


        protected override void SaveConfig()
        {
            Config.WriteObject(_config, true);
        }


        private void CreateDefaultMonuments()
        {            
            _defaultMonuments.Add (new DefaultMonument("harbor_1", "Large Harbor"));

            _defaultMonuments.Add (new DefaultMonument("harbor_2","Small Harbor"));

            _defaultMonuments.Add (new DefaultMonument("arctic_research_base_a", "Arctic Research Base"));

            _defaultMonuments.Add (new DefaultMonument("military_tunnel_1", "Military Tunnel"));

            _defaultMonuments.Add (new DefaultMonument("powerplant_1", "Power Plant"));

            _defaultMonuments.Add (new DefaultMonument("sphere_tank", "Dome"));

            _defaultMonuments.Add (new DefaultMonument("oilrig_1", "Large Oil"));

            _defaultMonuments.Add (new DefaultMonument("oilrig_2", "Small Oil"));

            _defaultMonuments.Add (new DefaultMonument("launch_site_1", "Launch"));

            _defaultMonuments.Add (new DefaultMonument("airfield_1", "Airfield"));

            _defaultMonuments.Add (new DefaultMonument("radtown_1", "Radtown"));

            _defaultMonuments.Add (new DefaultMonument("desert_military_base_a", "Desert Military Base A"));

            _defaultMonuments.Add (new DefaultMonument("desert_military_base_b", "Desert Military Base B"));

            _defaultMonuments.Add (new DefaultMonument("desert_military_base_c", "Desert Military Base C"));

            _defaultMonuments.Add (new DefaultMonument("desert_military_base_d", "Desert Military Base D"));

            _defaultMonuments.Add (new DefaultMonument("excavator_1", "Excavator"));

            _defaultMonuments.Add (new DefaultMonument("gas_station_1", "Oxums"));

            _defaultMonuments.Add (new DefaultMonument("supermarket_1", "Supermarket"));

            _defaultMonuments.Add (new DefaultMonument("junkyard_1", "Junkyard"));

            _defaultMonuments.Add (new DefaultMonument("mining_quarry_a", "Sulfur Mining Quarry"));

            _defaultMonuments.Add (new DefaultMonument("mining_quarry_b", "Stone Mining Quarry"));

            _defaultMonuments.Add (new DefaultMonument("mining_quarry_c", "HQM Mining Quarry"));

            _defaultMonuments.Add (new DefaultMonument("radtown_small_3", "Sewer Branch"));

            _defaultMonuments.Add (new DefaultMonument("satellite_dish", "Satellite Dish"));

            _defaultMonuments.Add (new DefaultMonument("trainyard_1", "Trainyard"));

            _defaultMonuments.Add (new DefaultMonument("underwater_lab_a", "Underwater Labs A"));

            _defaultMonuments.Add (new DefaultMonument("underwater_lab_b", "Underwater Labs B"));

            _defaultMonuments.Add (new DefaultMonument("underwater_lab_c", "Underwater Labs C"));

            _defaultMonuments.Add (new DefaultMonument("underwater_lab_d", "Underwater Labs D"));

            _defaultMonuments.Add (new DefaultMonument("water_treatment_plant_1", "Water Treatment Plant"));

            _defaultMonuments.Add (new DefaultMonument("warehouse", "Mining Outpost"));

            _defaultMonuments.Add (new DefaultMonument("fishing_village_a", "Fishing Village"));

            _defaultMonuments.Add (new DefaultMonument("fishing_village_b", "Fishing Village"));

            _defaultMonuments.Add (new DefaultMonument("fishing_village_c", "Fishing Village"));

            _defaultMonuments.Add (new DefaultMonument("fishing_village_d", "Fishing Village"));

            _defaultMonuments.Add (new DefaultMonument("stables_a", "Stables"));

            _defaultMonuments.Add (new DefaultMonument("stables_b", "Stables"));

            _defaultMonuments.Add (new DefaultMonument("stables_c", "Stables"));

            _defaultMonuments.Add (new DefaultMonument("stables_d", "Stables"));

            _defaultMonuments.Add (new DefaultMonument("ferry_terminal_1", "Ferry Terminal"));

            _defaultMonuments.Add (new DefaultMonument("nuclear_missile_silo", "Nuclear Missile Silo"));

            _defaultMonuments.Add (new DefaultMonument("bandit_town", "Bandits"));

            _defaultMonuments.Add (new DefaultMonument("compound", "Outpost"));
        }


        private void SyncMonuments()
        {
            CreateDefaultMonuments();
            DisableMonuments();

            //iteration through the monuments
            foreach( var _monument in TerrainMeta.Path.Monuments)
            {
                bool import_monument = true;
                string shortName = GetShortName(_monument.name.ToString());
                string displayName = GetDisplayName(shortName);

                //updating existing monuments
                foreach(Monument _configMonument in _config.Monuments) {
                    if(_configMonument.id == shortName + "_" + GetGridPosition(_monument.transform.position) && !isBlackListed(shortName)) {                   
                        _configMonument.is_active = true;                        
                        _configMonument.prefabName = _monument.name.ToString();
                        _configMonument.gridPos = GetGridPosition(_monument.transform.position);
                        _configMonument.v3Pos = _monument.transform.position.ToString();

                        import_monument = false;
                    }
                    //id isn't a match, but shortname is (new wipe likely) - let's preserve display name created by server admin
                    else if(_configMonument.shortName == shortName && !isBlackListed(shortName)) {
                        displayName = _configMonument.displayName;
                        import_monument = false;
                    }
                }


                //importing new monuments
                if(import_monument && !isBlackListed(shortName))
                {
                    _config.Monuments.Add (new Monument {                    
                        id = shortName + "_" + GetGridPosition(_monument.transform.position),
                        is_active = true,                        
                        prefabName = _monument.name.ToString(),
                        shortName = shortName,
                        displayName = displayName,                    
                        gridPos = GetGridPosition(_monument.transform.position),
                        v3Pos = _monument.transform.position.ToString(),
                    });
                }

                SaveConfig();

                //removing inactive monuments in config
                foreach(Monument _mon in _config.Monuments.ToList()) {
                    if(_mon.is_active == false) _config.Monuments.Remove(_mon);
                }
            }

        }


        private bool isBlackListed(string _shortName)
        {
            foreach(var _black in _config.BlackList) {
                if(_black == _shortName) return true;
            }

            return false;
        }

        private void DisableMonuments()
        {
            //make config monuments disabled
            foreach(Monument _monument in _config.Monuments) {
                _monument.is_active = false;
            }
           
            SaveConfig();
        }

        #endregion Config
    }
}

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