-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoTurretManager.cs
68 lines (61 loc) · 1.74 KB
/
AutoTurretManager.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
using System;
namespace HarmonyIOLoader
{
class AutoTurretManager : FacepunchBehaviour
{
public bool UnlimitedAmmo { get; private set; }
private AutoTurret AutoTurret;
private void Awake()
{
AutoTurret = GetComponent<AutoTurret>();
enabled = false;
}
public void Initialize(bool unlimitedAmmo, bool peacekeeperMode, string weaponName)
{
if (AutoTurret == null) return;
UnlimitedAmmo = unlimitedAmmo;
AutoTurret.SetPeacekeepermode(peacekeeperMode);
if (!string.IsNullOrEmpty(weaponName))
{
Item item = ItemManager.CreateByName(weaponName, 1, 0UL);
item.RemoveFromContainer();
item.position = 0;
item.SetParent(AutoTurret.inventory);
AutoTurret.inventory.MarkDirty();
item.MarkDirty();
if (!AutoTurret.IsInvoking(new Action(AutoTurret.UpdateAttachedWeapon)))
AutoTurret.Invoke(new Action(AutoTurret.UpdateAttachedWeapon), 0.5f);
if (unlimitedAmmo)
{
var weapon = AutoTurret.GetAttachedWeapon();
if(weapon != null)
weapon.primaryMagazine.contents = weapon.primaryMagazine.capacity;
AutoTurret.Invoke(new Action(RefillAmmo), 1f);
}
}
}
private void RefillAmmo()
{
if (AutoTurret == null) return;
var weapon = AutoTurret.GetAttachedWeapon();
if (!(weapon == null))
{
int? ammoID;
if (weapon == null)
{
ammoID = null;
}
else
{
ItemDefinition ammoType = weapon.primaryMagazine.ammoType;
ammoID = ((ammoType != null) ? new int?(ammoType.itemid) : null);
}
if (AutoTurret.inventory.GetAmount(ammoID.GetValueOrDefault(), true) < 1)
{
Item item = ItemManager.CreateByItemID(ammoID.GetValueOrDefault(), 128, 0UL);
item.MoveToContainer(AutoTurret.inventory, -1, true, true);
}
}
}
}
}