using System;
using System.Linq;
using System.Collections.Generic;
using Newtonsoft.Json;
using Oxide.Core;
using Oxide.Core.Plugins;
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
*
**********************************************************************/
#endregion
namespace Oxide.Plugins
{
[Info("MapBlockPLus", "Krungh Crow", "1.0.2")]
[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;
#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 = "[MapBlock+] ";
}
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();
}
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
{
["NoBroadcast"] = "Vendingmachine broadcasting is disabled on this server!",
["Info"] = "\nTo have a mutch better experience roaming the map we have blocked the view of the map",
["InvalidInput"] = "Please enter a valid command!",
["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;
}
else
{
Player.Message(player, prefix + string.Format(msg("InvalidInput", player.UserIDString)), chaticon);
}
}
}
#endregion
#region Core
void OnToggleVendingBroadcast(VendingMachine vendingmachine, BasePlayer player)
{
if (_UseVendingBlock)
{
vendingmachine.SetFlag(BaseEntity.Flags.Reserved4, false, false);
if (player != null)
{
Player.Message(player, prefix + string.Format(msg("NoBroadcast", player.UserIDString)), chaticon);
}
}
return;
}
private MapMarkerGenericRadius mapblocker;
private void OnPlayerInit(BasePlayer player)
{
if (_UseOverlay)
{
UpdateMapBlocker();
}
return;
}
private void OnServerInitialized()
{
if (_UseOverlay)
{
mapblocker = GameManager.server.CreateEntity(MapMarker, new Vector3(0, 100, 0)) as MapMarkerGenericRadius;
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();
}
if (_UseVendingBlock)
{
foreach (VendingMachine machine in UnityEngine.Object.FindObjectsOfType())
{
machine.SetFlag(BaseEntity.Flags.Reserved4, false, false);
machine.UpdateMapMarker();
}
}
else
{
foreach (VendingMachine machine in UnityEngine.Object.FindObjectsOfType())
{
machine.SetFlag(BaseEntity.Flags.Reserved4, true, false);
machine.UpdateMapMarker();
}
}
return;
}
private void Unload()
{
if (_UseOverlay)
{
mapblocker?.Kill();
foreach (VendingMachine machine in UnityEngine.Object.FindObjectsOfType())
{
machine.SetFlag(BaseEntity.Flags.Reserved4, true, true);
machine.UpdateMapMarker();
}
}
return;
}
private void UpdateMapBlocker()
{
mapblocker.Kill();
mapblocker.Spawn();
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
}
}