using System; using System.Linq; using UnityEngine; namespace Oxide.Plugins { [Info("Simple Range Finder", "NooBlet", "1.0.2")] [Description("Adds A Range finder to scopes and Binoculars")] public class SimpleRangeFinder : RustPlugin { internal const string PERMISSION = "SimpleRangeFinder.use"; internal const string PERMISSION2 = "SimpleRangeFinder.binocular"; private void Loaded() { permission.RegisterPermission(PERMISSION, this); permission.RegisterPermission(PERMISSION2, this); } private void OnPlayerInput(BasePlayer player, InputState input) { if (player == null || input == null/*||!player.IsAdmin*/|| !permission.UserHasPermission(player.UserIDString,PERMISSION)) return; if (player.GetHeldEntity() is BaseProjectile weapon) { if (weapon.primaryMagazine == null) return; if (input.IsDown(BUTTON.FIRE_SECONDARY)) { StartRaycastLogging(player); } else if (input.WasJustReleased(BUTTON.FIRE_SECONDARY)) { StopRaycastLogging(player); } } if (permission.UserHasPermission(player.UserIDString,PERMISSION2)&& player.GetHeldEntity() is Binocular) { if (input.IsDown(BUTTON.FIRE_SECONDARY)) { StartRaycastLogging(player); } else if (input.WasJustReleased(BUTTON.FIRE_SECONDARY)) { StopRaycastLogging(player); } } } private void StartRaycastLogging(BasePlayer player) { if (player.GetHeldEntity() is BaseProjectile weapon) { var item = weapon.GetItem(); if (item.contents == null) { return; } if (item.contents.itemList.Count == 0) { return; } var modItems = item.contents.itemList.Where(m => m.info.shortname.Contains("scope")); if (!modItems.Any()) { return; } if (!player.HasComponent()) { var logger = player.gameObject.AddComponent(); logger.Init(player); } } if (permission.UserHasPermission(player.UserIDString, PERMISSION2) && player.GetHeldEntity() is Binocular binocular) { if (!player.HasComponent()) { var logger = player.gameObject.AddComponent(); logger.Init(player); } } } private void StopRaycastLogging(BasePlayer player) { var logger = player.GetComponent(); if (logger != null) { logger.StopLogging(); } } public class RaycastLogger : MonoBehaviour { private BasePlayer player; private bool isLogging = false; private LayerMask layerMask = LayerMask.GetMask("Terrain", "Player (Server)", "Construction", "Tree"); public void Init(BasePlayer player) { this.player = player; StartLogging(); } private void StartLogging() { if (isLogging) return; isLogging = true; InvokeRepeating(nameof(LogRaycastDistance), 0f, 0.5f); } public void StopLogging() { if (!isLogging) return; isLogging = false; CancelInvoke(nameof(LogRaycastDistance)); Destroy(this); } private void LogRaycastDistance() { Ray ray = new Ray(player.eyes.position, player.eyes.BodyForward()); if (Physics.Raycast(ray, out RaycastHit hit, 500f, layerMask)) { float distance = (float)Math.Round(hit.distance); player.ShowToast(GameTip.Styles.Blue_Short, $"Distance: {distance}m"); } else { player.ShowToast(GameTip.Styles.Blue_Short, "Outside Max Range: 500m."); } } } } }