using System; using System.Collections.Generic; using System.Linq; using Oxide.Core.Plugins; using UnityEngine; namespace Oxide.Plugins { [Info("Siren", "GNGGOD", "1.1.5")] [Description("A plugin to play a siren sound in Rust")] public class Siren : RustPlugin { private ConfigData configData; private readonly Dictionary AlarmSound = new Dictionary(); // Permission name private const string PermissionUse = "siren.use"; // Configuration structure private class ConfigData { public string Command { get; set; } public float AlarmSoundTime { get; set; } public string SirenSoundPath { get; set; } public string NotificationMessage { get; set; } public bool EnableNotification { get; set; } public bool Debug { get; set; } public string Version { get; set; } } // Load configuration or create a default one protected override void LoadDefaultConfig() { PrintWarning("Creating a new configuration file."); configData = GetDefaultConfig(); SaveConfig(); } // Save the configuration data protected override void SaveConfig() { Config.WriteObject(configData, true); } // Load the configuration data private void LoadConfig() { try { configData = Config.ReadObject(); if (configData.Version != Version.ToString()) { UpdateConfig(); } } catch { PrintError("Error loading configuration, creating a new one."); configData = GetDefaultConfig(); SaveConfig(); } } private ConfigData GetDefaultConfig() { return new ConfigData { Command = "siren", AlarmSoundTime = 15.0f, SirenSoundPath = "assets/prefabs/io/electric/other/alarmsound.prefab", NotificationMessage = "The siren has been triggered!", EnableNotification = true, Debug = false, Version = Version.ToString() }; } private void UpdateConfig() { PrintWarning("Updating configuration file to the latest version."); var defaultConfig = GetDefaultConfig(); if (configData.Command == null) configData.Command = defaultConfig.Command; if (configData.AlarmSoundTime == 0) configData.AlarmSoundTime = defaultConfig.AlarmSoundTime; if (configData.SirenSoundPath == null) configData.SirenSoundPath = defaultConfig.SirenSoundPath; if (configData.NotificationMessage == null) configData.NotificationMessage = defaultConfig.NotificationMessage; if (configData.Version == null) configData.Version = defaultConfig.Version; configData.Version = Version.ToString(); SaveConfig(); } // Called when the plugin is loaded private void Init() { LoadConfig(); permission.RegisterPermission(PermissionUse, this); UnregisterCommands(); RegisterCommands(); Puts($"Siren plugin loaded! Use the command '{configData.Command}' in the console or chat to play the siren sound."); } private void RegisterCommands() { cmd.AddConsoleCommand(configData.Command, this, nameof(PlaySirenConsoleCommand)); cmd.AddChatCommand(configData.Command, this, nameof(PlaySirenChatCommand)); cmd.AddConsoleCommand("siren.setduration", this, nameof(SetSirenDurationCommand)); cmd.AddConsoleCommand("siren.setsound", this, nameof(SetSirenSoundCommand)); cmd.AddConsoleCommand("siren.setnotification", this, nameof(SetSirenNotificationCommand)); cmd.AddConsoleCommand("siren.enablenotification", this, nameof(EnableNotificationCommand)); } private void UnregisterCommands() { cmd.RemoveConsoleCommand(configData.Command, this); cmd.RemoveChatCommand(configData.Command, this); cmd.RemoveConsoleCommand("siren.setduration", this); cmd.RemoveConsoleCommand("siren.setsound", this); cmd.RemoveConsoleCommand("siren.setnotification", this); cmd.RemoveConsoleCommand("siren.enablenotification", this); } // Console command to play the siren sound [ConsoleCommand("siren")] private void PlaySirenConsoleCommand(ConsoleSystem.Arg arg) { if (arg.Connection != null && !HasPermission(arg.Connection.player as BasePlayer)) { SendReply(arg, "You don't have permission to use this command."); return; } StartSiren(); DebugLog("Siren sound command executed for all players."); SendReply(arg, "Siren sound command executed!"); } // Chat command to play the siren sound private void PlaySirenChatCommand(BasePlayer player, string command, string[] args) { if (!HasPermission(player)) { SendReply(player, "You don't have permission to use this command."); return; } StartSiren(); DebugLog($"Siren sound command executed by {player.displayName} for all players."); SendReply(player, "Siren sound command executed!"); } // Console command to set the siren duration [ConsoleCommand("siren.setduration")] private void SetSirenDurationCommand(ConsoleSystem.Arg arg) { if (arg.Args == null || arg.Args.Length < 1) { SendReply(arg, "Usage: siren.setduration "); return; } if (!float.TryParse(arg.Args[0], out float duration)) { SendReply(arg, "Invalid duration. Please enter a valid number."); return; } configData.AlarmSoundTime = duration; SaveConfig(); SendReply(arg, $"Siren duration set to {duration} seconds."); } // Console command to set the siren sound path [ConsoleCommand("siren.setsound")] private void SetSirenSoundCommand(ConsoleSystem.Arg arg) { if (arg.Args == null || arg.Args.Length < 1) { SendReply(arg, "Usage: siren.setsound "); return; } configData.SirenSoundPath = arg.Args[0]; SaveConfig(); SendReply(arg, $"Siren sound path set to: {configData.SirenSoundPath}"); } // Console command to set the siren notification message [ConsoleCommand("siren.setnotification")] private void SetSirenNotificationCommand(ConsoleSystem.Arg arg) { if (arg.Args == null || arg.Args.Length < 1) { SendReply(arg, "Usage: siren.setnotification "); return; } configData.NotificationMessage = string.Join(" ", arg.Args); SaveConfig(); SendReply(arg, $"Siren notification message set to: {configData.NotificationMessage}"); } // Console command to enable/disable the siren notification [ConsoleCommand("siren.enablenotification")] private void EnableNotificationCommand(ConsoleSystem.Arg arg) { if (arg.Args == null || arg.Args.Length < 1) { SendReply(arg, "Usage: siren.enablenotification "); return; } if (!bool.TryParse(arg.Args[0], out bool enable)) { SendReply(arg, "Invalid input. Please enter 'true' or 'false'."); return; } configData.EnableNotification = enable; SaveConfig(); SendReply(arg, $"Siren notification enabled: {enable}"); } private bool HasPermission(BasePlayer player) { return player != null && permission.UserHasPermission(player.UserIDString, PermissionUse); } private void StartSiren() { foreach (var target in BasePlayer.activePlayerList) { AddSoundEntity(target); if (configData.EnableNotification) { SendReply(target, configData.NotificationMessage); } } timer.Once(configData.AlarmSoundTime, () => { DebugLog("Stopping the siren sound for all players."); foreach (var target in AlarmSound.ToList()) { if (target.Value != null) target.Value.Kill(); AlarmSound.Remove(target.Key); } }); } private void AddSoundEntity(BasePlayer player) { foreach (var target in AlarmSound.ToList()) { float distance = Vector3.Distance(player.transform.position, target.Key.transform.position); if (distance < 20) { DebugLog($"Skipping creation of alarm entity for player {player.displayName} as one is already nearby."); return; } } IOEntity alarm = GameManager.server.CreateEntity(configData.SirenSoundPath, player.transform.position + new Vector3(0f, 2f, 0f)) as IOEntity; if (alarm == null) { DebugLog($"Failed to create alarm entity for player {player.displayName}."); return; } alarm.enableSaving = false; alarm.Spawn(); AlarmSound.Add(player, alarm); alarm.gameObject.SetActive(true); alarm.SetFlag(BaseEntity.Flags.On, true); alarm.UpdateFromInput(400, 0); alarm.SendNetworkUpdateImmediate(); DebugLog($"Siren sound started for player {player.displayName} at {player.transform.position + new Vector3(0f, 2f, 0f)}."); } private void DebugLog(string message) { if (configData.Debug) { Puts(message); } } void Unload() { DebugLog("Unloading Siren plugin and stopping all siren sounds."); foreach (var target in AlarmSound.ToList()) { if (target.Value != null) target.Value.Kill(); AlarmSound.Remove(target.Key); } } } }