Hey, unfortunately the code snippet you sent doesn’t make any sense 😃 It can’t work, because OnReloadRequested
is not even subscribed to any event. The code logic is also different from what you saying you want to achieve. It looks like in the code you sent you are trying to disable the guns included in disabledWeaponIds
from being able to reload.
Here’s a fixed version of your code:
using Rocket.Core.Logging;
using Rocket.Core.Plugins;
using SDG.Unturned;
using System.Collections.Generic;
namespace AutoReloadPlugin
{
public class Main : RocketPlugin
{
private HashSet<ushort> disabledWeaponIds;
protected override void Load()
{
disabledWeaponIds = [363, 18];
UseableGun.onChangeMagazineRequested += OnChangeMagazineRequested;
Logger.Log("AutoReloadPlugin loaded successfully!");
}
protected override void Unload()
{
UseableGun.onChangeMagazineRequested -= OnChangeMagazineRequested;
Logger.Log("AutoReloadPlugin unloaded successfully.");
}
private void OnChangeMagazineRequested(PlayerEquipment equipment, UseableGun gun, Item oldItem, ItemJar newItem, ref bool shouldAllow)
{
string playerName = equipment.player.channel.owner.playerID.playerName;
ushort weaponId = gun.equippedGunAsset.id;
if (disabledWeaponIds.Contains(weaponId))
{
Logger.Log($"Player {playerName} tried to reload a disabled gun.");
return;
}
Logger.Log($"Player has automatically reloaded the gun.");
}
}
}
However when it comes to making a gun actually automatically reload, this is unfortunately not an easy thing to make. Because the client (player) won’t send a request to reload a gun if he doesn’t have a magazine in inventory. Meaning it is not possible with a plugin to detect when someone presses R to reload their gun unless they have some magazine to that gun in their inventory. Depending on the type of server you are making, there are different solutions to this. Here’s some ideas:
- Ensure with a plugin that player always has a magazine to the gun they are currently equipping in their inventory
- Instead of R for reload, check every 0.5 seconds if a player has equipped gun, if they have then check if it has max capacity. If it doesn’t have full capacity and player isn’t currently using it for more than (3-5) seconds or is empty. Force reload it without requiring player to press R.
Neither of this solutions are perfect and I personally don’t have any better ideas unfortunately.
The plugin you are trying to make is really hard and especially if you are a beginner I recommend giving up on this idea, at least for now, and work on some other plugin until you gain more experience. Then you can talk to other devs also, maybe they have better ideas than me.