using System.Collections.Generic; using Newtonsoft.Json.Linq; using Oxide.Core; using Oxide.Core.Libraries.Covalence; using UnityEngine; namespace Oxide.Plugins { [Info("SpawnVehicles", "WuttyJ", "1.0.0")] [Description("Allows players to spawn vehicles using scrap")] class SpawnVehicles : RustPlugin { private static SpawnVehicles Instance; private ConfigData config; private class ConfigData { public Dictionary Vehicles { get; set; } } public class VehicleInfo { public int ScrapCost { get; set; } public string Prefab { get; set; } public bool UsePermission { get; set; } public string Permission { get; set; } public bool CheckForTCRange { get; set; } } protected override void LoadDefaultConfig() { Config.WriteObject(GetDefaultConfig(), true); } private ConfigData GetDefaultConfig() { return new ConfigData { Vehicles = new Dictionary { ["mini"] = new VehicleInfo { ScrapCost = 750, Prefab = "assets/content/vehicles/minicopter/minicopter.entity.prefab", UsePermission = true, Permission = "spawnvehicles.spawnmini", CheckForTCRange = true }, ["scrappy"] = new VehicleInfo { ScrapCost = 1250, Prefab = "assets/content/vehicles/scrap heli carrier/scraptransporthelicopter.prefab", UsePermission = true, Permission = "spawnvehicles.spawnscrappy", CheckForTCRange = true }, ["boat"] = new VehicleInfo { ScrapCost = 250, Prefab = "assets/content/vehicles/boats/rowboat/rowboat.prefab", UsePermission = true, Permission = "spawnvehicles.boat", CheckForTCRange = false } // Add your own vehicles under here. } }; } private void LoadConfigData() { config = Config.ReadObject(); if (config.Vehicles == null) { PrintWarning("Config: Vehicles not found, loading default config."); config.Vehicles = GetDefaultConfig().Vehicles; SaveConfig(); } } private void SaveConfig() => Config.WriteObject(config, true); [ChatCommand("spawn")] private void cmdSpawn(BasePlayer player, string command, string[] args) { if (args.Length == 0) { player.ChatMessage("You must specify a vehicle to spawn!"); return; } string vehicleName = args[0].ToLower(); if (!config.Vehicles.ContainsKey(vehicleName)) { player.ChatMessage($"The vehicle {vehicleName} does not exist!"); return; } HandleVehicleSpawn(player, vehicleName); } private void HandleVehicleSpawn(BasePlayer player, string vehicleName) { VehicleInfo vehicleInfo = config.Vehicles[vehicleName]; if (vehicleInfo.UsePermission && !permission.UserHasPermission(player.UserIDString, vehicleInfo.Permission)) { player.ChatMessage($"You do not have permission to spawn a {vehicleName}!"); return; } if (player.inventory.GetAmount(-932201673) < vehicleInfo.ScrapCost) { player.ChatMessage($"You dont have enough scrap to spawn a {vehicleName}! You currently have {player.inventory.GetAmount(-932201673)} scrap, and you are missing {vehicleInfo.ScrapCost - player.inventory.GetAmount(-932201673)} scrap."); return; } if (vehicleInfo.CheckForTCRange && !IsPlayerInTCRange(player)) { player.ChatMessage($"You must be in range of a tool cupboard to spawn a {vehicleName}!"); return; } else { player.inventory.Take(null, -932201673, vehicleInfo.ScrapCost); SpawnVehicle(player, vehicleInfo.Prefab); player.ChatMessage($"You have spawned a {vehicleName}!"); } } private bool IsPlayerInTCRange(BasePlayer player) { List buildingPrivlidges = new List(); Vis.Entities(player.transform.position, 50f, buildingPrivlidges); foreach (BuildingPrivlidge buildingPrivlidge in buildingPrivlidges) { if (buildingPrivlidge.IsAuthed(player)) { return true; } } return false; } private void SpawnVehicle(BasePlayer player, string prefab) { Vector3 position = player.transform.position + (player.eyes.BodyForward() * 5f) + new Vector3(0f, 2f, 0f); BaseEntity vehicle = GameManager.server.CreateEntity(prefab, position, Quaternion.identity); vehicle.Spawn(); } private void Init() { Instance = this; LoadConfigData(); permission.RegisterPermission(config.Vehicles["mini"].Permission, this); permission.RegisterPermission(config.Vehicles["scrappy"].Permission, this); permission.RegisterPermission(config.Vehicles["boat"].Permission, this); } } }