API Documentation/Example 03: Capturing head drop events & changing drops

Head drop events represent situations when a PlayerHeads-supported head item is being dropped from either a beheading or breaking a block.  With the use of a listener, it's possible to capture these events.

As a completely arbitrary example, the following listener prevents all Zombie heads from dropping from beheadings.

ZombieBlockerListener.java:

public class ZombieBlockerListener implements listener{
	private HeadType zombie;
	private PlayerHeadsAPI api;
	private Plugin yourPlugin;
	
	public ZombieBlockerListener(Plugin yourPlugin){
		this.yourPlugin = yourPlugin;
		api = PlayerHeads.getApiInstance();
		zombie = api.getHeadOf(EntityType.ZOMBIE);
	}
	
	@EventHandler(ignoreCancelled=true)
	public void onBehead(LivingEntityDropHeadEvent event){
		ItemStack drop = event.getDrop();
		if(drop==null) return;//don't process null drops.
		HeadType type = api.getHeadFrom(drop);
		if(type.equals(zombie))
			event.setCancelled(true);
	}
}

 And of course, don't forget to register the listener from your plugin's onEnable method:

getServer().getPluginManager().registerEvents(new ZombieBlockerListener(this), this);

Changing drops (5.2.0+)

Since 5.2.0 the *DropHead events have supported the setDrop(...) method, which lets you easily change the dropped item from your event handler.

 

Changing drops (5.3 only - not released) 5.3 API rewrites have been tabled at this time

In 5.3 the *DropHead events will support multiple item drops, which can be viewed by accessing or changed by modifying the list returned by the getDrops() method.