C#:
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Oxide.Core;
using Oxide.Core.Plugins;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("Key Binds", "RIPJAWBONES", "2.0.0")]
[Description("Trigger chat/console commands with key binds.")]
public class KeyBinds : RustPlugin
{
#region Variables
private Configuration configuration;
private readonly Dictionary<string, bool> keybindsEnabled = new Dictionary<string, bool>();
private readonly Dictionary<string, float> lastUsedTime = new Dictionary<string, float>();
private readonly Dictionary<string, float> lastGlobalUsedTime = new Dictionary<string, float>();
private readonly Dictionary<ulong, Dictionary<string, bool>> messageSentToPlayers =
new Dictionary<ulong, Dictionary<string, bool>>();
private readonly HashSet<string> alreadyRegistered = new HashSet<string>();
private const string PermissionUse = "keybinds.use";
#endregion
#region Configuration
private class Configuration
{
[JsonProperty("Global Settings")]
public GlobalSettings GlobalSettings { get; set; }
[JsonProperty("Player Keybinds")]
public Dictionary<string, KeybindConfig> PlayerKeybinds { get; set; }
[JsonProperty("Minicopter")]
public Dictionary<string, KeybindConfig> Minicopter { get; set; }
[JsonProperty("Scrap Transport Helicopter")]
public Dictionary<string, KeybindConfig> ScrapTransportHelicopter { get; set; }
[JsonProperty("Attack Helicopter")]
public Dictionary<string, KeybindConfig> AttackHelicopter { get; set; }
[JsonProperty("CH47 Helicopter")]
public Dictionary<string, KeybindConfig> CH47Helicopter { get; set; }
}
private class GlobalSettings
{
[JsonProperty("Auto Enable Key Binds")]
public bool AutoEnableKeyBinds { get; set; }
[JsonProperty("Global Cooldown Enabled")]
public bool GlobalCooldownEnabled { get; set; }
[JsonProperty("Global Cooldown Time")]
public float GlobalCooldownTime { get; set; }
[JsonProperty("Global Cooldown Message Enabled")]
public bool GlobalCooldownMessageEnabled { get; set; }
[JsonProperty("Global Cooldown Message")]
public string GlobalCooldownMessage { get; set; }
[JsonProperty("Enable Player Key Bind Notifications")]
public bool EnablePlayerKeyBindNotifications { get; set; }
[JsonProperty("Message Settings")]
public MessageSettings MsgSettings { get; set; }
}
private class MessageSettings
{
[JsonProperty("Message Icon SteamID")]
public string MessageIconSteamID { get; set; }
[JsonProperty("Message Prefix")]
public string MessagePrefix { get; set; }
[JsonProperty("Message Prefix Color")]
public string MessagePrefixColor { get; set; }
}
private class KeybindConfig
{
[JsonProperty("Command")]
public string Command { get; set; }
[JsonProperty("Permission")]
public string Permission { get; set; }
[JsonProperty("Cooldown")]
public float Cooldown { get; set; }
[JsonProperty("Enabled Cooldown Message")]
public bool EnabledCooldownMessage { get; set; }
[JsonProperty("Cooldown Message")]
public string CooldownMessage { get; set; }
}
#endregion
#region Load/Save Config
protected override void LoadConfig()
{
base.LoadConfig();
try
{
configuration = Config.ReadObject<Configuration>();
if (configuration == null)
throw new Exception("Config is null after reading.");
}
catch
{
PrintError("Configuration file is invalid; creating a new one with default values.");
LoadDefaultConfig();
}
SaveConfig();
}
protected override void LoadDefaultConfig()
{
PrintWarning("Creating a new configuration file...");
configuration = new Configuration
{
GlobalSettings = new GlobalSettings
{
AutoEnableKeyBinds = true,
GlobalCooldownEnabled = true,
GlobalCooldownTime = 1f,
GlobalCooldownMessageEnabled = true,
GlobalCooldownMessage = "You must wait {seconds}s before using any keybind.",
EnablePlayerKeyBindNotifications = true,
MsgSettings = new MessageSettings
{
MessageIconSteamID = "76561197960839785",
MessagePrefix = "[Key Bind Manager]\n",
MessagePrefixColor = "#5af"
}
},
PlayerKeybinds = new Dictionary<string, KeybindConfig>
{
{
"FIRE_THIRD",
new KeybindConfig
{
Command = "/menu",
Permission = "keybinds.player",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
{
"SPRINT_AND_FIRE_THIRD",
new KeybindConfig
{
Command = "",
Permission = "keybinds.player",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
{
"USE_AND_FIRE_THIRD",
new KeybindConfig
{
Command = "/shop",
Permission = "keybinds.player",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
{
"USE_AND_RELOAD",
new KeybindConfig
{
Command = "/kit",
Permission = "keybinds.player",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
},
Minicopter = new Dictionary<string, KeybindConfig>
{
{
"DUCK",
new KeybindConfig
{
Command = "/takeoff",
Permission = "keybinds.minicopter",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
{
"FIRE_PRIMARY",
new KeybindConfig
{
Command = "",
Permission = "keybinds.minicopter",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
{
"FIRE_SECONDARY",
new KeybindConfig
{
Command = "",
Permission = "keybinds.minicopter",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
{
"FIRE_THIRD",
new KeybindConfig
{
Command = "",
Permission = "keybinds.minicopter",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
{
"RELOAD",
new KeybindConfig
{
Command = "/radio",
Permission = "keybinds.minicopter",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
{
"SPRINT",
new KeybindConfig
{
Command = "/hover",
Permission = "keybinds.minicopter",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
{
"USE",
new KeybindConfig
{
Command = "",
Permission = "keybinds.minicopter",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
{
"SPRINT_AND_FIRE_THIRD",
new KeybindConfig
{
Command = "",
Permission = "keybinds.minicopter",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
{
"USE_AND_FIRE_THIRD",
new KeybindConfig
{
Command = "",
Permission = "keybinds.minicopter",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
{
"USE_AND_RELOAD",
new KeybindConfig
{
Command = "",
Permission = "keybinds.minicopter",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
},
ScrapTransportHelicopter = new Dictionary<string, KeybindConfig>
{
{
"DUCK",
new KeybindConfig
{
Command = "/takeoff",
Permission = "keybinds.scraptransporthelicopter",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
{
"FIRE_PRIMARY",
new KeybindConfig
{
Command = "",
Permission = "keybinds.scraptransporthelicopter",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
{
"FIRE_SECONDARY",
new KeybindConfig
{
Command = "",
Permission = "keybinds.scraptransporthelicopter",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
{
"FIRE_THIRD",
new KeybindConfig
{
Command = "",
Permission = "keybinds.scraptransporthelicopter",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
{
"RELOAD",
new KeybindConfig
{
Command = "/radio",
Permission = "keybinds.scraptransporthelicopter",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
{
"SPRINT",
new KeybindConfig
{
Command = "/hover",
Permission = "keybinds.scraptransporthelicopter",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
{
"USE",
new KeybindConfig
{
Command = "",
Permission = "keybinds.scraptransporthelicopter",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
{
"SPRINT_AND_FIRE_THIRD",
new KeybindConfig
{
Command = "",
Permission = "keybinds.scraptransporthelicopter",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
{
"USE_AND_FIRE_THIRD",
new KeybindConfig
{
Command = "",
Permission = "keybinds.scraptransporthelicopter",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
{
"USE_AND_RELOAD",
new KeybindConfig
{
Command = "",
Permission = "keybinds.scraptransporthelicopter",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
},
AttackHelicopter = new Dictionary<string, KeybindConfig>
{
{
"DUCK",
new KeybindConfig
{
Command = "/takeoff",
Permission = "keybinds.attackhelicopter",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
{
"FIRE_PRIMARY",
new KeybindConfig
{
Command = "",
Permission = "keybinds.attackhelicopter",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
{
"FIRE_SECONDARY",
new KeybindConfig
{
Command = "",
Permission = "keybinds.attackhelicopter",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
{
"FIRE_THIRD",
new KeybindConfig
{
Command = "",
Permission = "keybinds.attackhelicopter",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
{
"RELOAD",
new KeybindConfig
{
Command = "/radio",
Permission = "keybinds.attackhelicopter",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
{
"SPRINT",
new KeybindConfig
{
Command = "/hover",
Permission = "keybinds.attackhelicopter",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
{
"USE",
new KeybindConfig
{
Command = "",
Permission = "keybinds.attackhelicopter",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
{
"SPRINT_AND_FIRE_THIRD",
new KeybindConfig
{
Command = "",
Permission = "keybinds.attackhelicopter",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
{
"USE_AND_FIRE_THIRD",
new KeybindConfig
{
Command = "",
Permission = "keybinds.attackhelicopter",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
{
"USE_AND_RELOAD",
new KeybindConfig
{
Command = "",
Permission = "keybinds.attackhelicopter",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
},
CH47Helicopter = new Dictionary<string, KeybindConfig>
{
{
"DUCK",
new KeybindConfig
{
Command = "/takeoff",
Permission = "keybinds.ch47",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
{
"FIRE_PRIMARY",
new KeybindConfig
{
Command = "",
Permission = "keybinds.ch47",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
{
"FIRE_SECONDARY",
new KeybindConfig
{
Command = "",
Permission = "keybinds.ch47",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
{
"FIRE_THIRD",
new KeybindConfig
{
Command = "",
Permission = "keybinds.ch47",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
{
"RELOAD",
new KeybindConfig
{
Command = "/radio",
Permission = "keybinds.ch47",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
{
"SPRINT",
new KeybindConfig
{
Command = "/hover",
Permission = "keybinds.ch47",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
{
"USE",
new KeybindConfig
{
Command = "",
Permission = "keybinds.ch47",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
{
"SPRINT_AND_FIRE_THIRD",
new KeybindConfig
{
Command = "",
Permission = "keybinds.ch47",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
{
"USE_AND_FIRE_THIRD",
new KeybindConfig
{
Command = "",
Permission = "keybinds.ch47",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
{
"USE_AND_RELOAD",
new KeybindConfig
{
Command = "",
Permission = "keybinds.ch47",
Cooldown = 0f,
EnabledCooldownMessage = true,
CooldownMessage = "Cooldown! You must wait {seconds}s to use the {command} key bind."
}
},
}
};
}
protected override void SaveConfig() => Config.WriteObject(configuration, true);
#endregion
#region Initialization
private void OnServerInitialized()
{
LoadConfig();
RegisterConfigPermissions();
}
private void Init()
{
bool autoEnable = configuration?.GlobalSettings?.AutoEnableKeyBinds ?? true;
foreach (var player in BasePlayer.activePlayerList)
{
keybindsEnabled[player.UserIDString] = autoEnable;
}
}
private void OnPlayerConnected(BasePlayer player)
{
bool autoEnable = configuration?.GlobalSettings?.AutoEnableKeyBinds ?? true;
keybindsEnabled[player.UserIDString] = autoEnable;
}
#endregion
#region Commands
[ChatCommand("keybinds")]
private void ToggleKeybindsCommand(BasePlayer player, string command, string[] args)
{
ToggleKeybinds(player);
}
[ConsoleCommand("keybinds")]
private void ConsoleKeybindsCommand(ConsoleSystem.Arg arg)
{
if (arg.Connection?.player is BasePlayer player)
{
ToggleKeybinds(player);
}
}
#endregion
#region Keybind Handling
private void OnPlayerInput(BasePlayer player, InputState input)
{
if (player == null || !player.IsConnected || player.IsSleeping() || player.IsWounded())
return;
string userId = player.UserIDString;
if (!permission.UserHasPermission(userId, PermissionUse))
return;
BaseVehicle vehicle = player.GetMountedVehicle() as BaseVehicle;
bool isDrivingHelicopter = vehicle != null && vehicle.HasDriver();
if (isDrivingHelicopter)
{
if (!messageSentToPlayers.TryGetValue(player.userID, out var heliTypes))
{
heliTypes = new Dictionary<string, bool>();
messageSentToPlayers[player.userID] = heliTypes;
}
if (!heliTypes.ContainsKey(vehicle.ShortPrefabName))
heliTypes[vehicle.ShortPrefabName] = false;
if (!heliTypes[vehicle.ShortPrefabName] && configuration.GlobalSettings.EnablePlayerKeyBindNotifications)
{
if (!keybindsEnabled.ContainsKey(userId) || !keybindsEnabled[userId])
{
SendMessage(player, "Your helicopter keybinds are disabled! Use /keybinds to toggle them on and off.");
}
else
{
SendMessage(player, "Your helicopter keybinds are enabled! Use /keybinds to toggle them on and off.");
}
heliTypes[vehicle.ShortPrefabName] = true;
}
}
if (!keybindsEnabled.ContainsKey(userId) || !keybindsEnabled[userId])
return;
Dictionary<string, KeybindConfig> keybindsDict = isDrivingHelicopter
? GetHelicopterKeybinds(vehicle?.ShortPrefabName)
: configuration.PlayerKeybinds;
if (keybindsDict == null)
return;
if (HandleCombinationKeys(player, input, keybindsDict))
return;
string key = GetSingleInputButton(input);
if (!string.IsNullOrEmpty(key))
{
TryExecuteKeybind(player, key, keybindsDict);
}
}
#endregion
#region Helper Methods
private void RegisterConfigPermissions()
{
alreadyRegistered.Clear();
if (!alreadyRegistered.Contains(PermissionUse))
{
permission.RegisterPermission(PermissionUse, this);
alreadyRegistered.Add(PermissionUse);
}
if (configuration?.PlayerKeybinds != null)
RegisterKeybindPermissions(configuration.PlayerKeybinds);
if (configuration?.Minicopter != null)
RegisterKeybindPermissions(configuration.Minicopter);
if (configuration?.ScrapTransportHelicopter != null)
RegisterKeybindPermissions(configuration.ScrapTransportHelicopter);
if (configuration?.AttackHelicopter != null)
RegisterKeybindPermissions(configuration.AttackHelicopter);
if (configuration?.CH47Helicopter != null)
RegisterKeybindPermissions(configuration.CH47Helicopter);
}
private void RegisterKeybindPermissions(Dictionary<string, KeybindConfig> keybindDict)
{
foreach (var kvp in keybindDict)
{
var kbConfig = kvp.Value;
if (!string.IsNullOrEmpty(kbConfig.Permission) && !alreadyRegistered.Contains(kbConfig.Permission))
{
permission.RegisterPermission(kbConfig.Permission, this);
alreadyRegistered.Add(kbConfig.Permission);
}
}
}
private void ToggleKeybinds(BasePlayer player)
{
if (player == null)
return;
string userId = player.UserIDString;
if (!keybindsEnabled.TryGetValue(userId, out bool isEnabled))
{
keybindsEnabled[userId] = false;
SendMessage(player, "Keybinds disabled. Use '/keybinds' to enable them.");
return;
}
if (isEnabled)
{
keybindsEnabled[userId] = false;
SendMessage(player, "Keybinds disabled. Use '/keybinds' to enable them.");
}
else
{
keybindsEnabled[userId] = true;
SendMessage(player, "Keybinds enabled. Use '/keybinds' to disable them.");
}
}
private Dictionary<string, KeybindConfig> GetHelicopterKeybinds(string shortPrefabName)
{
switch (shortPrefabName)
{
case "minicopter.entity":
return configuration.Minicopter;
case "scraptransporthelicopter":
return configuration.ScrapTransportHelicopter;
case "attackhelicopter.entity":
return configuration.AttackHelicopter;
case "ch47.entity":
return configuration.CH47Helicopter;
default:
return null;
}
}
private bool HandleCombinationKeys(BasePlayer player, InputState input, Dictionary<string, KeybindConfig> keybindsDict)
{
if (!CheckDisabledKey(keybindsDict, "USE_AND_FIRE_THIRD") &&
input.IsDown(BUTTON.USE) && input.WasJustPressed(BUTTON.FIRE_THIRD))
{
TryExecuteKeybind(player, "USE_AND_FIRE_THIRD", keybindsDict);
return true;
}
if (!CheckDisabledKey(keybindsDict, "USE_AND_RELOAD") &&
input.IsDown(BUTTON.USE) && input.WasJustPressed(BUTTON.RELOAD))
{
TryExecuteKeybind(player, "USE_AND_RELOAD", keybindsDict);
return true;
}
if (!CheckDisabledKey(keybindsDict, "SPRINT_AND_FIRE_THIRD") &&
input.IsDown(BUTTON.SPRINT) && input.WasJustPressed(BUTTON.FIRE_THIRD))
{
TryExecuteKeybind(player, "SPRINT_AND_FIRE_THIRD", keybindsDict);
return true;
}
return false;
}
private string GetSingleInputButton(InputState input)
{
if (input.WasJustPressed(BUTTON.FIRE_PRIMARY)) return "FIRE_PRIMARY";
if (input.WasJustPressed(BUTTON.FIRE_SECONDARY)) return "FIRE_SECONDARY";
if (input.WasJustPressed(BUTTON.FIRE_THIRD)) return "FIRE_THIRD";
if (input.WasJustPressed(BUTTON.USE)) return "USE";
if (input.WasJustPressed(BUTTON.DUCK)) return "DUCK";
if (input.WasJustPressed(BUTTON.SPRINT)) return "SPRINT";
if (input.WasJustPressed(BUTTON.RELOAD)) return "RELOAD";
return null;
}
private void TryExecuteKeybind(BasePlayer player, string key, Dictionary<string, KeybindConfig> keybindsDict)
{
if (!keybindsDict.TryGetValue(key, out KeybindConfig config) || config == null)
return;
if (string.IsNullOrEmpty(config.Command) || config.Command.Equals("null", StringComparison.OrdinalIgnoreCase))
return;
if (!string.IsNullOrEmpty(config.Permission) &&
!permission.UserHasPermission(player.UserIDString, config.Permission))
return;
if (configuration.GlobalSettings.GlobalCooldownEnabled &&
configuration.GlobalSettings.GlobalCooldownTime > 0f)
{
string globalKey = player.UserIDString + "_GLOBAL";
if (lastGlobalUsedTime.TryGetValue(globalKey, out float lastGlobalUse))
{
float timeSinceGlobal = Time.realtimeSinceStartup - lastGlobalUse;
if (timeSinceGlobal < configuration.GlobalSettings.GlobalCooldownTime)
{
float remain = configuration.GlobalSettings.GlobalCooldownTime - timeSinceGlobal;
if (configuration.GlobalSettings.GlobalCooldownMessageEnabled)
{
int remainSeconds = Mathf.CeilToInt(remain);
string msg = string.IsNullOrEmpty(configuration.GlobalSettings.GlobalCooldownMessage)
? $"You must wait {remainSeconds}s before using {config.Command} key bind again."
: configuration.GlobalSettings.GlobalCooldownMessage
.Replace("{seconds}", remainSeconds.ToString())
.Replace("{command}", config.Command);
SendMessage(player, msg);
}
return;
}
}
}
string playerKey = player.UserIDString + key;
if (lastUsedTime.TryGetValue(playerKey, out float lastUsed))
{
float timeElapsed = Time.realtimeSinceStartup - lastUsed;
if (timeElapsed < config.Cooldown)
{
float remainCD = config.Cooldown - timeElapsed;
if (config.EnabledCooldownMessage)
{
int remainSeconds = Mathf.CeilToInt(remainCD);
string msg = string.IsNullOrEmpty(config.CooldownMessage)
? $"You must wait {remainSeconds}s to use {config.Command} key bind again."
: config.CooldownMessage
.Replace("{seconds}", remainSeconds.ToString())
.Replace("{command}", config.Command);
SendMessage(player, msg);
}
return;
}
}
if (keybindsDict == configuration.PlayerKeybinds &&
configuration.GlobalSettings.EnablePlayerKeyBindNotifications)
{
if (!messageSentToPlayers.TryGetValue(player.userID, out var infoDict))
{
infoDict = new Dictionary<string, bool>();
messageSentToPlayers[player.userID] = infoDict;
}
if (!infoDict.ContainsKey("PlayerKeybindNotificationSent"))
{
if (!keybindsEnabled[player.UserIDString])
{
SendMessage(player, "Your player keybinds are currently disabled! Use /keybinds to enable them.");
}
else
{
SendMessage(player, "Your player keybinds are currently enabled! Use /keybinds to disable them.");
}
infoDict["PlayerKeybindNotificationSent"] = true;
}
}
ExecuteKeybindCommand(player, config.Command);
lastUsedTime[playerKey] = Time.realtimeSinceStartup;
if (configuration.GlobalSettings.GlobalCooldownEnabled &&
configuration.GlobalSettings.GlobalCooldownTime > 0f)
{
lastGlobalUsedTime[player.UserIDString + "_GLOBAL"] = Time.realtimeSinceStartup;
}
}
private void ExecuteKeybindCommand(BasePlayer player, string command)
{
if (string.IsNullOrEmpty(command)) return;
if (command.StartsWith("/"))
{
player.SendConsoleCommand("chat.say", command);
}
else
{
player.SendConsoleCommand(command);
}
}
private bool CheckDisabledKey(Dictionary<string, KeybindConfig> dict, string key)
{
if (!dict.TryGetValue(key, out var config) || config == null)
return true;
return string.IsNullOrEmpty(config.Command) ||
config.Command.Equals("null", StringComparison.OrdinalIgnoreCase);
}
private void SendMessage(object player, string message)
{
if (player == null) return;
var basePlayer = player as BasePlayer;
if (basePlayer == null) return;
var msgSettings = configuration?.GlobalSettings?.MsgSettings;
if (msgSettings == null)
{
basePlayer.ChatMessage(message);
return;
}
var coloredPrefix = $"<color={msgSettings.MessagePrefixColor}>{msgSettings.MessagePrefix}</color>";
var fullMessage = $"{coloredPrefix}{message}";
basePlayer.SendConsoleCommand("chat.add", 2, msgSettings.MessageIconSteamID, fullMessage);
}
#endregion
#region Clean Up
private void Unload()
{
keybindsEnabled.Clear();
lastUsedTime.Clear();
lastGlobalUsedTime.Clear();
messageSentToPlayers.Clear();
alreadyRegistered.Clear();
}
#endregion
}
}