I'm trying to create a plugin that will call a helicopter every 30 minutes.
This plugin will have the remaining time until the next call and a force call command.
Do you think this will work?
using Oxide.Core.Plugins;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("AutoHeliCall", "yourname", "1.0.0")]
[Description("30分おきに自動でヘリを呼び、残り時間を放送。/helicall で即時召喚可能")]
public class AutoHeliCall : RustPlugin
{
private const float heliInterval = 1800f; // 30分 (秒)
private const float announceStep = 300f; // 5分 (秒)
private float timeRemaining;
private Timer heliTimer;
private Timer announceTimer;
void OnServerInitialized()
{
StartHeliCycle();
}
private void StartHeliCycle()
{
timeRemaining = heliInterval;
heliTimer?.Destroy();
announceTimer?.Destroy();
// 1秒ごとに残り時間をカウント
heliTimer = timer.Every(1f, () =>
{
timeRemaining -= 1f;
if (timeRemaining <= 0f)
{
CallHeli();
StartHeliCycle(); // 次のサイクル開始
}
});
// 5分おきに残り時間を放送
announceTimer = timer.Every(announceStep, () =>
{
if (timeRemaining > 0f)
AnnounceTime();
});
}
private void CallHeli()
{
// アタックヘリコプターをスポーン
BaseEntity heli = GameManager.server.CreateEntity("assets/prefabs/npc/patrol helicopter/patrolhelicopter.prefab", Vector3.up * 50f);
if (heli != null)
{
heli.Spawn();
PrintToChat("<color=#ff0000>[Helicopter]</color> <color=#ffff00>アタックヘリコプターが出現しました!</color>");
}
}
private void AnnounceTime()
{
int minutes = Mathf.CeilToInt(timeRemaining / 60f);
PrintToChat($"<color=#ff0000>[Helicopter]</color> <color=#00ffff>次のヘリコプター出現まで残り</color> <color=#ffff00>{minutes}分</color> <color=#00ffff>です。</color>");
}
[ChatCommand("helicall")]
private void CmdHeliCall(BasePlayer player, string command, string[] args)
{
CallHeli();
player.ChatMessage("<color=#ff0000>[Helicopter]</color> <color=#00ff00>ヘリを即時召喚しました!</color>");
// タイマーをリセットして30分後に次のヘリ
StartHeliCycle();
}
}
}