Saturday, April 7, 2012

Minecraft - How to create plugins for Bukkit

  1. Start by reading this - http://wiki.bukkit.org/HUGE_Plugin_Tutorial
  2. Download the latest Bukkit Library - Bukkit API - Development Snapshot 
  3. Download Eclipse(Java Development Tool) - http://www.eclipse.org/
  4. (optional) Setup a github account - https://github.com/
  5. Use examples from source code from generous developers
  6. Use a decompiler to view source code from any jar file
    1. Download   this decompiler
    2. Download any plugin as a jar file
    3. Extract the jar file using winzip
    4. Browse to any class file that was extracted
    5. Open it in the decompiler to view the code

Overview
----------------------------------
You write your code in Eclipse.
You can connect the Git to your project folder in Eclipse to use the Github online version control to aid in your development.
Save and export in Eclipse to a java plugin file.
Place that file in the plugins directory for your bukkit server to test/use it.

Step by Step
----------------------------------
Create a new project

  • Open Eclipse
  • File>New>Java Project
  • Enter a project name (Eg. HelloWorld)
  • Libraries>Add External Libraries> Add the library you downloaded from  Bukkit API - Development Snapshot 
  • Click Finish, you will see your project on the left in the Package explorer bar
  • In the Package Explorer bar on the left, expand your project, expand Referenced Libraries, Right click on the bukkit library, go to properties
  • Click Javadoc Location, enter this http://jd.bukkit.org/apidocs/
  • Click Validate, then OK, OK
  • Right click on your project name (HelloWorld)
  • Click New>File name it plugin.yml
  • Drag your plugin.yml into the editing window on the right to edit it.
  • Your plugin.yml should look like this
---------------------------------------------------------------------------------------------------
name: HelloWorld
main: com.github.Username.HelloWorld.HelloWorld
version: 1.0
commands:
h:
description: This command will say "Hello World".
usage: /h
e:
description: This is another command for something else.
usage: /e
---------------------------------------------------------------------------------------------------------
  • Right click src>New>Package
  • Name your package according to the tutorial (eg. com.github.username.HelloWorld)
  • Right click on the package, click New>Class
  • Name your class the same as the package (eg. HelloWorld)
  • In the editing window on the right, this code will give an example of a simple HelloWorld plugin
----------------------------------------------------------------------------------------------------
package com.github.Username.HelloWorld;

import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.logging.Logger;

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

import org.bukkit.entity.Player;

public class HelloWorld extends JavaPlugin{

protected static final Logger log = Logger.getLogger("Minecraft");

public void onEnable(){ 
log.info("HelloWorld is enabled");
}
 
public void onDisable(){ 
log.info("Helloworld is disabled");
}

public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){

// If the player typed /h then do the following...
if(cmd.getName().equalsIgnoreCase("h")){ 
// Sends message to sender only
sender.sendMessage( "You yell - Hello World!");
// Sends message to all players on server
Bukkit.getServer().broadcastMessage( "You hear " + sender.getName() + " yell - Hello World!");
// Sends message to console only
System.out.println("Hello World!");
return true;
} //If this has happened the function will break and return true.

System.out.println("debug-Command did not work");
return false; 
}

}
-------------------------------------------------------------------------------------------------------


  • To use the plugin on your server
  • Click File>Export>Jar File>Next>
  • Select your project, it will select everything
  • Select the export destination, click browse, browse to where you want to create the plugin file
  • Click Finish
  • It will create the HelloWorld.jar file in that location.
  • You can now place this file in your plugins folder on your bukkit server
All of my projects and examples can be found on https://github.com/Cowmaster

No comments:

Post a Comment