-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSupplyAlert.cs
More file actions
198 lines (159 loc) · 5.52 KB
/
Copy pathSupplyAlert.cs
File metadata and controls
198 lines (159 loc) · 5.52 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
using Newtonsoft.Json;
using Oxide.Game.Rust.Cui;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("Supply Alert", "Ultra", "1.1.2")]
[Description("Sound and visual alert for dropped supply")]
class SupplyAlert : RustPlugin
{
#region Fields
Timer guiAlertTimer;
CuiPanel alertPanel;
string alertPanelName = "alertPanelName";
#endregion
#region Chat Commands
[ChatCommand("satest")]
void SupplyAlertTest(BasePlayer player)
{
RunSupplyAlert();
}
#endregion
#region Oxide Hooks
void OnServerInitialized()
{
InitCUI();
}
void OnEntitySpawned(BaseEntity baseEntity)
{
if (baseEntity != null && baseEntity is SupplyDrop)
{
RunSupplyAlert();
}
}
void Unload()
{
DestroyGUIAlerts();
guiAlertTimer?.DestroyToPool();
guiAlertTimer = null;
}
#endregion
#region Core
void RunSupplyAlert()
{
foreach (var player in BasePlayer.activePlayerList)
{
if (configData.ChatAlertEnabled) SendReply(player, $"<color=#ee2211>{configData.ChatAlertText}</color>");
if (configData.SoundAlertEnabled) Effect.server.Run($"{configData.SoundAlertAsset}", player.transform.position);
if (configData.GUIAlertEnabled) DisplayAlertGUI(player);
}
if (configData.GUIAlertEnabled) guiAlertTimer = timer.Once(configData.GUIAlertDuration, () => DestroyGUIAlerts());
}
#endregion
#region Config
private ConfigData configData;
private class ConfigData
{
[JsonProperty(PropertyName = "Sound alert enabled")]
public bool SoundAlertEnabled;
[JsonProperty(PropertyName = "Sound alert asset")]
public string SoundAlertAsset;
[JsonProperty(PropertyName = "Chat alert enabled")]
public bool ChatAlertEnabled;
[JsonProperty(PropertyName = "Chat alert text")]
public string ChatAlertText;
[JsonProperty(PropertyName = "GUI alert enabled")]
public bool GUIAlertEnabled;
[JsonProperty(PropertyName = "GUI alert text")]
public string GUIAlertText;
[JsonProperty(PropertyName = "GUI alert duration (seconds)")]
public float GUIAlertDuration;
}
protected override void LoadConfig()
{
try
{
base.LoadConfig();
configData = Config.ReadObject<ConfigData>();
if (configData == null)
{
LoadDefaultConfig();
}
}
catch
{
LoadDefaultConfig();
}
SaveConfig();
}
protected override void LoadDefaultConfig()
{
configData = new ConfigData()
{
ChatAlertEnabled = true,
ChatAlertText = "Supply in the air",
GUIAlertEnabled = true,
GUIAlertText = "SUPPLY IN THE AIR",
GUIAlertDuration = 3,
SoundAlertEnabled = true,
SoundAlertAsset = "assets/bundled/prefabs/fx/invite_notice.prefab"
};
}
protected override void SaveConfig()
{
Config.WriteObject(configData, true);
base.SaveConfig();
}
#endregion
#region CUI
void InitCUI()
{
alertPanel = new CuiPanel
{
CursorEnabled = false,
RectTransform =
{
AnchorMin = $"0.4 0.77",
AnchorMax = $"0.6 0.81"
},
Image =
{ Color = "0 0 0 0.8" }
};
}
void DisplayAlertGUI(BasePlayer player)
{
CuiHelper.DestroyUi(player, alertPanelName);
var container = new CuiElementContainer();
container.Add(alertPanel, name: alertPanelName);
container.Add(GetLabel($"{configData.GUIAlertText}", align: TextAnchor.MiddleCenter), alertPanelName);
CuiHelper.AddUi(player, container);
}
void DestroyGUIAlerts()
{
foreach (var player in BasePlayer.activePlayerList)
{
CuiHelper.DestroyUi(player, alertPanelName);
}
}
CuiLabel GetLabel(string text, int size = 13, string anchorMin = "0.05 0.02", string anchorMax = "0.98 0.9", TextAnchor align = TextAnchor.MiddleCenter, string color = "1 1 1 1", string font = "robotocondensed-regular.ttf")
{
return new CuiLabel
{
Text =
{
Text = text,
FontSize = size,
Align = align,
Color = color,
Font = font
},
RectTransform =
{
AnchorMin = anchorMin,
AnchorMax = anchorMax
},
};
}
#endregion
}
}