Trying to limit chinook based on player count
Here is the plugin I try to make, but this is the first plugin I try to do, can someone help me ? The goal is to disable the Chinook Heli if there is less than 3 players on the servers and if the count come to 3 or more than the Chinook get reactivated

Here is what I made for the moment, but this isn't working can someone help me ?

​
using System;
using System.Collections.Generic;
using System.Linq;
using Oxide.Core;
using Oxide.Core.Libraries.Covalence;

namespace Oxide.Plugins
{
    class PlayerMinimum4Crates : CovalencePlugin
    {

        DisableLockedCrate();


        public int GetNbrPlayers(IPlayer player)
        {
            int playerCount = players.Connected.Count(p => !p.IsAdmin); ;

            return playerCount;
        }

        public void DisableLockedCrate(IPlayer player)
        {
            if(GetNbrPlayers(player) < 3)
            {
                rust.RunServerCommand("server.events 0");
            }
            else
            {
                rust.RunServerCommand("server.events 1");
            }
        }
    }


}
First of all, what isn't working about it. Second of all are you sure that command only disables chinook and nothing else like cargo or heli?

This should work

        private void OnEntitySpawned(CH47HelicopterAIController ch47)
        {
            if (BasePlayer.activePlayerList.Count >= 3) return;
            NextTick(() => { if (ch47 != null) ch47.Kill(); });
        }
I will try it, thanks, but where do you find every everything, I search on internet for 2 hours to find how to know the number of active players on the server and I didn't find it, is there like a Rust plugin wiki or something like that somewhere ?

Merged post

I tried it and it doesn't work, there are still locked crate in the server even if there are less than 3 players
I just tested and it works fine.
Easiest way is to check how they did it in other plugins, in my case I knew InfoPanel has a player counter, or DiscordRewards from cc also has a player counter. For everything else use a decompiler.

Runescape's solution will solve the helicopter spawning. If you wanted to remove all locked crates you would have to either find and destroy them, or stop them spawning with OnEntitySpawned().

As for everything, it's not very documented, most of it you have to find out by experimenting and knowledge you gain over time.

oh, ok because when I restarted the server I confirm I didn't see the Chinook but crates was spawning on both oil rig and in water treatment

Merged post

I made this 
private void OnEntitySpawned(CH47HelicopterAIController ch47, HackableLockedCreate crate)
    {
        if (BasePlayer.activePlayerList.Count >= 3) return;
        NextTick(() => 
        { 
            if (ch47 != null)
            { 
                ch47.Kill();
            }

            if (crate != null)
            {
                crate.kill();
            }
        }
        );
    }​
I will test it and give you reviews if it works

Merged post

Nah, it doesn't work, it can't find what is
HackableLockedCreate crate​

I need an assembly reference but idk which one



Merged post

Ok ok, now it seems to work, but if a crate is already spawned and the player number goes under 3 they still stay on the map
You would need to detect when the player population reaches or goes under 3, then find and destroy all crates on the map. This could be done by checking player count every time someone disconnects with OnPlayerDisconnected. There is probably a more elegant solution but that would be the easiest.
Alright, I made it, in theory it should work, but the problem is I don't know how to make a reference to every lockedcrate, I suppose they are stocked in a list or a table, but I don't know hwta is the name of this, I know I should use a foreach, but one information is missing for my foreach, where could I find it ?
foreach(HackableLockedCrate crate in ????)
{
crate.Kill();
}​


Merged post

EDIT*
I made this, but this isn't working do you know why ?
                foreach (HackableLockedCrate lockedcrate in GameObject.FindObjectsOfType<HackableLockedCrate>())
                {
                    lockedcrate.Kill();
                }​
BaseNetworkable.serverEntities.OfType<HackableLockedCrate>()
foreach (BaseNetworkable networkable in BaseNetworkable.serverEntities)
{
    if (networkable is HackableLockedCrate)
    {
        networkable.Kill();
    }
}

or

foreach (HackableLockedCrate crate in UnityEngine.Object.FindObjectsOfType<HackableLockedCrate>())
{
    crate.Kill();
}

should work.

Thx for all your help, I really appreciate, but no, nothing is working locked crate still spawn in water treatment, airfield and even on both oil rig. I will just give up
No no don't give up! Show us your entire code. Maybe we can spot the problem elsewhere.
How and when do you want to .Kill() the hackable crates? The plugin I posted here will prevent the Chinook from spawning, the crates all over your map were already there before you loaded the plugin I sent you, the exception being the Oil Rig crates, as those spawn on their own and are not dropped by the Chinook.
using System;
using System.Collections.Generic;
using System.Linq;
using Oxide.Core;
using Oxide.Core.Libraries.Covalence;
using ConVar;
using Newtonsoft.Json;
using Oxide.Core.Configuration;
using UnityEngine;

namespace Oxide.Plugins
{
    [Info("Chinook Event Minimum Players", "Dolotboy", "0.0.1")]
    [Description("Prevents Chinook event from spawning when there are less than 3 active players.")]
    class MinimumPlayer4LockedCrate : RustPlugin
    {
        private void OnPlayerDisconnect(CH47HelicopterAIController ch47, HackableLockedCrate crate) // C'est pour supprimer les crates qui sont resté loader lorsqu'il y avait 3 joueurs
        {
            if (BasePlayer.activePlayerList.Count >= 3) return;
            NextTick(() =>
            {
                foreach (BaseNetworkable networkable in BaseNetworkable.serverEntities)
                {
                    if (networkable is HackableLockedCrate)
                    {
                        networkable.Kill();
                    }
                }
            }
            );
        }

        private void OnPlayerConnect(CH47HelicopterAIController ch47, HackableLockedCrate crate) // C'est pour supprimer les crates qui sont resté loader lorsqu'il y avait 3 joueurs
        {
            if (BasePlayer.activePlayerList.Count >= 3) return;
            NextTick(() =>
            {
                foreach (BaseNetworkable networkable in BaseNetworkable.serverEntities)
                {
                    if (networkable is HackableLockedCrate)
                    {
                        networkable.Kill();
                    }
                }
            }
            );
        }

        private void OnEntitySpawn(CH47HelicopterAIController ch47, HackableLockedCrate crate) // Ici c'est pour supprimer les crates quand elle vont apparaitre si les il y a moins de 3 joueurs
        {
            if (BasePlayer.activePlayerList.Count >= 3) return;
            NextTick(() =>
            {
                if (ch47 != null)
                {
                    ch47.Kill();
                }

                if (crate != null)
                {
                    crate.Kill();
                }
            }
            );
        }

        private void Loaded()
        {
            PrintToConsole("The MinimumPlayersForCrates plugin has been loaded");
            PrintToChat("The MinimumPlayersForCrates plugin has been loaded");
        }

    }
}