Page 1 of 1

Weapon Restrictions

Posted: Mon Mar 14, 2016 12:28 pm
by satoon101
We are excited to announce that we have merged the weapon_restrictions branch into master! The best way to handle restricting weapons in your plugins is by creating a WeaponRestrictionHandler instance:

Syntax: Select all

from filters.weapons import WeaponClassIter
from players.entity import Player
from weapons.restrictions import WeaponRestrictionHandler

my_handler = WeaponRestrictionHandler()

# Restrict Terrorists from using any weapon besides their knife
2, *[weapon.basename for weapon in WeaponClassIter(not_filters='knife')])

# Restrict CTs from using knives
3, *[weapon.basename for weapon in WeaponClassIter('knife')])

# Restrict a specific player from using a glock
my_handler.add_player_restrictions(Player(1), 'glock')

# Allow Terrorists to use ak47s and glocks
my_handler.remove_team_restrictions(2, 'glock', 'ak47')

# Unrestrict the specific player from the glock
my_handler.remove_player_restrictions(Player(1), 'glock')

The handler gets added to the weapon_restriction_manager object. So, if you want to find out if a player/team is restricted from a weapon, use the weapon_restriction_manager object itself:

Syntax: Select all

from weapons.restrictions import weapon_restriction_manager

print(weapon_restriction_manager.is_player_restricted(Player(1), 'glock'))
print(weapon_restriction_manager.is_team_restricted(2, 'ak47')

You can also inherit from WeaponRestrictionHandler to override the built-in functionality of any of the following methods:

  • on_player_bumping_weapon
  • on_player_purchasing_weapon
  • is_player_restricted
  • is_team_restricted
  • on_player_restriction_added
  • on_team_restriction_added
  • on_player_carrying_restricted_weapon

For instance:

Syntax: Select all

from messages import SayText2
from players.teams import teams_by_number
from weapons.restrictions import WeaponRestrictionHandler

class TestWeaponRestrictionHandler(WeaponRestrictionHandler):

def on_player_purchasing_weapon(self, player, weapon):
"""Disable weapon purchasing altogether."""
return False

def on_team_restriction_added(self, team, weapon):
"""Notify the team that the weapon was restricted."""
SayText2('Your team was restricted from using weapon "{0}".'.format(
super().on_team_restriction_added(team, weapon)

def on_player_carrying_restricted_weapon(self, player, weapon):
"""Notify the player that the weapon was restricted."""
SayText2('You were just restricted from using weapon "{0}", '
'forcing your weapon to be dropped.'.format(

my_handler = TestWeaponRestrictionHandler()

Posted: Mon Mar 14, 2016 12:37 pm
by Mahi
Looks nice! Just one question, how do the weapon restriction handlers interact between two addons? Can one addon's handler automatically see what the other plugins have restricted?

Posted: Mon Mar 14, 2016 12:39 pm
by satoon101
All handlers are stored within the weapon_restriction_manager object. You can loop through each of them to see what they have restricted. Again, though, that is why you should use weapon_restriction_manager.is_player/team_restricted instead of the handler's is_player/team_restricted to see if a weapon is restricted by any of the registered handlers.

Posted: Tue Mar 15, 2016 7:59 am
by BackRaw
Really cool!!

Posted: Tue Mar 15, 2016 9:41 am
by iPlayer
Can I restrict certain weapon indexes?

Posted: Tue Mar 15, 2016 5:05 pm
by satoon101
That is not something that is directly supported. I've actually never known anyone that wanted to be able to do that. Seems an odd request to me. Why would you want to restrict it that way?

Posted: Tue Mar 15, 2016 6:59 pm
by iPlayer
Well, I don't know if SP weapon restriction system would fit this, but... You might heard of jail servers, Last Request games etc. There're minigames that 2 players can play together, say, whoever tosses their desert eagle farther. In such game one deagle is red, the other one is blue. I'd be nice to restrict players to interact with only the deagle of his own, not with any other weapons on the map.
There're lots of games that suppose that the player has their own weapon. Such weapon can be tracked by jail server, for example, in Hot Potato game whoever touches "hot potato deagle" last, dies.
The point is, class restriction is too broad for such strings, you need to work with exact entities.

But as I said, maybe it's not the best idea to utilize SP weapon restriction for this, since these games are all unique, buyzones are not a thing for jail-servers, and you'll probably need to hook weapon bumps by yourself for some other game-related reasons.

Posted: Tue Mar 15, 2016 8:37 pm
by satoon101
Yeah, I think your last paragraph is probably on point. I don't know that that is something the weapon restriction system should really worry about. You can still easily track the weapon using your own BumpWeapon hooks.