-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathBaseProtector.cs
More file actions
214 lines (176 loc) · 7.04 KB
/
Copy pathBaseProtector.cs
File metadata and controls
214 lines (176 loc) · 7.04 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
using System;
using UnityEngine;
using System.Globalization;
using Oxide.Game.Rust.Cui;
using Oxide.Core;
using Newtonsoft.Json;
namespace Oxide.Plugins
{
[Info("BaseProtector", "https://vk.com/nastroykarust", "1.0.2")]
public class BaseProtector : RustPlugin
{
#region Oxide Hooks
private void OnServerInitialized()
{
LoadConfig();
BasePlayer.activePlayerList.ForEach(OnPlayerInit);
if (_config.BaseProtect)
{
timer.Every(60, () =>
{
foreach (var players in BasePlayer.activePlayerList)
{
if (DateTime.Now.Hour - 1 > _config.FirstTime || DateTime.Now.Hour - 1 < _config.SecoundTime)
{
DrawGUI(players);
}
}
});
}
}
void OnPlayerInit(BasePlayer player)
{
if (player.IsReceivingSnapshot)
{
NextTick(() =>
{
OnPlayerInit(player);
});
return;
}
if (_config.BaseProtect)
{
if (DateTime.Now.Hour - 1 > _config.FirstTime || DateTime.Now.Hour - 1 < _config.SecoundTime)
{
DrawGUI(player);
}
}
}
private void OnEntityTakeDamage(BaseEntity entity, HitInfo hit)
{
if (entity.OwnerID == 0 || hit.InitiatorPlayer == null) return;
BasePlayer player = BasePlayer.FindByID(entity.OwnerID);
if (player == null) return;
if (_config.BaseProtect)
{
if (DateTime.Now.Hour - 1 > _config.FirstTime || DateTime.Now.Hour - 1 < _config.SecoundTime)
{
hit.damageTypes.ScaleAll(0.50f);
}
}
}
private void Unload()
{
foreach (BasePlayer player in BasePlayer.activePlayerList)
{
CuiHelper.DestroyUi(player, "BaseProtector_UI");
}
}
#endregion
#region UI
void DrawGUI(BasePlayer player)
{
string Layer = "BaseProtector_UI";
CuiHelper.DestroyUi(player, Layer);
var container = new CuiElementContainer();
var Panel = container.Add(new CuiPanel
{
Image = { Color = $"0 0 0 0" },
RectTransform = { AnchorMin = "0.0320313 0.936111", AnchorMax = "0.2617188 1.006944" },
CursorEnabled = false,
}, "Hud", Layer);
container.Add(new CuiElement
{
Parent = Layer,
Components =
{
new CuiImageComponent { Color = "1 1 1 0.3", Sprite = "assets/icons/weapon.png" },
new CuiRectTransformComponent { AnchorMin = "1.19071002 0.2761437", AnchorMax = "1.2689413 0.7271238" }
}
});
container.Add(new CuiElement
{
Parent = Layer,
Components =
{
new CuiImageComponent { Color = "0 0 0 0", Sprite = "assets/icons/market.png" },
new CuiRectTransformComponent { AnchorMin = "0.1427508 0.2565358", AnchorMax = "0.2515944 0.7467315" }
}
});
container.Add(new CuiElement
{
Parent = Layer,
Components =
{
new CuiTextComponent() { Color = "1 1 1 0.3", Text = "УРОН ПОНИЖЕН НА 50%", FontSize = 16, Align = TextAnchor.MiddleCenter, Font = "robotocondensed-bold.ttf" },
new CuiRectTransformComponent { AnchorMin = "0.5976485 0.3529414", AnchorMax = "1.2183674 0.8235297" },
}
});
container.Add(new CuiElement
{
Parent = Layer,
Components =
{
new CuiTextComponent() { Color = "1 1 1 0.3", Text = "С 23.00 до 11.00 по МСК.", Font = "robotocondensed-bold.ttf" },
new CuiRectTransformComponent { AnchorMin = "0.6760275 0.07843163", AnchorMax = "1.2557822 0.5490193" },
}
});
CuiHelper.AddUi(player, container);
}
#endregion
#region Utils
private static string HexToRustFormat(string hex)
{
if (string.IsNullOrEmpty(hex))
{
hex = "#FFFFFFFF";
}
var str = hex.Trim('#');
if (str.Length == 6)
str += "FF";
if (str.Length != 8)
{
throw new Exception(hex);
throw new InvalidOperationException("Cannot convert a wrong format.");
}
var r = byte.Parse(str.Substring(0, 2), NumberStyles.HexNumber);
var g = byte.Parse(str.Substring(2, 2), NumberStyles.HexNumber);
var b = byte.Parse(str.Substring(4, 2), NumberStyles.HexNumber);
var a = byte.Parse(str.Substring(6, 2), NumberStyles.HexNumber);
Color color = new Color32(r, g, b, a);
return string.Format("{0:F2} {1:F2} {2:F2} {3:F2}", color.r, color.g, color.b, color.a);
}
#endregion
#region Configuration
private static Configuration _config = new Configuration();
public class Configuration
{
[JsonProperty(PropertyName = "Включить ли защиту?")]
public bool BaseProtect = true;
[JsonProperty(PropertyName = "Первое значение (например: защита с 22)")]
public int FirstTime = 23;
[JsonProperty(PropertyName = "Первое значение (например: защита до 10)")]
public int SecoundTime = 11;
}
protected override void LoadConfig()
{
base.LoadConfig();
try
{
_config = Config.ReadObject<Configuration>();
if (_config == null) throw new Exception();
}
catch
{
Config.WriteObject(_config, false, $"{Interface.Oxide.ConfigDirectory}/{Name}.jsonError");
PrintError("The configuration file contains an error and has been replaced with a default config.\n" +
"The error configuration file was saved in the .jsonError extension");
LoadDefaultConfig();
}
SaveConfig();
}
protected override void LoadDefaultConfig() => _config = new Configuration();
protected override void SaveConfig() => Config.WriteObject(_config);
#endregion
}
}