Line 22: |
Line 22: |
| | | |
| == Event Bus == | | == Event Bus == |
− | An '''event bus''' is an object which holds a list of event listeners, and the logic for firing the events. Events may be posted on these event buses, which then invokes the handlers. | + | An '''event bus''' is an object which holds a list of event listeners, and the logic for firing the events. Events may be posted on these event buses, which then invokes the handlers. The main class for event buses is <code>IEventBus</code>, and a bus is created using <code>BusBuilder</code>. |
| | | |
| === Existing Buses === | | === Existing Buses === |
Line 28: |
Line 28: |
| | | |
| ==== Main Forge Event Bus ==== | | ==== Main Forge Event Bus ==== |
− |
| |
| The '''main Forge event bus''' is located at <code>MinecraftForge#EVENT_BUS</code>, and is where most events relating to ingame actions or events are fired on, such as events for ticking, block interactions, and entity interactions. | | The '''main Forge event bus''' is located at <code>MinecraftForge#EVENT_BUS</code>, and is where most events relating to ingame actions or events are fired on, such as events for ticking, block interactions, and entity interactions. |
| | | |
Line 38: |
Line 37: |
| | | |
| ==== Mod-Specific Event Buses ==== | | ==== Mod-Specific Event Buses ==== |
− |
| |
| The '''mod-specific event buses''' are the family of event buses where mod-related initialization and registration events are fired, such as the events for [[Registration|registering objects]] or setup on different physical sides. Only events which implement <code>IModBusEvent</code> may be fired or listened for on these event buses. | | The '''mod-specific event buses''' are the family of event buses where mod-related initialization and registration events are fired, such as the events for [[Registration|registering objects]] or setup on different physical sides. Only events which implement <code>IModBusEvent</code> may be fired or listened for on these event buses. |
| | | |
Line 46: |
Line 44: |
| | | |
| ==== Network Channel Event Buses ==== | | ==== Network Channel Event Buses ==== |
− |
| |
| The '''network channel event buses''' are the family of event buses where different network-related events are fired. Each registered [[Networking|networking channel]] has their own instance of an event-bus in <code>NetworkInstance</code>, where only events pertinent to that channel are fired on. | | The '''network channel event buses''' are the family of event buses where different network-related events are fired. Each registered [[Networking|networking channel]] has their own instance of an event-bus in <code>NetworkInstance</code>, where only events pertinent to that channel are fired on. |
| | | |
| The network channel event buses cannot be accessed directly; <code>EventNetworkChannel</code> provides methods to register events listeners to the event bus. | | The network channel event buses cannot be accessed directly; <code>EventNetworkChannel</code> provides methods to register events listeners to the event bus. |
| | | |
| + | == Event Listeners == |
| + | An '''event listener''' (also known as an '''event handler''') is a class, object, or method registered to an event bus to capture for specific events (and their subclasses). |
| + | |
| + | [[File:Guide to Event Handlers.png|thumb|upright 0.9|A visual guide on how event listeners are registered.]] |
| + | |
| + | An event listener may be registered in three ways: |
| + | * The <code>IEventBus#register(Object)</code> method on a class or instance; |
| + | * The <code>@EventBusSubscriber</code> annotation on a class; or |
| + | * The <code>IEventBus#addListener</code> and <code>IEventBus#addGenericListener</code> on a method reference or lambda. |
| + | |
| + | === Priority and Receiving Cancelled Events === |
| + | An event listener may be registered to a specific '''event priority''' and whether to '''receive cancelled events'''. |
| + | |
| + | The event priority levels allows a listener to react to an event before other event listeners of a lower level, such as to change the values within an event or cancel it outright. |
| + | |
| + | There are five levels of event listeners priority; in descending order from first to receive an event to last: <code>HIGHEST</code>, <code>HIGH</code>, <code>NORMAL</code>, <code>LOW</code>, and <code>LOWEST</code>. Event listeners are registered by default on a priority of <code>NORMAL</code>. |
| + | |
| + | An event listener normally never receives a cancelled event, however they may change this by being registered to receive these cancelled events. Non-cancellable events are unaffected by this, and will always be sent to all event listeners (in the order specified by their priority). |
| + | |
| + | === <tt>IEventBus.register</tt> === |
| + | The <code>IEventBus#register(Object)</code> method allows for registering an object instance or a <code>Class<?></code> to the event bus. The method exhibits two different behaviors, depending on what is passed into it: |
| + | |
| + | * '''an object instance''' - registers all ''instance or non-<code>static</code>'' methods annotated with <code>@SubscribeEvent</code> from the object |
| + | * '''a <code>Class<?></code> instance''' - registers all ''class or <code>static</code>'' methods annotated with <code>@SubscribeEvent</code> from the class which is represented by the passed in <code>Class<?></code> |
| + | |
| + | <tab collapsed name="Example of how event listeners are registered with the IEventBus.register method"> |
| + | <syntaxhighlight lang="java"> |
| + | class EventHandler { |
| + | public static void nonAnnotatedStatic(Event event) {} |
| + | |
| + | @SubscribeEvent |
| + | public static void annotatedStatic(Event event) {} |
| + | |
| + | public void nonAnnotatedInstance(Event event) {} |
| + | |
| + | @SubscribeEvent |
| + | public void annotatedInstance(Event event) {} |
| + | } |
| + | |
| + | // Within the execution of the program |
| + | IEventBus bus = ...; |
| + | |
| + | // This will register the "annotatedStatic" method from the class |
| + | bus.register(EventHandler.class); |
| + | // This would register "annotatedStatic", but it is already registered |
| + | bus.register(EventHandler.class); |
| + | |
| + | EventHandler instanceA = new EventHandler(); |
| + | EventHandler instanceB = new EventHandler(); |
| + | |
| + | // This will register the "annotatedInstance" method from the instanceA object, and will not touch the instanceB object |
| + | bus.register(instanceA); |
| + | </syntaxhighlight> |
| + | If an <code>Event</code> were to be fired on the event bus in the example above, the <code>EventHandler#annotatedStatic</code> would receive the event, and the <code>annotatedInstance</code> method from the <code>instanceA</code> object instance would receive the event. |
| + | |
| + | Neither the unnannotated methods will receive the event, nor will the <code>annotatedInstance</code> method from the <code>instanceB</code> object instance. |
| + | |
| + | </tab> |
| + | |
| + | ==== <tt>@SubscribeEvent</tt> ==== |
| + | The <code>@SubscribeEvent</code> annotation is used to mark a method as an event listener when its containing class or object is registered using <code>IEventBus#register</code>. |
| + | |
| + | They have two fields: <code>EventPriority priority</code> and <code>boolean receiveCancelled</code>, which sets the event priority for the listener and whether the listener receives cancelled events, respectively. |
| | | |
| + | = WIP = |
| + | TODO: @EventBusSubscriber, addListener, Generic Events? (probably higher up) |
| | | |
| | | |
| + | <tab name="Partial previous content of page, to be removed" collapsed> |
| == Forge and Mod Buses == | | == Forge and Mod Buses == |
| There are two event buses of note to a mod: the '''main Forge event bus''', and the '''mod-specific event bus'''. | | There are two event buses of note to a mod: the '''main Forge event bus''', and the '''mod-specific event bus'''. |
Line 283: |
Line 346: |
| | | |
| Like before, each of these events (especially the Super Events, which contain most of the useful events in the game) will have their own article explaining their purpose and usefulness. | | Like before, each of these events (especially the Super Events, which contain most of the useful events in the game) will have their own article explaining their purpose and usefulness. |
| + | </tab> |