-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntityLabel.cs
More file actions
386 lines (319 loc) · 13.6 KB
/
EntityLabel.cs
File metadata and controls
386 lines (319 loc) · 13.6 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Oxide.Game.Rust.Cui;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("Entity Label", "gamezoneone", "1.0.0")]
[Description("Label any deployed entity with custom text that appears when looking at it.")]
public class EntityLabel : RustPlugin
{
#region Config
private Configuration _config;
private class Configuration
{
[JsonProperty("Command")]
public string Command { get; set; } = "label";
[JsonProperty("Permission")]
public string Permission { get; set; } = "entitylabel.use";
[JsonProperty("MaxDistance")]
public float MaxDistance { get; set; } = 5f;
[JsonProperty("MaxLabelLength")]
public int MaxLabelLength { get; set; } = 50;
[JsonProperty("LabelFontSize")]
public int LabelFontSize { get; set; } = 14;
[JsonProperty("LabelColor")]
public string LabelColor { get; set; } = "1 1 1 1";
}
protected override void LoadDefaultConfig()
{
_config = new Configuration();
SaveConfig();
}
protected override void LoadConfig()
{
base.LoadConfig();
try
{
_config = Config.ReadObject<Configuration>();
if (_config == null) throw new System.Exception();
}
catch
{
PrintWarning("Invalid config — loading defaults.");
LoadDefaultConfig();
}
}
protected override void SaveConfig() => Config.WriteObject(_config);
#endregion
#region Lang
private string T(string key, string userId = null) => lang.GetMessage(key, this, userId);
protected override void LoadDefaultMessages()
{
lang.RegisterMessages(new Dictionary<string, string>
{
["NoPermission"] = "You don't have permission to use this.",
["NoEntity"] = "No labelable object in range.",
["NotAuthed"] = "Only TC-authorized players can label here.",
["TooLong"] = "Text too long (max. {0} characters).",
["LabelSet"] = "Label set.",
["LabelRemoved"] = "Label removed.",
["UI.Title"] = "Set Label",
["UI.SaveHint"] = "↵ Save",
["UI.Delete"] = "Delete",
["UI.Cancel"] = "Cancel",
}, this);
}
#endregion
#region Data
private readonly Dictionary<NetworkableId, string> _labels = new Dictionary<NetworkableId, string>();
private readonly Dictionary<ulong, NetworkableId> _showing = new Dictionary<ulong, NetworkableId>();
private readonly Dictionary<ulong, NetworkableId> _pending = new Dictionary<ulong, NetworkableId>();
private static readonly int _raycastMask = LayerMask.GetMask("Construction", "Deployed");
private Timer _displayTimer;
private const string UI_INPUT = "entitylabel.input";
private const string UI_DISPLAY = "entitylabel.display";
#endregion
#region Hooks
private void Init()
{
permission.RegisterPermission(_config.Permission, this);
cmd.AddChatCommand(_config.Command, this, nameof(CmdLabel));
cmd.AddConsoleCommand("entitylabel.submit", this, nameof(ConsoleSubmit));
cmd.AddConsoleCommand("entitylabel.cancel", this, nameof(ConsoleCancel));
cmd.AddConsoleCommand("entitylabel.delete", this, nameof(ConsoleDelete));
}
private void OnServerInitialized()
{
_displayTimer = timer.Every(0.3f, TickDisplay);
}
private void OnEntityKill(BaseNetworkable entity)
{
if (entity?.net == null) return;
_labels.Remove(entity.net.ID);
}
private void OnPlayerDisconnected(BasePlayer player, string reason)
{
if (player == null) return;
CuiHelper.DestroyUi(player, UI_DISPLAY);
CuiHelper.DestroyUi(player, UI_INPUT);
_showing.Remove(player.userID);
_pending.Remove(player.userID);
}
private void Unload()
{
_displayTimer?.Destroy();
foreach (var player in BasePlayer.activePlayerList)
{
if (player == null) continue;
CuiHelper.DestroyUi(player, UI_DISPLAY);
CuiHelper.DestroyUi(player, UI_INPUT);
}
}
#endregion
#region Command
private void CmdLabel(BasePlayer player, string command, string[] args)
{
if (player == null || !player.IsConnected) return;
if (!permission.UserHasPermission(player.UserIDString, _config.Permission))
{
player.ChatMessage(T("NoPermission", player.UserIDString));
return;
}
if (!Physics.Raycast(player.eyes.HeadRay(), out var hit, _config.MaxDistance,
_raycastMask, QueryTriggerInteraction.Ignore))
{
player.ChatMessage(T("NoEntity", player.UserIDString));
return;
}
var entity = hit.transform.GetComponentInParent<BaseEntity>();
if (entity == null || entity is BasePlayer || entity is BaseNpc || entity.net == null)
{
player.ChatMessage(T("NoEntity", player.UserIDString));
return;
}
var priv = entity.GetBuildingPrivilege();
if (priv != null && !priv.authorizedPlayers.Contains(player.userID))
{
player.ChatMessage(T("NotAuthed", player.UserIDString));
return;
}
_pending[player.userID] = entity.net.ID;
_labels.TryGetValue(entity.net.ID, out var existing);
OpenInputUI(player, existing ?? string.Empty);
}
private void ConsoleSubmit(ConsoleSystem.Arg arg)
{
var player = arg.Player();
if (player == null) return;
if (!permission.UserHasPermission(player.UserIDString, _config.Permission)) return;
if (!_pending.TryGetValue(player.userID, out var netId))
{
CloseInputUI(player);
return;
}
CloseInputUI(player);
var text = arg.Args != null ? string.Join(" ", arg.Args).Trim() : string.Empty;
if (text.Length > _config.MaxLabelLength)
{
player.ChatMessage(string.Format(T("TooLong", player.UserIDString), _config.MaxLabelLength));
return;
}
if (string.IsNullOrEmpty(text))
{
_labels.Remove(netId);
player.ChatMessage(T("LabelRemoved", player.UserIDString));
}
else
{
_labels[netId] = text;
player.ChatMessage(T("LabelSet", player.UserIDString));
}
}
private void ConsoleCancel(ConsoleSystem.Arg arg)
{
var player = arg.Player();
if (player != null) CloseInputUI(player);
}
private void ConsoleDelete(ConsoleSystem.Arg arg)
{
var player = arg.Player();
if (player == null) return;
if (!permission.UserHasPermission(player.UserIDString, _config.Permission)) return;
if (!_pending.TryGetValue(player.userID, out var netId)) { CloseInputUI(player); return; }
CloseInputUI(player);
_labels.Remove(netId);
player.ChatMessage(T("LabelRemoved", player.UserIDString));
}
private void OpenInputUI(BasePlayer player, string existing)
{
var uid = player.UserIDString;
CuiHelper.DestroyUi(player, UI_INPUT);
var container = new CuiElementContainer();
container.Add(new CuiPanel
{
Image = { Color = "0.08 0.08 0.08 0.92" },
RectTransform = { AnchorMin = "0.3 0.43", AnchorMax = "0.7 0.59" },
CursorEnabled = true
}, "Hud", UI_INPUT);
container.Add(new CuiLabel
{
Text = { Text = T("UI.Title", uid), FontSize = 13, Align = TextAnchor.MiddleCenter, Color = "0.9 0.9 0.9 1" },
RectTransform = { AnchorMin = "0 0.78", AnchorMax = "1 1" }
}, UI_INPUT);
container.Add(new CuiPanel
{
Image = { Color = "0.18 0.18 0.18 1" },
RectTransform = { AnchorMin = "0.04 0.46", AnchorMax = "0.96 0.76" }
}, UI_INPUT, UI_INPUT + ".bg");
container.Add(new CuiElement
{
Name = UI_INPUT + ".field",
Parent = UI_INPUT + ".bg",
Components =
{
new CuiInputFieldComponent
{
Text = existing,
FontSize = 14,
Align = TextAnchor.MiddleLeft,
Color = "1 1 1 1",
Command = "entitylabel.submit",
CharsLimit = _config.MaxLabelLength,
IsPassword = false,
NeedsKeyboard = true
},
new CuiRectTransformComponent { AnchorMin = "0.03 0", AnchorMax = "1 1" }
}
});
container.Add(new CuiLabel
{
Text = { Text = T("UI.SaveHint", uid), FontSize = 10, Align = TextAnchor.MiddleRight, Color = "0.5 0.5 0.5 1" },
RectTransform = { AnchorMin = "0 0.30", AnchorMax = "0.97 0.46" }
}, UI_INPUT);
container.Add(new CuiButton
{
Button = { Command = "entitylabel.delete", Color = "0.6 0.15 0.15 1" },
RectTransform = { AnchorMin = "0.04 0.04", AnchorMax = "0.49 0.28" },
Text = { Text = T("UI.Delete", uid), FontSize = 12, Align = TextAnchor.MiddleCenter, Color = "1 1 1 1" }
}, UI_INPUT);
container.Add(new CuiButton
{
Button = { Command = "entitylabel.cancel", Color = "0.25 0.25 0.25 1" },
RectTransform = { AnchorMin = "0.51 0.04", AnchorMax = "0.96 0.28" },
Text = { Text = T("UI.Cancel", uid), FontSize = 12, Align = TextAnchor.MiddleCenter, Color = "1 1 1 1" }
}, UI_INPUT);
CuiHelper.AddUi(player, container);
}
private void CloseInputUI(BasePlayer player)
{
CuiHelper.DestroyUi(player, UI_INPUT);
_pending.Remove(player.userID);
}
#endregion
#region Display
private void TickDisplay()
{
var players = BasePlayer.activePlayerList.ToArray();
foreach (var player in players)
{
if (player == null || !player.IsConnected) continue;
if (_pending.ContainsKey(player.userID)) continue;
NetworkableId hitId = default;
string label = null;
BaseEntity hitEntity = null;
if (Physics.Raycast(player.eyes.HeadRay(), out var hit, _config.MaxDistance,
_raycastMask, QueryTriggerInteraction.Ignore))
{
var entity = hit.transform.GetComponentInParent<BaseEntity>();
if (entity != null && entity.net != null && !(entity is BasePlayer) && !(entity is BaseNpc)
&& _labels.TryGetValue(entity.net.ID, out var l))
{
hitId = entity.net.ID;
label = l;
hitEntity = entity;
}
}
var wasShowing = _showing.TryGetValue(player.userID, out var currentId);
if (label != null)
{
if (!wasShowing || currentId != hitId)
{
ShowLabel(player, label);
_showing[player.userID] = hitId;
}
}
else if (wasShowing)
{
HideLabel(player);
}
}
}
private void ShowLabel(BasePlayer player, string label)
{
CuiHelper.DestroyUi(player, UI_DISPLAY);
var container = new CuiElementContainer();
float halfW = Mathf.Max(0.04f, label.Length * 0.0038f + 0.022f);
string aMin = $"{0.5f - halfW:F3} 0.638";
string aMax = $"{0.5f + halfW:F3} 0.668";
container.Add(new CuiPanel
{
Image = { Color = "0.08 0.08 0.08 0.78" },
RectTransform = { AnchorMin = aMin, AnchorMax = aMax }
}, "Hud", UI_DISPLAY);
container.Add(new CuiLabel
{
Text = { Text = label, FontSize = _config.LabelFontSize, Align = TextAnchor.MiddleCenter, Color = _config.LabelColor },
RectTransform = { AnchorMin = "0 0", AnchorMax = "1 1" }
}, UI_DISPLAY);
CuiHelper.AddUi(player, container);
}
private void HideLabel(BasePlayer player)
{
CuiHelper.DestroyUi(player, UI_DISPLAY);
_showing.Remove(player.userID);
}
#endregion
}
}