Skip to content

Instantly share code, notes, and snippets.

@popey456963
Last active June 11, 2016 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save popey456963/7db84090012eb6706f8b318006eb9cf3 to your computer and use it in GitHub Desktop.
Save popey456963/7db84090012eb6706f8b318006eb9cf3 to your computer and use it in GitHub Desktop.
Source Python FAQ

How do I make things happen on load/unload?

def load():
  print('[SP] Plugin has been loaded successfully!')

def unload():
  print('[SP] Plugin has been unloaded successfully!')

Write in text chat?

from messages import SayText2
SayText2("Some text")

Hide a command?

from commands import CommandReturn

@SayCommand("/command")
def awesome_command(command, target:str, time:int=10):
  # Do your magic
  return CommandReturn.BLOCK

Target Groups?

def target_filter(filterby, source=None, multitarget=True):
    """Processes a target string and resolves it to one or more players
    Args:
        filterby: A string filterby in the form of
                Name: "<playername>" ex: "necavi"
                Userid: "#<index>" ex: "#5"
                Multi-Filter: "@<filterby>" ex "@me"
        source: A player to consider the source, used for filters such as @me
    Returns:
        A list of players that fit the filterby string
        :param source:
        :param filterby:
        :param multitarget:
    """
    playerlist = []
    if filterby == "":
        pass
    elif multitarget and filterby[0] == "@":
        if len(filterby) > 1 and filterby[1] == "!":
            if source is not None and len(filterby) > 2 and filterby[2:] == "me":
                source_index = source
                for index in PlayerIter():
                    if index != source_index:
                        playerlist.append(index)
            else:
                try:
                    playerlist = [x for x in PlayerIter(not_filters=filterby[2:])]
                except:
                    pass
        else:
            if source is not None and filterby[1:] == "me":
                playerlist.append(source)
            else:
                try:
                    playerlist = [x for x in PlayerIter(is_filters=filterby[1:])]
                except:
                    pass
    elif filterby[0] == "#":
        index = filterby[1:]
        if index.isnumeric():
            try:
                playerlist.append(index_from_userid(int(index)))
            except ValueError:
                pass
    else:
        for index in PlayerIter():
            playerinfo = playerinfo_from_index(index)
            filterby = filterby.casefold()
            if filterby in playerinfo.get_name().casefold():
                playerlist.append(index)
    return playerlist if multitarget else playerlist[0] if len(playerlist) == 1 else None

Hide Events?

from events.hooks import EventAction
from events.hooks import PreEvent

@PreEvent('server_cvar', 'player_team', 'player_connect', 'player_disconnect')
def pre_player_team(game_event):
    return EventAction.STOP_BROADCAST

Handling CVARs?

from cvars import ConVar

# Get an existing ConVar
mp_timelimit = ConVar('mp_timelimit')
print(mp_timelimit.get_float())

# Create a new ConVar
my_convar = ConVar('my_convar')

# Set a ConVar
my_convar.set_int(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment