using System.Collections.Generic; using UnityEngine; using VLB; using Color = UnityEngine.Color; /** * VERSION HISTORY * * V 1.0.5 * - check if a marker already exists and return false * **/ namespace Oxide.Plugins { [Info("Marker API", "The_Kiiiing", "1.0.5")] [Description("Allows placement of markers on in-game map")] public class MarkerAPI : RustPlugin { #region Fields private const string genericPrefab = "assets/prefabs/tools/map/genericradiusmarker.prefab"; private const string vendingPrefab = "assets/prefabs/deployable/vendingmachine/vending_mapmarker.prefab"; private const string markerLimited = "mapmarker.net.limited"; private Dictionary cachedMarkers = new Dictionary(); #endregion #region Core private bool CreateMarker(Vector3 position, int duration, float refreshRate, string name, string displayName, float radius = 0.3f, string colorMarker = "00FFFF", string colorOutline = "00FFFFFF") { if (cachedMarkers.ContainsKey(name)) { return false; } var marker = new GameObject().AddComponent(); marker.name = name; marker.displayName = displayName; marker.radius = radius; marker.position = position; marker.duration = duration; marker.refreshRate = refreshRate; ColorUtility.TryParseHtmlString($"#{colorMarker}", out marker.color1); ColorUtility.TryParseHtmlString($"#{colorOutline}", out marker.color2); cachedMarkers.Add(name, marker); return true; } private bool CreateMarker(BaseEntity entity, int duration, float refreshRate, string name, string displayName, float radius = 0.3f, string colorMarker = "00FFFF", string colorOutline = "00FFFFFF") { if (cachedMarkers.ContainsKey(name)) { return false; } var marker = entity.gameObject.GetOrAddComponent(); marker.name = name; marker.displayName = displayName; marker.radius = radius; marker.refreshRate = refreshRate; marker.parent = entity; marker.position = entity.transform.position; marker.duration = duration; ColorUtility.TryParseHtmlString($"#{colorMarker}", out marker.color1); ColorUtility.TryParseHtmlString($"#{colorOutline}", out marker.color2); cachedMarkers.Add(name, marker); return true; } private bool CreateMarkerPrivate(Vector3 position, int duration, float refreshRate, string name, string displayName, ulong ownerid, float radius = 0.3f, string colorMarker = "00FFFF", string colorOutline = "00FFFFFF") { if (cachedMarkers.ContainsKey(name)) { return false; } var marker = new GameObject().AddComponent(); marker.name = name; marker.displayName = displayName; marker.radius = radius; marker.refreshRate = refreshRate; marker.position = position; marker.duration = duration; marker.ownerid = ownerid; marker.goname = markerLimited; ColorUtility.TryParseHtmlString($"#{colorMarker}", out marker.color1); ColorUtility.TryParseHtmlString($"#{colorOutline}", out marker.color2); cachedMarkers.Add(name, marker); return true; } private bool CreateMarkerPrivate(BaseEntity entity, int duration, float refreshRate, string name, string displayName, ulong ownerid, float radius = 0.3f, string colorMarker = "00FFFF", string colorOutline = "00FFFFFF") { if (cachedMarkers.ContainsKey(name)) { return false; } var marker = entity.gameObject.GetOrAddComponent(); marker.name = name; marker.displayName = displayName; marker.radius = radius; marker.refreshRate = refreshRate; marker.parent = entity; marker.position = entity.transform.position; marker.duration = duration; marker.ownerid = ownerid; marker.goname = markerLimited; ColorUtility.TryParseHtmlString($"#{colorMarker}", out marker.color1); ColorUtility.TryParseHtmlString($"#{colorOutline}", out marker.color2); cachedMarkers.Add(name, marker); return true; } private void RemoveMarker(string name) { foreach (var marker in UnityEngine.Object.FindObjectsOfType()) { if (marker.name == name) { UnityEngine.Object.Destroy(marker); } } } private void RemoveMarkers() { foreach (var marker in UnityEngine.Object.FindObjectsOfType()) { UnityEngine.Object.Destroy(marker); } } private void RemoveCachedMarker(string uname) { CustomMapMarker marker; if (cachedMarkers.TryGetValue(uname, out marker)) { UnityEngine.Object.Destroy(marker); cachedMarkers.Remove(uname); } } #endregion #region API private bool API_CreateMarkerPublic(Vector3 position, string uname, int duration = 0, float refreshRate = 3f, float radius = 0.4f, string displayName = "Marker", string colorMarker = "00FFFF", string colorOutline = "00FFFFFF") { return CreateMarker(position, duration, refreshRate, uname, displayName, radius, colorMarker, colorOutline); } private bool API_CreateMarkerPublic(BaseEntity entity, string uname, int duration = 0, float refreshRate = 3f, float radius = 0.4f, string displayName = "Marker", string colorMarker = "00FFFF", string colorOutline = "00FFFFFF") { return CreateMarker(entity, duration, refreshRate, uname, displayName, radius, colorMarker, colorOutline); } private bool API_CreateMarkerPrivate(Vector3 position, string uname, ulong ownerid, int duration = 0, float refreshRate = 3f, float radius = 0.4f, string displayName = "Marker", string colorMarker = "00FFFF", string colorOutline = "00FFFFFF") { return CreateMarkerPrivate(position, duration, refreshRate, uname, displayName, ownerid, radius, colorMarker, colorOutline); } private bool API_CreateMarkerPrivate(BaseEntity entity, string uname, ulong ownerid, int duration = 0, float refreshRate = 3f, float radius = 0.4f, string displayName = "Marker", string colorMarker = "00FFFF", string colorOutline = "00FFFFFF") { return CreateMarkerPrivate(entity, duration, refreshRate, uname, displayName, ownerid, radius, colorMarker, colorOutline); } private void API_RemoveMarker(string uname) { RemoveMarker(uname); } private void API_RemoveCachedMarker(string uname) { RemoveCachedMarker(uname); } #endregion #region Hooks private void Unload() { RemoveMarkers(); } #endregion #region Mono Behavior private class CustomMapMarker : MonoBehaviour { private VendingMachineMapMarker vending; private MapMarkerGenericRadius generic; public BaseEntity parent; private bool asChild; public float radius; public Color color1; public Color color2; public string displayName; public float refreshRate; public Vector3 position; public int duration; public bool placedByPlayer; public string goname; public ulong ownerid; private void Start() { transform.position = position; asChild = parent != null; CreateMarkers(); } private void CreateMarkers() { vending = GameManager.server.CreateEntity(vendingPrefab, position) .GetComponent(); vending.markerShopName = displayName; vending.enableSaving = false; vending.name = goname; vending.OwnerID = ownerid; vending.Spawn(); generic = GameManager.server.CreateEntity(genericPrefab).GetComponent(); generic.color1 = color1; generic.color2 = color2; generic.radius = radius; generic.alpha = 1f; generic.enableSaving = false; generic.SetParent(vending); generic.name = goname; generic.OwnerID = ownerid; generic.Spawn(); if (duration != 0) { Invoke(nameof(DestroyMakers), duration); } UpdateMarkers(); if (refreshRate > 0f) { if (asChild) { InvokeRepeating(nameof(UpdatePosition), refreshRate, refreshRate); } else { InvokeRepeating(nameof(UpdateMarkers), refreshRate, refreshRate); } } } private void UpdatePosition() { if (asChild) { if (!parent.IsValid()) { Destroy(this); return; } else { var pos = parent.transform.position; transform.position = pos; vending.transform.position = pos; } } UpdateMarkers(); } private void UpdateMarkers() { vending.SendNetworkUpdate(); generic.SendUpdate(); } private void DestroyMakers() { if (vending.IsValid()) { vending.Kill(); } if (generic.IsValid()) { generic.Kill(); } } private void OnDestroy() { DestroyMakers(); } } #endregion #region Networking object CanNetworkTo(MapMarkerGenericRadius radius, BasePlayer target) { if (radius.name == markerLimited) { if (target.userID == radius.OwnerID) return null; else return false; } return null; } object CanNetworkTo(VendingMachineMapMarker vmarker, BasePlayer target) { if (vmarker.name == markerLimited) { if (target.userID == vmarker.OwnerID) return null; else return false; } return null; } #endregion } }