Tutorial Making a successful Bukkit plugin. #2 Events

Discussion in 'Resources' started by PhantomUnicorns, Mar 4, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    PhantomUnicorns

    Here is the second Bukkit plugin tutorial of the series, number 1 can be found here.

    Introduction:
    We are now going to deal with events. Events are certain methods with a certain parameter and a certain annotation (@EventHandler) So first let's decuss what we would like to do. For this tutorial I'll be doing a mini-plugin that increases the amount of blocks dropped from anything.

    Notice how this next part is in general for all events.
    We need to register our event in our onEnable, like so:
    Code:
    public void onEnable() {
      Bukkit.getPluginManager().registerEvents(Listener, MainClass/JavaPlugin);
    }
    
    For the Listener we create a new class that implements Listener. For MainClass/JavaPlugin you included the instance of the class we are already in, this. Inside the class that we implemented Listener, we create our onItemSpawn method w/ the @EventHandler annotation.
    Code:
    @EventHandler
    public void onItemSpawn (ItemSpawnEvent event) {
    }
    
    Now we store the itemstack that is droping
    Code:
    ItemStack drop = event.getEntity().getItemStack();
    
    Now we simply double the amount of the itemstack inside of that:
    Code:
    int amount = drop.getAmount();
    drop.setAmount(amount * 2);
    

    And this is done! For the plugin.yml it is the same thing as tutorial one!
    NOTE: Here for all events!
     
    Zombie_Striker likes this.
Thread Status:
Not open for further replies.

Share This Page