forked from bennvh/Rust-Plugin-Fixes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pigrels.cs
469 lines (419 loc) · 16.4 KB
/
Pigrels.cs
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
using Oxide.Core;
using System;
using System.Text;
using UnityEngine;
using System.Linq;
using System.Collections.Generic;
using UnityEngine;
using Oxide.Core.Plugins;
using System.Reflection;
using System.Text;
using Newtonsoft.Json;
using Oxide.Core.Libraries.Covalence;
using System.Diagnostics;
namespace Oxide.Plugins
{
[Info("Pigrels", "ichaleynbin, mspeedie", "2.0.1", ResourceId = 0)]
[Description("Random chance of animal spawning when a barrel breaks")]
class Pigrels : CovalencePlugin
{
private System.Random random = new System.Random();
private int SumChances = 0;
private ConfigDats configData;
private string pig_string = "assets/rust.ai/agents/boar/boar.prefab";
private string chicken_string = "assets/rust.ai/agents/chicken/chicken.prefab";
private string horse_string = "assets/rust.ai/agents/horse/horse.prefab";
private string stag_string = "assets/rust.ai/agents/stag/stag.prefab";
private string bear_string = "assets/rust.ai/agents/bear/bear.prefab";
private string wolf_string = "assets/rust.ai/agents/wolf/wolf.prefab";
private string zombie_string = "assets/rust.ai/agents/zombie/zombie.prefab";
private readonly string permAdmin = "pigrels.admin";
private string Lang(string key, string id = null, params object[] args) => string.Format(lang.GetMessage(key, this, id), args);
private Dictionary<string, string> messages = new Dictionary<string,string>();
class ConfigDats
{
public int ChanceOfSpawn { get; set; }
public int ChanceOfPig { get; set; }
public int ChanceOfChicken { get; set; }
public int ChanceOfHorse { get; set; }
public int ChanceOfStag { get; set; }
public int ChanceOfBear { get; set; }
public int ChanceOfWolf { get; set; }
public int ChanceOfZombie { get; set; }
public int AirDropSurprise { get; set; }
public int MinSurprise { get; set; }
public int MaxSurprise { get; set; }
}
bool IsAllowed(IPlayer player)
{
return player != null && (player.IsAdmin || player.HasPermission(permAdmin));
}
void SpawnSpecific(Vector3 pos, string whatanimal)
{
BaseEntity Surprise = GameManager.server.CreateEntity(whatanimal, pos, new Quaternion(), true);
Surprise.Spawn();
return;
}
void OnEntitySpawned(BaseNetworkable ent)
{
if (ent && ent is Zombie)
{
(ent as BaseEntity).Kill();
}
}
void SpawnSurprise(Vector3 pos)
{
System.Random rndnum = new System.Random();
System.Random rnd = new System.Random();
int num_animal = rndnum.Next(configData.MinSurprise, configData.MaxSurprise);
if (num_animal < 1)
num_animal = 1;
for (int i = 0; i < num_animal; i++)
{
int find_animal = rnd.Next(1, SumChances);
string supriseanimal = null;
if (configData.ChanceOfPig > 0 && find_animal <= configData.ChanceOfPig)
supriseanimal = pig_string;
else if (configData.ChanceOfChicken > 0 && find_animal <= configData.ChanceOfPig + configData.ChanceOfChicken)
supriseanimal = chicken_string;
else if (configData.ChanceOfHorse > 0 && find_animal <= configData.ChanceOfPig + configData.ChanceOfChicken + configData.ChanceOfHorse)
supriseanimal = horse_string;
else if (configData.ChanceOfStag > 0 && find_animal <= configData.ChanceOfPig + configData.ChanceOfChicken + configData.ChanceOfHorse + configData.ChanceOfStag)
supriseanimal = stag_string;
else if (configData.ChanceOfBear > 0 && find_animal <= configData.ChanceOfPig + configData.ChanceOfChicken + configData.ChanceOfHorse + configData.ChanceOfStag + configData.ChanceOfBear)
supriseanimal = bear_string;
else if (configData.ChanceOfWolf > 0 && find_animal <= configData.ChanceOfPig + configData.ChanceOfChicken + configData.ChanceOfHorse + configData.ChanceOfStag + configData.ChanceOfBear + configData.ChanceOfWolf)
supriseanimal = wolf_string;
else if (configData.ChanceOfZombie > 0)
supriseanimal = zombie_string;
else
{
Puts(string.Format(lang.GetMessage("nochances", this)));
supriseanimal = pig_string;
}
SpawnSpecific(pos, supriseanimal);
}
return;
}
void OnEntityDeath(BaseCombatEntity entity, HitInfo info)
{
if (entity.name.Contains("barrel") && (random.Next(1, 100)<= configData.ChanceOfSpawn))
{
SpawnSurprise(entity.transform.position);
var player = info.Initiator?.ToPlayer();
if (player != null)
{
var Iplayer = players.FindPlayerById(player.UserIDString);
if (Iplayer != null)
Iplayer.Reply(Lang("Surprise", Iplayer.Id));
}
}
return;
}
void OnAirdrop(CargoPlane plane, Vector3 dropPosition)
{
if (configData.AirDropSurprise == null || configData.AirDropSurprise < 1)
return;
if (random.Next(1, 100)<= configData.AirDropSurprise)
{
SpawnSurprise(dropPosition);
}
}
// might not need these two
void OnExplosiveThrown(BasePlayer player, BaseEntity entity)
{
if (entity == null || !(entity is SupplySignal)) return;
if (entity.net == null)
entity.net = Network.Net.sv.CreateNetworkable();
if (configData.AirDropSurprise == null || configData.AirDropSurprise < 1)
return;
if (random.Next(1, 100)<= configData.AirDropSurprise)
{
Vector3 playerposition = player.transform.position;
SpawnSurprise(playerposition);
}
}
void OnExplosiveDropped(BasePlayer player, BaseEntity entity)
{
if (entity == null || !(entity is SupplySignal)) return;
if (configData.AirDropSurprise == null || configData.AirDropSurprise < 1)
return;
if (random.Next(1, 100)<= configData.AirDropSurprise)
{
Vector3 playerposition = player.transform.position;
SpawnSurprise(playerposition);
}
}
protected override void LoadDefaultConfig()
{
configData = new ConfigDats
{
ChanceOfSpawn = 5
, ChanceOfPig = 1
, ChanceOfChicken = 1
, ChanceOfHorse = 1
, ChanceOfStag = 1
, ChanceOfBear = 1
, ChanceOfWolf = 1
, ChanceOfZombie = 1
, AirDropSurprise = 0
, MinSurprise = 1
, MaxSurprise = 1
};
SumChances = configData.ChanceOfPig + configData.ChanceOfChicken + configData.ChanceOfHorse +
configData.ChanceOfStag + configData.ChanceOfBear + configData.ChanceOfWolf + configData.ChanceOfZombie;
Config.WriteObject(configData,true);
}
[ChatCommand("pigrels")]
void SetPigrel(IPlayer player, string command, string[] args)
{
if (!IsAllowed(player))
{
player.Reply(Lang("Noperms", player.Id));
return;
}
if (args.Count() == 2)
{
int chance;
try
{
chance = Convert.ToInt32(args[1]);
}
catch
{
player.Reply(Lang("FailChange", player.Id));
return;
}
if (chance >100)
chance = 100;
else if (chance < 0)
chance = 0;
if (args[0].ToLower() == "spawn")
configData.ChanceOfSpawn = chance;
else if (args[0].ToLower() == "pig")
configData.ChanceOfPig = chance;
else if (args[0].ToLower() == "chicken")
configData.ChanceOfChicken = chance;
else if (args[0].ToLower() == "horse")
configData.ChanceOfHorse = chance;
else if (args[0].ToLower() == "stag")
configData.ChanceOfStag = chance;
else if (args[0].ToLower() == "bear")
configData.ChanceOfBear = chance;
else if (args[0].ToLower() == "wolf")
configData.ChanceOfWolf = chance;
else if (args[0].ToLower() == "zombie")
configData.ChanceOfZombie = chance;
else if (args[0].ToLower() == "airdrop")
configData.AirDropSurprise = chance;
else if (args[0].ToLower() == "min")
{
if (chance < 1) // has to be > 0
{
chance = 1;
player.Reply(Lang("mustbegt1", player.Id, args[0], chance.ToString()));
}
if (chance > configData.MaxSurprise) // min must be <= max
{
player.Reply(Lang("badmin", player.Id, args[0], chance.ToString()));
}
else
configData.MinSurprise = chance;
}
else if (args[0].ToLower() == "max") // has to be > 0
{
if (chance < 1)
{
chance = 1;
player.Reply(Lang("mustbegt1", player.Id, args[0], chance.ToString()));
}
if (chance < configData.MinSurprise) // max must be >= min
{
player.Reply(Lang("badmax", player.Id, args[0], chance.ToString()));
}
else
configData.MaxSurprise = chance;
}
SumChances = configData.ChanceOfPig + configData.ChanceOfChicken + configData.ChanceOfHorse +
configData.ChanceOfStag + configData.ChanceOfBear + configData.ChanceOfWolf + configData.ChanceOfZombie;
player.Reply(Lang("Changing", player.Id, args[0], chance.ToString()));
Config.WriteObject(configData);
}
}
private BasePlayer FindPlayerByPartialName(string name)
{
if (string.IsNullOrEmpty(name)) return null;
var players = covalence.Players.Connected.ToList();
int playerCount = players.Count;
BasePlayer player = null;
string lower_name = name.ToLower().Trim();
if (lower_name == null || playerCount == null && playerCount > 0)
return null;
else
{
for (int i = 0; i < playerCount; i++)
{
//Puts(i.ToString());
var icplayer = players[i];
var cplayer = icplayer.Object as BasePlayer;
if (cplayer.displayName.ToLower().Contains(lower_name) == true || cplayer.displayName.ToLower().Trim() == lower_name)
{
if (player != null) // too many players match
{
Puts(string.Format(lang.GetMessage("toomanyplayer", this)),name);
return null;
}
else
{
player = cplayer;
}
}
}
}
return player;
}
private BasePlayer FindPlayerByID(ulong userID) { return BasePlayer.FindByID(userID) ?? BasePlayer.FindSleeping(userID) ?? null; }
[Command("surpriseplayer", "sp")]
void SurprisePlayer(IPlayer player, string command, string[] args)
{
Puts("SurprisePlayer");
if (!IsAllowed(player))
{
player.Reply(Lang("Noperms", player.Id));
return;
}
if (args == null || args.Length < 1)
{
Puts(string.Format(lang.GetMessage("noplayer", this)));
return;
}
ulong ID = 0ul;
BasePlayer target = null;
// Puts("args[0]" + args[0]);
if (ID != null)
try
{
ulong.TryParse(args[0], out ID);
target = FindPlayerByID(ID);
}
catch
{
target = null;
}
if (target == null)
try
{
target = FindPlayerByPartialName(args[0]);
}
catch
{
target = null;
}
if (target == null)
{
Puts(string.Format(lang.GetMessage("playerNotFound", this),args[0]));
return;
}
var pos = target?.transform?.position ?? Vector3.zero;
if (pos == null)
{
Puts(string.Format(lang.GetMessage("playerPosNotFound", this),args[0]));
return;
}
if ( args.Length < 2)
{
Puts(string.Format(lang.GetMessage("randomanimal", this)));
SpawnSurprise(pos);
return;
}
else
{
//Puts ("args[1]" + args[1]);
var check_name = args[1].Trim().ToLower();
//Puts("check_name: " + check_name);
if (check_name == null)
{
Puts(string.Format(lang.GetMessage("randomanimal", this)));
SpawnSurprise(pos);
return;
}
else if (check_name == "boar" || check_name == "pig" || check_name == "schwein" || check_name == "porco" || check_name == "porc" || check_name == "cerdo" ||
check_name == "puerco" || check_name == "sanglier" || check_name == "jabali" || check_name == "javali" || check_name == "eber" || check_name == "porky" || check_name == "swine")
{
Puts(string.Format(lang.GetMessage("specificanimal", this),check_name));
SpawnSpecific(pos, pig_string);
}
else if (check_name == "chicken" || check_name == "rooster" || check_name == "kip" || check_name == "poulet" || check_name == "frango" || check_name == "pollo" ||
check_name == "hähnchen" || check_name == "hahn" || check_name == "gallo" || check_name == "galo" || check_name == "coq" || check_name == "cock")
{
Puts(string.Format(lang.GetMessage("specificanimal", this),check_name));
SpawnSpecific(pos, chicken_string);
}
else if (check_name == "horse" || check_name == "cheval" || check_name == "pfred" || check_name == "cavalo" || check_name == "caballo")
{
Puts(string.Format(lang.GetMessage("specificanimal", this),check_name));
SpawnSpecific(pos, horse_string);
}
else if (check_name == "stag" || check_name == "deer" || check_name == "bambi" || check_name == "ciervo" || check_name == "veado" || check_name == "cerf" || check_name == "hirsch")
{
Puts(string.Format(lang.GetMessage("specificanimal", this),check_name));
SpawnSpecific(pos, stag_string);
}
else if (check_name == "wolf" || check_name == "dog" || check_name == "doggo" || check_name == "lobo" || check_name == "loup" ||
check_name == "wilk" || check_name == "ulv" || check_name == "perro" || check_name == "hund" || check_name == "cachorro" || check_name == "chien")
{
Puts(string.Format(lang.GetMessage("specificanimal", this),check_name));
SpawnSpecific(pos, wolf_string);
}
else if (check_name == "bear" || check_name == "yogi" || check_name == "oso" || check_name == "ours" || check_name == "bär" || check_name == "urso")
{
Puts(string.Format(lang.GetMessage("specificanimal", this),check_name));
SpawnSpecific(pos, bear_string);
}
else if (check_name == "zombie" || check_name == "gumby" || check_name == "zumbi" || check_name == "zombi" || check_name == "zambi")
{
Puts(string.Format(lang.GetMessage("specificanimal", this),check_name));
SpawnSpecific(pos, zombie_string);
}
else
Puts(string.Format(lang.GetMessage("invalidanimal", this),check_name));
}
return;
}
void Init()
{
try
{
if (!permission.PermissionExists(permAdmin)) permission.RegisterPermission(permAdmin, this);
configData = Config.ReadObject<ConfigDats>();
SumChances = configData.ChanceOfPig + configData.ChanceOfChicken + configData.ChanceOfHorse +
configData.ChanceOfStag + configData.ChanceOfBear + configData.ChanceOfWolf + configData.ChanceOfZombie;
}
catch
{
LoadDefaultConfig();
Config.WriteObject(configData);
}
messages["Changing"] = "Changing chance of {0} to {1}";
messages["FailChange"] = "Invalid integer chance format: chance unchanged.";
messages["Surprise"] = "That wasn't just a barrel- it was a suprise!";
messages["playerNotFound"] = "Could not find player: {0}";
messages["playerPosNotFound"] = "Could not find player position: {0}";
messages["noplayer"] = "you need to specify a player";
messages["toomanyplayer"] = "too many players match that name: {0}";
messages["invalidanimal"] = "invalid animal: {0}";
messages["randomanimal"] = "spawing random animal";
messages["specificanimal"] = "spawning specific animal: {0}";
messages["nochances"] = "all chances zero, fix pigrel config!";
messages["badmin"] = "min must be less than max.";
messages["badmax"] = "max must be more than min.";
messages["mustbegt1"] = "must be one or more.";
messages["noperms"] = "you do not have permissions for pigrels.";
lang.RegisterMessages(messages,this);
}
// void OnServerSave()
// {
// Config.WriteObject(configData);
// }
}
}