Weapon Restrictions

Official Announcements about Source.Python.
User avatar
satoon101
Project Leader
Posts: 2698
Joined: Sat Jul 07, 2012 1:59 am

Weapon Restrictions

Postby satoon101 » Mon Mar 14, 2016 12:28 pm

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
my_handler.add_team_restrictions(
2, *[weapon.basename for weapon in WeaponClassIter(not_filters='knife')])

# Restrict CTs from using knives
my_handler.add_team_restrictions(
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(
weapon).send(PlayerIter(teams_by_number[team]))
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(
weapon)).send(player.index)

my_handler = TestWeaponRestrictionHandler()
Image
User avatar
Mahi
Senior Member
Posts: 236
Joined: Wed Aug 29, 2012 8:39 pm
Location: Finland

Postby Mahi » Mon Mar 14, 2016 12:37 pm

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

Postby satoon101 » Mon Mar 14, 2016 12:39 pm

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.
Image
User avatar
BackRaw
Senior Member
Posts: 537
Joined: Sun Jul 15, 2012 1:46 am
Location: Germany
Contact:

Postby BackRaw » Tue Mar 15, 2016 7:59 am

Really cool!!
My Github repositories:

Source.Python: https://github.com/backraw
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Postby iPlayer » Tue Mar 15, 2016 9:41 am

Can I restrict certain weapon indexes?
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image
User avatar
satoon101
Project Leader
Posts: 2698
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Tue Mar 15, 2016 5:05 pm

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?
Image
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Postby iPlayer » Tue Mar 15, 2016 6:59 pm

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.
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image
User avatar
satoon101
Project Leader
Posts: 2698
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Tue Mar 15, 2016 8:37 pm

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

Return to “News & Announcements”

Who is online

Users browsing this forum: No registered users and 2 guests