Page 1 of 1

Client command filter help. blocking weapon drop

Posted: Sat Aug 20, 2016 4:50 pm
by meow
I am new because I was using eventscripts.

I am trying to convert my script from eventscripts and A part of the script is blocking weapon drop.

Here is my previous eventscripts script which worked:

Syntax: Select all

import es

def preventweapondrop(userid, args):
if args[0] == 'drop' or args[0] == 'buy':
return False
return True

def load():
es.addons.registerClientCommandFilter(preventweapondrop)


I made this, which is for source.python:

Syntax: Select all

from commands.client import register_client_command_filter
from commands.client import unregister_client_command_filter

def enable_prevent_weapon_drop_mod():
register_client_command_filter(preventweapondrop)
print("[prevent_weapon_drop] enabled.")

def disable_prevent_weapon_drop_mod():
unregister_client_command_filter(preventweapondrop)
print("[prevent_weapon_drop] disabled.")

def preventweapondrop(userid, args):
if args[0] == 'drop' or args[0] == 'buy':
return False
return True


When a try to drop a weapon ingame I get this error:
[SP] Caught an Exception:
Traceback (most recent call last):
File '../addons/source-python/plugins/mod/prevent_weapon_drop/prevent_weapon_drop.py', line 13, in preventweapondrop
if args[0] == 'drop' or args[0] == 'buy':

TypeError: 'int' object is not subscriptable


Idk if this info helps but I tried to have print(args) and it would output 1:

Syntax: Select all

def preventweapondrop(userid, args):
# if args[0] == 'drop' or args[0] == 'buy':
# return False
print(args)
return True


ServerConsole:
1
1
1
1

Re: Client command filter help. blocking weapon drop

Posted: Sat Aug 20, 2016 5:12 pm
by L'In20Cible
First of all, welcome to Source.Python!! :smile:

I don't have time to elaborate (will do so later or tomorrow) but your code should looks like:

Syntax: Select all

from commands import CommandReturn
from commands.client import ClientCommandFilter

@ClientCommandFilter
def client_command_filter(command, player_index):
if command[0] in ('buy', 'drop'):
return CommandReturn.BLOCK

Re: Client command filter help. blocking weapon drop

Posted: Sat Aug 20, 2016 7:55 pm
by satoon101
If you are looking for certain commands, you don't have to use a filter. You can instead register both of the commands:

Syntax: Select all

from commands import CommandReturn
from commands.client import ClientCommand


@ClientCommand(['drop', 'buy'])
def _restrict_drop_and_buy(command, player_index):
return CommandReturn.BLOCK


Also, if you want to fully prevent players from purchasing weapons, hooking the 'buy' command is not enough. Players can still use autobuy and rebuy. The best way would be to hook CCSPlayer::HandleCommand_Buy_Internal, which we already include in our data for both CS:S and CS:GO.

Syntax: Select all

from entities.hooks import EntityCondition, EntityPreHook


@EntityPreHook(EntityCondition.is_player, 'buy_internal')
def _pre_buy_internal(stack_data):
return False


As an fyi, all of our decorators are designed to cleanup on unload. That is why you don't see us registering and unregistering in the scripts we have posted. That is all handled internally.