using System;
using System.Collections.Generic;
using Rust;
using UnityEngine;
namespace RocketPistol
{
public class RocketPistol : RustPlugin
{
private readonly Dictionary<ulong, PlayerWeapon> _players = new Dictionary<ulong, PlayerWeapon>();
private readonly HashSet<ulong> _rocketModeEnabled = new HashSet<ulong>();
private void OnPlayerInput(BasePlayer player, InputState input)
{
if (input.IsDown(BUTTON.FIRE_PRIMARY))
{
PlayerWeapon weapon;
if (!_players.TryGetValue(player.userID, out weapon))
{
weapon = new PlayerWeapon(player);
_players[player.userID] = weapon;
}
if (_rocketModeEnabled.Contains(player.userID))
{
weapon.FireRocket();
}
}
}
private void OnServerCommand(ConsoleSystem.Arg args)
{
var player = args.Player();
if (player != null)
{
if (args.cmd.FullName == "rocketmode enable")
{
_rocketModeEnabled.Add(player.userID);
PrintToChat(player, "Rocket mode enabled!");
}
else if (args.cmd.FullName == "rocketmode disable")
{
_rocketModeEnabled.Remove(player.userID);
PrintToChat(player, "Rocket mode disabled!");
}
}
}
private class PlayerWeapon
{
private readonly BasePlayer _player;
private readonly Item _rocketLauncher;
public PlayerWeapon(BasePlayer player)
{
_player = player;
_rocketLauncher = ItemManager.CreateByName("rocket.launcher", 1, 0);
}
public void FireRocket()
{
var activeItem = _player.GetActiveItem();
if (activeItem != null && activeItem.info.shortname == "pistol.revolver")
{
_player.Command("noteamspeak " + _player.net.ID);
_player.inventory.Take(null, activeItem.info.itemid, 1);
_player.inventory.GiveItem(ItemManager.CreateByName("ammo.rocket.basic", 1, 0));
_player.Command("notetamspeak " + _player.net.ID);
Effect.server.Run("assets/bundled/prefabs/fx/impacts/arrow/arrow-wood.prefab", _player.eyes.position, Vector3.up);
_player.inventory.GiveItem(_rocketLauncher);
_rocketLauncher.GetHeldEntity().DoAttack();
_player.inventory.Take(null, _rocketLauncher.info.itemid, 1);
}
}
}
private void Unload()
{
foreach (var player in BasePlayer.activePlayerList)
{
PlayerWeapon weapon;
if (_players.TryGetValue(player.userID, out weapon))
{
if (weapon != null)
{
weapon.Dispose();
}
_players.Remove(player.userID);
_rocketModeEnabled.Remove(player.userID);
}
}
}
}
}