-
Notifications
You must be signed in to change notification settings - Fork 2
Documenting event system and world multithreading. General improvements. #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Janmm14
wants to merge
15
commits into
gomint:master
Choose a base branch
from
Janmm14:patch-1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ee10602
Start documenting event system
Janmm14 9ba61a1
Small fixes based on omniboy's feedback
Janmm14 77f6b6c
wip
Janmm14 1b244d6
Create plugin-world-restriction.md
Janmm14 5b82c15
Update getting-started.md
Janmm14 276a7a7
Update getting-started.md
Janmm14 6a3b164
Update creating-a-first-plugin.md
Janmm14 44e3ca3
Update creating-your-first-event-listener.md
Janmm14 747cff8
Update creating-your-first-event-listener.md
Janmm14 ee19ef9
Merge branch 'master' into patch-1
Janmm14 0485895
Update creating-your-first-event-listener.md
Janmm14 2962656
Update creating-your-first-command.md
Janmm14 c591e03
stuff
Janmm14 36d2848
ignore intellij idea files
Janmm14 6549b73
Update documentation to latest changes in world multithreading PR.
Janmm14 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| --- | ||
| id: creating-your-first-event-listener | ||
| title: Creating Your First Event Listener | ||
| sidebar_label: Creating Your First Event Listener | ||
| --- | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| This guide assumes that you have a [plugin environment](creating-a-first-plugin.md) setup. | ||
| If you have not already, follow that guide first. | ||
|
|
||
| ## Event Concept | ||
|
|
||
| Many actions done by players, blocks or mobs trigger an event. Technically: a class extending `Event`. Events allow you to know, edit or cancel default behaviour. | ||
| For your plugin to get informed when an event happens, you need to create a class implementing `EventListener` (a marker interface): | ||
|
|
||
| ```java | ||
| public class MyFirstEventListener implements EventListener {} | ||
| ``` | ||
|
|
||
| Now we marked the class for us and the api as event listener. Then we have to register our class to the API. We do that for example in the `onInstall` method in our `TestPlugin` class. Note that we use the method [`registerActiveWorldsListener`](https://janmm14.de/static/gomint/gomint.api/io/gomint/plugin/Plugin.html#registerActiveWorldsListener(io.gomint.event.EventListener)) which automatically filters events so we only get events taking place in any of the [plugin's active worlds](../get-started/plugin-world-restriction.md). | ||
|
|
||
| ```java | ||
| @Override | ||
| public void onInstall() { | ||
| registerActiveWorldsListener(new MyFirstEventListener()); | ||
| } | ||
| ``` | ||
| Next up we need to define which event(s) we want to listen to in this event listener. | ||
| This is done simply by creating a method annotated with `@EventHandler` which takes one argument (the event we want to listen to) and returns `void`: | ||
|
|
||
| ```java | ||
| public class MyFirstEventListener implements EventListener { | ||
| @EventHandler | ||
| public void onExplode(EntityExplodeEvent event) { | ||
| // this method gets called every time an entity explodes | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| Now we just need to implement some logic. We can praise explosions for example with a chat message sent to every player: | ||
|
|
||
|
|
||
| ```java | ||
| public class MyFirstEventListener implements EventListener { | ||
| @EventHandler | ||
| public void onExplode(EntityExplodeEvent event) { | ||
| GoMint.instance().onlinePlayers().forEach(p -> p.sendMessage("Hooray, " + event.affectedBlocks().size() + " are gone!")); | ||
|
Janmm14 marked this conversation as resolved.
|
||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Cancellable events | ||
|
|
||
| We changed our minds now and want to prevent explosions from happening. Great that [EntityExplodeEvent](https://janmm14.de/static/gomint/index.html?gomint.api/io/gomint/event/entity/EntityExplodeEvent.html) extends [CancellableEvent](https://janmm14.de/static/gomint/index.html?gomint.api/io/gomint/event/CancellableEvent.html). To cancel events we write this method in our event listener: | ||
|
|
||
| ```java | ||
| @EventHandler | ||
| public void preventExplosions(EntityExplodeEvent event) { | ||
| event.cancelled(true); | ||
| } | ||
| ``` | ||
|
|
||
| ## ```@EventHandler``` options | ||
|
|
||
| | option | possible values | | ||
| |-----------------|-----------------| | ||
| | priority | [EventPriority](https://janmm14.de/static/gomint/index.html?gomint.api/io/gomint/event/EventPriority.html) enum: `LOWEST`, `LOW`, `NORMAL` (default), `HIGH`, `HIGHEST` | | ||
| | ignoreCancelled | <ul><li>`true` method will not be called for cancelled events<br></li><li>`false` (default) method will be called regardless of event cancelled state</li></ul> | | ||
|
|
||
| ### Details on priority option | ||
|
|
||
| We have two event listeners for the same event. So how do we define the order in which they will execute? | ||
|
|
||
| This is possible with the priority option of the `@EventHandler` annotation. | ||
|
|
||
| ```java | ||
| @EventHandler(priority = EventPriority.HIGHEST) | ||
| ``` | ||
|
|
||
| Priority `LOWEST` is called first, `HIGHEST` will be called last. So you should choose `HIGHEST` if you want to monitor the result of an event, use `HIGH` to override other plugins and use `LOWEST` or `LOW` for changes other plugins who listen on a higher priority should be able to react to. | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.