C#:
using System;
using UnityEngine;
using Newtonsoft.Json;
namespace Oxide.Plugins
{
[Info("AdminChinook", "The_Kiiiing", "1.0.8")]
[Description("Allows Admins to fly the Chinook")]
public class AdminChinook : RustPlugin
{
#region Configuration
private static Configuration _config;
private class Configuration
{
[JsonProperty(PropertyName = "Time until heli despawn after last player dismounts (seconds)", ObjectCreationHandling = ObjectCreationHandling.Replace)]
public float DESPAWN_TIME = 10f;
[JsonProperty(PropertyName = "Time until heli despawn after spawn if no player mounts (seconds)", ObjectCreationHandling = ObjectCreationHandling.Replace)]
public float INITIAL_DESPAWN_TIME = 20f;
[JsonProperty(PropertyName = "Allow anyone to mount the heli", ObjectCreationHandling = ObjectCreationHandling.Replace)]
public bool ANYONE_MOUNT = false;
}
protected override void LoadConfig()
{
base.LoadConfig();
try
{
_config = Config.ReadObject<Configuration>();
if (_config == null) throw new Exception();
SaveConfig();
}
catch
{
PrintError("Your configuration file contains an error. Using default configuration values.");
LoadDefaultConfig();
}
}
private void Unload()
{
_config = null;
}
protected override void SaveConfig() => Config.WriteObject(_config);
protected override void LoadDefaultConfig() => _config = new Configuration();
#endregion
#region Fields
private static string CHINOOK_PREFAB = "assets/prefabs/npc/ch47/ch47.entity.prefab";
private static string PERM_USE = "adminchinook.use";
private const float SPAWN_DISTANCE = 5f; //Distance between player and Chinook at spawn
#endregion
#region Functions
private void invokeDespawnTimer(CH47Helicopter ch, BasePlayer player)
{
ch.Invoke(() =>
{
if (!ch.IsDestroyed && !ch.AnyMounted()) ch.AdminKill();
}, _config.DESPAWN_TIME);
}
#endregion
#region Commands
[ChatCommand("ch47")]
void spawn(BasePlayer player)
{
if (!permission.UserHasPermission(player.UserIDString, PERM_USE))
{
player.ChatMessage("You don't have permission to do this");
return;
}
Quaternion rot = Quaternion.Euler(0, player.GetNetworkRotation().eulerAngles.y + 270, 0);
Vector3 pos = player.transform.position;
Vector3 spawn_pos = pos + (rot * (player.transform.right * SPAWN_DISTANCE + player.transform.up));
var heli = (CH47Helicopter)GameManager.server.CreateEntity(CHINOOK_PREFAB, pos, rot, true);
heli.Spawn();
heli.Invoke(() =>
{
if (!heli.IsDestroyed && !heli.AnyMounted()) heli.AdminKill();
}, _config.INITIAL_DESPAWN_TIME);
player.ChatMessage("Chinook has been spawned");
}
#endregion
#region Oxide Hooks
void Init()
{
permission.RegisterPermission(PERM_USE, this);
}
void OnEntityDismounted(BaseMountable entity, BasePlayer player)
{
var parent_entity = entity.GetParentEntity();
if (parent_entity is CH47Helicopter)
{
invokeDespawnTimer(parent_entity as CH47Helicopter, player);
}
}
object CanMountEntity(BasePlayer player, BaseMountable entity)
{
if (entity.GetParentEntity() is CH47Helicopter && !_config.ANYONE_MOUNT)
{
if (!permission.UserHasPermission(player.UserIDString, PERM_USE))
{
player.ChatMessage("You don't have permission to do this");
return false;
}
}
return null;
}
#endregion
}
}