Listen for Events

Now we get to the best part. You know how to write code for commands the user types in and you know how to do things to affect the world. Now it’s time to see how to monitor what’s going on in the world so you can respond to gameplay automatically, without typing in a command or anything.

It works like this: you set up your code such that your functions get called when some interesting event happens. In your function, you can let the events happen or you can stop them.

Here’s a skeleton of what we need to add to our basic plugin in order to incorporate a listener. There are four parts to it:

  1. Import the plugin listener and the event hook(s) classes.

  2. Declare that your plugin implements PluginListener.

  3. Register to listen ...

Get Learn to Program with Minecraft Plugins, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.