using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using Newtonsoft.Json; using Oxide.Core; using Oxide.Core.Plugins; using Oxide.Game.Rust.Cui; using Rust; using UnityEngine; namespace Oxide.Plugins { [Info("OneTimeSpawn", "xkrystalll", "1.0.0")] class OneTimeSpawn : RustPlugin { #region Fields private List _alreadySpawned; private BasePlayer.SpawnPoint cachedSpawnpoint = null; #endregion #region Hooks private void OnServerInitialized() { LoadData(); if (cfg.Position == Vector3.zero) { PrintError("Spawn position is (0, 0, 0). Please install position via command /spawnpos when you have ownerid"); PrintError("Spawn position is (0, 0, 0). Please install position via command /spawnpos when you have ownerid"); PrintError("Spawn position is (0, 0, 0). Please install position via command /spawnpos when you have ownerid"); PrintError("Spawn position is (0, 0, 0). Please install position via command /spawnpos when you have ownerid"); PrintError("Spawn position is (0, 0, 0). Please install position via command /spawnpos when you have ownerid"); return; } cachedSpawnpoint = new() { pos = cfg.Position, rot = default }; } private void OnNewSave() { if (cfg.WipeData) { PrintWarning("Players data was successfully wiped with server wipe!"); _alreadySpawned.Clear(); SaveData(); } if (cfg.DeleteSpawnPositionOnMapWipe) { PrintWarning("Spawnpoint was wiped with server wipe! Please, install new position with command /spawnpos and reload plugin"); cfg.Position = Vector3.zero; SaveConfig(); cachedSpawnpoint = null; } } private void Unload() => SaveData(); private void OnServerSave() => SaveData(); private object OnPlayerRespawn(BasePlayer player, BasePlayer.SpawnPoint spawnPoint) { if (cachedSpawnpoint == null) return null; if (!_alreadySpawned.Contains(player.userID)) { _alreadySpawned.Add(player.userID); return cachedSpawnpoint; } return null; } #endregion #region Commands [ChatCommand("spawnpos")] private void cmdSpawnPos(BasePlayer player) { if (player == null) return; if (!player.IsAdmin) return; cfg.Position = player.GetNetworkPosition(); cachedSpawnpoint = new() { pos = cfg.Position, rot = default }; SaveConfig(); player.ChatMessage("Position was installed successfully"); } #endregion #region Config private ConfigData cfg; public class ConfigData { [JsonProperty("Spawn position")] public Vector3 Position; [JsonProperty("Wipe already spawned players on map wipe?")] public bool WipeData; [JsonProperty("Delete spawn position on map wipe?")] public bool DeleteSpawnPositionOnMapWipe; } protected override void LoadDefaultConfig() { var config = new ConfigData() { Position = Vector3.zero, WipeData = false, DeleteSpawnPositionOnMapWipe = false }; SaveConfig(config); } protected override void LoadConfig() { base.LoadConfig(); cfg = Config.ReadObject(); SaveConfig(cfg); } private void SaveConfig(object config) { Config.WriteObject(config, true); } #endregion #region Data private void SaveData() { Interface.Oxide.DataFileSystem.WriteObject($"{Title}/alreadySpawned", _alreadySpawned); } void LoadData() { _alreadySpawned = Interface.Oxide?.DataFileSystem?.ReadObject>($"{Title}/alreadySpawned") ?? new(); } #endregion } }