I wrote this for an associate of mine who's trying to get into Bukkit programming yesterday, but I thought you guys'd like it too. It was a common methods/stuff you need to know kinda deal.
//Inventory manipulation:
//you need to get the inventory before you can do anything with it
Inventory i = player.getInventory();
//add an item; you add ItemStacks.
ItemStack it = new ItemStack(Material.TNT);
//add item/s
i.addItem(it); OR i.addItem(new ItemStack(Material.TNT, 1)); OR i.addItem(new ItemStack[] { new ItemStack(Material.TNT, 1), it});
//remove items
i.remove(it);, etc
//sending messages
player.sendMessage(string);
sender.sendMessage(string);
Bukkit.getServer().broadcastMessage(string);
//teleportation
player.teleport(world, x, y, z);
event.setRespawnLocation(world, x, y, z);
You can get worlds by doing Bukkit.getServer().getWorld(String worldname);
//hashmaps
These are the go-to thing for player-specific values.
The way hashmaps work, is that for every key(HashMap), there is an associated value(HashMap).
Do hashmap.put(playername, integer), to put the key and the value.
The best thing to do, is get the player's name, instead of the player object. Player objects tend to leak memory.
HashMap hash = new HashMap();
//arraylists
These are good things to use for teams, and stuff.
ArrayList redteam = new ArrayList();
redteam.add(player.getName());
redteam.remove(player.getName());
//events && listeners
You *must* register your listener classes in the onEnable().
public void onEnable() {
Bukkit.getServer().getPluginManager().registerEvents(new PluginListenerClass(this), this);
}
In your class declaration, it must implement Listener.
It must have a constructor, through which you pass the main class.
You must annotate it with @EventHandler.
You can name the method anything, it only looks for the event.
private MainClass plugin;
public PluginListenerClass(MainClass plugin) {
this.plugin = plugin;
}
@EventHandler
public void onPlayerJoin(PlayerJoinEvent e) {
e.kickPlayer("teehee");
}
//commands
IF YOU'RE USING A SEPARATE EXECUTOR CLASS: As with listeners, you must put something in the onEnable(), but, for this, it's the command you're executing.
onEnable() {
getCommand("testcommand").setExecutor(pluginexecutor(this));
}
You must also pass the main class through the constructor.
the executor class must
Unlike listeners, the command method has a exact format and name =
@Override
public void onCommand(CommandSender s, String command, String label(//this is the alias used for the command, if there is one), String[] arguments) {
if(command.getName().equalsIgnoreCase("testcommand") {
sender.sendMessage("you ran the test command!");
}
}
//permissions
Permissions are really simple.
You can either do if(player.hasPermission("plugin.random.permission"),
or you can put stuff in the plugin.yml, as follows:
//plugin.yml
You *have* to have a plugin.yml in your plugin, otherwise bukkit will not recognize your plugin.
The three required fields are name, main, and version. Name(obvious), main(the qualified main class of the plugin), and version.
YAML files are based on a space hierarchy.
DO NOT TAB. YAML does not recognize it. space twice for each level.
You must register each command and permission in the .yml.
Example .yml is below:
name: Plugin
main: pluginpackage.mainclass
version: 1.0
commands:
weedle:
usage: /weedle
description: weedle leedle
permission: plugin.weedle
permission-message: You don't have permission!
permissions:
plugin.*:
children:
plugin.weedle: true
default: op
plugin.weedle:
default: op
description: Permission
EDIT: Damn Xenforo, messing up mah spaces.
Etc.
If you have any questions, feel free to ask me or Kedalion, we'll be happy to help! :D