C#:
// ╔═══════════════════════════════════════════════════════════════════════════╗
// ║
// ║ Plugin : CupboardMarker
// ║ Author : WuyZar
// ║ Discord : https://discord.gg/NVwRcQwGwh
// ║ Shop : https://codefling.com/wuyzar
// ║
// ║ All rights reserved. Redistribution and modification are forbidden !
// ║
// ╚═══════════════════════════════════════════════════════════════════════════╝
using System.Collections.Generic; using Facepunch; using UnityEngine;
namespace Oxide.Plugins
{
[Info("CupboardMarker", "WuyZar", "1.0.0")]
[Description("Discord: https://discord.gg/NVwRcQwGwh")]
public class CupboardMarker : RustPlugin
{
private const string PermUse = "cupboardmarker.use";
private const string Prefab = "assets/prefabs/tools/map/genericradiusmarker.prefab";
private const string VendingPrefab = "assets/prefabs/deployable/vendingmachine/vending_mapmarker.prefab";
private Dictionary<ulong, MapMarkerGenericRadius> _outer = new(); private Dictionary<ulong, MapMarkerGenericRadius> _inner = new(); private Dictionary<ulong, VendingMachineMapMarker> _vending = new(); private PluginConfig config;
private class PluginConfig
{
public float Radius { get; set; } = 0.5f;
public float InnerOffset { get; set; } = 0.05f;
}
protected override void LoadDefaultConfig() { config = new PluginConfig(); SaveConfig(); }
protected override void SaveConfig() => Config.WriteObject(config, true);
protected override void LoadConfig() { base.LoadConfig(); config = Config.ReadObject<PluginConfig>() ?? new PluginConfig(); }
private void Init() { permission.RegisterPermission(PermUse, this); }
private void OnServerInitialized()
{
foreach (var entity in BaseNetworkable.serverEntities)
{
var tc = entity as BuildingPrivlidge;
if (tc != null) SpawnMarker(tc);
}
}
private void OnEntitySpawned(BuildingPrivlidge tc) { if (tc != null) NextTick(() => SpawnMarker(tc)); }
private void OnEntityKill(BuildingPrivlidge tc) { if (tc != null) KillMarker(tc.net.ID.Value); }
private void OnPlayerConnected(BasePlayer player) { if (permission.UserHasPermission(player.UserIDString, PermUse)) SendAllVending(player); }
private void OnCupboardAuthorize(BuildingPrivlidge tc, BasePlayer player) => NextTick(() => UpdateVending(tc));
private void OnCupboardDeauthorize(BuildingPrivlidge tc, BasePlayer player) => NextTick(() => UpdateVending(tc));
private void OnCupboardClearList(BuildingPrivlidge tc, BasePlayer player) => NextTick(() => UpdateVending(tc));
private void OnUserPermissionGranted(string userId, string perm)
{
if (perm != PermUse) return;
var player = BasePlayer.Find(userId);
if (player != null && player.IsConnected) SendAllVending(player);
}
private void OnUserPermissionRevoked(string userId, string perm) { if (perm == PermUse) RefreshVendings(); }
private void OnGroupPermissionRevoked(string group, string perm) { if (perm == PermUse) RefreshVendings(); }
private void OnGroupPermissionGranted(string group, string perm)
{
if (perm != PermUse) return;
foreach (var player in BasePlayer.activePlayerList)
if (permission.UserHasPermission(player.UserIDString, PermUse)) SendAllVending(player);
}
private void OnUserGroupAdded(string userId, string group)
{
var player = BasePlayer.Find(userId);
if (player == null || !player.IsConnected) return;
if (permission.UserHasPermission(userId, PermUse)) SendAllVending(player);
}
private void OnUserGroupRemoved(string userId, string group) { if (permission.GroupHasPermission(group, PermUse)) RefreshVendings(); }
[ConsoleCommand("cm.reload")]
private void CmdReload(ConsoleSystem.Arg arg)
{
var player = arg.Player();
if (player != null && !permission.UserHasPermission(player.UserIDString, PermUse)) return;
RefreshAll();
arg.ReplyWith("<size=15><color=#CC3326>Cupboard Marker :</color></size>\nAll markers refreshed !");
}
private void Unload()
{
foreach (var m in _outer.Values) if (m != null && !m.IsDestroyed) m.Kill();
foreach (var m in _inner.Values) if (m != null && !m.IsDestroyed) m.Kill();
foreach (var m in _vending.Values) if (m != null && !m.IsDestroyed) m.Kill();
_outer.Clear(); _inner.Clear(); _vending.Clear();
}
private void SpawnMarker(BuildingPrivlidge tc)
{
if (tc == null || tc.IsDestroyed || tc.net == null) return;
var id = tc.net.ID.Value;
if (_outer.ContainsKey(id)) return;
var pos = tc.transform.position;
var ring = GameManager.server.CreateEntity(Prefab, pos) as MapMarkerGenericRadius;
if (ring == null) return;
ring.radius = config.Radius; ring.alpha = 1f;
ring.color1 = new Color(0f, 0f, 0f, 0f);
ring.color2 = new Color(0xCC / 255f, 0x33 / 255f, 0x26 / 255f, 1f);
ring.Spawn(); ring.SendUpdate();
_outer[id] = ring;
var fill = GameManager.server.CreateEntity(Prefab, pos) as MapMarkerGenericRadius;
if (fill == null) return;
fill.radius = config.Radius - config.InnerOffset; fill.alpha = 1f;
fill.color1 = new Color(0x1A / 255f, 0x1A / 255f, 0x1A / 255f, 1f);
fill.color2 = new Color(0f, 0f, 0f, 0f);
fill.Spawn(); fill.SendUpdate();
_inner[id] = fill;
SpawnVending(tc, pos, id);
}
private void SpawnVending(BuildingPrivlidge tc, Vector3 pos, ulong id)
{
var vending = GameManager.server.CreateEntity(VendingPrefab, pos) as VendingMachineMapMarker;
if (vending == null) return;
vending.markerShopName = BuildAuthList(tc);
vending.limitNetworking = true;
vending.Spawn(); vending.SendNetworkUpdate();
_vending[id] = vending;
foreach (var player in BasePlayer.activePlayerList)
if (permission.UserHasPermission(player.UserIDString, PermUse))
vending.SendAsSnapshot(player.Connection);
}
private void KillMarker(ulong id)
{
MapMarkerGenericRadius m; VendingMachineMapMarker v;
if (_outer.TryGetValue(id, out m)) { if (m != null && !m.IsDestroyed) m.Kill(); _outer.Remove(id); }
if (_inner.TryGetValue(id, out m)) { if (m != null && !m.IsDestroyed) m.Kill(); _inner.Remove(id); }
if (_vending.TryGetValue(id, out v)) { if (v != null && !v.IsDestroyed) v.Kill(); _vending.Remove(id); }
}
private void SendAllVending(BasePlayer player)
{
foreach (var v in _vending.Values)
if (v != null && !v.IsDestroyed) v.SendAsSnapshot(player.Connection);
}
private void UpdateVending(BuildingPrivlidge tc)
{
if (tc == null || tc.net == null) return;
VendingMachineMapMarker v;
if (!_vending.TryGetValue(tc.net.ID.Value, out v) || v == null || v.IsDestroyed) return;
v.markerShopName = BuildAuthList(tc);
v.SendNetworkUpdate();
}
private void RefreshVendings()
{
foreach (var v in _vending.Values) if (v != null && !v.IsDestroyed) v.Kill();
_vending.Clear();
foreach (var entity in BaseNetworkable.serverEntities)
{
var tc = entity as BuildingPrivlidge;
if (tc == null || tc.IsDestroyed || tc.net == null || !_outer.ContainsKey(tc.net.ID.Value)) continue;
SpawnVending(tc, tc.transform.position, tc.net.ID.Value);
}
}
private void RefreshAll()
{
foreach (var m in _outer.Values) if (m != null && !m.IsDestroyed) m.Kill();
foreach (var m in _inner.Values) if (m != null && !m.IsDestroyed) m.Kill();
foreach (var m in _vending.Values) if (m != null && !m.IsDestroyed) m.Kill();
_outer.Clear(); _inner.Clear(); _vending.Clear();
foreach (var entity in BaseNetworkable.serverEntities)
{
var tc = entity as BuildingPrivlidge;
if (tc != null) SpawnMarker(tc);
}
}
private string BuildAuthList(BuildingPrivlidge tc)
{
if (tc.authorizedPlayers.Count == 0) return "No authorized players";
var names = Pool.Get<List<string>>();
foreach (var uid in tc.authorizedPlayers)
{
var p = BasePlayer.FindByID(uid) ?? BasePlayer.FindSleeping(uid);
names.Add(p != null ? p.displayName : covalence.Players.FindPlayerById(uid.ToString())?.Name ?? uid.ToString());
}
var result = string.Join(", ", names);
Pool.FreeUnmanaged(ref names);
return result;
}
}
}