C#:
using System;
using System.Linq;
using System.Collections.Generic;
using Newtonsoft.Json;
using Oxide.Core;
using Oxide.Core.Plugins;
using ProtoBuf;
using UnityEngine;
#region Changelogs and ToDo
/**********************************************************************
*
* 1.0.0 : - Initial release
* 1.0.1 : - Added blocking of vendingamchine broadcasting
* 1.0.2 : - Added true false on block map and/or vendingmachine markers
* 1.1.1 : - Updated for Sept 3rd Rust changes
* - Vendingmachine optimisations.
*
**********************************************************************/
#endregion
namespace Oxide.Plugins
{
[Info("MapBlockPLus" , "Krungh Crow" , "1.1.1")]
[Description("Block map and assign a color and alpha to the overlay")]
class MapBlockPlus : RustPlugin
{
#region Variables
string MapMarker = "assets/prefabs/tools/map/genericradiusmarker.prefab";
ulong chaticon = 0;
string prefix;
bool _UseOverlay;
bool _UseVendingBlock;
private MapMarkerGenericRadius mapblocker;
private readonly Dictionary<NetworkableId , bool> vendingStates =
new Dictionary<NetworkableId , bool>();
#endregion
#region Configuration
void Init()
{
if (!LoadConfigVariables())
{
Puts("Config file issue detected. Please delete file, or check syntax and fix.");
return;
}
prefix = configData.PlugCFG.Prefix;
_UseOverlay = configData.BlockerCFG.UseOverlay;
_UseVendingBlock = configData.BlockerCFG.UseVendingBlock;
}
private ConfigData configData;
class ConfigData
{
[JsonProperty(PropertyName = "Plugin config")]
public SettingsPlugin PlugCFG = new SettingsPlugin();
[JsonProperty(PropertyName = "MapBlocker setup")]
public SettingsMapBlocker BlockerCFG = new SettingsMapBlocker();
}
class SettingsPlugin
{
[JsonProperty(PropertyName = "Chat Prefix")]
public string Prefix = "[<color=yellow>MapBlock+</color>] ";
}
class SettingsMapBlocker
{
[JsonProperty(PropertyName = "Use Map overlay")]
public bool UseOverlay = true;
[JsonProperty(PropertyName = "Block VendingMachine Markers")]
public bool UseVendingBlock = true;
[JsonProperty(PropertyName = "Overlay Alpha")]
public float Alpha = 1f;
[JsonProperty(PropertyName = "Overlay Color")]
public string OverlayColor = "blue";
}
private bool LoadConfigVariables()
{
try
{
configData = Config.ReadObject<ConfigData>();
}
catch
{
return false;
}
SaveConf();
return true;
}
protected override void LoadDefaultConfig()
{
Puts("Fresh install detected Creating a new config file.");
configData = new ConfigData();
SaveConf();
}
void SaveConf() => Config.WriteObject(configData , true);
#endregion
#region LanguageAPI
protected override void LoadDefaultMessages()
{
lang.RegisterMessages(new Dictionary<string , string>
{
["NoBroadcast"] = "<color=red>Vendingmachine broadcasting is disabled on this server!</color>" ,
["Info"] = "\n<color=green>To have a mutch better experience roaming the map we have blocked the view of the map</color>" ,
["InvalidInput"] = "<color=red>Please enter a valid command!</color>" ,
["Version"] = "Version : V" ,
} , this);
}
#endregion
#region Commands
[ChatCommand("mapblock")]
private void cmdPrimary(BasePlayer player , string command , string[] args)
{
if (args.Length == 0)
{
Player.Message(player , prefix + string.Format(msg("InvalidInput" , player.UserIDString)) , chaticon);
}
else
{
if (args[0].ToLower() == "info")
{
Player.Message(
player ,
prefix +
string.Format(msg("Version" , player.UserIDString)) +
this.Version.ToString() +
" By : " +
this.Author.ToString() +
msg("Info") ,
chaticon
);
return;
}
Player.Message(player , prefix + string.Format(msg("InvalidInput" , player.UserIDString)) , chaticon);
}
}
#endregion
#region Core
object OnMapMarkerAdd(BasePlayer player , MapNote note)
{
if (_UseOverlay && note != null)
return true;
return null;
}
void OnToggleVendingBroadcast(VendingMachine vendingmachine , BasePlayer player)
{
if (!_UseVendingBlock || vendingmachine == null || vendingmachine.IsDestroyed)
return;
SetVendingBroadcast(vendingmachine , false);
if (player != null)
{
Player.Message(
player ,
prefix + string.Format(msg("NoBroadcast" , player.UserIDString)) ,
chaticon
);
}
}
private void OnEntitySpawned(VendingMachine machine)
{
if (!_UseVendingBlock || machine == null)
return;
NextTick(() =>
{
if (machine == null || machine.IsDestroyed || machine.net == null)
return;
RememberVendingState(machine);
SetVendingBroadcast(machine , false);
machine.UpdateMapMarker();
});
}
private void OnEntityKill(VendingMachine machine)
{
if (machine == null || machine.net == null)
return;
vendingStates.Remove(machine.net.ID);
}
private void OnPlayerInit(BasePlayer player)
{
if (_UseOverlay)
UpdateMapBlocker();
}
void OnPlayerSleepEnded(BasePlayer player)
{
if (player != null && _UseOverlay)
UpdateMapBlocker();
}
object OnPlayerSpawn(BasePlayer player)
{
if (player != null && _UseOverlay)
UpdateMapBlocker();
return null;
}
private void OnServerInitialized()
{
if (_UseOverlay)
CreateMapBlocker();
if (!_UseVendingBlock)
return;
foreach (VendingMachine machine in UnityEngine.Object.FindObjectsOfType<VendingMachine>())
{
if (machine == null || machine.IsDestroyed || machine.net == null)
continue;
RememberVendingState(machine);
SetVendingBroadcast(machine , false);
machine.UpdateMapMarker();
}
}
private void CreateMapBlocker()
{
if (mapblocker != null && !mapblocker.IsDestroyed)
return;
mapblocker = GameManager.server.CreateEntity(
MapMarker ,
new Vector3(0 , 100 , 0)
) as MapMarkerGenericRadius;
if (mapblocker == null)
return;
mapblocker.alpha = configData.BlockerCFG.Alpha;
mapblocker.color1 = UpdateColor(configData.BlockerCFG.OverlayColor);
mapblocker.color2 = UpdateColor(configData.BlockerCFG.OverlayColor);
mapblocker.radius = ConVar.Server.worldsize;
mapblocker.Spawn();
mapblocker.SendUpdate();
}
private void RememberVendingState(VendingMachine machine)
{
if (machine == null || machine.net == null)
return;
if (!vendingStates.ContainsKey(machine.net.ID))
{
vendingStates[machine.net.ID] =
machine.HasFlag(BaseEntity.Flags.Reserved4);
}
}
private void SetVendingBroadcast(VendingMachine machine , bool state)
{
if (machine == null || machine.IsDestroyed)
return;
using (var flags = machine.StartSetFlags(BaseEntity.FlagsUpdateMode.SendNetworkUpdate_Flags))
{
flags.Set(BaseEntity.Flags.Reserved4 , state);
}
}
private void RestoreVendingStates()
{
foreach (KeyValuePair<NetworkableId , bool> entry in vendingStates)
{
VendingMachine machine =
BaseNetworkable.serverEntities.Find(entry.Key) as VendingMachine;
if (machine == null || machine.IsDestroyed)
continue;
SetVendingBroadcast(machine , entry.Value);
machine.UpdateMapMarker();
}
vendingStates.Clear();
}
private void Unload()
{
if (mapblocker != null && !mapblocker.IsDestroyed)
mapblocker.Kill();
mapblocker = null;
RestoreVendingStates();
}
private void UpdateMapBlocker()
{
if (mapblocker == null || mapblocker.IsDestroyed)
return;
mapblocker.SendUpdate();
}
private static Color UpdateColor(string hex)
{
Color color;
return ColorUtility.TryParseHtmlString(hex , out color)
? color
: Color.black;
}
#endregion
#region Message helper
private string msg(string key , string id = null) =>
lang.GetMessage(key , this , id);
#endregion
}
}