[Cs:s] Grenades Buy limit

A place for requesting new Source.Python plugins to be made for your server.

Please request only one plugin per thread.
cssbestrpg
Senior Member
Posts: 287
Joined: Sun May 17, 2020 7:56 am
Location: Finland
Contact:

[Cs:s] Grenades Buy limit

Postby cssbestrpg » Thu Aug 06, 2020 8:37 am

Hi, as the title say can some one make grenades buy limit, when reach the limit it wouldn't let buy anymore grenades even not with f2
User avatar
VinciT
Senior Member
Posts: 331
Joined: Thu Dec 18, 2014 2:41 am

Re: [Cs:s] Grenades Buy limit

Postby VinciT » Thu Aug 06, 2020 11:30 pm

Hi, give this a try:

Syntax: Select all

# ../nade_limit/nade_limit.py

# Source.Python
from cvars import ConVar
from engines.sound import Sound
from entities.hooks import EntityPreHook, EntityCondition
from events import Event
from messages import SayText2
from players.entity import Player


MAX_HEGRENADES = 1
MAX_FLASHBANGS = 2
MAX_SMOKEGRENADES = 1


# Dictionary used to store relevant information about grenades.
# grenade name: (buy limit, max ammo, ammo prop)
GRENADE_INFO = {
'hegrenade': (
MAX_HEGRENADES,
ConVar('ammo_hegrenade_max'), 'localdata.m_iAmmo.011'),
'flashbang': (
MAX_FLASHBANGS,
ConVar('ammo_flashbang_max'), 'localdata.m_iAmmo.012'),
'smokegrenade': (
MAX_SMOKEGRENADES,
ConVar('ammo_smokegrenade_max'), 'localdata.m_iAmmo.013')
}


# Sound that plays when the player tries to buy a grenade after reaching the
# buy limit.
DENIED_SOUND = Sound('buttons/weapon_cant_buy.wav', volume=0.3)


class PlayerNL(Player):
"""Modified Player class."""

def __init__(self, index, caching=True):
"""Initializes the object."""
super().__init__(index, caching)
self.reset_nade_limit()

def reset_nade_limit(self):
"""Resets the grenade buy limit for the player."""
# Create a new dictionary by copying the keys (grenade names) from
# 'GRENADE_INFO' and initialize the values as 0.
self.bought_nades = dict.fromkeys(GRENADE_INFO.keys(), 0)


@Event('player_spawn')
def player_spawn(event):
# Make sure the player's grenade buy limit has been reset.
PlayerNL.from_userid(event['userid']).reset_nade_limit()


@EntityPreHook(EntityCondition.is_player, 'buy_internal')
def buy_internal_pre(stack_data):
# Store the name of the weapon the player is attempting to purchase.
weapon = stack_data[1]

try:
buy_limit, max_ammo, ammo_prop = GRENADE_INFO[weapon]
except KeyError:
# Not a grenade, don't go further.
return None

# Get the PlayerNL instance.
player = PlayerNL._obj(stack_data[0])

# Is the player carrying the max amount of this grenade type?
if player.get_property_int(ammo_prop) >= max_ammo.get_int():
return None

# Has the player reached their buy limit for this grenade type?
if player.bought_nades[weapon] >= buy_limit:
# Notify the player.
SayText2(f'{weapon.capitalize()} is out of stock!').send(player.index)
DENIED_SOUND.play(player.index)
# Block the purchase.
return True

# Increase the buy counter for this grenade type.
player.bought_nades[weapon] += 1
ImageImageImageImageImage
cssbestrpg
Senior Member
Posts: 287
Joined: Sun May 17, 2020 7:56 am
Location: Finland
Contact:

Re: [Cs:s] Grenades Buy limit

Postby cssbestrpg » Fri Aug 07, 2020 9:27 am

Thanks VinciT it works perfectly
User avatar
VinciT
Senior Member
Posts: 331
Joined: Thu Dec 18, 2014 2:41 am

Re: [Cs:s] Grenades Buy limit

Postby VinciT » Fri Aug 07, 2020 4:44 pm

Awesome, glad I could help. :smile:
ImageImageImageImageImage

Return to “Plugin Requests”

Who is online

Users browsing this forum: No registered users and 23 guests