using System.Collections.Generic; using System.IO; using Newtonsoft.Json; using Oxide.Core; using Oxide.Core.Libraries.Covalence; using UnityEngine; namespace Oxide.Plugins { [Info("Town", "M&B-Studios", "1.0.1")] [Description("Allows Admins to set a teleport point and teleport to it.")] public class Town : RustPlugin { private const string SetSpawnPermission = "town.setspawn"; private const string TeleportPermission = "town.teleport"; private const string DeleteSpawnPermission = "town.deletespawn"; private Dictionary teleportPoints = new Dictionary(); private string spawnDataFile = "spawnpoints.json"; private void Init() { permission.RegisterPermission(SetSpawnPermission, this); permission.RegisterPermission(TeleportPermission, this); permission.RegisterPermission(DeleteSpawnPermission, this); } private void LoadDefaultMessages() { lang.RegisterMessages(new Dictionary { ["TeleportSuccess"] = "You have been teleported to the Town teleport point.", ["SetSpawnSuccess"] = "Town teleport point has been set at your current location.", ["NoPermission"] = "You don't have permission to use this command.", ["NoSpawnSet"] = "The Town teleport point has not been set yet.", ["SpawnDeleted"] = "The Town teleport point has been deleted." }, this); } private void SaveData() { Interface.Oxide.DataFileSystem.WriteObject(this.Title, teleportPoints); // Save spawn point data to file string json = JsonConvert.SerializeObject(teleportPoints); File.WriteAllText(spawnDataFile, json); } private void LoadData() { teleportPoints = Interface.Oxide.DataFileSystem.ReadObject>(this.Title); if (teleportPoints == null) { teleportPoints = new Dictionary(); } // Load spawn point data from file spawnDataFile = $"{Interface.Oxide.DataDirectory}/{spawnDataFile}"; if (File.Exists(spawnDataFile)) { string json = File.ReadAllText(spawnDataFile); Dictionary spawnPoints = JsonConvert.DeserializeObject>(json); if (spawnPoints != null) { foreach (var pair in spawnPoints) { teleportPoints[pair.Key] = pair.Value; } } } } private void TeleportPlayer(BasePlayer player, Vector3 destination) { player.Teleport(destination); SendChatMessage(player, "TeleportSuccess"); } private bool CanSetSpawn(BasePlayer player) { return permission.UserHasPermission(player.UserIDString, SetSpawnPermission); } private bool CanTeleport(BasePlayer player) { return permission.UserHasPermission(player.UserIDString, TeleportPermission) || permission.UserHasPermission(player.UserIDString, DeleteSpawnPermission); } [ChatCommand("town")] private void TeleportToSpawnCommand(BasePlayer player, string command, string[] args) { if (!CanTeleport(player)) { SendChatMessage(player, "NoPermission"); return; } Vector3 destination; if (teleportPoints.TryGetValue(player.UserIDString, out destination)) { TeleportPlayer(player, destination); } else { SendChatMessage(player, "NoSpawnSet"); } } [ChatCommand("setspawn")] private void SetSpawnCommand(BasePlayer player, string command, string[] args) { if (!CanSetSpawn(player)) { SendChatMessage(player, "NoPermission"); return; } Vector3 currentLocation = player.transform.position; teleportPoints[player.UserIDString] = currentLocation; SaveData(); SendChatMessage(player, "SetSpawnSuccess"); } [ChatCommand("delspawn")] private void DeleteSpawnCommand(BasePlayer player, string command, string[] args) { if (!permission.UserHasPermission(player.UserIDString, DeleteSpawnPermission)) { SendChatMessage(player, "NoPermission"); return; } if (teleportPoints.Remove(player.UserIDString)) { SaveData(); SendChatMessage(player, "SpawnDeleted"); } else { SendChatMessage(player, "NoSpawnSet"); } } private void SendChatMessage(BasePlayer player, string key, params object[] args) { if (player == null) return; string message = lang.GetMessage(key, this, player.UserIDString); if (args != null && args.Length > 0) message = string.Format(message, args); player.ChatMessage(message); } } }