Advertisement
MONaH-Rasta

MapNoteTeleport

Aug 14th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.03 KB | None | 0 0
  1. using Newtonsoft.Json;
  2. using Oxide.Core;
  3. using ProtoBuf;
  4. using Rust;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using UnityEngine;
  9.  
  10. namespace Oxide.Plugins
  11. {
  12.     [Info("MapNote Teleport", "Talha/MON@H", "2.0.0")]
  13.     [Description("Teleports player to marker on map when placed.")]
  14.     public class MapNoteTeleport : RustPlugin
  15.     {
  16.         private bool IsGod = false;
  17.         private const string PERMISSION_USE = "mapnoteteleport.use";
  18.  
  19.         private void Init()
  20.         {
  21.             LoadData();
  22.             permission.RegisterPermission(PERMISSION_USE, this);
  23.             foreach (var command in configData.chatS.commands)
  24.                 cmd.AddChatCommand(command, this, nameof(CmdMapNoteTeleport));
  25.         }
  26.  
  27.         private void OnServerInitialized()
  28.         {
  29.             UpdateConfig();
  30.         }
  31.  
  32.         private void UpdateConfig()
  33.         {
  34.             if (configData.chatS.commands.Length == 0)
  35.                 configData.chatS.commands = new[] { "mnt" };
  36.             SaveConfig();
  37.         }
  38.  
  39.         private void OnServerSave() => timer.Once(UnityEngine.Random.Range(0f, 60f), SaveData);
  40.  
  41.         private void Unload()
  42.         {
  43.             SaveData();
  44.         }
  45.  
  46.         private void TP(BasePlayer player, MapNote note)
  47.         {
  48.             var err = CheckPlayer(player);
  49.             if (err != null)
  50.             {
  51.                 Print(player, Lang(err, player.UserIDString));
  52.                 return;
  53.             }
  54.  
  55.             IsGod = true;
  56.             player.flyhackPauseTime = 10f;
  57.             var pos = note.worldPosition + new Vector3(0,configData.coordinateY,0);
  58.             player.Teleport(pos);
  59.             var cooldown = configData.globalS.defaultCooldown;
  60.             var playerData = GetPlayerData(player.userID);
  61.             if (playerData != null)
  62.             {
  63.                 cooldown = playerData.time;
  64.             }
  65.             Print(player, Lang("Teleported", player.UserIDString, pos, cooldown));
  66.  
  67.             timer.Once(cooldown, () => {
  68.                 IsGod = false;
  69.                 if (player == null) return;
  70.                 Print(player, Lang("CooldownEnded", player.UserIDString));
  71.             });
  72.         }
  73.  
  74.         private string CheckPlayer(BasePlayer player)
  75.         {
  76.             if (player.isMounted)
  77.             {
  78.                 return "MapNoteTeleportMounted";
  79.             }
  80.  
  81.             if (!player.IsAlive())
  82.             {
  83.                 return "MapNoteTeleportDead";
  84.             }
  85.  
  86.             return null;
  87.         }
  88.  
  89.         private void OnMapMarkerAdded(BasePlayer player, MapNote note)
  90.         {
  91.             if (player == null || note == null || IsGod) return;
  92.  
  93.             if (configData.usePermission && !permission.UserHasPermission(player.UserIDString, PERMISSION_USE))
  94.             {
  95.                 if (!configData.globalS.adminsAllowed || !player.IsAdmin)
  96.                 {
  97.                     return;
  98.                 }
  99.             }
  100.  
  101.             var playerData = GetPlayerData(player.userID);
  102.             var enabled = configData.globalS.defaultEnabled;
  103.             if (playerData != null) enabled = playerData.enabled;
  104.             if (!enabled) return;
  105.  
  106.             TP(player, note);
  107.         }
  108.  
  109.         private object OnEntityTakeDamage(BasePlayer player, HitInfo info)
  110.         {
  111.             if (player == null || !player.userID.IsSteamId()) return null;
  112.  
  113.             if (IsGod)
  114.             {
  115.                 NullifyDamage(ref info);
  116.                 return true;
  117.             }
  118.  
  119.             return null;
  120.         }
  121.  
  122.         private static void NullifyDamage(ref HitInfo info)
  123.         {
  124.             info.damageTypes = new DamageTypeList();
  125.             info.HitMaterial = 0;
  126.             info.PointStart = Vector3.zero;
  127.         }
  128.  
  129.         private StoredData.PlayerData GetPlayerData(ulong playerID)
  130.         {
  131.             StoredData.PlayerData playerData;
  132.             if (!storedData.playerData.TryGetValue(playerID, out playerData))
  133.             {
  134.                 return null;
  135.             }
  136.  
  137.             return playerData;
  138.         }
  139.  
  140.         #region ChatCommand
  141.  
  142.         private void CmdMapNoteTeleport(BasePlayer player, string command, string[] args)
  143.         {
  144.  
  145.             if (configData.usePermission && !permission.UserHasPermission(player.UserIDString, PERMISSION_USE))
  146.             {
  147.                 if (!configData.globalS.adminsAllowed || !player.IsAdmin)
  148.                 {
  149.                     Print(player, Lang("NotAllowed", player.UserIDString));
  150.                     return;
  151.                 }
  152.             }
  153.  
  154.             var playerData = GetPlayerData(player.userID);
  155.             if (playerData == null)
  156.             {
  157.                 playerData = new StoredData.PlayerData
  158.                 {
  159.                     enabled = configData.globalS.defaultEnabled,
  160.                     time = configData.globalS.defaultCooldown,
  161.                 };
  162.                 storedData.playerData.Add(player.userID, playerData);
  163.             }
  164.  
  165.             if (args == null || args.Length == 0)
  166.             {
  167.                 playerData.enabled = !playerData.enabled;
  168.                 Print(player, Lang("MapNoteTeleport", player.UserIDString, playerData.enabled ? Lang("Enabled", player.UserIDString) : Lang("Disabled", player.UserIDString)));
  169.                 return;
  170.             }
  171.  
  172.             float time;
  173.             if (float.TryParse(args[0], out time))
  174.             {
  175.                 if (time <= configData.globalS.maximumCooldown && time >= configData.globalS.minimumCooldown)
  176.                 {
  177.                     playerData.time = time;
  178.                     if (!playerData.enabled) playerData.enabled = true;
  179.                     Print(player, Lang("MapNoteTeleportCooldown", player.UserIDString, time));
  180.                     return;
  181.                 }
  182.                 Print(player, Lang("MapNoteTeleportCooldownLimit", player.UserIDString, configData.globalS.minimumCooldown, configData.globalS.maximumCooldown));
  183.                 return;
  184.             }
  185.  
  186.             switch (args[0].ToLower())
  187.             {
  188.                 case "h":
  189.                 case "help":
  190.                     StringBuilder stringBuilder = new StringBuilder();
  191.                     stringBuilder.AppendLine();
  192.                     var firstCmd = configData.chatS.commands[0];
  193.                     stringBuilder.AppendLine(Lang("MapNoteTeleportSyntax", player.UserIDString, firstCmd));
  194.                     stringBuilder.AppendLine(Lang("MapNoteTeleportSyntax1", player.UserIDString, firstCmd, configData.globalS.minimumCooldown, configData.globalS.maximumCooldown));
  195.                     Print(player, stringBuilder.ToString());
  196.                     return;
  197.             }
  198.             Print(player, Lang("SyntaxError", player.UserIDString, configData.chatS.commands[0]));
  199.         }
  200.  
  201.         #endregion ChatCommand
  202.  
  203.         #region ConfigurationFile
  204.  
  205.         private ConfigData configData;
  206.  
  207.         private class ConfigData
  208.         {
  209.             [JsonProperty(PropertyName = "Use permissions")]
  210.             public bool usePermission = true;
  211.  
  212.             [JsonProperty(PropertyName = "Teleportation height (coordinate y)")]
  213.             public short coordinateY = 120;
  214.  
  215.             [JsonProperty(PropertyName = "Global settings")]
  216.             public GlobalSettings globalS = new GlobalSettings();
  217.  
  218.             [JsonProperty(PropertyName = "Chat settings")]
  219.             public ChatSettings chatS = new ChatSettings();
  220.  
  221.             public class GlobalSettings
  222.             {
  223.                 [JsonProperty(PropertyName = "Allows admins to teleport without permission")]
  224.                 public bool adminsAllowed = true;
  225.  
  226.                 [JsonProperty(PropertyName = "Default enabled")]
  227.                 public bool defaultEnabled = true;
  228.  
  229.                 [JsonProperty(PropertyName = "Default cooldown")]
  230.                 public float defaultCooldown = 10f;
  231.  
  232.                 [JsonProperty(PropertyName = "Maximum cooldown")]
  233.                 public float maximumCooldown = 15f;
  234.  
  235.                 [JsonProperty(PropertyName = "Minimum cooldown")]
  236.                 public float minimumCooldown = 5f;
  237.             }
  238.  
  239.             public class ChatSettings
  240.             {
  241.                 [JsonProperty(PropertyName = "Chat command")]
  242.                 public string[] commands = new[] { "mnt", "mapnoteteleport" };
  243.  
  244.                 [JsonProperty(PropertyName = "Chat prefix")]
  245.                 public string prefix = "[MapNote Teleport]: ";
  246.  
  247.                 [JsonProperty(PropertyName = "Chat prefix color")]
  248.                 public string prefixColor = "#00FFFF";
  249.  
  250.                 [JsonProperty(PropertyName = "Chat steamID icon")]
  251.                 public ulong steamIDIcon = 0;
  252.             }
  253.         }
  254.  
  255.         protected override void LoadConfig()
  256.         {
  257.             base.LoadConfig();
  258.             try
  259.             {
  260.                 configData = Config.ReadObject<ConfigData>();
  261.                 if (configData == null)
  262.                     LoadDefaultConfig();
  263.             }
  264.             catch
  265.             {
  266.                 PrintError("The configuration file is corrupted");
  267.                 LoadDefaultConfig();
  268.             }
  269.             SaveConfig();
  270.         }
  271.  
  272.         protected override void LoadDefaultConfig()
  273.         {
  274.             PrintWarning("Creating a new configuration file");
  275.             configData = new ConfigData();
  276.         }
  277.  
  278.         protected override void SaveConfig() => Config.WriteObject(configData);
  279.  
  280.         #endregion ConfigurationFile
  281.  
  282.         #region DataFile
  283.  
  284.         private StoredData storedData;
  285.  
  286.         private class StoredData
  287.         {
  288.             public readonly Dictionary<ulong, PlayerData> playerData = new Dictionary<ulong, PlayerData>();
  289.  
  290.             public class PlayerData
  291.             {
  292.                 public bool enabled;
  293.                 public float time;
  294.             }
  295.         }
  296.  
  297.         private void LoadData()
  298.         {
  299.             try
  300.             {
  301.                 storedData = Interface.Oxide.DataFileSystem.ReadObject<StoredData>(Name);
  302.             }
  303.             catch
  304.             {
  305.                 storedData = null;
  306.             }
  307.             finally
  308.             {
  309.                 if (storedData == null)
  310.                 {
  311.                     ClearData();
  312.                 }
  313.             }
  314.         }
  315.  
  316.         private void SaveData() => Interface.Oxide.DataFileSystem.WriteObject(Name, storedData);
  317.  
  318.         private void ClearData()
  319.         {
  320.             storedData = new StoredData();
  321.             SaveData();
  322.         }
  323.  
  324.         private void OnNewSave(string filename)
  325.         {
  326.             SaveData();
  327.         }
  328.  
  329.         #endregion DataFile
  330.  
  331.         #region LanguageFile
  332.  
  333.         private void Print(BasePlayer player, string message)
  334.         {
  335.             Player.Message(player, message, string.IsNullOrEmpty(configData.chatS.prefix) ? string.Empty : $"<color={configData.chatS.prefixColor}>{configData.chatS.prefix}</color>", configData.chatS.steamIDIcon);
  336.         }
  337.  
  338.         private string Lang(string key, string id = null, params object[] args) => string.Format(lang.GetMessage(key, this, id), args);
  339.  
  340.         protected override void LoadDefaultMessages()
  341.         {
  342.             lang.RegisterMessages(new Dictionary<string, string>
  343.             {
  344.                 ["NotAllowed"] = "You do not have permission to use this command",
  345.                 ["Enabled"] = "<color=#8ee700>Enabled</color>",
  346.                 ["Disabled"] = "<color=#ce422b>Disabled</color>",
  347.                 ["Teleported"] = "Teleported to <color=#FFA500>{0}</color>. Godmode is now <color=#8ee700>on</color> for <color=#FFA500>{1}</color>s while teleport is on cooldown.",
  348.                 ["CooldownEnded"] = "Godmode is now <color=#ce422b>off</color>. You can teleport again.",
  349.                 ["MapNoteTeleport"] = "Teleporting to map marker is now {0}",
  350.                 ["MapNoteTeleportCooldown"] = "Teleporting to map marker cooldown set to <color=#FFA500>{0}</color>s.",
  351.                 ["MapNoteTeleportCooldownLimit"] = "Teleporting to map marker cooldown allowed is between <color=#FFA500>{0}</color>s and <color=#FFA500>{1}</color>s",
  352.                 ["MapNoteTeleportMounted"] = "You can't teleport while seated!",
  353.                 ["MapNoteTeleportDead"] = "You can't teleport while being dead!",
  354.                 ["SyntaxError"] = "Syntax error, type '<color=#FFFF00>/{0} <help | h></color>' to view help",
  355.  
  356.                 ["MapNoteTeleportSyntax"] = "<color=#FFFF00>/{0} </color> - Enable/Disable teleporting to map marker",
  357.                 ["MapNoteTeleportSyntax1"] = "<color=#FFFF00>/{0} <time (seconds)></color> - Set teleport cooldown time, the allowed time is between <color=#FFA500>{1}</color>s and <color=#FFA500>{2}</color>s.",
  358.             }, this);
  359.             lang.RegisterMessages(new Dictionary<string, string>
  360.             {
  361.                 ["NotAllowed"] = "У вас нет разрешения на использование этой команды",
  362.                 ["Enabled"] = "<color=#8ee700>Включена</color>",
  363.                 ["Disabled"] = "<color=#ce422b>Отключена</color>",
  364.                 ["Teleported"] = "Вы телепортированы на <color=#FFA500>{0}</color>. Режим Бога <color=#8ee700>включен</color> на <color=#FFA500>{1}</color> сек, пока телепорт перезаряжается.",
  365.                 ["CooldownEnded"] = "Режим Бога <color=#ce422b>отключен</color>. Можете телепортироваться снова.",
  366.                 ["MapNoteTeleport"] = "Телепортация к маркеру на карте сейчас {0}",
  367.                 ["MapNoteTeleportCooldown"] = "Время перезарядки телепорта к маркеру на карте установлено на <color=#FFA500>{0}</color> секунд.",
  368.                 ["MapNoteTeleportCooldownLimit"] = "Значение времени перезарядки телепорта к маркеру на карте должно быть между <color=#FFA500>{0}</color> сек и <color=#FFA500>{1}</color> сек",
  369.                 ["MapNoteTeleportMounted"] = "Вы не можете телепортироваться, когда сидите!",
  370.                 ["MapNoteTeleportDead"] = "Вы не можете телепортироваться, пока мертвы!",
  371.                 ["SyntaxError"] = "Синтаксическая ошибка, напишите '<color=#FFFF00>/{0} <help | h></color>' чтобы отобразить подсказки",
  372.  
  373.                 ["MapNoteTeleportSyntax"] = "<color=#FFFF00>/{0} </color> - Включить/Выключить телепортацию к маркеру на карте",
  374.                 ["MapNoteTeleportSyntax1"] = "<color=#FFFF00>/{0} <time (seconds)></color> - Установить время перезарядки телепорта к маркеру на карте. Значение должно быть между <color=#FFA500>{1}</color> сек и <color=#FFA500>{2}</color> сек.",
  375.             }, this, "ru");
  376.         }
  377.  
  378.         #endregion LanguageFile
  379.     }
  380. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement