C#:
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Oxide.Core.Plugins;
using Oxide.Core;
namespace Oxide.Plugins
{
[Info("Radio On", "Sigilo", "1.0.2")]
[Description("Turns radios on when deployed, on server restarts, and every X minutes.")]
public class RadioOn : RustPlugin
{
private static PluginConfig _config;
private Timer _timer;
private HashSet<BoomBox> _trackedBoomboxes = new HashSet<BoomBox>();
void Init()
{
_config = Config.ReadObject<PluginConfig>();
}
void OnServerInitialized()
{
Puts("RadioOn plugin initialized, starting radio detection...");
int foundBoombox = 0;
int foundStatic = 0;
var deployedBoomboxes = UnityEngine.Object.FindObjectsOfType<DeployableBoomBox>();
Puts($"Found {deployedBoomboxes.Length} boombox entities to process");
foreach (var entity in deployedBoomboxes)
{
if (entity == null || entity.IsDestroyed)
continue;
BoomBox boxController = entity.BoxController;
if (boxController == null || boxController.baseEntity == null || boxController.baseEntity.IsDestroyed)
continue;
string prefabName = entity.ShortPrefabName;
bool shouldTrack = false;
if (prefabName == "boombox.deployed" && _config.SetDefaultDeployed)
{
shouldTrack = true;
foundBoombox++;
}
else if (prefabName == "boombox.static" && _config.SetDefaultStatic)
{
shouldTrack = true;
foundStatic++;
}
if (shouldTrack)
{
_trackedBoomboxes.Add(boxController);
}
}
Puts($"Found {foundBoombox} deployed and {foundStatic} static boomboxes");
if (_config.EnableOnServerStart)
{
TurnOnAllRadios();
}
Puts($"Initially tracked {_trackedBoomboxes.Count} radios");
if (_config.EnableAutoRestart)
{
float intervalSeconds = _config.IntervalMinutes * 60f;
_timer = timer.Every(intervalSeconds, () => {
Puts($"Timer triggered - attempting to turn on {_trackedBoomboxes.Count} radios");
TurnOnAllRadios();
});
Puts($"Auto-restart timer initialized for every {_config.IntervalMinutes} minutes");
}
}
void OnEntitySpawned(BaseEntity entity)
{
// Only process if it's a DeployableBoomBox, skip all other entity types
if (!(entity is DeployableBoomBox))
return;
string prefabName = entity.ShortPrefabName;
// Only log boombox-related spawns
Puts($"Boombox entity spawned: {prefabName}");
BoomBox boxController = null;
if (prefabName == "boombox.deployed" && _config.SetDefaultDeployed)
{
boxController = (entity as DeployableBoomBox)?.BoxController;
Puts($"Deployable boombox spawned - tracking enabled: {_config.SetDefaultDeployed}");
}
else if (prefabName == "boombox.static" && _config.SetDefaultStatic)
{
boxController = (entity as DeployableBoomBox)?.BoxController;
Puts($"Static boombox spawned - tracking enabled: {_config.SetDefaultStatic}");
}
if (boxController != null)
{
_trackedBoomboxes.Add(boxController);
SetBoomboxRadioIP(boxController);
Puts($"Added boombox to tracking, total count: {_trackedBoomboxes.Count}");
}
}
void OnEntityKill(BaseEntity entity)
{
BoomBox boxController = null;
if (entity is DeployableBoomBox deployable)
{
boxController = deployable.BoxController;
}
if (boxController != null)
{
_trackedBoomboxes.Remove(boxController);
}
}
void Unload()
{
_config = null;
Config.Clear();
if (_timer != null) _timer.Destroy();
_trackedBoomboxes.Clear();
}
private void SetBoomboxRadioIP(BoomBox box)
{
if (box == null || box.baseEntity == null || box.baseEntity.IsDestroyed)
return;
try
{
box.CurrentRadioIp = _config.DefaultRadioStationUrl;
box.baseEntity.ClientRPC(RpcTarget.NetworkGroup("OnRadioIPChanged", box.baseEntity), box.CurrentRadioIp);
box.SetFlag(BaseEntity.Flags.On, true);
box.baseEntity.SendNetworkUpdate();
}
catch (System.Exception ex)
{
PrintError($"Failed to set radio: {ex.Message}");
}
}
private void TurnOnAllRadios()
{
int turnedOn = 0;
int deployedCount = 0;
int staticCount = 0;
Puts("TurnOnAllRadios called - checking all tracked radios");
foreach (var box in _trackedBoomboxes.ToList())
{
if (box == null || box.baseEntity == null || box.baseEntity.IsDestroyed || box.HasFlag(BaseEntity.Flags.On))
continue;
BaseEntity entity = box.baseEntity;
string prefabName = entity.ShortPrefabName;
bool shouldTurnOn = false;
Puts($"Checking radio: {prefabName}");
if (prefabName == "boombox.deployed" && _config.SetDefaultDeployed)
{
shouldTurnOn = true;
deployedCount++;
}
else if (prefabName == "boombox.static" && _config.SetDefaultStatic)
{
shouldTurnOn = true;
staticCount++;
}
if (shouldTurnOn)
{
SetBoomboxRadioIP(box);
turnedOn++;
}
}
Puts($"Turned on {turnedOn} radios ({deployedCount} deployed, {staticCount} static) out of {_trackedBoomboxes.Count} tracked radios");
}
private static PluginConfig GetDefaultConfig()
{
return new PluginConfig
{
DefaultRadioStationUrl = "http://s-00.wefunkradio.com:81/wefunk64.mp3",
SetDefaultDeployed = false,
SetDefaultStatic = true,
IntervalMinutes = 60,
EnableAutoRestart = true,
EnableOnServerStart = true
};
}
private class PluginConfig
{
[JsonProperty("Radio Stream URL")]
public string DefaultRadioStationUrl { get; set; }
[JsonProperty("Enable for deployed boomboxes")]
public bool SetDefaultDeployed { get; set; }
[JsonProperty("Enable for static/monument boomboxes")]
public bool SetDefaultStatic { get; set; }
[JsonProperty("Auto-restart interval (minutes)")]
public int IntervalMinutes { get; set; }
[JsonProperty("Enable auto-restart")]
public bool EnableAutoRestart { get; set; }
[JsonProperty("Turn on radios after server restart")]
public bool EnableOnServerStart { get; set; }
}
protected override void LoadDefaultConfig()
{
Config.Clear();
Config.WriteObject(GetDefaultConfig(), true);
}
protected override void LoadConfig()
{
base.LoadConfig();
_config = Config.ReadObject<PluginConfig>();
}
}
}