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
{
[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")]
public class AutoRadioManager : RustPlugin
{
@ -40,6 +40,9 @@ namespace Oxide.Plugins
[JsonProperty("Turn on radios after server restart")]
public bool TurnOnAfterServerRestart = true;
[JsonProperty("Debug mode")]
public bool DebugMode = false;
}
protected override void LoadDefaultConfig() => config = new Configuration();
@ -110,6 +113,9 @@ namespace Oxide.Plugins
if (!config.EnableForDeployed || boombox == null)
return;
if (config.DebugMode)
Puts($"DEBUG: Deployable boombox spawned: {boombox}");
// Use a slight delay to ensure the entity is fully initialized
timer.Once(0.5f, () => {
if (boombox != null && boombox.BoxController != null)
@ -122,6 +128,9 @@ namespace Oxide.Plugins
if (!config.EnableForHandheld || boombox == null)
return;
if (config.DebugMode)
Puts($"DEBUG: Handheld boombox spawned: {boombox}");
// Use a slight delay to ensure the entity is fully initialized
timer.Once(0.5f, () => {
if (boombox != null && boombox.BoxController != null)
@ -131,13 +140,23 @@ namespace Oxide.Plugins
private void OnEntitySpawned(BoomBox boombox)
{
if (config.DebugMode)
Puts($"DEBUG: BoomBox entity spawned: {boombox}, Type: {boombox?.GetType().Name}");
if (!config.EnableForStatic || boombox == null)
return;
// Use a slight delay to ensure the entity is fully initialized
timer.Once(0.5f, () => {
if (boombox != null && IsStaticBoombox(boombox))
if (boombox != null)
{
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");
}
}
// Fixed method to identify static boomboxes based on name
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
var deployables = UnityEngine.Object.FindObjectsOfType<DeployableBoomBox>();
var handhelds = UnityEngine.Object.FindObjectsOfType<HeldBoomBox>();
Puts($"DEBUG: Checking if {boombox} is static. Type: {boombox.GetType().Name}");
// Check if this boombox is a controller for any of them
foreach (var deployable in deployables)
// Debug the hierarchy
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)
return false; // It's a deployed boombox
Puts($"DEBUG: Entity name: {boombox.baseEntity.name}");
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)
{
if (handheld.BoxController == boombox)
return false; // It's a handheld boombox
if (handheld != null && handheld.BoxController == boombox)
return false; // It's already identified as handheld
}
// Check if it has an owner
if (boombox.baseEntity != null && boombox.baseEntity.OwnerID != 0)
return false; // It has an owner, so likely not a static/monument boombox
if (config.DebugMode)
Puts($"DEBUG: Identified as static by having no owner");
return true; // If it's not part of a deployable or handheld and has no owner, assume it's static
}
catch (Exception ex)
{
PrintError($"Error in IsStaticBoombox: {ex.Message}");
return false; // Default to not treating it as static in case of error
return true;
}
if (config.DebugMode)
Puts($"DEBUG: Not identified as static");
return false;
}
private void SetBoomboxRadioIP(BoomBox box)
@ -239,6 +285,9 @@ namespace Oxide.Plugins
try
{
if (config.DebugMode)
Puts($"DEBUG: Setting radio IP for {box}");
// Set the radio IP to the configured stream URL
box.CurrentRadioIp = config.RadioStreamUrl;
@ -248,6 +297,9 @@ namespace Oxide.Plugins
// Turn on the radio
box.SetFlag(BaseEntity.Flags.On, true);
box.baseEntity.SendNetworkUpdate();
if (config.DebugMode)
Puts($"DEBUG: Radio configured successfully for {box}");
}
catch (Exception ex)
{
@ -286,10 +338,23 @@ namespace Oxide.Plugins
if (config.EnableForDeployed)
{
var deployables = UnityEngine.Object.FindObjectsOfType<DeployableBoomBox>();
if (config.DebugMode)
Puts($"DEBUG: Found {deployables.Length} deployable boomboxes");
foreach (var deployable in deployables)
{
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);
deployedCount++;
}
@ -300,6 +365,9 @@ namespace Oxide.Plugins
if (config.EnableForHandheld)
{
var handhelds = UnityEngine.Object.FindObjectsOfType<HeldBoomBox>();
if (config.DebugMode)
Puts($"DEBUG: Found {handhelds.Length} handheld boomboxes");
foreach (var handheld in handhelds)
{
if (handheld != null && handheld.BoxController != null)
@ -314,14 +382,70 @@ namespace Oxide.Plugins
if (config.EnableForStatic)
{
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)
{
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);
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");
@ -350,6 +474,15 @@ namespace Oxide.Plugins
var deployables = UnityEngine.Object.FindObjectsOfType<DeployableBoomBox>();
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)
{
deployable.BoxController.SetFlag(BaseEntity.Flags.On, false);
@ -380,7 +513,9 @@ namespace Oxide.Plugins
var allBoomboxes = UnityEngine.Object.FindObjectsOfType<BoomBox>();
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.baseEntity.SendNetworkUpdate();