Change Static Lookup

This commit is contained in:
saulteafarmer 2025-03-17 10:35:42 +00:00
parent ed6530e8db
commit b4ad0c9bca

View File

@ -5,7 +5,7 @@ using System;
namespace Oxide.Plugins namespace Oxide.Plugins
{ {
[Info("Auto Radio Manager", "RustySats", "1.0.0")] [Info("Auto Radio Manager", "RustySats", "1.0.1")]
[Description("Automatically manages boomboxes in your Rust server with various configuration options")] [Description("Automatically manages boomboxes in your Rust server with various configuration options")]
public class AutoRadioManager : RustPlugin public class AutoRadioManager : RustPlugin
{ {
@ -40,6 +40,9 @@ namespace Oxide.Plugins
[JsonProperty("Turn on radios after server restart")] [JsonProperty("Turn on radios after server restart")]
public bool TurnOnAfterServerRestart = true; public bool TurnOnAfterServerRestart = true;
[JsonProperty("Debug mode")]
public bool DebugMode = false;
} }
protected override void LoadDefaultConfig() => config = new Configuration(); protected override void LoadDefaultConfig() => config = new Configuration();
@ -109,6 +112,9 @@ namespace Oxide.Plugins
{ {
if (!config.EnableForDeployed || boombox == null) if (!config.EnableForDeployed || boombox == null)
return; return;
if (config.DebugMode)
Puts($"DEBUG: Deployable boombox spawned: {boombox}");
// Use a slight delay to ensure the entity is fully initialized // Use a slight delay to ensure the entity is fully initialized
timer.Once(0.5f, () => { timer.Once(0.5f, () => {
@ -121,6 +127,9 @@ namespace Oxide.Plugins
{ {
if (!config.EnableForHandheld || boombox == null) if (!config.EnableForHandheld || boombox == null)
return; return;
if (config.DebugMode)
Puts($"DEBUG: Handheld boombox spawned: {boombox}");
// Use a slight delay to ensure the entity is fully initialized // Use a slight delay to ensure the entity is fully initialized
timer.Once(0.5f, () => { timer.Once(0.5f, () => {
@ -131,13 +140,23 @@ namespace Oxide.Plugins
private void OnEntitySpawned(BoomBox boombox) private void OnEntitySpawned(BoomBox boombox)
{ {
if (config.DebugMode)
Puts($"DEBUG: BoomBox entity spawned: {boombox}, Type: {boombox?.GetType().Name}");
if (!config.EnableForStatic || boombox == null) if (!config.EnableForStatic || boombox == null)
return; return;
// Use a slight delay to ensure the entity is fully initialized // Use a slight delay to ensure the entity is fully initialized
timer.Once(0.5f, () => { timer.Once(0.5f, () => {
if (boombox != null && IsStaticBoombox(boombox)) if (boombox != null)
SetBoomboxRadioIP(boombox); {
bool isStatic = IsStaticBoombox(boombox);
if (config.DebugMode)
Puts($"DEBUG: IsStaticBoombox result: {isStatic} for {boombox}");
if (isStatic)
SetBoomboxRadioIP(boombox);
}
}); });
} }
@ -197,40 +216,67 @@ namespace Oxide.Plugins
Puts($"Added {stationEntries.Count} additional radio stations to server list"); Puts($"Added {stationEntries.Count} additional radio stations to server list");
} }
} }
// Fixed method to identify static boomboxes based on name
private bool IsStaticBoombox(BoomBox boombox) private bool IsStaticBoombox(BoomBox boombox)
{ {
// This determines if a boombox is a static/monument boombox if (boombox == null)
return false;
try
if (config.DebugMode)
{ {
// Find all deployable and held boomboxes Puts($"DEBUG: Checking if {boombox} is static. Type: {boombox.GetType().Name}");
var deployables = UnityEngine.Object.FindObjectsOfType<DeployableBoomBox>();
var handhelds = UnityEngine.Object.FindObjectsOfType<HeldBoomBox>();
// Check if this boombox is a controller for any of them // Debug the hierarchy
foreach (var deployable in deployables) if (boombox.GetComponentInParent<DeployableBoomBox>() != null)
Puts($"DEBUG: Has DeployableBoomBox parent");
if (boombox.GetComponentInParent<HeldBoomBox>() != null)
Puts($"DEBUG: Has HeldBoomBox parent");
// Log entity name if available
if (boombox.baseEntity != null)
{ {
if (deployable.BoxController == boombox) Puts($"DEBUG: Entity name: {boombox.baseEntity.name}");
return false; // It's a deployed boombox Puts($"DEBUG: Owner ID: {boombox.baseEntity.OwnerID}");
} }
}
// Check if the name contains "static" - this is the most reliable way to identify them
if (boombox.baseEntity != null && boombox.baseEntity.name != null)
{
string entityName = boombox.baseEntity.name.ToLower();
if (entityName.Contains("static"))
{
if (config.DebugMode)
Puts($"DEBUG: Identified as static by name: {entityName}");
return true;
}
}
// The original method wasn't reliable, so we'll skip parent component checks
// As a fallback, use the owner ID check
if (boombox.baseEntity != null && boombox.baseEntity.OwnerID == 0)
{
// For additional safety, make sure it's not already counted as handheld
var handhelds = UnityEngine.Object.FindObjectsOfType<HeldBoomBox>();
foreach (var handheld in handhelds) foreach (var handheld in handhelds)
{ {
if (handheld.BoxController == boombox) if (handheld != null && handheld.BoxController == boombox)
return false; // It's a handheld boombox return false; // It's already identified as handheld
} }
// Check if it has an owner if (config.DebugMode)
if (boombox.baseEntity != null && boombox.baseEntity.OwnerID != 0) Puts($"DEBUG: Identified as static by having no owner");
return false; // It has an owner, so likely not a static/monument boombox
return true; // If it's not part of a deployable or handheld and has no owner, assume it's static return true;
}
catch (Exception ex)
{
PrintError($"Error in IsStaticBoombox: {ex.Message}");
return false; // Default to not treating it as static in case of error
} }
if (config.DebugMode)
Puts($"DEBUG: Not identified as static");
return false;
} }
private void SetBoomboxRadioIP(BoomBox box) private void SetBoomboxRadioIP(BoomBox box)
@ -239,6 +285,9 @@ namespace Oxide.Plugins
try try
{ {
if (config.DebugMode)
Puts($"DEBUG: Setting radio IP for {box}");
// Set the radio IP to the configured stream URL // Set the radio IP to the configured stream URL
box.CurrentRadioIp = config.RadioStreamUrl; box.CurrentRadioIp = config.RadioStreamUrl;
@ -248,6 +297,9 @@ namespace Oxide.Plugins
// Turn on the radio // Turn on the radio
box.SetFlag(BaseEntity.Flags.On, true); box.SetFlag(BaseEntity.Flags.On, true);
box.baseEntity.SendNetworkUpdate(); box.baseEntity.SendNetworkUpdate();
if (config.DebugMode)
Puts($"DEBUG: Radio configured successfully for {box}");
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -286,10 +338,23 @@ namespace Oxide.Plugins
if (config.EnableForDeployed) if (config.EnableForDeployed)
{ {
var deployables = UnityEngine.Object.FindObjectsOfType<DeployableBoomBox>(); var deployables = UnityEngine.Object.FindObjectsOfType<DeployableBoomBox>();
if (config.DebugMode)
Puts($"DEBUG: Found {deployables.Length} deployable boomboxes");
foreach (var deployable in deployables) foreach (var deployable in deployables)
{ {
if (deployable != null && deployable.BoxController != null) if (deployable != null && deployable.BoxController != null)
{ {
// Skip if this is a static boombox incorrectly identified as deployed
if (deployable.BoxController.baseEntity != null &&
deployable.BoxController.baseEntity.name != null &&
deployable.BoxController.baseEntity.name.ToLower().Contains("static"))
{
if (config.DebugMode)
Puts($"DEBUG: Skipping static boombox incorrectly identified as deployed: {deployable.BoxController.baseEntity.name}");
continue;
}
SetBoomboxRadioIP(deployable.BoxController); SetBoomboxRadioIP(deployable.BoxController);
deployedCount++; deployedCount++;
} }
@ -300,6 +365,9 @@ namespace Oxide.Plugins
if (config.EnableForHandheld) if (config.EnableForHandheld)
{ {
var handhelds = UnityEngine.Object.FindObjectsOfType<HeldBoomBox>(); var handhelds = UnityEngine.Object.FindObjectsOfType<HeldBoomBox>();
if (config.DebugMode)
Puts($"DEBUG: Found {handhelds.Length} handheld boomboxes");
foreach (var handheld in handhelds) foreach (var handheld in handhelds)
{ {
if (handheld != null && handheld.BoxController != null) if (handheld != null && handheld.BoxController != null)
@ -314,14 +382,70 @@ namespace Oxide.Plugins
if (config.EnableForStatic) if (config.EnableForStatic)
{ {
var allBoomboxes = UnityEngine.Object.FindObjectsOfType<BoomBox>(); var allBoomboxes = UnityEngine.Object.FindObjectsOfType<BoomBox>();
if (config.DebugMode)
Puts($"DEBUG: Found {allBoomboxes.Length} total boomboxes of all types");
// First, find all boomboxes with "static" in their name
foreach (var boombox in allBoomboxes) foreach (var boombox in allBoomboxes)
{ {
if (boombox != null && IsStaticBoombox(boombox)) if (boombox != null && boombox.baseEntity != null &&
boombox.baseEntity.name != null &&
boombox.baseEntity.name.ToLower().Contains("static"))
{ {
if (config.DebugMode)
Puts($"DEBUG: Found static boombox by name: {boombox.baseEntity.name}");
SetBoomboxRadioIP(boombox); SetBoomboxRadioIP(boombox);
staticCount++; staticCount++;
} }
} }
// If no static boomboxes were found by name, try other methods
if (staticCount == 0)
{
foreach (var boombox in allBoomboxes)
{
if (boombox != null && boombox.baseEntity != null &&
boombox.baseEntity.OwnerID == 0)
{
// Make sure it's not already handled as handheld or deployed
bool isHandheld = false;
bool isDeployed = false;
var handhelds = UnityEngine.Object.FindObjectsOfType<HeldBoomBox>();
foreach (var handheld in handhelds)
{
if (handheld != null && handheld.BoxController == boombox)
{
isHandheld = true;
break;
}
}
if (!isHandheld)
{
var deployables = UnityEngine.Object.FindObjectsOfType<DeployableBoomBox>();
foreach (var deployable in deployables)
{
if (deployable != null && deployable.BoxController == boombox)
{
isDeployed = true;
break;
}
}
}
if (!isHandheld && !isDeployed)
{
if (config.DebugMode)
Puts($"DEBUG: Found static boombox by elimination: {boombox}");
SetBoomboxRadioIP(boombox);
staticCount++;
}
}
}
}
} }
Puts($"Turned on {deployedCount} deployed, {handheldCount} handheld, and {staticCount} static boomboxes"); Puts($"Turned on {deployedCount} deployed, {handheldCount} handheld, and {staticCount} static boomboxes");
@ -350,6 +474,15 @@ namespace Oxide.Plugins
var deployables = UnityEngine.Object.FindObjectsOfType<DeployableBoomBox>(); var deployables = UnityEngine.Object.FindObjectsOfType<DeployableBoomBox>();
foreach (var deployable in deployables) foreach (var deployable in deployables)
{ {
// Skip if this is a static boombox incorrectly identified as deployed
if (deployable != null && deployable.BoxController != null &&
deployable.BoxController.baseEntity != null &&
deployable.BoxController.baseEntity.name != null &&
deployable.BoxController.baseEntity.name.ToLower().Contains("static"))
{
continue;
}
if (deployable != null && deployable.BoxController != null) if (deployable != null && deployable.BoxController != null)
{ {
deployable.BoxController.SetFlag(BaseEntity.Flags.On, false); deployable.BoxController.SetFlag(BaseEntity.Flags.On, false);
@ -380,7 +513,9 @@ namespace Oxide.Plugins
var allBoomboxes = UnityEngine.Object.FindObjectsOfType<BoomBox>(); var allBoomboxes = UnityEngine.Object.FindObjectsOfType<BoomBox>();
foreach (var boombox in allBoomboxes) foreach (var boombox in allBoomboxes)
{ {
if (boombox != null && IsStaticBoombox(boombox)) if (boombox != null && boombox.baseEntity != null &&
boombox.baseEntity.name != null &&
boombox.baseEntity.name.ToLower().Contains("static"))
{ {
boombox.SetFlag(BaseEntity.Flags.On, false); boombox.SetFlag(BaseEntity.Flags.On, false);
boombox.baseEntity.SendNetworkUpdate(); boombox.baseEntity.SendNetworkUpdate();