using System.Collections.Generic; using System.Linq; using Rust; using UnityEngine; namespace Oxide.Plugins { [Info("CratesEvent", "FuZZED", "0.0.1")] [Description("Randomly selects a monument and spawn a set amout of locked crates there")] public class CratesEvent : RustPlugin { int randomNumber; private List _crates = new List(); private readonly Dictionary _launchsitePositions = new Dictionary { { "1", new SpawnData(new Vector3(123f, 4f, 3f), new Vector3(0f, 90f, 0f)) }, { "2", new SpawnData(new Vector3(123f, 4f, -3f), new Vector3(0f, 90f, 0f)) }, { "3", new SpawnData(new Vector3(129, 4f, 3f), new Vector3(0f, 270f, 0f)) }, { "4", new SpawnData(new Vector3(129f, 4f, -3f), new Vector3(0f, 270f, 0f)) } }; private readonly Dictionary _domePositions = new Dictionary { { "1", new SpawnData(new Vector3(-24.5f, 80f, -9f), new Vector3(0f, 20f, 0f)) }, { "2", new SpawnData(new Vector3(-15.5f, 80f, 14f), new Vector3(0f, 220f, 0f)) }, { "3", new SpawnData(new Vector3(-23.5f, 60f, -6f), new Vector3(0f, 80f, 0f)) }, { "4", new SpawnData(new Vector3(-13f, 60f, -14f), new Vector3(0f, 20f, 0f)) } }; private readonly Dictionary _trainYardPositions = new Dictionary { { "1", new SpawnData(new Vector3(0f, 0f, 15f), new Vector3(0f, 0f, 0f)) }, { "2", new SpawnData(new Vector3(-4f, 0f, 15f), new Vector3(0f, 0f, 0f)) }, { "3", new SpawnData(new Vector3(0f, 0f, 20f), new Vector3(0f, 180f, 0f)) }, { "4", new SpawnData(new Vector3(-4f, 0f, 20f), new Vector3(0f, 180f, 0f)) } }; private readonly Dictionary _waterTreatmentPositions = new Dictionary { { "1", new SpawnData(new Vector3(18.5f, 0f, -57f), new Vector3(0f, 0f, 0f)) }, { "2", new SpawnData(new Vector3(14f, 0f, -57f), new Vector3(0f, 0f, 0f)) }, { "3", new SpawnData(new Vector3(18.5f, 0f, -47f), new Vector3(0f, 180f, 0f)) }, { "4", new SpawnData(new Vector3(14f, 0f, -47f), new Vector3(0f, 180f, 0f)) } }; void OnServerInitialized(bool initial) { permission.RegisterPermission("cratesevent.admin", this); } void Unload() { foreach (var crate in _crates) { crate.Kill(); } } [ChatCommand("startCratesEvent")] void startEventCmd(BasePlayer player) { if (!permission.UserHasPermission(player.UserIDString, "cratesevent.admin")) { player.ChatMessage("Creates Event:\nYou Do Not Have Permission To Use This Command!"); return; } StartEvent(); } [ConsoleCommand("startCratesEvent")] void StartEventConsole(ConsoleSystem.Arg arg) { var player = arg.Connection?.player as BasePlayer; if (player != null) { if (!permission.UserHasPermission(player.UserIDString, "cratesevent.admin")) { player.SendConsoleCommand("echo Creates Event: You Do Not Have Permission To Use This Command!"); return; } StartEvent(); return; } StartEvent(); } void StartEvent() { foreach (var crate in _crates) { crate.Kill(); } SpawnData spawnData; randomNumber = Random.Range(1, 5); foreach (var monument in TerrainMeta.Path.Monuments?.Where(x => x.shouldDisplayOnMap).ToList() ?? new List()) { if (randomNumber == 1) { if (monument.displayPhrase?.token == "launchsite") { if (_launchsitePositions.TryGetValue("1", out spawnData)) { SpawnCrate(monument.transform, spawnData.Position, spawnData.Rotation); } if (_launchsitePositions.TryGetValue("2", out spawnData)) { SpawnCrate(monument.transform, spawnData.Position, spawnData.Rotation); } if (_launchsitePositions.TryGetValue("3", out spawnData)) { SpawnCrate(monument.transform, spawnData.Position, spawnData.Rotation); } if (_launchsitePositions.TryGetValue("4", out spawnData)) { SpawnCrate(monument.transform, spawnData.Position, spawnData.Rotation); } MessageAllPlayers("Crate Event Started at Launch Site!\n4 Locked Crates Have Been Spawn At Launch Site"); } } if (randomNumber == 2) { if (monument.displayPhrase?.token == "dome_monument_name") { if (_domePositions.TryGetValue("1", out spawnData)) { SpawnCrate(monument.transform, spawnData.Position, spawnData.Rotation); } if (_domePositions.TryGetValue("2", out spawnData)) { SpawnCrate(monument.transform, spawnData.Position, spawnData.Rotation); } if (_domePositions.TryGetValue("3", out spawnData)) { SpawnCrate(monument.transform, spawnData.Position, spawnData.Rotation); } if (_domePositions.TryGetValue("4", out spawnData)) { SpawnCrate(monument.transform, spawnData.Position, spawnData.Rotation); } MessageAllPlayers("Crate Event Started at Dome!\n4 Locked Crates Have Been Spawn At Dome"); } } if (randomNumber == 3) { if (monument.displayPhrase?.token == "train_yard_display_name") { if (_trainYardPositions.TryGetValue("1", out spawnData)) { SpawnCrate(monument.transform, spawnData.Position, spawnData.Rotation); } if (_trainYardPositions.TryGetValue("2", out spawnData)) { SpawnCrate(monument.transform, spawnData.Position, spawnData.Rotation); } if (_trainYardPositions.TryGetValue("3", out spawnData)) { SpawnCrate(monument.transform, spawnData.Position, spawnData.Rotation); } if (_trainYardPositions.TryGetValue("4", out spawnData)) { SpawnCrate(monument.transform, spawnData.Position, spawnData.Rotation); } MessageAllPlayers("Crate Event Started at Train Yard!\n4 Locked Crates Have Been Spawn At Train Yard"); } } if (randomNumber == 4) { if (monument.displayPhrase?.token == "water_treatment_plant_display_name") { if (_waterTreatmentPositions.TryGetValue("1", out spawnData)) { SpawnCrate(monument.transform, spawnData.Position, spawnData.Rotation); } if (_waterTreatmentPositions.TryGetValue("2", out spawnData)) { SpawnCrate(monument.transform, spawnData.Position, spawnData.Rotation); } if (_waterTreatmentPositions.TryGetValue("3", out spawnData)) { SpawnCrate(monument.transform, spawnData.Position, spawnData.Rotation); } if (_waterTreatmentPositions.TryGetValue("4", out spawnData)) { SpawnCrate(monument.transform, spawnData.Position, spawnData.Rotation); } MessageAllPlayers("Crate Event Started at Water Treatment!\n4 Locked Crates Have Been Spawn At Water Treatment"); } } } } private void SpawnCrate(Transform objTransform, Vector3 spawnOffset, Vector3 rotationOffset) { var mtx = objTransform.localToWorldMatrix; var finalPos = mtx.MultiplyPoint3x4(spawnOffset); var oilRot = mtx.rotation * Quaternion.Euler(rotationOffset); if (!GamePhysics.CheckSphere(finalPos, .1f, Layers.Server.Deployed, QueryTriggerInteraction.Ignore)) { var entity = GameManager.server.CreateEntity("assets/prefabs/deployable/chinooklockedcrate/codelockedhackablecrate.prefab", finalPos, oilRot); if (entity != null) { entity.EnableSaving(false); entity.Spawn(); _crates.Add(entity); } } } private void MessageAllPlayers(string message) { foreach (BasePlayer player in BasePlayer.activePlayerList) { if (player == null) return; player.ChatMessage($"Creates Event:\n{message}"); } } private class SpawnData { public SpawnData(Vector3 position, Vector3 rotation) { Position = position; Rotation = rotation; } public Vector3 Position { get; set; } public Vector3 Rotation { get; set; } } } }