-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMiniLight.cs
169 lines (142 loc) · 5.82 KB
/
MiniLight.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
//Some changes to facepunch code has broken this 1. when it does work it has a delay when switching states.
//Special thanks to MinicopterOptions developer for this method, I literally just used the code from that plugin to attach the light, and the method used to toggle the light on and off. now we have night lights on minicopters, and can use jet engine from skill tree with no interference.
using System.Collections.Generic;
using UnityEngine;
using Oxide.Core.Plugins;
using Oxide.Core;
namespace Oxide.Plugins
{
[Info("MiniLight", "Tacman", "1.0.0")]
[Description("Adds a search light to Minicopters.")]
public class MiniLight : RustPlugin
{
#region Configuration
private readonly string minicopterPrefab = "assets/content/vehicles/minicopter/minicopter.entity.prefab";
private readonly string searchLightPrefab = "assets/prefabs/deployable/search light/searchlight.deployed.prefab";
private readonly string spherePrefab = "assets/prefabs/visualization/sphere.prefab";
private static readonly Vector3 searchLightOffset = new Vector3(0, 0.24f, 1.8f);
#endregion
#region Oxide Hooks
private void Init()
{
Subscribe(nameof(OnEntitySpawned));
Subscribe(nameof(OnServerCommand));
}
private void OnServerInitialized()
{
// Get all Minicopters in the server
foreach (BaseNetworkable entity in BaseNetworkable.serverEntities)
{
if (entity is Minicopter copter)
{
OnEntitySpawned(copter);
}
}
}
private void OnEntitySpawned(BaseNetworkable entity)
{
if (entity is Minicopter copter)
{
timer.Once(1f, () => AddSearchLight(copter));
}
}
private void Unload()
{
// Perform any necessary cleanup
}
#endregion
#region Search Light Attachment
private void SetupSphereEntity(SphereEntity sphereEntity)
{
sphereEntity.EnableSaving(true);
sphereEntity.EnableGlobalBroadcast(false);
}
private void SetupSearchLight(SearchLight searchLight)
{
searchLight.pickup.enabled = false;
DestroyMeshCollider(searchLight);
DestroyGroundComp(searchLight);
}
private void AddSearchLight(Minicopter copter)
{
var sphereEntity = GameManager.server.CreateEntity(spherePrefab, new Vector3(0, -100, 0), Quaternion.identity) as SphereEntity;
if (sphereEntity == null)
return;
SetupSphereEntity(sphereEntity);
sphereEntity.SetParent(copter);
sphereEntity.Spawn();
var searchLight = GameManager.server.CreateEntity(searchLightPrefab, sphereEntity.transform.position) as SearchLight;
if (searchLight == null)
return;
SetupSearchLight(searchLight);
searchLight.Spawn();
SetupInvincibility(searchLight);
searchLight.SetFlag(BaseEntity.Flags.Reserved5, true);
searchLight.SetFlag(BaseEntity.Flags.Busy, true);
searchLight.SetParent(sphereEntity);
searchLight.transform.localPosition = Vector3.zero;
searchLight.transform.localRotation = Quaternion.Euler(-20, 180, 180);
sphereEntity.currentRadius = 0.1f;
sphereEntity.lerpRadius = 0.1f;
sphereEntity.UpdateScale();
sphereEntity.SendNetworkUpdateImmediate();
timer.Once(3f, () =>
{
if (sphereEntity != null)
{
sphereEntity.transform.localPosition = searchLightOffset;
}
});
}
private void SetupInvincibility(BaseCombatEntity entity)
{
entity._maxHealth = 99999999f;
entity._health = 99999999f;
entity.SendNetworkUpdate();
}
private void DestroyGroundComp(BaseEntity ent)
{
UnityEngine.Object.DestroyImmediate(ent.GetComponent<DestroyOnGroundMissing>());
UnityEngine.Object.DestroyImmediate(ent.GetComponent<GroundWatch>());
}
private void DestroyMeshCollider(BaseEntity ent)
{
foreach (var mesh in ent.GetComponentsInChildren<MeshCollider>())
{
UnityEngine.Object.DestroyImmediate(mesh);
}
}
#endregion
#region Command Handling
private object OnServerCommand(ConsoleSystem.Arg arg)
{
if (arg.Connection == null || arg.cmd.FullName != "inventory.lighttoggle")
return null;
var player = arg.Player();
if (player == null)
return null;
var mini = player.GetMountedVehicle() as Minicopter;
if (mini == null)
return null;
if (!mini.IsDriver(player))
return null;
foreach (var child in mini.children)
{
var sphere = child as SphereEntity;
if (sphere == null)
continue;
foreach (var grandChild in sphere.children)
{
var light = grandChild as SearchLight;
if (light == null)
continue;
light.SetFlag(IOEntity.Flag_HasPower, !light.IsPowered());
// Prevent other lights from toggling.
return false;
}
}
return null;
}
#endregion
}
}