Ejemplo n.º 1
0
 def __init__(self, ServerControl):
     self.CommandTable = OrderedDict()
     self.ServerControl = ServerControl
     self.LogFile = None
     if ServerControl.LogCommands:
         try:
             self.LogFile = open("Logs/commands.log", "a")
         except IOError:
             Console.Warning("Logging", "Unable to open file \"Logs/commands.log\" - logging disabled")
Ejemplo n.º 2
0
 def _SortCommands(self):
     self.CommandTable = OrderedDict(sorted(self.CommandTable.items(), key = lambda i: i[0]))
Ejemplo n.º 3
0
class CommandHandler(object):
    '''Stores all the commands available on opticraft and processes any command messages'''
    def __init__(self, ServerControl):
        self.CommandTable = OrderedDict()
        self.ServerControl = ServerControl
        self.LogFile = None
        if ServerControl.LogCommands:
            try:
                self.LogFile = open("Logs/commands.log", "a")
            except IOError:
                Console.Warning("Logging", "Unable to open file \"Logs/commands.log\" - logging disabled")

    def HandleCommand(self, pPlayer, Message):
        '''Called when a player types a slash command'''
        if Message == '':
            pPlayer.SendMessage("&RPlease enter in a command!")
            return
        Tokens = Message.split()
        Command = Tokens[0].lower()
        if self.CommandTable.has_key(Command) == False:
            pPlayer.SendMessage("&RNo such command. Type /cmdlist for a list of commands")
            return
        else:
            CommandObj = self.CommandTable[Command]
            CommandObj.Execute(pPlayer, Message)

    def AddCommandObj(self, CmdObj):
        self._AddCommandObj(CmdObj, CmdObj.Name)
        for Alias in CmdObj.Aliases:
            self._AddCommandObj(CmdObj, Alias)
            
    def _AddCommandObj(self, CmdObj, Name):
        if self.CommandTable.has_key(Name.lower()):
            Console.Warning("Commands", "Command %s has been defined twice. Posible plugin conflict" % Name)
        self.CommandTable[Name.lower()] = CmdObj
        
        Permission = self.ServerControl.ConfigValues.GetValue("commandoverrides", Name, '')
        if Permission != '':
            if self.ServerControl.IsValidRank(Permission):
                Console.Out("CommandOverride", "Overrode command %s to rank %s" % (Name, Permission))
                self.OverrideCommandPermissions(CmdObj, Permission)
            else:
                Console.Warning("CommandOverride", "Failed to override command %s, rank %s does not exist" % (Name, Permission))
        self._SortCommands()
            
    def _SortCommands(self):
        self.CommandTable = OrderedDict(sorted(self.CommandTable.items(), key = lambda i: i[0]))

    def RemoveCommand(self, CmdObj):
        ToRemove = []
        for CommandName in self.CommandTable:
            if self.CommandTable[CommandName] == CmdObj:
                ToRemove.append(CommandName)
                
        while len(ToRemove) > 0:
            del self.CommandTable[ToRemove.pop()]

    def OverrideCommandPermissions(self, CmdObj, NewPermission):
        CmdObj.Permissions = NewPermission.lower()
        CmdObj.PermissionLevel = self.ServerControl.GetRankLevel(NewPermission.lower())
        return True