Syntax: Select all
# ../sprestrict/sprestrict.py
"""Simple weapon restriction plugin."""
# =============================================================================
# >> IMPORTS
# =============================================================================
# Python Imports
# Collections
from collections import defaultdict
# Souce.Python Imports
# Entities
from entities.hooks import EntityPreHook
from entities.hooks import EntityCondition
# Filters
from filters.players import PlayerIter
# Listeners
from listeners import LevelInit
from listeners import LevelShutdown
# Memory
from memory import make_object
# Paths
from paths import PLUGIN_DATA_PATH
# Players
from players.entity import PlayerEntity
# Weapons
from weapons.manager import weapon_manager
# =============================================================================
# >> GLOBAL VARIABLES
# =============================================================================
# The path to the file
# ../addons/source-python/data/plugins/sprestrict/restrictions.txt
restrictions_txt = PLUGIN_DATA_PATH.joinpath('sprestrict', 'restrictions.txt')
# Hold any found items in the file in this list
restrictions = defaultdict(list)
# =============================================================================
# >> FUNCTIONS
# =============================================================================
def load_restrictions():
"""Load the items from the txt file into memory."""
# Open the restrictions file
with open(restrictions_txt) as file_object:
# Store the lines read
lines = filter(lambda x: weapon_manager.prefix in x,
file_object.read().replace('\r', '').replace('@', '').split('\n')
)
# Loop through each line
for line in lines:
# Get the team and the weapon
team, weapon = line.split(' ')
# Append the weapon
restrictions[team].append(weapon)
# =============================================================================
# >> LISTENERS
# =============================================================================
@LevelInit
def level_init(mapname):
"""Called when the map is about to be changed."""
# Load the restrictions into memory
load_restrictions()
@LevelShutdown
def level_shutdown():
"""Called when the map is shut down."""
# Clear the restrictions dict
restrictions.clear()
# =============================================================================
# >> ENTITY HOOKS
# =============================================================================
@EntityPreHook(EntityCondition.is_player, 'buy_internal')
def pre_buy(args):
"""Called when a player is about to buy something."""
# Get a PlayerEntity object from the pointer passed
player = make_object(PlayerEntity, args[0])
# Get the weapon name the player is about to buy
weapon = weapon_manager.prefix + args[1]
# Loop through the restrictions
for team in restrictions:
# Is the weapon restricted?
if weapon in restrictions[team]:
# Is the player in the right team?
if player.index in PlayerIter(is_filters=team):
# If yes, return False to block the purchase
return False
# =============================================================================
# >> LOAD & UNLOAD
# =============================================================================
def load():
"""Called when this plugin is loaded."""
# Load the restrictions into memory
load_restrictions()
def unload():
"""Called when this plugin is unloaded."""
# Clear the restrictions dict
restrictions.clear()
Create the file: ../<game>/addons/source-python/data/plugins/sprestrict/restrictions.txt
Its contents should read:
Code: Select all
@all weapon_awp
@all weapon_sg550
@all weapon_scout
@all weapon_g3sg1
Code: Select all
sp load sprestrict
Note: Everytime the plugin is reloaded or the map has changed, the file plugin reads the restrictions.txt file again.
Edit: Had to update it, because I forgot the team restriction side of things.