-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRefreshAnimals.cs
More file actions
172 lines (140 loc) · 4.84 KB
/
Copy pathRefreshAnimals.cs
File metadata and controls
172 lines (140 loc) · 4.84 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
using Oxide.Core.Libraries.Covalence;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Oxide.Plugins
{
[Info("Refresh Animals", "moochannel", "1.0.0")]
[Description("Kill animals to respawn frequently")]
class RefreshAnimals : CovalencePlugin
{
#region Configuration
bool PurgeOnLogin => GetConfig("PurgeOnLogin", true);
protected override void LoadDefaultConfig()
{
Config["PurgeOnLogin"] = PurgeOnLogin;
SaveConfig();
}
#endregion
#region Localization
protected override void LoadDefaultMessages()
{
lang.RegisterMessages(new Dictionary<string, string>
{
{"AdminOnly", "Auth level 2 required."},
{"AdminAndModerator", "Auth level 1 or greater required."},
}, this);
lang.RegisterMessages(new Dictionary<string, string>
{
{"AdminOnly", "管理者権限が必要です"},
{"AdminOrModerator", "管理者かモデレータ権限が必要です"},
}, this, "ja");
}
#endregion
#region Initialization
void Loaded()
{
#if !RUST
throw new NotSupportedException($"This plugin does not support {(covalence.Game ?? "this game")}");
#endif
LoadDefaultConfig();
LoadDefaultMessages();
}
#endregion
#region Refresh Animals On Logging in
void OnPlayerInit(BasePlayer player)
{
if (PurgeOnLogin)
PurgeAnimals();
}
#endregion
#region Console Commands
[Command("refreshanimals.list")]
void AnimalsListCommand(IPlayer player, string command, string[] args)
{
if (args.Length != 0)
{
Puts("usage: refreshanimals.list");
return;
}
if (!player.IsAdmin)
{
Puts(GetMessage("AdminOnly", null));
return;
}
var animals = GetAllAnimals();
Puts($"There are {animals.Count} animals.");
foreach (var animal in animals)
{
Puts($"{animal.GetType()}: stopped?={animal.IsStopped}, nav_running?={animal.IsNavRunning()}");
}
}
[Command("refreshanimals.stats")]
void AnimalsStatsCommand(IPlayer player, string command, string[] args)
{
if (args.Length != 0)
{
Puts("usage: refreshanimals.stats");
return;
}
if (!player.IsAdmin)
{
Puts(GetMessage("AdminOnly", null));
return;
}
var animals = GetAllAnimals();
var animalGroup = animals.Select(rec => new { AnimalType = rec.GetType() })
.GroupBy(rec => rec.AnimalType )
.Select(rec => new { AnimalType = rec.Key, Amount = rec.Count() })
.OrderBy(rec => rec.AnimalType.ToString());
Puts($"There are {animals.Count} animals.");
foreach (var stat in animalGroup)
{
Puts($"{stat.AnimalType}: {stat.Amount}");
}
}
[Command("refreshanimals.purge")]
void AnimalsPurgeCommand(IPlayer player, string command, string[] args)
{
if (args.Length != 0)
{
Puts("usage: refreshanimals.purge");
return;
}
if (!player.IsAdmin)
{
Puts(GetMessage("AdminOnly", null));
return;
}
PurgeAnimals();
}
#endregion
#region Animal Control Methods
private List<BaseAnimalNPC> GetAllAnimals()
{
var animals = new List<BaseAnimalNPC>(BaseAnimalNPC.FindObjectsOfType<BaseAnimalNPC>());
return animals;
}
private void PurgeAnimals()
{
var animals = GetAllAnimals();
var purged = 0;
var willPurgedAnimals = animals.Where(rec => rec.IsStopped && !rec.IsNavRunning());
foreach (var animal in willPurgedAnimals)
{
animal.DieInstantly();
purged++;
}
Puts($"{purged} animal(s) purged. (before: {animals.Count})");
}
#endregion
#region Helper Methods
private T GetConfig<T>(string name, T defaultValue)
{
if (Config[name] == null) return defaultValue;
return (T)Convert.ChangeType(Config[name], typeof(T));
}
private string GetMessage(string key, string steamId = null) => lang.GetMessage(key, this, steamId);
#endregion
}
}