Teknisk > Generelt teknisk

Bukkit API tips & tricks

(1/1)

Cybersyn:
Some of the more useful features of the Bukkit API are not very well documented outside the javadocs. I've stumbled upon quite a few:

Graphical UI using inventories
It's possible to create inventories of any size, type and title on the fly using the org.bukkit.Server.createInventory() series of functions. Combined with org.bukkit.event.inventory events and org.bukkit.entity.HumanEntity.openInventory(), you can create creative ways for players to interact with your plugin.

Scheduled tasks
You can schedule tasks to run later or repeatedly. You can also run tasks asynchronously to avoid clogging up your main thread. There are mainly two ways to schedule tasks:

* using org.bukkit.scheduler.BukkitRunnable
* using org.bukkit.Server.getScheduler() and java.lang.RunnableExample using method a:

--- Kode: ---public class Broadcaster extends JavaPlugin {
    private final MyRunnable runnable = new MyRunnable(this.getServer());
   
    @Override
    public void onEnable() {
        // run task every minute (=1200 ticks)
        this.runnable.runTaskTimer(this, 0, 1200);
    }
   
    @Override
    public void onDisable() {
        // cancel task
        this.runnable.cancel();
    }
}

class MyRunnable extends BukkitRunnable {
    private final Server server;
   
    public MyRunnable(Server server) {
        this.server = server;
    }
   
    @Override
    public void run() {
        this.server.broadcastMessage("It works!");
    }
}

--- Slutt kode ---
Example using method b:

--- Kode: ---public class Broadcaster extends JavaPlugin implements Runnable {
    @Override
    public void run() {
        this.getServer().broadcastMessage("It works!");
    }

    @Override
    public void onEnable() {
        // run task every minute (=1200 ticks)
        this.getServer().getScheduler().runTaskTimer(this, this, 0, 1200);
    }

    @Override
    public void onDisable() {
        // cancel all tasks scheduled by this plugin
        this.getServer().getScheduler().cancelTasks(this);
    }
}

--- Slutt kode ---

Custom recipes
You can't create custom items, but you can add custom recipes.
Define the recipe by instantiating one of the classes implementing org.bukkit.inventory.Recipe. Then add the recipe to the server using org.bukkit.Server.addRecipe().
Some other functions for managing recipes:

* org.bukkit.Server.clearRecipes() removes all recipes (even default ones).
* org.bukkit.Server.getRecipesFor() lists all recipes for producing a specific item stack.
* org.bukkit.Server.recipeIterator() retrieves an iterator that can be used to remove single recipes.
* org.bukkit.Server.resetRecipes() resets the recipe manager to defaults, removing non-default recipes.Example:

--- Kode: ---// define recipe
public final Recipe myRecipe = new FurnaceRecipe(new ItemStack(Material.WATER_BUCKET), Material.ICE);

@Override
public void onEnable() {
    // add recipe
    this.getServer().addRecipe(myRecipe);
}

@Override
public void onDisable() {
    // remove recipe
    Iterator<Recipe> iterator = this.getServer().iterateRecipes();
    while (iterator.hasNext()) if (iterator.next() == this.myRecipe) iterator.remove();
}

--- Slutt kode ---

I'd also like to point out org.bukkit.conversations, though I've never used it myself.

Feel free to post your own tips & tricks.

Navigering

[0] Oversikt

Skift til full visning