You are here: Big Brother Bot ForumCommunity DevelopersThe Code BinConverting Simple Modifications into Plugins
Pages: [1]   Go Down
  Print  
Author Topic: Converting Simple Modifications into Plugins  (Read 5435 times) Bookmark and Share
B3 Contrib/Support
*
OS: --No B3 installed--
Type: --No B3 installed--
Posts: 1225
Offline Offline
Support Specialty: B3-Core, CoD/BFBC2 parsers, FTP-functionality, Plugin development
« on: April 19, 2009, 08:18:28 AM »

I know some of you have added commands to the B3 bot by making code hacks to admin.py/other files. Whilst this is extremely easy, it also increases the difficulty of updates (since you'd need to transfer your modifications between versions), and would also be hard to share, should you want to (Download and Install plugin is easier than 'On line 234 of admin.py, insert this data, then in admin.xml do this, then in punkbuster.py do this, etc etc).

In this short example, I will be converting the poke command into a plugin.

Code: python
    def cmd_poke(self, data, client=None, cmd=None):
       """\
        - Notify a player that he needs to move
       """

       m = self.parseUserCmd(data)
       if not m:
           client.message('^7Invalid parameters, you must supply a player name')
           return False

       if m[0] == 'b3':
           self.warnClient(client, 'Do not poke b3!', None, False, '', 1)
       else:
           sclient = self.findClientPrompt(m[0], client)
           if sclient:
               self.console.say('^7%s %s^7!' % (random.choice(('Wake up', '*poke*', 'Attention', 'Get up', 'Go', 'Move out')), sclient.exactName))

That is the b3 code for !poke, found in admin.py.

This is what I did to make it a plugin:

Quote from: poke.py

__version__ = '1.0'
__author__  = 'Bakes'

import b3, re
import b3.events
# Import the necessary libaries you need here, for example, I need random for the randomization of answers part of it.
import random
#--------------------------------------------------------------------------------------------------
#This lot doesn't need to be changed for simple commands, it gets the admin plugin and registers commands.
class PokePlugin(b3.plugin.Plugin):
    _adminPlugin = None

    def startup(self):
      """\
      Initialize plugin settings
      """

   # get the admin plugin so we can register commands
      self._adminPlugin = self.console.getPlugin('admin')
      if not self._adminPlugin:
      # something is wrong, can't start without admin plugin
        self.error('Could not find admin plugin')
        return False

    
    # register our commands (you can ignore this bit)
      if 'commands' in self.config.sections():
        for cmd in self.config.options('commands'):
          level = self.config.get('commands', cmd)
          sp = cmd.split('-')
          alias = None
          if len(sp) == 2:
            cmd, alias = sp

          func = self.getCmd(cmd)
          if func:
            self._adminPlugin.registerCommand(self, cmd, level, func, alias)

      self.debug('Started')


    def getCmd(self, cmd):
      cmd = 'cmd_%s' % cmd
      if hasattr(self, cmd):
        func = getattr(self, cmd)
        return func

      return None
#--------------------------------------------------------------------
# Your commands go under here

    def cmd_poke(self, data, client=None, cmd=None):
        """\
        <player> - Notify a player that he needs to move
        """

        m = self._adminPlugin.parseUserCmd(data)
        if not m:
            client.message('^7Invalid parameters, you must supply a player name')
            return False

        if m[0] == 'b3':
            self._adminPlugin.warnClient(client, 'Do not poke b3!', None, False, '', 1)
        else:
            sclient = self._adminPlugin.findClientPrompt(m[0], client)
            if sclient:
                self.console.say('^7%s %s^7!' % (random.choice(('Wake up', '*poke*', 'Attention', 'Get up', 'Go', 'Move out')), sclient.exactName))

Quote from: plugin_poke.xml
<configuration plugin="banter">
   <settings name="commands">
      <set name="poke">2</set>
   </settings>
</configuration>
Key:
green: replace this with your plugin name. (the plugin file (poke.py in this case) should be similar)

red: This is stuff I changed between the two code pieces, because they relate to, in this case, the admin plugin (though you can change that to your liking, could be the punkbuster plugin, etc.). For example, self._adminPlugin.warn calls the part of admin.py that deals with warnings.

orange: These parts are the parts that deal with importing other plugins. They import the admin plugin, but you could use them for punkbuster, poweradmin, etc. Why would I need to do this? One server has a '!dirtyhacker' command, that takes a pbss, pm's them 'You Dirty Hacker, why would you do such a thing' and blows them up.
As you can see, _adminPlugin is a variable, in theory (but I wouldn't ever do it, it's bad practice), I could call the admin plugin as _cheese, and use _cheese.warn, it's all up to you.

So, that really is all you need to know to make a plugin out of code modification. This is, of course, providing that you have only modified a plugin, rather than actual b3 code; If you have actually improved the code, it would be nice to see your results Smiley
« Last Edit: May 25, 2009, 09:20:10 AM by Bakes » Logged


Senior Dev.
*
OS: Linux
Type: Home user
Posts: 3347
Offline Offline
WWW
Support Specialty: B3-Core, UrT/SmG/BFBC2 parsers, Plugin development
« Reply #1 on: April 19, 2009, 08:30:51 AM »

nice guide Bakes. Note that you also have to write a config file for the new plugin
Logged

B3 Contrib/Support
*
OS: --No B3 installed--
Type: --No B3 installed--
Posts: 1225
Offline Offline
Support Specialty: B3-Core, CoD/BFBC2 parsers, FTP-functionality, Plugin development
« Reply #2 on: April 19, 2009, 09:08:42 AM »

oh yeah, i'll just edit it.
Logged

[ www.xlrstats.com ]
Project Lead
*
OS: Linux
Type: Owner dedicated server(s)
Gameservers: CoD, CoD2, CoD5, UrT
Posts: 2009
Online Online
WWW
Support Specialty: B3-Core, CoD/UrT/WoP/ETPro parsers, Plugin development
« Reply #3 on: April 19, 2009, 11:37:41 AM »

Good post! Maybe something for the wiki as well...
Logged

Full Member
***
Posts: 84
Offline Offline
WWW
« Reply #4 on: April 19, 2009, 06:43:40 PM »

awesome.

now take all that positivity you've built up, and do one for python scripting.

kidding, have a beer!

this is actually terrific, maybe you could expand on this. 

 i'm actually exploring TWiki as an option for clan policy and project management. 

i just mean that we have up to 4 different games running over up to 12 servers at any given time, and all the b3 work falls to me, and usually a different ruleset or gametype on each instance of a server and not being able to carry my custom stuff over makes it nearly impossible for someone else to upgrade or anything.  it's so complex at times i'm actually considering making it more complex in the hope's it will eventually get easier to manage everything amongst several people.

oh yes, +1 to Bakes.  i call dibs on a !votemap plugin.
« Last Edit: April 19, 2009, 06:50:27 PM by intrebulon » Logged
Newbie
*
Posts: 4
Offline Offline
« Reply #5 on: January 25, 2010, 08:14:59 AM »

good job
Logged
Beta Testers
*
OS: Windows
Type: Owner dedicated server(s)
Gameservers: BF3
Posts: 134
Offline Offline
eliteclangaming.com
WWW
« Reply #6 on: June 25, 2010, 08:57:56 PM »

Thanks a lot for putting this post together. Very helpful and I was able to use it to put together a rather useful plugin in about an hour. Much easier than my first plugin was.
Logged

xfire: blitzwright
Tags:
Pages: [1]   Go Up
  Print  
 
Jump to:  


Rate this page +1 at Google Search


SimplePortal 2.3.1 © 2008-2009, SimplePortal