Developers

Developers

Please be aware this is a W.I.P.

MonsterIRC plugins are a way to enhance MonsterIRC. The posiblitys with miniplugins are limitless from channel moderation to fun plugins.


What you will need :


Create the project :

Now that you have downloaded the programs I will show you how to set up Eclipse (same as most IDE's)


  • First of all, create a new project and set this up as demonstrated :

http://dl.dropbox.com/u/62801617/Tuto Miniplugin/1.jpg

tut2

  • Now you will get a new page :
    • Select the tab "Libraries" and click "Add external Jars"
    • You will have to choose both last Bukkit and MonsterIRC jars
    • Click "Open" and then "Finish"

      tut3

  • You have created your project ! You have to create the first class of the plugin :

    • Under the PluginName directory, right click "src" and select new->class


      tut4

      tut5

Creating your first plugin:

Now that you have eclipse set up you will get something like this on your screen:
http://dl.dropbox.com/u/62801617/Tuto Miniplugin/FirstClass.jpg

Add :
http://dl.dropbox.com/u/62801617/Tuto Miniplugin/PluginManifest.jpg
Right above :
http://dl.dropbox.com/u/62801617/Tuto Miniplugin/public class.jpg


Example plugin

Here is a little plugin I made that auto responds to people

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerChatEvent;
import org.monstercraft.irc.ircplugin.IRCPlugin;
import org.monstercraft.irc.ircplugin.PluginManifest;
import org.monstercraft.irc.ircplugin.event.listeners.IRCListener;
import org.monstercraft.irc.plugin.wrappers.IRCChannel;
import org.monstercraft.irc.plugin.wrappers.IRCServer;

//Must have a plugin manifest and extends IRC plugin for it to be a valid plugin.
@PluginManifest(name = "Simple Auto Responder")
public class SimpleAutoResponder extends IRCPlugin implements IRCListener,
		Listener {

	private FileConfiguration config;
	private List<String> input;
	private List<String> output;

	@Override
	public void onFinish() {
		// Logs that it has successfully stopped.
		log("Simple auto responder has stopped.");
	}

	@Override
	public boolean onStart() {
		// Gets the file configuration.
		config = getConfig();

		// Creats an empty array list for us to save the settings to.
		output = new ArrayList<String>();
		input = new ArrayList<String>();

		// The settings file to load
		File SETTINGS_FILE = new File(getCacheDirectory() + File.separator
				+ "Config.yml");

		// Checks if the file exists.
		boolean exists = SETTINGS_FILE.exists();

		if (exists) {
			try {
				// Loads the settings file if it exists
				config.load(SETTINGS_FILE);

				// set the variables
				input = config.getStringList("AUTO_RESPONDER.MESSAGE");
				output = config.getStringList("AUTO_RESPONDER.RESPONSE");
			} catch (Exception e) {
				debug(e);
			}
		} else {
			// add samples so people know how to correctly modify it
			input.add("hi");
			output.add("Hello.");

			// set the configs
			config.set("AUTO_RESPONDER.MESSAGE", input);
			config.set("AUTO_RESPONDER.RESPONSE", output);

			// save the configs
			saveConfig(config, SETTINGS_FILE);
		}

		// Logs in the console that we have started up successfully
		log("Simple auto responder started.");

               //registers the bukkit listeners
		registerBukkitListener(this);
		return true;// True for the plugin to start, otherwise false to just kill the plugin
	}

	public void onAction(IRCChannel arg0, String arg1, String arg2) {
	}
	public void onConnect(IRCServer arg0) {
	}
	public void onDisconnect(IRCServer arg0) {
	}
	public void onJoin(IRCChannel arg0, String arg1) {
	}
	public void onKick(IRCChannel arg0, String arg1, String arg2) {
	}

	public void onMessage(IRCChannel c, String sender, String msg) {
		// Check if the input strings
		for (String s : input) {
			// Get the indes of the string in the input
			int index = input.indexOf(s);
			// Check if the input contains the string
			if (LineContainsWord(msg, s)) {
				// check for the output message
				if (output.get(index) != null) {
					// send the output message
					sendMessage(c, output.get(index));
				} else {
					// The output message was null, something in the config was
					// wrong.
					log("Invalid configuration file for SimpleAutoResponder");
				}
			}
		}
	}
	public void onMode(IRCChannel arg0, String arg1, String arg2, String arg3) {
	}
	public void onPart(IRCChannel arg0, String arg1) {
	}
	public void onPrivateMessage(String arg0, String arg1, String arg2) {
	}
	public void onQuit(IRCChannel channel, String arg1) {
	}

	public boolean LineContainsWord(final String line, final String Word) {
		try {
			return Pattern.compile("\\b" + Word + "\\b").matcher(line).find();
		} catch (IllegalStateException e) {
			return false;
		} catch (Exception e) {
			return false;
		}
	}

	@EventHandler
	public void onPlayerChat(PlayerChatEvent event) {
		// Check if the input strings
		for (String s : input) {
			// Get the indes of the string in the input
			int index = input.indexOf(s);
			// Check if the input contains the string
			if (LineContainsWord(event.getMessage(), s)) {
				// check for the output message
				if (output.get(index) != null) {
					// send the output message
					event.getPlayer().sendMessage(output.get(index));
				} else {
					// The output message was null, something in the config was
					// wrong.
					log("Invalid configuration file for SimpleAutoResponder");
				}
			}
		}
	}

}



Let's break this section by section :




  • First there are the imports.
    You have to tell the compiler the locations of the classes you don't make on your own. Called external classes.
    Eclipse will tell you which imports you'll have to use, looking for it in the libraries you have set up (Bukkit and MonsterIRC Jars).

http://dl.dropbox.com/u/62801617/Tuto Miniplugin/Imports.2.jpg




  • The @PluginManifest annotation is required. This tells MonsterIRC that you are a plugin!! We have seen this in the first section.

@PluginManifest(name = "Simple Auto Responder")




  • Next we have the Main class for your plugin

http://dl.dropbox.com/u/62801617/Tuto Miniplugin/MainClass.jpg



You can see that I extend the abstract class IRCPlugin This acts as the base for your plugin.
Next you can see I implement IRCListener IRCListener is a way of telling your plugin what is going on in IRC by firing events such as onMessage.
Lastly you can see I also implement Listener, this is just bukkits way of firing events.


  • Next are the variables, anyone who is a bit familiar with Java can get this, otherwise, read the / / comments

http://dl.dropbox.com/u/62801617/Tuto Miniplugin/variables.jpg



The onStart and onFinish methods are called when your plugin is started and stopped. Pretty self explanatory. As you can see in my sample plugin I load all of the settings when starting up. onStart will return true if you want to start the plugin otherwise false to stop it.


  • Then I implemented some methods which are called when an event happens in IRC :

http://dl.dropbox.com/u/62801617/Tuto Miniplugin/onActionMethods.jpg

All of the above methods are called when an event happens in IRC. As you can see I make use of the onMessage by checking what the message is, and then replying to it. All of the methods are called when an event is fired in IRC. To have this you must implement IRCListener


  • Finally, here is the exact same method as onMessage when a player chats in the game and not in irc :

http://dl.dropbox.com/u/62801617/Tuto Miniplugin/EventHandler.jpg

This is it for the moment, more to come soon :)


Comments

Posts Quoted:
Reply
Clear All Quotes