mirror of
https://github.com/GeyserMC/Geyser.git
synced 2024-08-14 23:57:35 +00:00
Implement simple event system
This commit is contained in:
parent
778f004d99
commit
b82c661688
14 changed files with 677 additions and 1 deletions
|
@ -244,6 +244,13 @@
|
|||
<version>${adventure.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<!-- Kyori Misc -->
|
||||
<dependency>
|
||||
<groupId>net.kyori</groupId>
|
||||
<artifactId>event-api</artifactId>
|
||||
<version>3.0.0</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<!-- Other -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
|
|
|
@ -49,9 +49,13 @@ import org.geysermc.floodgate.crypto.Base64Topping;
|
|||
import org.geysermc.floodgate.crypto.FloodgateCipher;
|
||||
import org.geysermc.floodgate.news.NewsItemAction;
|
||||
import org.geysermc.geyser.api.GeyserApi;
|
||||
import org.geysermc.geyser.api.event.EventBus;
|
||||
import org.geysermc.geyser.api.event.lifecycle.GeyserPostInitializeEvent;
|
||||
import org.geysermc.geyser.api.event.lifecycle.GeyserShutdownEvent;
|
||||
import org.geysermc.geyser.command.CommandManager;
|
||||
import org.geysermc.geyser.configuration.GeyserConfiguration;
|
||||
import org.geysermc.geyser.entity.EntityDefinitions;
|
||||
import org.geysermc.geyser.event.GeyserEventBus;
|
||||
import org.geysermc.geyser.extension.GeyserExtensionManager;
|
||||
import org.geysermc.geyser.level.WorldManager;
|
||||
import org.geysermc.geyser.network.ConnectorServerEventHandler;
|
||||
|
@ -123,6 +127,7 @@ public class GeyserImpl implements GeyserApi {
|
|||
private final PlatformType platformType;
|
||||
private final GeyserBootstrap bootstrap;
|
||||
|
||||
private final EventBus eventBus;
|
||||
private final GeyserExtensionManager extensionManager;
|
||||
|
||||
private Metrics metrics;
|
||||
|
@ -158,6 +163,7 @@ public class GeyserImpl implements GeyserApi {
|
|||
MinecraftLocale.init();
|
||||
|
||||
/* Load Extensions */
|
||||
this.eventBus = new GeyserEventBus();
|
||||
this.extensionManager = new GeyserExtensionManager();
|
||||
this.extensionManager.init();
|
||||
|
||||
|
@ -411,6 +417,8 @@ public class GeyserImpl implements GeyserApi {
|
|||
}
|
||||
|
||||
newsHandler.handleNews(null, NewsItemAction.ON_SERVER_STARTED);
|
||||
|
||||
this.eventBus.fire(new GeyserPostInitializeEvent());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -466,6 +474,8 @@ public class GeyserImpl implements GeyserApi {
|
|||
|
||||
ResourcePack.PACKS.clear();
|
||||
|
||||
this.eventBus.fire(new GeyserShutdownEvent());
|
||||
|
||||
this.extensionManager.disableExtensions();
|
||||
|
||||
bootstrap.getGeyserLogger().info(GeyserLocale.getLocaleStringLog("geyser.core.shutdown.done"));
|
||||
|
@ -494,6 +504,11 @@ public class GeyserImpl implements GeyserApi {
|
|||
return this.extensionManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EventBus eventBus() {
|
||||
return this.eventBus;
|
||||
}
|
||||
|
||||
public static GeyserImpl start(PlatformType platformType, GeyserBootstrap bootstrap) {
|
||||
if (instance == null) {
|
||||
return new GeyserImpl(platformType, bootstrap);
|
||||
|
|
110
core/src/main/java/org/geysermc/geyser/event/GeyserEventBus.java
Normal file
110
core/src/main/java/org/geysermc/geyser/event/GeyserEventBus.java
Normal file
|
@ -0,0 +1,110 @@
|
|||
/*
|
||||
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* @author GeyserMC
|
||||
* @link https://github.com/GeyserMC/Geyser
|
||||
*/
|
||||
|
||||
package org.geysermc.geyser.event;
|
||||
|
||||
import net.kyori.event.SimpleEventBus;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.geysermc.geyser.api.event.Event;
|
||||
import org.geysermc.geyser.api.event.EventBus;
|
||||
import org.geysermc.geyser.api.event.EventSubscription;
|
||||
import org.geysermc.geyser.api.event.Subscribe;
|
||||
import org.geysermc.geyser.api.extension.Extension;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Set;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class GeyserEventBus implements EventBus {
|
||||
private final SimpleEventBus<Event> bus = new SimpleEventBus<>(Event.class);
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public <T extends Event> EventSubscription<T> subscribe(@NonNull Class<T> eventClass, @NonNull Consumer<? super T> consumer) {
|
||||
return this.subscribe(eventClass, consumer, null, Subscribe.Priority.NORMAL);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public <T extends Event> EventSubscription<T> subscribe(@NonNull Extension extension, @NonNull Class<T> eventClass, @NonNull Consumer<? super T> consumer) {
|
||||
return this.subscribe(eventClass, consumer, extension, Subscribe.Priority.NORMAL);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends Event> void unsubscribe(@NonNull EventSubscription<T> subscription) {
|
||||
this.bus.unregister((GeyserEventSubscription<T>) subscription);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public void register(@NonNull Extension extension, @NonNull Object eventHolder) {
|
||||
for (Method method : eventHolder.getClass().getMethods()) {
|
||||
if (!method.isAnnotationPresent(Subscribe.class)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (method.getParameterCount() > 1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!Event.class.isAssignableFrom(method.getParameters()[0].getType())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Subscribe subscribe = method.getAnnotation(Subscribe.class);
|
||||
this.subscribe((Class<? extends Event>) method.getParameters()[0].getType(), (event) -> {
|
||||
try {
|
||||
method.invoke(eventHolder, event);
|
||||
} catch (IllegalAccessException | InvocationTargetException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}, extension, subscribe.priority());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean fire(@NonNull Event event) {
|
||||
return this.bus.post(event).wasSuccessful();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@NonNull
|
||||
@Override
|
||||
public <T extends Event> Set<EventSubscription<T>> subscriptions(@NonNull Class<T> eventClass) {
|
||||
return bus.subscribers().values()
|
||||
.stream()
|
||||
.filter(sub -> sub instanceof EventSubscription && ((EventSubscription<?>) sub).eventClass().isAssignableFrom(eventClass))
|
||||
.map(sub -> ((EventSubscription<T>) sub))
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
private <T extends Event> EventSubscription<T> subscribe(Class<T> eventClass, Consumer<? super T> handler, Extension extension, Subscribe.Priority priority) {
|
||||
GeyserEventSubscription<T> eventSubscription = new GeyserEventSubscription<>(this, eventClass, handler, extension, priority);
|
||||
this.bus.register(eventClass, eventSubscription);
|
||||
return eventSubscription;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,82 @@
|
|||
/*
|
||||
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* @author GeyserMC
|
||||
* @link https://github.com/GeyserMC/Geyser
|
||||
*/
|
||||
|
||||
package org.geysermc.geyser.event;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.experimental.Accessors;
|
||||
import net.kyori.event.EventSubscriber;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.geysermc.geyser.api.event.Event;
|
||||
import org.geysermc.geyser.api.event.EventBus;
|
||||
import org.geysermc.geyser.api.event.EventSubscription;
|
||||
import org.geysermc.geyser.api.event.Subscribe;
|
||||
import org.geysermc.geyser.api.extension.Extension;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
@Getter
|
||||
@Accessors(fluent = true)
|
||||
@RequiredArgsConstructor
|
||||
public class GeyserEventSubscription<T extends Event> implements EventSubscription<T>, EventSubscriber<T> {
|
||||
private final EventBus eventBus;
|
||||
private final Class<T> eventClass;
|
||||
private final Consumer<? super T> eventConsumer;
|
||||
private final Extension owner;
|
||||
private final Subscribe.Priority priority;
|
||||
@Getter(AccessLevel.NONE) private boolean active;
|
||||
|
||||
@Override
|
||||
public boolean isActive() {
|
||||
return this.active;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unsubscribe() {
|
||||
if (!this.active) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.active = false;
|
||||
this.eventBus.unsubscribe(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invoke(@NonNull T event) throws Throwable {
|
||||
try {
|
||||
this.eventConsumer.accept(event);
|
||||
} catch (Throwable ex) {
|
||||
this.owner.logger().warning("Unable to fire event " + event.getClass().getSimpleName() + " with subscription " + this.eventConsumer.getClass().getSimpleName());
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int postOrder() {
|
||||
return this.priority.postOrder();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue