|
| 1 | +/* |
| 2 | + * This file is part of Necrify (formerly Velocity Punishment), which is licensed under the MIT license. |
| 3 | + * |
| 4 | + * Copyright (c) 2022-2024 JvstvsHD |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +package de.jvstvshd.necrify.api.event; |
| 26 | + |
| 27 | +import org.greenrobot.eventbus.EventBus; |
| 28 | +import org.greenrobot.eventbus.Logger; |
| 29 | +import org.greenrobot.eventbus.Subscribe; |
| 30 | +import org.greenrobot.eventbus.ThreadMode; |
| 31 | +import org.jetbrains.annotations.NotNull; |
| 32 | + |
| 33 | +import java.util.Objects; |
| 34 | +import java.util.concurrent.CompletableFuture; |
| 35 | +import java.util.concurrent.ExecutorService; |
| 36 | + |
| 37 | +/** |
| 38 | + * The event dispatcher is used to dispatch events to the event bus. Events may be cancelled or dispatched synchronously or asynchronously. |
| 39 | + * Moreover, listeners can be registered and unregistered; or you may directly listen to events. |
| 40 | + * |
| 41 | + * @see NecrifyEvent |
| 42 | + * @see EventBus |
| 43 | + * @since 1.2.0 |
| 44 | + */ |
| 45 | +public class EventDispatcher { |
| 46 | + |
| 47 | + private final EventBus eventbus; |
| 48 | + private final ExecutorService executorService; |
| 49 | + |
| 50 | + /** |
| 51 | + * Creates a new event dispatcher that can be customized completely. |
| 52 | + * |
| 53 | + * @param eventbus The event bus to use. See the <a href="https://greenrobot.org/eventbus/documentation/configuration/"> |
| 54 | + * GreenRobot EventBus documentation</a> for more information. |
| 55 | + * @param executorService The executor service to use for asynchronous event dispatching. Note: this service must not |
| 56 | + * correspond with the {@link org.greenrobot.eventbus.EventBusBuilder#executorService(ExecutorService) event bus' executor service}. |
| 57 | + */ |
| 58 | + public EventDispatcher(@NotNull EventBus eventbus, ExecutorService executorService) { |
| 59 | + this.eventbus = eventbus; |
| 60 | + this.executorService = executorService; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Creates a new event dispatcher with the given executor service and logger. This event bus does not log messages |
| 65 | + * if an dispatched event has no subscriber and does not send an event if no subscriber is registered. |
| 66 | + * |
| 67 | + * @param executorService The executor service to use for asynchronous event dispatching and for asynchronous event |
| 68 | + * handling execution (only for {@link org.greenrobot.eventbus.ThreadMode thread modes} |
| 69 | + * {@link org.greenrobot.eventbus.ThreadMode#ASYNC} and {@link org.greenrobot.eventbus.ThreadMode#BACKGROUND}). |
| 70 | + * @param logger The logger to use for logging messages. If null, sysout will be used for logging. It may be better |
| 71 | + * to delegate messages to the platform's logger, for example with the {@link Slf4jLogger}. |
| 72 | + */ |
| 73 | + public EventDispatcher(ExecutorService executorService, Logger logger) { |
| 74 | + this.executorService = executorService; |
| 75 | + this.eventbus = EventBus |
| 76 | + .builder() |
| 77 | + .logNoSubscriberMessages(false) |
| 78 | + .sendNoSubscriberEvent(false) |
| 79 | + .logger(logger) |
| 80 | + .executorService(executorService) |
| 81 | + .build(); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Dispatches the given event synchronously. This method will block until all listeners have been executed. Listener |
| 86 | + * methods registered with {@link org.greenrobot.eventbus.ThreadMode#ASYNC} or {@link ThreadMode#BACKGROUND} will be executed |
| 87 | + * asynchronously through the given {@link org.greenrobot.eventbus.EventBusBuilder#executorService(ExecutorService) eventbus executor}; thus the method will not block until these listeners have been executed. |
| 88 | + * |
| 89 | + * @param event The event to dispatch. |
| 90 | + */ |
| 91 | + public void dispatch(NecrifyEvent event) { |
| 92 | + eventbus.post(event.setExecutingDispatcher(this)); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Dispatches the given event asynchronously. This method will not block until all listeners have been executed. |
| 97 | + * Listener methods registered with {@link org.greenrobot.eventbus.ThreadMode#ASYNC} or {@link ThreadMode#BACKGROUND} will be executed |
| 98 | + * asynchronously; thus the method will not block until these listeners have been executed. |
| 99 | + * |
| 100 | + * @param event The event to dispatch. |
| 101 | + * @return A future that will be completed when the event has been dispatched. |
| 102 | + */ |
| 103 | + public CompletableFuture<Void> dispatchAsync(NecrifyEvent event) { |
| 104 | + var future = new CompletableFuture<Void>(); |
| 105 | + executorService.execute(() -> { |
| 106 | + dispatch(event); |
| 107 | + future.complete(null); |
| 108 | + }); |
| 109 | + return future; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Registers the given listener to the event bus. The listener will be able to listen to events via subscribed methods. |
| 114 | + * These methods must be annotated with {@link Subscribe} and must have a single parameter of the event type. |
| 115 | + * |
| 116 | + * @param listener the listener instance to register. |
| 117 | + */ |
| 118 | + public void register(@NotNull Object listener) { |
| 119 | + eventbus.register(Objects.requireNonNull(listener, "listener must not be null in order to get registered")); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Unregisters the given listener from the event bus. The listener will no longer be able to listen to events. |
| 124 | + * |
| 125 | + * @param listener the listener instance to unregister. |
| 126 | + */ |
| 127 | + public void unregister(@NotNull Object listener) { |
| 128 | + eventbus.unregister(Objects.requireNonNull(listener, "listener must not be null in order to get unregistered")); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Cancels the given event. This method will prevent the event from being delivered to any new listeners, though it |
| 133 | + * may already have been delivered to some listeners. |
| 134 | + * <p>This method is implicitly called though {@link NecrifyEvent#cancel()}</p> |
| 135 | + * |
| 136 | + * @param event the event to cancel. |
| 137 | + */ |
| 138 | + public void cancelEvent(@NotNull NecrifyEvent event) { |
| 139 | + eventbus.cancelEventDelivery(Objects.requireNonNull(event, "event must not be null in order to get cancelled")); |
| 140 | + } |
| 141 | +} |
0 commit comments