using Oxide.Core.Plugins; using System.Linq; using UnityEngine; namespace Oxide.Plugins { [Info("BikeLight", "magado", "1.0.12")] [Description("Adds a custom light to bikes to illuminate the front and allows toggling it with the F key.")] public class BikeLight : RustPlugin { private const string PedalBikePrefab = "assets/content/vehicles/bikes/pedalbike.prefab"; private const string PedalTrikePrefab = "assets/content/vehicles/bikes/pedaltrike.prefab"; private const string SearchLightPrefab = "assets/prefabs/deployable/search light/searchlight.deployed.prefab"; private const string SpherePrefab = "assets/prefabs/visualization/sphere.prefab"; void OnEntitySpawned(BaseNetworkable entity) { if (entity == null || !(entity is BaseVehicle)) return; var vehicle = entity as BaseVehicle; if (vehicle.PrefabName == PedalBikePrefab || vehicle.PrefabName == PedalTrikePrefab) { timer.Once(1f, () => AddSearchLightToBike(vehicle)); // Delay to allow proper initialization } } void AddSearchLightToBike(BaseVehicle bike) { if (bike.GetComponentInChildren() != null) { return; } // SphereEntityの作成 var sphereEntity = GameManager.server.CreateEntity(SpherePrefab, new Vector3(0, -100, 0), Quaternion.identity) as SphereEntity; if (sphereEntity == null) { return; } // SphereEntityのセットアップ sphereEntity.EnableSaving(true); sphereEntity.EnableGlobalBroadcast(false); sphereEntity.SetParent(bike); sphereEntity.Spawn(); // サーチライトの作成 var searchLight = GameManager.server.CreateEntity(SearchLightPrefab, sphereEntity.transform.position) as SearchLight; if (searchLight == null) { return; } // サーチライトのセットアップ searchLight.pickup.enabled = false; DestroyMeshCollider(searchLight); DestroyGroundComp(searchLight); searchLight.Spawn(); 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(-10, 180, 180); // 角度を-10度に設定して前方に向ける searchLight.SendNetworkUpdateImmediate(); // ライトを常時点灯に設定 searchLight.SetFlag(BaseEntity.Flags.On, true); searchLight.SetFlag(IOEntity.Flag_HasPower, true); // 追加のフラグ設定 searchLight.SendNetworkUpdateImmediate(); // SphereEntityの位置調整 sphereEntity.currentRadius = 0.1f; sphereEntity.lerpRadius = 0.1f; sphereEntity.UpdateScale(); sphereEntity.SendNetworkUpdateImmediate(); timer.Once(3f, () => { if (sphereEntity != null) { sphereEntity.transform.localPosition = new Vector3(0, 1.0f, 0.6f); } }); // 自転車にライトコントローラーを追加 bike.gameObject.AddComponent().Initialize(searchLight); } void DestroyMeshCollider(BaseEntity entity) { foreach (var mesh in entity.GetComponentsInChildren()) { UnityEngine.Object.DestroyImmediate(mesh); } } void DestroyGroundComp(BaseEntity entity) { UnityEngine.Object.DestroyImmediate(entity.GetComponent()); UnityEngine.Object.DestroyImmediate(entity.GetComponent()); } private class BikeLightController : MonoBehaviour { private SearchLight light; public void Initialize(SearchLight lightEntity) { light = lightEntity; } void Update() { if (light == null || light.IsDestroyed) return; var vehicle = light.GetParentEntity() as BaseVehicle; if (vehicle == null) return; } } object OnEntityMounted(BaseMountable mountable, BasePlayer player) { if (mountable.GetParentEntity() is BaseVehicle vehicle && (vehicle.PrefabName == PedalBikePrefab || vehicle.PrefabName == PedalTrikePrefab)) { var light = vehicle.GetComponentInChildren(); if (light != null) { light.SetFlag(BaseEntity.Flags.On, true); light.SetFlag(IOEntity.Flag_HasPower, true); light.SendNetworkUpdateImmediate(); } } return null; } object OnEntityDismounted(BaseMountable mountable, BasePlayer player) { if (mountable.GetParentEntity() is BaseVehicle vehicle && (vehicle.PrefabName == PedalBikePrefab || vehicle.PrefabName == PedalTrikePrefab)) { var light = vehicle.GetComponentInChildren(); if (light != null) { light.SetFlag(BaseEntity.Flags.On, false); light.SetFlag(IOEntity.Flag_HasPower, false); light.SendNetworkUpdateImmediate(); } } return null; } } }