Addon Making/Lesson 2 - Hello World

In this lesson we will create our first addon. First of all, let's summary what we will need for making an addon:

What we need

Addon interface
An interface connection for the addon to make the plugin able to handle it.
Basic API's of the plugin
Access to all basic stuff of the core (clevercommand) plugin
Addon loading data
Must define how the addon will be loaded: what other addons it needs, what is its name, and where to find the main class

Start coding

Now let's start to code our main class for our plugin. The .class file can be located in any package (except org.bukkit.* forbidden by bukkit), so make a new class somewhere, and lets start the code:

package x.y.z;

import hu.satoru.ccmd.addon.CCAddon;
import x.y.z.MyExecutor;

public class MyAddon extends CCAddon {

   //this is shown in the addon list, and other addons can find yours
   //by AddonManager.getAddonByName(String) method with this name
   public String getName() { return "MyTestAddon"; }

   private boolean enabled = false;

   //influences the plugin's reaction to this addon
   public boolean isEnabled() { return enabled; }

   public void onEnable() {
      if (enabled) return;
      exe.register();
      enabled = true;
   }

   public void onDisable() {
      if (!enabled) return;
      exe.unregister();
      enabled = false;
   }

   //the executor class of our addon (central handler of all commands)
   private MyExecutor exe = new MyExecutor(this);

}

If you want a really basic plugin, then do not write the followings, instead remove the exe variable from your plugin. Where it gets missed delete the lines (exe.register() and exe.unregister() lines), and jump the following code. If you want to continue to expand your addon to be able to handle its own commands then we need to start to code that AddonExecutor class:

package x.y.z;

import hu.satoru.ccmd.command.AddonExecutor;
import hu.satoru.ccmd.command.CCCArgs;

import org.bukkit.command.CommandExecutor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;

public class MyExecutor extends AddonExecutor implements CommandExecutor {
   //store the addon, altrough we could use a static
   //getInstance() call for the addon if we write one, in
   //that case we don't need to store it here
   private MyAddon addon;
   public MyAddon getAddon() { return addon; }

   //our own root (bukkit) command
   private RootCommand myAsdCmd;

   //executor initialization
   public MyExecutor(MyAddon addon) {
      this.addon = addon;
      myAsdCmd = new RootCommand("MyAsd");
      myAsdCmd.getAliases().add("asd");
      myAsdCmd.setExecutor(this);
   }

   //we overwrite the original AddonExecutor.register() method
   @Override
   public void register() {
      myAsdCmd.register(); //register our own root command
      super.register(); //do the original registering stuff too
   }

   //we overwrite the original AddonExecutor.unregister() method
   @Override
   public void unregister() {
      myAsdCmd.unregister(); //unregister our own root command
      super.unregister(); //do the original unregistering stuff too
   }
   
   //must return the right addon, if not, this executor won't be called
   public CCAddon/MyAddon getAddon() { return addon; }

   //the method which handles /cc commands
   //arg0 is the 1st argument lowercased, to make addons react faster
   public boolean runCmd(CCCArgs cmd, String arg0) {
      if (arg0.equals("asd")) {
         cmd.inform("[MyAddon] Asd cc command used!");
         return true;
      }
      return false;
   }

   //reacts to the bukkit commands,
   //but only for the ones which one's executor is this
   //in this case only one bukkit command has this as
   //its executor, the MyAsd command.
   public boolean onCommand(CommandSender sender, Command cmd,
          String label, String[] args) {
      sender.sendMessage("[MyAddon] MyAsd root command used!");
      return true;
   }

}

In this state our plugin is nearly complete. But now, we must add a file to the project, beside the "sources" or "src" folder, or whatever depends on what java developer tool you use. The goal is to that file got compiled into the root of the jar file. In Eclipse, it is beside the "src" folder. The .yml files can be edited with any text editors, for further use I advise the use of Notepad++.
Into the addon.yml file write:

name: MyAddon
main: x.y.z.MyAddon

When finished this, your plugin is ready to be runned. Compile it to a jar and place it into the "plugins\CleverCommand" folder. When you start or restart your server next time it will load the CleverCommand plugin (if it is placed into the plugins folder) and in this case it will load its addons. Then your addon file will be found, and got loaded immediately.

next lesson: Permissions
continue to lesson 3 --->


Comments

Posts Quoted:
Reply
Clear All Quotes