forked from PenisBlistashiq/Rust-plugins-236-240-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildTools.cs
More file actions
1740 lines (1400 loc) · 58.4 KB
/
Copy pathBuildTools.cs
File metadata and controls
1740 lines (1400 loc) · 58.4 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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Oxide.Core;
using Oxide.Core.Libraries.Covalence;
using Oxide.Core.Plugins;
using Oxide.Game.Rust.Cui;
using Rust;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("Build Tools", "Menevt", "1.4.0")]
public class BuildTools : RustPlugin
{
#region Fields
[PluginReference] private Plugin ImageLibrary, NoEscape, Clans, Friends, Notify, UINotify, RaidZone;
private const string Layer = "UI.BuildTools";
private static BuildTools _instance;
private enum Types
{
None = -1,
Remove = 5,
Wood = 1,
Stone = 2,
Metal = 3,
TopTier = 4
}
private const string PermAll = "buildtools.all";
private const string PermFree = "buildtools.free";
#endregion
#region Config
private static Configuration _config;
private class Configuration
{
[JsonProperty(PropertyName = "Remove Commands")]
public string[] RemoveCommands = { "remove" };
[JsonProperty(PropertyName = "Upgrade Commands")]
public string[] UpgradeCommands = { "up", "building.upgrade" };
[JsonProperty(PropertyName = "Work with Notify?")]
public bool UseNotify = true;
[JsonProperty(PropertyName = "Setting Modes", ObjectCreationHandling = ObjectCreationHandling.Replace)]
public List<Mode> Modes = new List<Mode>
{
new Mode
{
Type = Types.Remove,
Icon = "assets/icons/clear.png",
Permission = string.Empty
},
new Mode
{
Type = Types.Wood,
Icon = "assets/icons/level_wood.png",
Permission = string.Empty
},
new Mode
{
Type = Types.Stone,
Icon = "assets/icons/level_stone.png",
Permission = string.Empty
},
new Mode
{
Type = Types.Metal,
Icon = "assets/icons/level_metal.png",
Permission = string.Empty
},
new Mode
{
Type = Types.TopTier,
Icon = "assets/icons/level_top.png",
Permission = string.Empty
}
};
[JsonProperty(PropertyName = "Upgrade Settings")]
public UpgradeSettings Upgrade = new UpgradeSettings
{
ActionTime = 30,
Cooldown = 0,
VipCooldown = new Dictionary<string, int>
{
["buildtool.vip"] = 0,
["buildtool.premium"] = 0
},
AfterWipe = 0,
VipAfterWipe = new Dictionary<string, int>
{
["buildtool.vip"] = 0,
["buildtool.premium"] = 0
}
};
[JsonProperty(PropertyName = "Remove Settings")]
public RemoveSettings Remove = new RemoveSettings
{
ActionTime = 30,
Cooldown = 0,
VipCooldown = new Dictionary<string, int>
{
["buildtool.vip"] = 0,
["buildtool.premium"] = 0
},
AfterWipe = 0,
VipAfterWipe = new Dictionary<string, int>
{
["buildtool.vip"] = 0,
["buildtool.premium"] = 0
},
Condition = new ConditionSettings
{
Default = true,
Percent = false,
PercentValue = 0
},
ReturnItem = true,
ReturnPercent = 100,
BlockedList = new List<string>
{
"shortname 1",
"shortname 2",
"shortname 3"
}
};
[JsonProperty(PropertyName = "Block Settings")]
public BlockSettings Block = new BlockSettings
{
UseNoEscape = true,
UseClans = true,
UseFriends = true,
UseCupboard = true
};
[JsonProperty(PropertyName = "Additional Slot Settings")]
public AdditionalSlot AdditionalSlot = new AdditionalSlot
{
Enabled = true
};
[JsonProperty(PropertyName = "UI Settings")]
public InterfaceSettings UI = new InterfaceSettings
{
Color1 = new IColor("#4B68FF"),
Color2 = new IColor("#2C2C2C"),
Color3 = new IColor("#B64040"),
OffsetY = 0,
OffsetX = 0
};
}
private class AdditionalSlot
{
[JsonProperty(PropertyName = "Enabled")]
public bool Enabled;
public static void Get(BasePlayer player)
{
var item = ItemManager.CreateByName("hammer");
if (item == null) return;
if (player.inventory.containerBelt.capacity < 7)
player.inventory.containerBelt.capacity++;
item.LockUnlock(true);
item.MoveToContainer(player.inventory.containerBelt, 6);
}
public static void Remove(BasePlayer player)
{
var item = player.inventory.containerBelt.GetSlot(6);
if (item == null || item.info.shortname != "hammer") return;
item.RemoveFromContainer();
item.Remove();
ItemManager.DoRemoves();
player.inventory.containerBelt.capacity--;
}
}
private class InterfaceSettings
{
[JsonProperty(PropertyName = "Color 1")]
public IColor Color1;
[JsonProperty(PropertyName = "Color 2")]
public IColor Color2;
[JsonProperty(PropertyName = "Color 3")]
public IColor Color3;
[JsonProperty(PropertyName = "Offset Y")]
public float OffsetY;
[JsonProperty(PropertyName = "Offset X")]
public float OffsetX;
}
private class IColor
{
[JsonProperty(PropertyName = "HEX")] public string Hex;
[JsonProperty(PropertyName = "Opacity (0 - 100)")]
public float Alpha;
[JsonProperty] private string _color;
[JsonIgnore]
public string Get
{
get
{
if (string.IsNullOrEmpty(_color))
_color = GetColor();
return _color;
}
}
private string GetColor()
{
if (string.IsNullOrEmpty(Hex)) Hex = "#FFFFFF";
var str = Hex.Trim('#');
if (str.Length != 6) throw new Exception(Hex);
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);
return $"{(double)r / 255} {(double)g / 255} {(double)b / 255} {Alpha / 100}";
}
public IColor()
{
}
public IColor(string hex, float alpha = 100)
{
Hex = hex;
Alpha = alpha;
}
}
private class ConditionSettings
{
[JsonProperty(PropertyName = "Default (from game)")]
public bool Default;
[JsonProperty(PropertyName = "Use percent?")]
public bool Percent;
[JsonProperty(PropertyName = "Percent (value)")]
public float PercentValue;
}
private class BlockSettings
{
[JsonProperty(PropertyName = "Work with NoEscape?")]
public bool UseNoEscape;
[JsonProperty(PropertyName = "Work with Clans? (clan members will be able to delete/upgrade)")]
public bool UseClans;
[JsonProperty(PropertyName = "Work with Friends? (friends will be able to delete/upgrade)")]
public bool UseFriends;
[JsonProperty(PropertyName = "Can those authorized in the cupboard delete/upgrade?")]
public bool UseCupboard;
[JsonProperty(PropertyName = "Is an upgrade/remove cupbaord required?")]
public bool NeedCupboard;
}
private abstract class TotalSettings
{
[JsonProperty(PropertyName = "Time of action")]
public int ActionTime;
[JsonProperty(PropertyName = "Cooldown (default | 0 - disable)")]
public int Cooldown;
[JsonProperty(PropertyName = "Cooldowns", ObjectCreationHandling = ObjectCreationHandling.Replace)]
public Dictionary<string, int> VipCooldown;
[JsonProperty(PropertyName = "Block After Wipe (default | 0 - disable)")]
public int AfterWipe;
[JsonProperty(PropertyName = "Block After Wipe", ObjectCreationHandling = ObjectCreationHandling.Replace)]
public Dictionary<string, int> VipAfterWipe;
public int GetCooldown(BasePlayer player)
{
return (from check in VipCooldown
where player.IPlayer.HasPermission(check.Key)
select check.Value).Prepend(Cooldown).Min();
}
public int GetWipeCooldown(BasePlayer player)
{
return (from check in VipAfterWipe
where player.IPlayer.HasPermission(check.Key)
select check.Value).Prepend(AfterWipe).Min();
}
}
private class UpgradeSettings : TotalSettings
{
}
private class RemoveSettings : TotalSettings
{
[JsonProperty(PropertyName = "Blocked items to remove (prefab)",
ObjectCreationHandling = ObjectCreationHandling.Replace)]
public List<string> BlockedList;
[JsonProperty(PropertyName = "Return Item")]
public bool ReturnItem;
[JsonProperty(PropertyName = "Returnable Item Percentage")]
public float ReturnPercent;
[JsonProperty(PropertyName = "Can friends remove? (Friends)")]
public bool CanFriends;
[JsonProperty(PropertyName = "Can clanmates remove? (Clans)")]
public bool CanClan;
[JsonProperty(PropertyName = "Can teammates remove?")]
public bool CanTeams;
[JsonProperty(PropertyName = "Require a cupboard")]
public bool RequireCupboard;
[JsonProperty(PropertyName = "Remove by cupboard? (those who are authorized in the cupboard can remove)")]
public bool RemoveByCupboard;
[JsonProperty(PropertyName = "Condition Settings")]
public ConditionSettings Condition;
}
private class Mode
{
[JsonProperty(PropertyName = "Icon (assets/url)")]
public string Icon;
[JsonProperty(PropertyName = "Type (Remove/Wood/Stone/Metal/TopTier)")]
[JsonConverter(typeof(StringEnumConverter))]
public Types Type;
[JsonProperty(PropertyName = "Permission (ex: buildtools.1)")]
public string Permission;
}
protected override void LoadConfig()
{
base.LoadConfig();
try
{
_config = Config.ReadObject<Configuration>();
if (_config == null) throw new Exception();
SaveConfig();
}
catch (Exception ex)
{
PrintError("Your configuration file contains an error. Using default configuration values.");
LoadDefaultConfig();
Debug.LogException(ex);
}
}
protected override void SaveConfig()
{
Config.WriteObject(_config);
}
protected override void LoadDefaultConfig()
{
_config = new Configuration();
}
#endregion
#region Data
private PluginData _data;
private void SaveData()
{
Interface.Oxide.DataFileSystem.WriteObject(Name, _data);
}
private void LoadData()
{
try
{
_data = Interface.Oxide.DataFileSystem.ReadObject<PluginData>(Name);
}
catch (Exception e)
{
PrintError(e.ToString());
}
if (_data == null) _data = new PluginData();
}
private class PluginData
{
[JsonProperty(PropertyName = "Players", ObjectCreationHandling = ObjectCreationHandling.Replace)]
public Dictionary<ulong, PlayerData> Players = new Dictionary<ulong, PlayerData>();
}
private class PlayerData
{
[JsonProperty(PropertyName = "Last Upgrade")]
public DateTime LastUpgrade = new DateTime(1970, 1, 1, 0, 0, 0);
[JsonProperty(PropertyName = "Last Remove")]
public DateTime LastRemove = new DateTime(1970, 1, 1, 0, 0, 0);
public int LeftTime(bool remove, int cooldown)
{
var time = remove
? LastRemove
: LastUpgrade;
return (int)time.AddSeconds(cooldown).Subtract(DateTime.UtcNow).TotalSeconds;
}
public bool HasCooldown(bool remove, int cooldown)
{
var time = remove
? LastRemove
: LastUpgrade;
return DateTime.UtcNow.Subtract(time).TotalSeconds < cooldown;
}
public static bool HasWipeCooldown(int cooldown)
{
return DateTime.UtcNow.Subtract(SaveRestore.SaveCreatedTime.ToUniversalTime()).TotalSeconds < cooldown;
}
public static int WipeLeftTime(int cooldown)
{
return (int)SaveRestore.SaveCreatedTime.ToUniversalTime().AddSeconds(cooldown).Subtract(DateTime.UtcNow)
.TotalSeconds;
}
}
private PlayerData GetPlayerData(ulong userId)
{
PlayerData playerData;
if (!_data.Players.TryGetValue(userId, out playerData))
_data.Players.Add(userId, playerData = new PlayerData());
return playerData;
}
#endregion
#region Hooks
private void Init()
{
_instance = this;
LoadData();
RegisterPermissions();
AddCovalenceCommand(_config.UpgradeCommands, nameof(CmdUpgrade));
AddCovalenceCommand(_config.RemoveCommands, nameof(CmdRemove));
if (!_config.AdditionalSlot.Enabled)
{
Unsubscribe(nameof(OnPlayerConnected));
Unsubscribe(nameof(OnPlayerDisconnected));
Unsubscribe(nameof(OnPlayerDeath));
Unsubscribe(nameof(OnActiveItemChanged));
}
}
private void OnServerInitialized()
{
LoadImages();
if (_config.AdditionalSlot.Enabled)
foreach (var player in BasePlayer.activePlayerList)
OnPlayerConnected(player);
}
private void Unload()
{
foreach (var player in BasePlayer.activePlayerList)
{
CuiHelper.DestroyUi(player, Layer);
if (_config.AdditionalSlot.Enabled)
OnPlayerDisconnected(player, string.Empty);
}
Array.ForEach(_components.Values.ToArray(), build =>
{
if (build != null)
build.Kill();
});
SaveData();
_config = null;
_instance = null;
}
void OnPlayerRespawned(BasePlayer player)
{
if (player == null || player.IsNpc) return;
AdditionalSlot.Get(player);
}
private void OnPlayerConnected(BasePlayer player)
{
if (player == null || player.IsNpc) return;
AdditionalSlot.Get(player);
}
private void OnPlayerDisconnected(BasePlayer player, string reason)
{
if (player == null || player.IsNpc) return;
AdditionalSlot.Remove(player);
}
private void OnPlayerDeath(BasePlayer player, HitInfo info)
{
if (player == null || player.IsNpc) return;
AdditionalSlot.Remove(player);
}
private void OnActiveItemChanged(BasePlayer player, Item oldItem, Item newItem)
{
if (player == null || player.IsNpc) return;
if (oldItem != null && oldItem.position == 6)
{
var build = GetBuild(player);
if (build != null && build.activeByItem)
build.Kill();
return;
}
if (newItem != null && newItem.position == 6)
{
if (GetBuild(player) != null) return;
AddOrGetBuild(player, true).GoNext();
}
}
private object OnHammerHit(BasePlayer player, HitInfo info)
{
if (player == null || info == null) return null;
var entity = info.HitEntity as BaseCombatEntity;
if (entity == null || entity.OwnerID == 0) return null;
var build = GetBuild(player);
if (build == null) return null;
var mode = build.GetMode();
if (mode == null) return null;
if (!player.CanBuild())
{
SendNotify(player, BuildingBlocked, 1);
return true;
}
if (_config.Block.UseNoEscape && NoEscape != null && NoEscape.IsLoaded && IsRaidBlocked(player))
{
SendNotify(player, mode.Type == Types.Remove ? RemoveRaidBlocked : UpgradeRaidBlocked, 1);
return true;
}
var cupboard = entity.GetBuildingPrivilege();
if (cupboard == null && _config.Block.NeedCupboard)
{
SendNotify(player, CupboardRequired, 1);
return true;
}
if (entity.OwnerID != player.userID) //NOT OWNER
{
var any =
_config.Block.UseFriends && Friends != null && Friends.IsLoaded &&
IsFriends(player.OwnerID, entity.OwnerID) ||
_config.Block.UseClans && Clans != null && Clans.IsLoaded &&
IsClanMember(player.OwnerID, entity.OwnerID) ||
_config.Block.UseCupboard && (cupboard == null || cupboard.IsAuthed(player));
if (!any)
{
SendNotify(player, mode.Type == Types.Remove ? CantRemove : CantUpgrade, 1);
return true;
}
}
if (mode.Type == Types.Remove)
{
if (_config.Remove.BlockedList.Contains(entity.name))
{
SendNotify(player, mode.Type == Types.Remove ? CantRemove : CantUpgrade, 1);
return true;
}
}
else
{
var block = entity as BuildingBlock;
if (block != null && (int)block.grade >= (int)mode.Type) return true;
}
build.DoIt(entity);
return true;
}
private void OnEntityBuilt(Planner plan, GameObject go)
{
var player = plan.GetOwnerPlayer();
if (player == null) return;
var block = go.ToBaseEntity() as BuildingBlock;
if (block == null) return;
var build = GetBuild(player);
if (build == null) return;
var mode = build.GetMode();
if (mode == null || mode.Type == Types.Remove) return;
build.DoIt(block);
}
#endregion
#region Commands
private void CmdRemove(IPlayer cov, string command, string[] args)
{
var player = cov.Object as BasePlayer;
if (player == null) return;
var mode = _config.Modes.Find(x => x.Type == Types.Remove);
if (mode == null || !string.IsNullOrEmpty(mode.Permission) && !cov.HasPermission(mode.Permission))
{
SendNotify(player, NoPermission, 1);
return;
}
if (args.Length > 0 && args[0] == "all")
{
if (!cov.HasPermission(PermAll))
{
SendNotify(player, NoPermission, 1);
return;
}
var cupboard = player.GetBuildingPrivilege();
if (cupboard == null)
{
SendNotify(player, NoCupboard, 1);
return;
}
var data = GetPlayerData(player.userID);
var cooldown = _config.Remove.GetCooldown(player);
if (cooldown > 0 && data.HasCooldown(false, cooldown))
{
SendNotify(player, RemoveCanThrough, 1,
data.LeftTime(false, cooldown));
return;
}
var blockWipe = _config.Remove.GetWipeCooldown(player);
if (blockWipe > 0 && PlayerData.HasWipeCooldown(blockWipe))
{
SendNotify(player, RemoveCanThrough, 1,
PlayerData.WipeLeftTime(blockWipe));
return;
}
var entities = BaseNetworkable.serverEntities
.OfType<BaseCombatEntity>()
.Where(x => !(x is BasePlayer) && x.GetBuildingPrivilege() == cupboard)
.ToList();
if (entities.Count == 0 || entities.Any(x => !CanRemove(player, x)))
return;
Global.Runner.StartCoroutine(StartRemove(player, entities));
SendNotify(player, SuccessfullyUpgrade, 0);
return;
}
AddOrGetBuild(player).Init(mode);
}
private void CmdUpgrade(IPlayer cov, string command, string[] args)
{
var player = cov.Object as BasePlayer;
if (player == null) return;
if (args.Length == 0)
{
AddOrGetBuild(player).GoNext();
return;
}
switch (args[0])
{
case "all":
{
if (!cov.HasPermission(PermAll))
{
SendNotify(player, NoPermission, 1);
return;
}
Types upgradeType;
if (args.Length < 2 || ParseType(args[1], out upgradeType) == Types.None)
{
cov.Reply($"Error syntax! Use: /{command} {args[0]} [wood/stone/metal/toptier]");
return;
}
var cupboard = player.GetBuildingPrivilege();
if (cupboard == null)
{
SendNotify(player, NoCupboard, 1);
return;
}
if (!player.CanBuild())
{
SendNotify(player, BuildingBlocked, 1);
return;
}
if (_config.Block.UseNoEscape && NoEscape != null && NoEscape.IsLoaded && IsRaidBlocked(player))
{
SendNotify(player, UpgradeRaidBlocked, 1);
return;
}
var data = GetPlayerData(player.userID);
var cooldown = _config.Upgrade.GetCooldown(player);
if (cooldown > 0 && data.HasCooldown(false, cooldown))
{
SendNotify(player, UpgradeCanThrough, 1,
data.LeftTime(false, cooldown));
return;
}
var blockWipe = _config.Upgrade.GetWipeCooldown(player);
if (blockWipe > 0 && PlayerData.HasWipeCooldown(blockWipe))
{
SendNotify(player, UpgradeCanThrough, 1,
PlayerData.WipeLeftTime(blockWipe));
return;
}
var grade = GetEnum(upgradeType);
var buildingBlocks = BaseNetworkable.serverEntities
.OfType<BuildingBlock>()
.Where(x =>
x.GetBuildingPrivilege() == cupboard &&
x.grade <= grade &&
x.CanChangeToGrade(grade, player))
.ToList();
if (buildingBlocks.Count == 0) return;
if (!cov.HasPermission(PermFree))
{
if (!CanAffordUpgrade(buildingBlocks, grade, player))
{
SendNotify(player, NotEnoughResources, 1);
return;
}
PayForUpgrade(buildingBlocks, grade, player);
}
Global.Runner.StartCoroutine(StartUpgrade(player, buildingBlocks, grade));
SendNotify(player, SuccessfullyUpgrade, 0);
break;
}
default:
{
Types type;
if (ParseType(args[0], out type) != Types.None)
{
var modes = GetPlayerModes(player);
if (modes == null) return;
var mode = modes.Find(x => x.Type == type);
if (mode == null || !string.IsNullOrEmpty(mode.Permission) &&
!cov.HasPermission(mode.Permission))
{
SendNotify(player, NoPermission, 1);
return;
}
var build = AddOrGetBuild(player);
build.Init(mode);
}
else
{
AddOrGetBuild(player).GoNext();
}
break;
}
}
}
[ConsoleCommand("UI_Builder")]
private void CmdConsoleBuilding(ConsoleSystem.Arg arg)
{
var player = arg.Player();
if (player == null || !arg.HasArgs()) return;
switch (arg.Args[0])
{
case "mode":
{
int index;
if (!arg.HasArgs(2) || !int.TryParse(arg.Args[1], out index)) return;
var mode = GetPlayerModes(player)[index];
if (mode == null) return;
AddOrGetBuild(player)?.Init(mode);
break;
}
case "close":
{
GetBuild(player)?.Kill();
break;
}
}
}
#endregion
#region Component
private readonly Dictionary<BasePlayer, BuildComponent> _components =
new Dictionary<BasePlayer, BuildComponent>();
private BuildComponent GetBuild(BasePlayer player)
{
BuildComponent build;
return _components.TryGetValue(player, out build) ? build : null;
}
private BuildComponent AddOrGetBuild(BasePlayer player, bool item = false)
{
BuildComponent build;
if (_components.TryGetValue(player, out build))
return build;
build = player.gameObject.AddComponent<BuildComponent>();
build.activeByItem = item;
return build;
}
private class BuildComponent : FacepunchBehaviour
{
#region Fields
private BasePlayer _player;
private Mode _mode;
private float _startTime;
private readonly CuiElementContainer _container = new CuiElementContainer();
private bool _started = true;
private float _cooldown;
public bool activeByItem;
#endregion
#region Init
private void Awake()
{
_player = GetComponent<BasePlayer>();
_instance._components[_player] = this;
enabled = false;
}
public void Init(Mode mode)
{
if (mode == null)
mode = GetPlayerModes(_player).FirstOrDefault();
_mode = mode;
_startTime = Time.time;
_cooldown = GetCooldown();
MainUi();
enabled = true;
_started = true;
}
#endregion
#region Interface
public void MainUi()
{
_container.Clear();
_container.Add(new CuiPanel
{
RectTransform = { AnchorMin = "0 0", AnchorMax = "0 0" },
Image = { Color = "0 0 0 0" }
}, "Overlay", Layer);
#region Modes
var modes = GetPlayerModes(_player);
var width = 30f;
var margin = 5f;
var xSwitch = 15f + _config.UI.OffsetX;
for (var i = 0; i < modes.Count; i++)
{
var mode = modes[i];
_container.Add(new CuiPanel
{
RectTransform =
{
AnchorMin = "0 0", AnchorMax = "1 1",
OffsetMin = $"{xSwitch} {15 + _config.UI.OffsetY}",
OffsetMax = $"{xSwitch + width} {45 + _config.UI.OffsetY}"
},
Image =
{
Color = mode.Type == _mode.Type ? _config.UI.Color1.Get : _config.UI.Color2.Get
}
}, Layer, Layer + $".Mode.{i}");
#region Icon
if (mode.Icon.Contains("assets/icon"))
_container.Add(new CuiPanel
{
RectTransform =
{
AnchorMin = "0 0", AnchorMax = "1 1",