Weapon restrict
Posted: Thu Sep 24, 2015 11:56 am
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()
Code: Select all
@all weapon_awp
@all weapon_sg550
@all weapon_scout
@all weapon_g3sg1
Code: Select all
sp load sprestrict
satoon101 wrote:Just so you know, we are still working on adding a basic restriction system to the API. It will hook CCSPlayer::HandleCommand_Buy_Internal to stop weapon purchasing.
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()
Jerome69 wrote:Works fine
I think it's my "bot.cfg" which was the problem.
Thanks a lot.
Now I will try to make a new menu without eventscript (I don't want to surcharge CSS ^^).
Development status update (November 2015) wrote:All listeners are now starting with the "On" prefix.
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 OnLevelInit
from listeners import OnLevelShutdown
# Memory
from memory import make_object
# Paths
from paths import PLUGIN_DATA_PATH
# Players
from players.entity import Player
# 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
# =============================================================================
@OnLevelInit
def level_init(mapname):
"""Called when the map is about to be changed."""
# Load the restrictions into memory
load_restrictions()
@OnLevelShutdown
def on_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 Player object from the pointer passed
player = make_object(Player, 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()
Syntax: Select all
# ../sprestrict/sprestrict.py
"""Simple weapon restriction plugin."""
# =============================================================================
# >> IMPORTS
# =============================================================================
# Python Imports
# Collections
from collections import defaultdict
# Souce.Python Imports
# Colors
from colors import RED
from colors import WHITE
# Commands
from commands.client import ClientCommand
# Filters
from filters.players import PlayerIter
# Listeners
from listeners import OnLevelInit
from listeners import OnLevelShutdown
# Messages
from messages import SayText2
# Paths
from paths import PLUGIN_DATA_PATH
# 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(set)
# =============================================================================
# >> FUNCTIONS
# =============================================================================
def load_restrictions():
"""Load the items from the txt file into memory."""
# Open the restrictions file
with restrictions_txt.open() as file_object:
# Store its contents
contents = file_object.read().replace('\r', '').split('\n')
# Loop through each line
for line in filter(lambda x: weapon_manager.prefix in x and x.startswith('@'), contents):
# Get the team and the weapon
team, weapon = line[1:].split(' ')
# Append the weapon
restrictions[team].add(weapon)
# =============================================================================
# >> LISTENERS
# =============================================================================
@OnLevelInit
def level_init(map_name):
"""Called when the map is about to be changed."""
# Load the restrictions into memory
load_restrictions()
@OnLevelShutdown
def level_shutdown():
"""Called when the map is shut down."""
# Clear the restrictions dict
restrictions.clear()
# =============================================================================
# >> CLIENT COMMANDS
# =============================================================================
@ClientCommand('buy')
def on_buy(command, index, team=None):
"""Hooks the 'buy' command and allows or restricts the purchase of a weapon."""
# Get the full weapon name
weapon = weapon_manager.prefix + command[1]
# Loop through the restrictions
for team, restricted in restrictions.items():
# Ignore this team if the weapon is not restricted
if weapon not in restricted:
continue
# Is the player purchasing the weapon on a team where the weapon is restricted?
if index in map(lambda x: x.index, PlayerIter(is_filters=team)):
SayText2('{0}SP-Restrict: {1}{2} is restricted.'.format(
RED, WHITE, command[1]
)).send(index)
# If yes, return False to block the purchase
return False
# Return True to allow the purchase
return True
# =============================================================================
# >> 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()
satoon101 wrote:That is probably because he only restricts the buy command instead of using a buy_internal hook. I have been working on SP's weapon restriction and am close to pushing some changes to a new test branch. Once that is available, this sort of thing will be much easier to implement.
Syntax: Select all
# ../sprestrict/sprestrict.py
"""Simple weapon restriction plugin."""
# =============================================================================
# >> IMPORTS
# =============================================================================
# Python Imports
# Collections
from collections import defaultdict
# Souce.Python Imports
# Colors
from colors import RED
from colors import WHITE
# Entities
from entities.hooks import EntityCondition
from entities.hooks import EntityPreHook
# Listeners
from listeners import OnLevelInit
from listeners import OnLevelShutdown
# Memory
from memory import make_object
# Messages
from messages import SayText2
# Paths
from paths import PLUGIN_DATA_PATH
# Players
from players.entity import Player
# 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 a list
restrictions = defaultdict(set)
# Hold team names and their values in a dict
teams = {
'ct': 3,
't': 2
}
# =============================================================================
# >> FUNCTIONS
# =============================================================================
def load_restrictions():
"""Load the items from the txt file into memory."""
# Open the restrictions file
with restrictions_txt.open() as file_object:
# Store its contents
contents = file_object.read().replace('\r', '').split('\n')
# Loop through each line
for line in filter(lambda x: weapon_manager.prefix in x and x.startswith('@'), contents):
# Get the team and the weapon
team, weapon = line[1:].split(' ')
# Append the weapon
restrictions[team].add(weapon)
# =============================================================================
# >> LISTENERS
# =============================================================================
@OnLevelInit
def level_init(map_name):
"""Called when the map is about to be changed."""
# Load the restrictions into memory
load_restrictions()
@OnLevelShutdown
def level_shutdown():
"""Called when the map is shut down."""
# Clear the restrictions dict
restrictions.clear()
# =============================================================================
# >> CLIENT COMMANDS
# =============================================================================
@EntityPreHook(EntityCondition.is_player, 'buy_internal')
def pre_buy(args):
"""Hooks the 'buy' command and allows or restricts the purchase of a weapon."""
# Get the player entity
player = make_object(Player, args[0])
# Get the full weapon name
weapon = weapon_manager.prefix + args[1]
# Loop through the restrictions
for team, restricted in restrictions.items():
# Ignore this team if the weapon is not restricted
if weapon not in restricted:
continue
if team == 'all' or teams[team] == player.team:
SayText2('{0}SP-Restrict: {1}{2} is restricted.'.format(
RED, WHITE, args[1]
)).send(player.index)
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()