C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Oxide.Core;
using Oxide.Core.Plugins;
using UnityEngine;
using ProtoBuf;
namespace Oxide.Plugins {
[Info("AutoCodeLock", "Fujikura", "1.5.1", ResourceId = 15)]
[Description("Codelock automation")]
class AutoCodeLock : RustPlugin {
bool Changed = false;
bool Initialized = false;
StoredData playerPrefs = new StoredData();
FieldInfo _hasCode = typeof(CodeLock).GetField("hasCode", (BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic));
class StoredData {
public Dictionary<ulong, PlayerInfo> PlayerInfo = new Dictionary<ulong, PlayerInfo>();
public StoredData() {
}
}
class PlayerInfo {
public bool AL;
public bool DLD;
public bool DLB;
public bool DLL;
public bool DLC;
public int PC;
public PlayerInfo() {
}
}
string codelockCommand;
bool notifyAuthCodeLock;
string permissionDeployDoor;
string permissionDeployBox;
string permissionDeployLocker;
string permissionDeployCupboard;
string permissionAutoLock;
string permissionNoLockNeed;
string permissionAll;
string pluginPrefix;
string prefixColor;
string prefixFormat;
string colorTextMsg;
string colorCmdUsage;
string colorON;
string colorOFF;
bool AutoLock;
bool DeployDoor;
bool DeployBox;
bool DeployLocker;
bool DeployCupboard;
object GetConfig(string menu, string datavalue, object defaultValue) {
var data = Config[menu] as Dictionary<string, object>;
if (data == null) {
data = new Dictionary<string, object>();
Config[menu] = data;
Changed = true;
}
object value;
if (!data.TryGetValue(datavalue, out value)) {
value = defaultValue;
data[datavalue] = value;
Changed = true;
}
return value;
}
void LoadVariables() {
codelockCommand = Convert.ToString(GetConfig("Command", "codelockCommand", "codelock"));
notifyAuthCodeLock = Convert.ToBoolean(GetConfig("Options", "notifyAuthCodeLock", true));
permissionDeployDoor = Convert.ToString(GetConfig("Permissions", "permissionDeployDoor", "autocodelock.deploydoor"));
permissionDeployBox = Convert.ToString(GetConfig("Permissions", "permissionDeployBox", "autocodelock.deploybox"));
permissionDeployLocker = Convert.ToString(GetConfig("Permissions", "permissionDeployLocker", "autocodelock.deploylocker"));
permissionDeployCupboard = Convert.ToString(GetConfig("Permissions", "permissionDeployCupboard", "autocodelock.deploycup"));
permissionAutoLock = Convert.ToString(GetConfig("Permissions", "permissionAutoLock", "autocodelock.autolock"));
permissionNoLockNeed = Convert.ToString(GetConfig("Permissions", "permissionNoLockNeed", "autocodelock.nolockneed"));
permissionAll = Convert.ToString(GetConfig("Permissions", "permissionAll", "autocodelock.all"));
pluginPrefix = Convert.ToString(GetConfig("Formatting", "pluginPrefix", "AutoCodeLock"));
prefixColor = Convert.ToString(GetConfig("Formatting", "prefixColor", "orange"));
prefixFormat = Convert.ToString(GetConfig("Formatting", "prefixFormat", "<color={0}>{1}</color>: "));
colorTextMsg = Convert.ToString(GetConfig("Formatting", "colorTextMsg", "white"));
colorCmdUsage = Convert.ToString(GetConfig("Formatting", "colorCmdUsage", "yellow"));
colorON = Convert.ToString(GetConfig("Formatting", "colorON", "green"));
colorOFF = Convert.ToString(GetConfig("Formatting", "colorOFF", "silver"));
AutoLock = Convert.ToBoolean(GetConfig("PlayerDefaults", "AutoLock", false));
DeployDoor = Convert.ToBoolean(GetConfig("PlayerDefaults", "DeployDoor", false));
DeployBox = Convert.ToBoolean(GetConfig("PlayerDefaults", "DeployBox", false));
DeployLocker = Convert.ToBoolean(GetConfig("PlayerDefaults", "DeployLocker", false));
DeployCupboard = Convert.ToBoolean(GetConfig("PlayerDefaults", "DeployCupboard", false));
if (!Changed) return;
SaveConfig();
Changed = false;
}
protected override void LoadDefaultConfig() {
Config.Clear();
LoadVariables();
}
void LoadDefaultMessages() {
lang.RegisterMessages(new Dictionary<string, string> {
{
"AutoLockEnabled", "CodeLock automation (secure and lock) enabled"
}
, {
"AutoLockDisabled", "CodeLock automation disabled"
}
, {
"DeployLockDoorEnabled", "Doors will include codelocks on deploy"
}
, {
"DeployLockDoorDisabled", "Doors will not include codelocks on deploy"
}
, {
"DeployLockBoxEnabled", "Boxes will include codelocks on deploy"
}
, {
"DeployLockBoxDisabled", "Boxes will not include codelocks on deploy"
}
, {
"DeployLockLockerEnabled", "Locker will include codelocks on deploy"
}
, {
"DeployLockLockerDisabled", "Locker will not include codelocks on deploy"
}
, {
"DeployLockCupEnabled", "Cupboards will include codelocks on deploy"
}
, {
"DeployLockCupDisabled", "Cupboards will not include codelocks on deploy"
}
, {
"CodeAuth", "CodeLock secured and locked with '{0}'"
}
, {
"NoAccess", "You are not granted for this feature"
}
, {
"NotSupported", "The specific function '{0}' is not available"
}
, {
"CommandUsage", "Command usage:"
}
, {
"CommandToggle", "All switches toggle their setting (on<>off)"
}
, {
"CommandAutolock", "Autolock feature:"
}
, {
"CommandPinCode", "Your current PIN:"
}
, {
"CommandPinCodeNotSet", "Random 8-Digits"
}
, {
"CommandPinCodeSetTo", "Your Pin was succesful set to:"
}
, {
"CommandPinCodeHelp", "Set your PIN with <color={0}>/{1} pin <1234></color> (4-Digits)"
}
, {
"CommandDeployDoor", "Deploy with Door:"
}
, {
"CommandDeployBox", "Deploy with Box:"
}
, {
"CommandDeployLocker", "Deploy with Locker:"
}
, {
"CommandDeployCupboard", "Deploy with Cupboard:"
}
,
}
, this);
}
void Loaded() {
LoadVariables();
LoadDefaultMessages();
cmd.AddChatCommand(codelockCommand, this, "CodeLockCommand");
}
void OnServerInitialized() {
if (!permission.PermissionExists(permissionDeployDoor)) permission.RegisterPermission(permissionDeployDoor, this);
if (!permission.PermissionExists(permissionAutoLock)) permission.RegisterPermission(permissionAutoLock, this);
if (!permission.PermissionExists(permissionDeployBox)) permission.RegisterPermission(permissionDeployBox, this);
if (!permission.PermissionExists(permissionDeployLocker)) permission.RegisterPermission(permissionDeployLocker, this);
if (!permission.PermissionExists(permissionDeployCupboard)) permission.RegisterPermission(permissionDeployCupboard, this);
if (!permission.PermissionExists(permissionAll)) permission.RegisterPermission(permissionAll, this);
if (!permission.PermissionExists(permissionNoLockNeed)) permission.RegisterPermission(permissionNoLockNeed, this);
playerPrefs = Interface.GetMod().DataFileSystem.ReadObject<StoredData>(this.Title);
foreach(var player in BasePlayer.activePlayerList) SetPlayer(player);
foreach(var player in BasePlayer.sleepingPlayerList) SetPlayer(player);
Interface.Oxide.DataFileSystem.WriteObject(this.Title, playerPrefs);
Initialized = true;
}
void SetPlayer(BasePlayer player) {
if (player == null) return;
PlayerInfo p = null;
if (!playerPrefs.PlayerInfo.TryGetValue(player.userID, out p)) {
var info = new PlayerInfo();
info.AL = AutoLock;
info.DLD = DeployDoor;
info.DLB = DeployBox;
info.DLL = DeployLocker;
info.DLC = DeployCupboard;
info.PC = UnityEngine.Random.Range(1999, 9999);
playerPrefs.PlayerInfo.Add(player.userID, info);
return;
}
return;
}
void OnPlayerInit(BasePlayer player) {
SetPlayer(player);
}
void Unload() {
SaveData();
}
void OnServerSave() {
SaveData();
}
void SaveData() {
Interface.Oxide.DataFileSystem.WriteObject(this.Title, playerPrefs);
}
void OnItemDeployed(Deployer deployer, BaseEntity entity) {
if (!Initialized || deployer.GetOwnerPlayer() == null || deployer.GetModDeployable() == null || deployer.GetModDeployable().name == "doorcloser.item" || !entity.HasSlot(BaseEntity.Slot.Lock) || !(entity.GetSlot(BaseEntity.Slot.Lock) is CodeLock)) return;
var owner = deployer.GetOwnerPlayer();
if ((permission.UserHasPermission(owner.UserIDString, permissionAutoLock) || permission.UserHasPermission(owner.UserIDString, permissionAll)) && playerPrefs.PlayerInfo[owner.userID].AL) {
var codelock = entity.GetSlot(BaseEntity.Slot.Lock) as CodeLock;
if (playerPrefs.PlayerInfo[owner.userID].PC != 0) codelock.code = Convert.ToString(playerPrefs.PlayerInfo[owner.userID].PC);
else codelock.code = Convert.ToString(UnityEngine.Random.Range(1234, 9876));
_hasCode.SetValue(codelock, true);
codelock.whitelistPlayers.Add(owner.userID);
(codelock as BaseEntity).SetFlag(BaseEntity.Flags.Locked, true, false);
Effect.server.Run("assets/prefabs/locks/keypad/effects/lock.code.updated.prefab", entity.transform.position);
if (notifyAuthCodeLock) {
var code = codelock.code;
if (owner.net.connection.info.GetBool("global.streamermode")) code = "****";
PrintToChat(owner, string.Format(prefixFormat, prefixColor, pluginPrefix) + $"<color={colorTextMsg}>"+ lang.GetMessage("CodeAuth", this, owner.UserIDString), code +"</color>");
}
}
}
void LockPlacing(BasePlayer player, BaseEntity entity) {
if (permission.UserHasPermission(player.UserIDString, permissionNoLockNeed)) {
var codeLock = GameManager.server.CreateEntity("assets/prefabs/locks/keypad/lock.code.prefab", new Vector3(), new Quaternion(), true);
if (codeLock == null) return;
codeLock.gameObject.Identity();
codeLock.SetParent(entity, entity.GetSlotAnchorName(BaseEntity.Slot.Lock));
codeLock.OnDeployed(entity);
codeLock.Spawn();
entity.SetSlot(BaseEntity.Slot.Lock, codeLock);
if ((permission.UserHasPermission(player.UserIDString, permissionAutoLock) || permission.UserHasPermission(player.UserIDString, permissionAll)) && playerPrefs.PlayerInfo[player.userID].AL) {
if (playerPrefs.PlayerInfo[player.userID].PC != 0) (codeLock as CodeLock).code = Convert.ToString(playerPrefs.PlayerInfo[player.userID].PC);
else (codeLock as CodeLock).code = Convert.ToString(UnityEngine.Random.Range(1234, 9876));
_hasCode.SetValue(codeLock as CodeLock, true);
(codeLock as CodeLock).whitelistPlayers.Add(player.userID);
codeLock.SetFlag(BaseEntity.Flags.Locked, true, false);
Effect.server.Run("assets/prefabs/locks/keypad/effects/lock.code.updated.prefab", entity.transform.position);
if (notifyAuthCodeLock) {
var code = (codeLock as CodeLock).code;
if (player.net.connection.info.GetBool("global.streamermode")) code = "****";
PrintToChat(player, string.Format(prefixFormat, prefixColor, pluginPrefix) + $"<color={colorTextMsg}>"+ lang.GetMessage("CodeAuth", this, player.UserIDString), code +"</color>");
}
}
return;
}
else {
Item codelock = player.inventory.FindItemID(-975723312);
if(codelock == null) return;
var deploy = codelock.GetHeldEntity() as Deployer;
deploy.DoDeploy_Slot(deploy.GetDeployable(), player.eyes.HeadRay(), entity.net.ID);
}
}
void OnEntityBuilt(Planner planner, GameObject obj) {
if (!Initialized || planner == null || planner.GetOwnerPlayer() == null || obj.GetComponent<BaseEntity>() == null || obj.GetComponent<BaseEntity>().OwnerID == 0) return;
BaseEntity entity = obj.GetComponent<BaseEntity>();
BasePlayer player = planner.GetOwnerPlayer();
if (player == null || !playerPrefs.PlayerInfo.ContainsKey(player.userID)) return;
if (entity is Door && (entity as Door).canTakeLock) {
if ((permission.UserHasPermission(player.UserIDString, permissionDeployDoor) || permission.UserHasPermission(player.UserIDString, permissionAll)) && playerPrefs.PlayerInfo[player.userID].DLD) LockPlacing(player, entity);
return;
}
else if (entity is BoxStorage && (entity as BaseEntity).HasSlot(BaseEntity.Slot.Lock)) {
if ((permission.UserHasPermission(player.UserIDString, permissionDeployBox) || permission.UserHasPermission(player.UserIDString, permissionAll)) && playerPrefs.PlayerInfo[player.userID].DLB) LockPlacing(player, entity);
return;
}
else if (entity is Locker && (entity as BaseEntity).HasSlot(BaseEntity.Slot.Lock)) {
if ((permission.UserHasPermission(player.UserIDString, permissionDeployLocker) || permission.UserHasPermission(player.UserIDString, permissionAll)) && playerPrefs.PlayerInfo[player.userID].DLL) LockPlacing(player, entity);
return;
}
else if (entity is BuildingPrivlidge && (entity as BaseEntity).HasSlot(BaseEntity.Slot.Lock)) {
if ((permission.UserHasPermission(player.UserIDString, permissionDeployCupboard) || permission.UserHasPermission(player.UserIDString, permissionAll)) && playerPrefs.PlayerInfo[player.userID].DLC) LockPlacing(player, entity);
return;
}
}
void CodeLockCommand(BasePlayer player, string command, string[] args) {
if (!permission.UserHasPermission(player.UserIDString, permissionAutoLock) && !permission.UserHasPermission(player.UserIDString, permissionDeployDoor) && !permission.UserHasPermission(player.UserIDString, permissionDeployBox) && !permission.UserHasPermission(player.UserIDString, permissionDeployLocker) && !permission.UserHasPermission(player.UserIDString, permissionDeployCupboard) && !permission.UserHasPermission(player.UserIDString, permissionAll)) {
PrintToChat(player, string.Format(prefixFormat, prefixColor, pluginPrefix) + $"<color={colorTextMsg}>"+ lang.GetMessage("NoAccess", this, player.UserIDString) +"</color>");
return;
}
if(args.Length == 0) {
var sb = new StringBuilder();
sb.AppendLine($"<size=16><color={prefixColor}>{pluginPrefix}</color></size>");
sb.Append($"<color={colorTextMsg}>");
sb.AppendLine(lang.GetMessage("CommandUsage", this, player.UserIDString)+$"<color={colorCmdUsage}>/{codelockCommand} <option></color>\n"+lang.GetMessage("CommandToggle", this, player.UserIDString));
if (permission.UserHasPermission(player.UserIDString, permissionAutoLock) || permission.UserHasPermission(player.UserIDString, permissionAll)) sb.AppendLine($"<color={colorCmdUsage}>auto</color> - " +lang.GetMessage("CommandAutolock", this, player.UserIDString)+" "+(playerPrefs.PlayerInfo[player.userID].AL ? $"<color={colorON}>ВКЛ</color>" : $"<color={colorOFF}>ВЫКЛ</color>"));
var pin = Convert.ToString(playerPrefs.PlayerInfo[player.userID].PC);
if (player.net.connection.info.GetBool("global.streamermode")) pin = "****";
sb.AppendLine($"<color={colorCmdUsage}>pin</color> - " +lang.GetMessage("CommandPinCode", this, player.UserIDString)+" "+(playerPrefs.PlayerInfo[player.userID].PC > 0 ? $"<color={colorCmdUsage}>{pin}</color>" : $"<color={colorOFF}>{lang.GetMessage("CommandPinCodeNotSet", this, player.UserIDString)}</color>") );
if (permission.UserHasPermission(player.UserIDString, permissionDeployDoor) || permission.UserHasPermission(player.UserIDString, permissionAll)) sb.AppendLine($"<color={colorCmdUsage}>door</color> - " +lang.GetMessage("CommandDeployDoor", this, player.UserIDString)+" "+(playerPrefs.PlayerInfo[player.userID].DLD ? $"<color={colorON}>ВКЛ</color>" : $"<color={colorOFF}>ВЫКЛ</color>"));
if (permission.UserHasPermission(player.UserIDString, permissionDeployBox) || permission.UserHasPermission(player.UserIDString, permissionAll)) sb.AppendLine($"<color={colorCmdUsage}>box</color> - " +lang.GetMessage("CommandDeployBox", this, player.UserIDString)+" "+(playerPrefs.PlayerInfo[player.userID].DLB ? $"<color={colorON}>ВКЛ</color>" : $"<color={colorOFF}>ВЫКЛ</color>"));
if (permission.UserHasPermission(player.UserIDString, permissionDeployLocker) || permission.UserHasPermission(player.UserIDString, permissionAll)) sb.AppendLine($"<color={colorCmdUsage}>locker</color> - " +lang.GetMessage("CommandDeployLocker", this, player.UserIDString)+" "+(playerPrefs.PlayerInfo[player.userID].DLL ? $"<color={colorON}>ВКЛ</color>" : $"<color={colorOFF}>ВЫКЛ</color>"));
if (permission.UserHasPermission(player.UserIDString, permissionDeployCupboard) || permission.UserHasPermission(player.UserIDString, permissionAll)) sb.AppendLine($"<color={colorCmdUsage}>cup</color> - " +lang.GetMessage("CommandDeployCupboard", this, player.UserIDString)+" "+(playerPrefs.PlayerInfo[player.userID].DLC ? $"<color={colorON}>ВКЛ</color>" : $"<color={colorOFF}>ВЫКЛ</color>"));
sb.Append("</color>");
SendReply(player, sb.ToString().TrimEnd());
return;
}
switch (args[0].ToLower()) {
case "auto" : if (permission.UserHasPermission(player.UserIDString, permissionAutoLock) || permission.UserHasPermission(player.UserIDString, permissionAll)) {
playerPrefs.PlayerInfo[player.userID].AL = !playerPrefs.PlayerInfo[player.userID].AL;
PrintToChat(player, string.Format(prefixFormat, prefixColor, pluginPrefix) + $"<color={colorTextMsg}>" + (playerPrefs.PlayerInfo[player.userID].AL ? lang.GetMessage("AutoLockEnabled", this, player.UserIDString) : lang.GetMessage("AutoLockDisabled", this, player.UserIDString)) +"</color>");
}
else goto case "noaccess";
break;
case "door" : if (permission.UserHasPermission(player.UserIDString, permissionDeployDoor) || permission.UserHasPermission(player.UserIDString, permissionAll)) {
playerPrefs.PlayerInfo[player.userID].DLD = !playerPrefs.PlayerInfo[player.userID].DLD;
PrintToChat(player, string.Format(prefixFormat, prefixColor, pluginPrefix) + $"<color={colorTextMsg}>" + (playerPrefs.PlayerInfo[player.userID].DLD ? lang.GetMessage("DeployLockDoorEnabled", this, player.UserIDString) : lang.GetMessage("DeployLockDoorDisabled", this, player.UserIDString)) +"</color>");
}
else goto case "noaccess";
break;
case "box" : if (permission.UserHasPermission(player.UserIDString, permissionDeployBox) || permission.UserHasPermission(player.UserIDString, permissionAll)) {
playerPrefs.PlayerInfo[player.userID].DLB = !playerPrefs.PlayerInfo[player.userID].DLB;
PrintToChat(player, string.Format(prefixFormat, prefixColor, pluginPrefix) + $"<color={colorTextMsg}>" + (playerPrefs.PlayerInfo[player.userID].DLB ? lang.GetMessage("DeployLockBoxEnabled", this, player.UserIDString) : lang.GetMessage("DeployLockBoxDisabled", this, player.UserIDString)) +"</color>");
}
else goto case "noaccess";
break;
case "locker" : if (permission.UserHasPermission(player.UserIDString, permissionDeployLocker) || permission.UserHasPermission(player.UserIDString, permissionAll)) {
playerPrefs.PlayerInfo[player.userID].DLL = !playerPrefs.PlayerInfo[player.userID].DLL;
PrintToChat(player, string.Format(prefixFormat, prefixColor, pluginPrefix) + $"<color={colorTextMsg}>" + (playerPrefs.PlayerInfo[player.userID].DLL ? lang.GetMessage("DeployLockLockerEnabled", this, player.UserIDString) : lang.GetMessage("DeployLockLockerDisabled", this, player.UserIDString)) +"</color>");
}
else goto case "noaccess";
break;
case "cup" : if (permission.UserHasPermission(player.UserIDString, permissionDeployCupboard) || permission.UserHasPermission(player.UserIDString, permissionAll)) {
playerPrefs.PlayerInfo[player.userID].DLC = !playerPrefs.PlayerInfo[player.userID].DLC;
PrintToChat(player, string.Format(prefixFormat, prefixColor, pluginPrefix) + $"<color={colorTextMsg}>" + (playerPrefs.PlayerInfo[player.userID].DLC ? lang.GetMessage("DeployLockCupEnabled", this, player.UserIDString) : lang.GetMessage("DeployLockCupDisabled", this, player.UserIDString)) +"</color>");
}
else goto case "noaccess";
break;
case "pin" : if (permission.UserHasPermission(player.UserIDString, permissionAutoLock) || permission.UserHasPermission(player.UserIDString, permissionAll)) {
int pin;
if (args.Length != 2 || int.TryParse(args[1], out pin) == false || args[1].Length != 4 || pin.ToString().Length < 4) {
PrintToChat(player, string.Format(prefixFormat, prefixColor, pluginPrefix) + string.Format(lang.GetMessage("CommandPinCodeHelp", this, player.UserIDString), colorCmdUsage, codelockCommand));
return;
}
playerPrefs.PlayerInfo[player.userID].PC = pin;
PrintToChat(player, string.Format(prefixFormat, prefixColor, pluginPrefix) + $"<color={colorTextMsg}>" + string.Format(lang.GetMessage("CommandPinCodeSetTo", this, player.UserIDString)) + $" <color={colorCmdUsage}>{pin}</color></color>");
}
else goto case "noaccess";
break;
case "noaccess" : PrintToChat(player, string.Format(prefixFormat, prefixColor, pluginPrefix) + $"<color={colorTextMsg}>"+ lang.GetMessage("NoAccess", this, player.UserIDString) +"</color>");
break;
default : PrintToChat(player, string.Format(prefixFormat, prefixColor, pluginPrefix) + $"<color={colorTextMsg}>" + string.Format(lang.GetMessage("NotSupported", this, player.UserIDString), args[0]) + "</color>");
break;
}
}
}
}