How to make a new PPF Plugin

Note: Eclipse 3.2.1 has been used and it assumed that the PPF, PircBot_1.4.6, and PPF-OSGi-Dependents projects are in the workspace

File -> New -> Project...

Plug-In Development -> Plug-in Project


Next >

Project Name: HelloWorld
Change Target Platform to use an OSGi framework and select Equinox


Next >

Activator: net.sourceforge.ppf.plugin.helloworld.Activator



Finish

Now you will end up with a new project looking something like this:


Double click the MANIFEST.MF file to open the plugin editor.
Click the Dependencies tab at the bottom.
In the Required Plugins section, click the Add... button.
A dialogue will appear with all of the plugins available in.
Enter p into the text entry to get it to list only plugins starting with p:


Select PircBot and PPF and press OK
Save the file.

Now we will make the functionality for the plugin, in this case we just want a response from the bot when someone types "hello" into a channel.
Select the HelloWorld project folder, then the src folder, then select the package net.sourceforge.ppf.plugin.helloworld



Right click on the selected package and select New -> Class
Into the Name: field, enter: HelloWorld
Finish

Now the editor opens up for HelloWorld. We want to extend PPFPlugin.
We also want to act when a message is typed in a channel so the PircBot onMessage() needs to be overridden. To do this, we implement the IMessage interface. Change the class definition to: public class HelloWorld extends PPFPlugin implements IMessage {

Every onXYZ() method from the PircBot API (plus onAuth() from PPF) has its own interface which is named the same but without the on prefix. These are also the interfaces used when PPF needs to tell each plugin that something has happened (so by implementing IMessage, you are saying that your plugin wants to know about any onMessage() event). This is the service interface. It also makes it easy to add the correct method signature with Eclipse as you can use the Quick Fix and add any unimplemented methods. When you have done that, add the functionality into the onMessage method so the end result will be:

package net.sourceforge.ppf.plugin.helloworld; import net.sourceforge.ppf.PPFPlugin; import net.sourceforge.ppf.service.IMessage; public class HelloWorld extends PPFPlugin implements IMessage { public void onMessage(String channel, String sender, String login, String hostname, String message) { if(message.equalsIgnoreCase("hello")) { getBot().sendMessage(channel, sender +": Hello World!"); } } }
Save the file.

Now we will change the generated Activator class to extend PPFAbstractBundleActivator. This takes care of the normal bundle start and stop as well as getting a reference to the bot. All we need to do here is create an instance of the plugin class and register each service that we want to notified of.
package net.sourceforge.ppf.plugin.helloworld; import net.sourceforge.ppf.PPFAbstractBundleActivator; import net.sourceforge.ppf.PPFPlugin; import net.sourceforge.ppf.service.IMessage; import org.osgi.framework.BundleContext; public class Activator extends PPFAbstractBundleActivator { public PPFPlugin init(BundleContext context) { HelloWorld plugin = new HelloWorld(); context.registerService(IMessage.class.getName(), plugin, null); return plugin; } }
Save the file.

Now we want to deploy the plugin and use it in the bot.

Right click the HelloWorld project folder and choose Export...

Plug-In Development -> Deployable plug-ins and fragments


Next >

Select the Destination tab at the bottom and choose Directory: and enter C:\PPF-OSGi
Select the Options tab and check Save as Ant script:. Browse to your HelloWorld directory root and enter the name as deploy.xml
Finish

Going back to the running build console:
type: install file:plugins/HelloWorld_1.0.0.jar
type: start 5 (or whatever the bundle number is given as when installing)

Now the HelloWorld plugin has been added and started. Type hello into a channel the bot is in and it will now respond.