Client command filter help. blocking weapon drop

Please post any questions about developing your plugin here. Please use the search function before posting!
meow
Junior Member
Posts: 1
Joined: Sat Aug 20, 2016 4:38 pm

Client command filter help. blocking weapon drop

Postby meow » Sat Aug 20, 2016 4:50 pm

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
Last edited by L'In20Cible on Sat Aug 20, 2016 5:13 pm, edited 1 time in total.
Reason: [code] → [python]
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: Client command filter help. blocking weapon drop

Postby L'In20Cible » Sat Aug 20, 2016 5:12 pm

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
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Re: Client command filter help. blocking weapon drop

Postby satoon101 » Sat Aug 20, 2016 7:55 pm

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.
Image

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 20 guests