Forums.ATC.no

Teknisk => Generelt teknisk => Emne startet av: Cybersyn på 31. Januar 2016, 12:26 pm

Tittel: Bukkit API tips & tricks
Skrevet av: Cybersyn31. Januar 2016, 12:26 pm
Some of the more useful features of the Bukkit API are not very well documented outside the javadocs (https://hub.spigotmc.org/javadocs/bukkit/). I've stumbled upon quite a few:

Graphical UI using inventories (https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/inventory/package-frame.html)
It's possible to create inventories of any size, type and title on the fly using the org.bukkit.Server.createInventory() (https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/Server.html#createInventory%28org.bukkit.inventory.InventoryHolder,%20org.bukkit.event.inventory.InventoryType%29) series of functions. Combined with org.bukkit.event.inventory (https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/event/inventory/package-frame.html) events and org.bukkit.entity.HumanEntity.openInventory() (https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/entity/HumanEntity.html#openInventory%28org.bukkit.inventory.Inventory%29), you can create creative ways for players to interact with your plugin.

Scheduled tasks (https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/scheduler/package-frame.html)
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:
Example using method a:
Kode: [Velg]
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!");
    }
}
Example using method b:
Kode: [Velg]
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);
    }
}

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 (https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/inventory/Recipe.html). Then add the recipe to the server using org.bukkit.Server.addRecipe() (https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/Server.html#addRecipe(org.bukkit.inventory.Recipe)).
Some other functions for managing recipes:
Example:
Kode: [Velg]
// 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();
}

I'd also like to point out org.bukkit.conversations (https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/conversations/package-frame.html), though I've never used it myself.

Feel free to post your own tips & tricks.