Your first Lambda Event Listener

You can easily add event listeners using so-called lambda expressions.

Catalyst.events()
		.on(PlayerJoinEvent.class)
    .perform(e -> e.getPlayer().sendMessage("Hello there!"));

This is an example listener, that sends “Hello there!” to every player who joins.

<aside> ⚠️ Important! You must specify your lambda expressions in onEnable() in order to work like intended. This means you must call them directly in onEnable() or use a wrapper method that contains your lambdas.

</aside>

public class CatalystExample extends JavaPlugin {

    @Override
    public void onEnable() {

        Catalyst.init(this);

				/* Here is an example where the lambda is called directly */
        Catalyst.events()
						.on(PlayerJoinEvent.class)
            .perform(e -> e.getPlayer().sendMessage("Hello there!"));

    }

}
public class CatalystExample extends JavaPlugin {

    @Override
    public void onEnable() {

        Catalyst.init(this);

				/* Here is an example where the lambdas are called from a wrapper*/
        loadLambdas();

    }

		private void loadLambdas() {

        Catalyst.events()
						.on(PlayerJoinEvent.class)
            .perform(e -> e.getPlayer().sendMessage("Hello there!"));

        Catalyst.events()
						.on(PlayerQuitEvent.class)
            .perform(e -> {

								for(Player player : Bukkit.getOnlinePlayers()) {
										player.sendMessage(e.getPlayer().getName() + "has left the server.");
								}

						});

		}

}

Cancel Clauses

You can specify scenarios when the event should be cancelled

/* You can't break any blocks except Emerald Blocks */
Catalyst.events()
		.on(BlockBreakEvent.class)
    .perform(e -> {

				e.getPlayer().sendMessage("You broke an emerald block.");

		})
		.cancelIf(e -> e.getBlock().getType() != Material.EMERALD_BLOCK);

Of course you can cancel the event inside the perform( ) with e.setCancelled(true), but it looks cooler isn’t it?

NEXT: AUTOLOADING LISTENERS