Skrevet av Emne: Bukkit API tips & tricks  (Lest 11392 ganger)

Utlogget Cybersyn

  • n00b
  • *
  • Innlegg: 2
  • Karma: +1/-0
    • Vis profil
    • localhost
Bukkit API tips & tricks
« på: 31. Januar 2016, 12:26 pm »
  • [applaud]0
  • [smite]0
  • 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:
    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. Then add the recipe to the server using org.bukkit.Server.addRecipe().
    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, though I've never used it myself.

    Feel free to post your own tips & tricks.
    « Siste redigering: 31. Januar 2016, 13:24 pm av BrutalOst »