2022-01-16 04:54:08 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
2022-01-16 21:09:53 +00:00
|
|
|
import net.kyori.event.EventSubscriber;
|
2022-01-16 04:54:08 +00:00
|
|
|
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;
|
2022-01-17 04:52:27 +00:00
|
|
|
import org.lanternpowered.lmbda.LambdaFactory;
|
2022-01-16 04:54:08 +00:00
|
|
|
|
2022-01-17 04:52:27 +00:00
|
|
|
import java.lang.invoke.MethodHandles;
|
2022-01-16 04:54:08 +00:00
|
|
|
import java.lang.reflect.Method;
|
|
|
|
import java.util.Set;
|
2022-01-17 04:52:27 +00:00
|
|
|
import java.util.function.BiConsumer;
|
2022-01-16 04:54:08 +00:00
|
|
|
import java.util.function.Consumer;
|
2022-01-16 21:09:53 +00:00
|
|
|
import java.util.function.Predicate;
|
2022-01-16 04:54:08 +00:00
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
public class GeyserEventBus implements EventBus {
|
2022-01-17 04:52:27 +00:00
|
|
|
private static final MethodHandles.Lookup CALLER = MethodHandles.lookup();
|
|
|
|
|
2022-01-16 04:54:08 +00:00
|
|
|
private final SimpleEventBus<Event> bus = new SimpleEventBus<>(Event.class);
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
public <T extends Event> EventSubscription<T> subscribe(@NonNull Extension extension, @NonNull Class<T> eventClass, @NonNull Consumer<? super T> consumer) {
|
2022-01-16 21:16:52 +00:00
|
|
|
return this.subscribe(eventClass, consumer, extension, Subscribe.PostOrder.NORMAL);
|
2022-01-16 04:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public <T extends Event> void unsubscribe(@NonNull EventSubscription<T> subscription) {
|
2022-01-17 04:52:27 +00:00
|
|
|
this.bus.unregister((AbstractEventSubscription<T>) subscription);
|
2022-01-16 04:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@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);
|
2022-01-17 04:52:27 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
Class<? extends Event> type = (Class<? extends Event>) method.getParameters()[0].getType();
|
|
|
|
this.subscribe(type, eventHolder, LambdaFactory.createBiConsumer(CALLER.unreflect(method)), extension, subscribe.postOrder());
|
|
|
|
} catch (IllegalAccessException ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
2022-01-16 04:54:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-16 21:09:53 +00:00
|
|
|
@Override
|
|
|
|
public void unregisterAll(@NonNull Extension extension) {
|
2022-01-17 04:52:27 +00:00
|
|
|
this.bus.unregister((Predicate<EventSubscriber<?>>) subscriber -> extension.equals(((AbstractEventSubscription<?>) subscriber).owner()));
|
2022-01-16 21:09:53 +00:00
|
|
|
}
|
|
|
|
|
2022-01-16 04:54:08 +00:00
|
|
|
@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());
|
|
|
|
}
|
|
|
|
|
2022-01-16 21:16:52 +00:00
|
|
|
private <T extends Event> EventSubscription<T> subscribe(Class<T> eventClass, Consumer<? super T> handler, Extension extension, Subscribe.PostOrder postOrder) {
|
2022-01-17 04:52:27 +00:00
|
|
|
BaseEventSubscription<T> eventSubscription = new BaseEventSubscription<>(this, eventClass, extension, postOrder, handler);
|
|
|
|
this.bus.register(eventClass, eventSubscription);
|
|
|
|
return eventSubscription;
|
|
|
|
}
|
|
|
|
|
|
|
|
private <T extends Event> EventSubscription<T> subscribe(Class<T> eventClass, Object eventHolder, BiConsumer<Object, ? super T> handler, Extension extension, Subscribe.PostOrder postOrder) {
|
|
|
|
GeneratedEventSubscription<T> eventSubscription = new GeneratedEventSubscription<>(this, eventClass, extension, postOrder, eventHolder, handler);
|
2022-01-16 04:54:08 +00:00
|
|
|
this.bus.register(eventClass, eventSubscription);
|
|
|
|
return eventSubscription;
|
|
|
|
}
|
|
|
|
}
|