thankyou
Donate to the B3 fund!

"even a small donation helps!"
Donate with PayPal!

Author Topic: Converting Simple Modifications into Plugins  (Read 14522 times)

Offline Bakes

  • B3 Contrib/Support
  • Hero Member
  • *
  • Posts: 1226
Converting Simple Modifications into Plugins
« on: April 19, 2009, 06:18:28 PM »
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 [Select]
    def cmd_poke(self, data, client=None, cmd=None):
        """\
        <player> - 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 :)
« Last Edit: May 25, 2009, 07:20:10 PM by Bakes »

Offline Courgette

  • Senior Dev.
  • Hero Member
  • *
  • Posts: 4883
    • Github repository
Re: Converting Simple Modifications into Plugins
« Reply #1 on: April 19, 2009, 06:30:51 PM »
nice guide Bakes. Note that you also have to write a config file for the new plugin

Offline Bakes

  • B3 Contrib/Support
  • Hero Member
  • *
  • Posts: 1226
Re: Converting Simple Modifications into Plugins
« Reply #2 on: April 19, 2009, 07:08:42 PM »
oh yeah, i'll just edit it.

Offline xlr8or

  • [ www.xlrstats.com ]
  • Project Lead
  • Hero Member
  • *
  • Posts: 2057
    • The Art of Tactical Gaming
Re: Converting Simple Modifications into Plugins
« Reply #3 on: April 19, 2009, 09:37:41 PM »
Good post! Maybe something for the wiki as well...

Offline intrebulon

  • Full Member
  • ***
  • Posts: 84
    • Knight Of Fury Clan
Re: Converting Simple Modifications into Plugins
« Reply #4 on: April 20, 2009, 04:43:40 AM »
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 20, 2009, 04:50:27 AM by intrebulon »

Offline Paladino

  • Newbie
  • *
  • Posts: 4
Re: Converting Simple Modifications into Plugins
« Reply #5 on: January 25, 2010, 05:14:59 PM »
good job

Offline Wright

  • Full Member
  • ***
  • Posts: 133
  • eliteclangaming.com
    • Elite Gaming
Re: Converting Simple Modifications into Plugins
« Reply #6 on: June 26, 2010, 06:57:56 AM »
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.
xfire: blitzwright

Offline suryaarief

  • Jr. Member
  • **
  • Posts: 19
Re: Converting Simple Modifications into Plugins
« Reply #7 on: September 26, 2015, 09:46:26 AM »
Nice :)
Can you give some sample, like command rcon, to change fov or etc?
What is the code? Thanks.

Offline Fenix

  • Dev. Team
  • Hero Member
  • *
  • Posts: 999
    • GitHub
Re: Converting Simple Modifications into Plugins
« Reply #8 on: September 26, 2015, 06:11:25 PM »
This topic is quite old and I guess the original author is not around anymore. If you have a plugin request you should highlight what the plugin should do. For example you are asking commands to change fov: B3 needs an rcon command provided by the game server to do that (and possibly the server needs some modification to do that, which we do not support). Once you post the requirements we will be able to help you.

Offline suryaarief

  • Jr. Member
  • **
  • Posts: 19
Re: Converting Simple Modifications into Plugins
« Reply #9 on: September 27, 2015, 07:44:58 AM »
Thanks Bro Fenix, 
Sorry if I make a mistake, because i am still searching... searching.. and learning what the B3 plugin and Mod code for modify.
I am still cannot find the answer what code change fov and fps. Thanks very much :)

 


Rate this page +1 at Google Search

anything