-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFreeRT.cs
More file actions
267 lines (239 loc) · 9.85 KB
/
Copy pathFreeRT.cs
File metadata and controls
267 lines (239 loc) · 9.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
* < ----- End-User License Agreement ----->
*
* You may not copy, modify, merge, publish, distribute, sublicense, or sell copies of this software without the developer’s consent.
*
* THIS SOFTWARE IS PROVIDED BY IIIaKa AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Developer: IIIaKa
* https://t.me/iiiaka
* Discord: @iiiaka
* https://github.com/IIIaKa
* https://umod.org/user/IIIaKa
* https://codefling.com/iiiaka
* https://lone.design/vendor/iiiaka/
* https://www.patreon.com/iiiaka
* https://boosty.to/iiiaka
* GitHub repository page: https://github.com/IIIaKa/FreeRT
*
* uMod plugin page: https://umod.org/plugins/free-rt
* uMod license: https://umod.org/plugins/free-rt#license
*
* Codefling plugin page: https://codefling.com/plugins/free-rt
* Codefling license: https://codefling.com/plugins/free-rt?tab=downloads_field_4
*
* Lone.Design plugin page: https://lone.design/product/free-rt/
*
* Copyright © 2020-2026 IIIaKa
*/
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using Newtonsoft.Json;
using Facepunch;
using UnityEngine;
using Oxide.Core;
using Oxide.Core.Plugins;
namespace Oxide.Plugins
{
[Info("Free RT", "IIIaKa", "0.1.9")]
[Description("A simple plugin that allows players with permissions to open card-locked doors in Rad Towns without a card.")]
class FreeRT : RustPlugin
{
[PluginReference]
private Plugin NCP, UINotify;
#region ~Variables~
private bool _ncpIsLoaded = false, _uiNotifyIsLoaded = false;
private const string PERMISSION_ALL = "freert.all", PERMISSION_GREEN = "freert.green", PERMISSION_BLUE = "freert.blue", PERMISSION_RED = "freert.red";
#endregion
#region ~Configuration~
private static Configuration _config;
private class Configuration
{
[JsonProperty(PropertyName = "Is it worth showing messages to players who don't have permissions?")]
public bool ShowMessage = true;
[JsonProperty(PropertyName = "Is it worth enabling GameTips for messages?")]
public bool GameTips_Enabled = true;
[JsonProperty(PropertyName = "Is it worth using Notify plugins for messages instead of the vanilla UI?")]
public bool Notify_Enabled = true;
[JsonProperty(PropertyName = "Specify the message type for notify")]
public int Notify_Type = 1;
[JsonProperty(PropertyName = "Time in seconds(1-10) after which the door will close(hinged doors only)")]
public float CloseTime = 5f;
public Oxide.Core.VersionNumber Version;
}
protected override void LoadConfig()
{
base.LoadConfig();
try { _config = Config.ReadObject<Configuration>(); }
catch (Exception ex) { PrintError($"{ex.Message}\n\n[{Title}] Your configuration file contains an error."); }
if (_config == null || _config.Version == new VersionNumber())
{
PrintWarning("The configuration file is not found or contains errors. Creating a new one...");
LoadDefaultConfig();
}
else if (_config.Version < Version)
{
PrintWarning($"Your configuration file version({_config.Version}) is outdated. Updating it to {Version}...");
string cfgPath = $"{Interface.Oxide.ConfigDirectory}{Path.DirectorySeparatorChar}{Name}.json";
if (File.Exists(cfgPath))
File.Move(cfgPath, $"{Interface.Oxide.ConfigDirectory}{Path.DirectorySeparatorChar}_old_{Name}({_config.Version}).json");
_config.Version = Version;
PrintWarning($"The configuration file has been successfully updated to version {_config.Version}!");
}
_config.CloseTime = Mathf.Clamp(_config.CloseTime, 1f, 10f);
SaveConfig();
}
protected override void SaveConfig() => Config.WriteObject(_config);
protected override void LoadDefaultConfig() => _config = new Configuration() { Version = Version };
#endregion
#region ~Language~
protected override void LoadDefaultMessages()
{
lang.RegisterMessages(new Dictionary<string, string>
{
["MsgNotAllowed"] = "You do not have permission to open this door without the card!"
}, this);
lang.RegisterMessages(new Dictionary<string, string>
{
["MsgNotAllowed"] = "У вас недостаточно прав для открытия этой двери без карточки!"
}, this, "ru");
}
#endregion
#region ~Methods~
private void TryOpenDoor(CardReader cardReader, BasePlayer player, Door door = null)
{
bool canOpen = permission.UserHasPermission(player.UserIDString, PERMISSION_ALL);
if (!canOpen)
{
switch (cardReader.accessLevel)
{
case 1:
canOpen = permission.UserHasPermission(player.UserIDString, PERMISSION_GREEN);
break;
case 2:
canOpen = permission.UserHasPermission(player.UserIDString, PERMISSION_BLUE);
break;
case 3:
canOpen = permission.UserHasPermission(player.UserIDString, PERMISSION_RED);
break;
default:
break;
}
}
if (!canOpen)
{
if (!_config.ShowMessage)
return;
string text = lang.GetMessage("MsgNotAllowed", this, player.UserIDString);
if (_config.Notify_Enabled)
{
if (_ncpIsLoaded)
{
NCP.Call("SendNotify", player, _config.Notify_Type, text);
return;
}
if (_uiNotifyIsLoaded)
{
UINotify.Call("SendNotify", player, _config.Notify_Type, text);
return;
}
}
if (_config.GameTips_Enabled)
player.Command("gametip.showtoast", (int)GameTip.Styles.Error, text, string.Empty);
else
player.ChatMessage(text);
return;
}
if (door == null)
{
cardReader.GrantCard();
return;
}
door.SetFlag(BaseEntity.Flags.Open, true);
timer.Once(_config.CloseTime, () =>
{
if (door != null && (cardReader == null || !cardReader.HasFlag(BaseEntity.Flags.On)))
door.SetFlag(BaseEntity.Flags.Open, false);
});
}
#endregion
#region ~Oxide Hooks~
void OnDoorKnocked(Door door, BasePlayer player)
{
if (!door.isSecurityDoor || door.IsOpen())
return;
var crList = Pool.Get<List<CardReader>>();
Vis.Entities(door.transform.position, 6f, crList);
if (crList.Count > 0)
TryOpenDoor(crList[0], player, door);
Pool.FreeUnmanaged(ref crList);
}
void OnSwitchToggled(ElectricSwitch electricSwitch, BasePlayer player)
{
var crList = Pool.Get<List<CardReader>>();
Vis.Entities(electricSwitch.transform.position, 2f, crList);
if (crList.Count > 0)
TryOpenDoor(crList[0], player);
Pool.FreeUnmanaged(ref crList);
}
void OnButtonPress(PressButton button, BasePlayer player)
{
CardReader cardReader;
foreach (var input in button.inputs)
{
cardReader = input.connectedTo?.ioEnt as CardReader;
if (cardReader != null)
{
TryOpenDoor(cardReader, player);
break;
}
}
}
void OnPluginLoaded(Plugin plugin)
{
if (plugin == NCP)
_ncpIsLoaded = NCP != null && NCP.IsLoaded;
else if (plugin == UINotify)
_uiNotifyIsLoaded = UINotify != null && UINotify.IsLoaded;
}
void OnPluginUnloaded(Plugin plugin)
{
if (plugin.Name == "NCP")
_ncpIsLoaded = false;
else if (plugin.Name == "UINotify")
_uiNotifyIsLoaded = false;
}
void Init()
{
Unsubscribe(nameof(OnPluginLoaded));
Unsubscribe(nameof(OnPluginUnloaded));
Unsubscribe(nameof(OnDoorKnocked));
Unsubscribe(nameof(OnSwitchToggled));
Unsubscribe(nameof(OnButtonPress));
permission.RegisterPermission(PERMISSION_ALL, this);
permission.RegisterPermission(PERMISSION_GREEN, this);
permission.RegisterPermission(PERMISSION_BLUE, this);
permission.RegisterPermission(PERMISSION_RED, this);
}
void OnServerInitialized(bool initial)
{
_ncpIsLoaded = NCP != null && NCP.IsLoaded;
_uiNotifyIsLoaded = UINotify != null && UINotify.IsLoaded;
Subscribe(nameof(OnDoorKnocked));
Subscribe(nameof(OnSwitchToggled));
Subscribe(nameof(OnButtonPress));
Subscribe(nameof(OnPluginLoaded));
Subscribe(nameof(OnPluginUnloaded));
if (_config.Notify_Enabled && !_ncpIsLoaded && !_uiNotifyIsLoaded)
PrintWarning("You have Notify plugin support enabled, but none were found!\n* https://codefling.com/plugins/ncp\n* https://umod.org/plugins/ui-notify");
}
void Unload() => _config = null;
#endregion
}
}