Fix event registration and listener subscriptions (#251)

Co-authored-by: Riley Park <[email protected]>
This commit is contained in:
Josua ParksandRiley Park authored and GitHub committed 2023-06-15 19:08:23 -07:00
1 parent c716fcac3b
commit 9cb0795379
17 files changed
+61 -92

No files matched your search

@@ -20,8 +20,11 @@
package net.draycia.carbon.common;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Provider;
import com.google.inject.TypeLiteral;
import java.nio.file.Path;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@@ -31,8 +34,7 @@ import net.draycia.carbon.api.event.CarbonEventHandler;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.common.channels.CarbonChannelRegistry;
import net.draycia.carbon.common.command.commands.ExecutionCoordinatorHolder;
import net.draycia.carbon.common.event.CarbonEventHandlerImpl;
import net.draycia.carbon.common.listeners.RadiusListener;
import net.draycia.carbon.common.listeners.Listener;
import net.draycia.carbon.common.messages.CarbonMessages;
import net.draycia.carbon.common.messaging.MessagingManager;
import net.draycia.carbon.common.users.ProfileCache;
@@ -40,7 +42,6 @@ import net.draycia.carbon.common.users.ProfileResolver;
import net.draycia.carbon.common.users.UserManagerInternal;
import net.draycia.carbon.common.util.CloudUtils;
import net.draycia.carbon.common.util.ConcurrentUtil;
import net.draycia.carbon.common.util.ListenerUtils;
import net.draycia.carbon.common.util.PlayerUtils;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
@@ -81,7 +82,7 @@ public abstract class CarbonChatInternal<C extends CarbonPlayer> implements Carb
final ExecutionCoordinatorHolder commandExecutor,
final CarbonServer carbonServer,
final CarbonMessages carbonMessages,
final CarbonEventHandlerImpl eventHandler,
final CarbonEventHandler eventHandler,
final CarbonChannelRegistry channelRegistry,
final IMessageRenderer<Audience, String, Component, Component> renderer,
final Provider<MessagingManager> messagingManagerProvider
@@ -105,8 +106,7 @@ public abstract class CarbonChatInternal<C extends CarbonPlayer> implements Carb
protected void init() {
// Listeners
ListenerUtils.registerCommonListeners(this.injector);
this.injector.getInstance(RadiusListener.class);
final Set<Listener> listeners = this.injector.getInstance(Key.get(new TypeLiteral<Set<Listener>>() {}));
// Commands
// This is a bit awkward looking
@@ -24,6 +24,7 @@ import com.google.inject.Injector;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.google.inject.assistedinject.FactoryModuleBuilder;
import com.google.inject.multibindings.Multibinder;
import io.leangen.geantyref.TypeToken;
import java.util.Objects;
import java.util.UUID;
@@ -35,6 +36,13 @@ import net.draycia.carbon.common.command.ArgumentFactory;
import net.draycia.carbon.common.command.commands.ExecutionCoordinatorHolder;
import net.draycia.carbon.common.config.ConfigFactory;
import net.draycia.carbon.common.event.CarbonEventHandlerImpl;
import net.draycia.carbon.common.listeners.DeafenHandler;
import net.draycia.carbon.common.listeners.IgnoreHandler;
import net.draycia.carbon.common.listeners.ItemLinkHandler;
import net.draycia.carbon.common.listeners.Listener;
import net.draycia.carbon.common.listeners.MuteHandler;
import net.draycia.carbon.common.listeners.PingHandler;
import net.draycia.carbon.common.listeners.RadiusListener;
import net.draycia.carbon.common.messages.CarbonMessageSender;
import net.draycia.carbon.common.messages.CarbonMessageSource;
import net.draycia.carbon.common.messages.CarbonMessages;
@@ -84,12 +92,6 @@ public final class CarbonCommonModule extends AbstractModule {
return ConcurrentUtil.createPeriodicTasksPool(logger);
}
@Provides
@Singleton
public CarbonEventHandler eventHandler() {
return new CarbonEventHandlerImpl();
}
@Provides
@Singleton
public CarbonMessages carbonMessages(
@@ -128,6 +130,15 @@ public final class CarbonCommonModule extends AbstractModule {
this.install(new FactoryModuleBuilder().build(ArgumentFactory.class));
this.install(new FactoryModuleBuilder().build(PacketFactory.class));
this.bind(ChannelRegistry.class).to(CarbonChannelRegistry.class);
this.bind(CarbonEventHandler.class).to(CarbonEventHandlerImpl.class);
final Multibinder<Listener> listeners = Multibinder.newSetBinder(this.binder(), Listener.class);
listeners.addBinding().to(DeafenHandler.class);
listeners.addBinding().to(IgnoreHandler.class);
listeners.addBinding().to(ItemLinkHandler.class);
listeners.addBinding().to(MuteHandler.class);
listeners.addBinding().to(PingHandler.class);
listeners.addBinding().to(RadiusListener.class);
}
}
@@ -60,7 +60,6 @@ import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import net.kyori.registry.DefaultedRegistryGetter;
import ninja.egg82.messenger.services.PacketService;
import org.apache.logging.log4j.Logger;
@@ -19,6 +19,9 @@
*/
package net.draycia.carbon.common.event;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.seiama.event.EventConfig;
import com.seiama.event.EventSubscriber;
import com.seiama.event.EventSubscription;
import com.seiama.event.bus.EventBus;
@@ -37,8 +40,14 @@ import org.checkerframework.framework.qual.DefaultQualifier;
* @since 1.0.0
*/
@DefaultQualifier(NonNull.class)
@Singleton
public final class CarbonEventHandlerImpl implements CarbonEventHandler {
@Inject
private CarbonEventHandlerImpl() {
}
private final EventRegistry<CarbonEvent> eventRegistry = new SimpleEventRegistry<>(CarbonEvent.class);
private final EventBus<CarbonEvent> eventBus = new SimpleEventBus<>(this.eventRegistry, this::onException);
@@ -54,14 +63,16 @@ public final class CarbonEventHandlerImpl implements CarbonEventHandler {
return this.eventRegistry.subscribe(eventClass, subscriber);
}
// TODO: support EventConfig#exact()
@Override
public <T extends CarbonEvent> EventSubscription<T> subscribe(
final Class<T> eventClass,
final int priority,
final int order,
final boolean acceptsCancelled,
final Consumer<T> consumer
) {
return this.eventRegistry.subscribe(eventClass, new EventSubscriberImpl<>(consumer, priority, acceptsCancelled));
final EventConfig eventConfig = new CarbonEventConfig(order, acceptsCancelled, false);
return this.eventRegistry.subscribe(eventClass, eventConfig, consumer::accept);
}
@Override
@@ -69,4 +80,8 @@ public final class CarbonEventHandlerImpl implements CarbonEventHandler {
this.eventBus.post(event);
}
private record CarbonEventConfig(int order, boolean acceptsCancelled, boolean exact) implements EventConfig {
}
}
@@ -27,7 +27,7 @@ import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
@DefaultQualifier(NonNull.class)
public class DeafenHandler {
public class DeafenHandler implements Listener {
@Inject
public DeafenHandler(final CarbonEventHandler events) {
@@ -27,7 +27,7 @@ import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
@DefaultQualifier(NonNull.class)
public class IgnoreHandler {
public class IgnoreHandler implements Listener {
@Inject
public IgnoreHandler(final CarbonEventHandler events) {
@@ -25,11 +25,11 @@ import net.draycia.carbon.api.event.events.CarbonChatEvent;
import net.draycia.carbon.api.util.InventorySlot;
import net.kyori.adventure.text.TextReplacementConfig;
public class ItemLinkHandler {
public class ItemLinkHandler implements Listener {
@Inject
public ItemLinkHandler(final CarbonEventHandler events) {
events.subscribe(CarbonChatEvent.class, 1, false, event -> {
events.subscribe(CarbonChatEvent.class, 2, false, event -> {
if (!event.sender().hasPermission("carbon.itemlink")) {
return;
}
@@ -17,18 +17,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.common.event;
import com.seiama.event.EventSubscriber;
import java.util.function.Consumer;
import net.draycia.carbon.api.event.CarbonEvent;
import org.checkerframework.checker.nullness.qual.NonNull;
public record EventSubscriberImpl<T extends CarbonEvent>(Consumer<T> consumer, int postOrder, boolean acceptsCancelled) implements EventSubscriber<T> {
@Override
public void on(final @NonNull T event) {
this.consumer.accept(event);
}
package net.draycia.carbon.common.listeners;
public interface Listener {
}
@@ -33,7 +33,7 @@ import static net.draycia.carbon.api.util.KeyedRenderer.keyedRenderer;
import static net.kyori.adventure.key.Key.key;
@DefaultQualifier(NonNull.class)
public class MuteHandler {
public class MuteHandler implements Listener {
private final Key muteKey = key("carbon", "mute");
private CarbonMessages carbonMessages;
@@ -20,6 +20,7 @@
package net.draycia.carbon.common.listeners;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.util.Optional;
import java.util.regex.Pattern;
import net.draycia.carbon.api.event.CarbonEventHandler;
@@ -41,16 +42,17 @@ import static net.draycia.carbon.api.util.KeyedRenderer.keyedRenderer;
import static net.kyori.adventure.key.Key.key;
@DefaultQualifier(NonNull.class)
public class PingHandler {
@Singleton
public class PingHandler implements Listener {
private final Key muteKey = key("carbon", "pings");
private final Key pingKey = key("carbon", "pings");
private final KeyedRenderer renderer;
private final ConfigFactory configFactory;
@Inject
public PingHandler(final CarbonEventHandler events, final ConfigFactory configFactory) {
this.configFactory = configFactory;
this.renderer = keyedRenderer(this.muteKey, (sender, recipient, message, originalMessage) -> {
this.renderer = keyedRenderer(this.pingKey, (sender, recipient, message, originalMessage) -> {
if (!(recipient instanceof CarbonPlayer recipientPlayer)) {
return message;
}
@@ -28,7 +28,7 @@ import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
@DefaultQualifier(NonNull.class)
public class RadiusListener {
public class RadiusListener implements Listener {
@Inject
public RadiusListener(
@@ -1,45 +0,0 @@
/*
* CarbonChat
*
* Copyright (c) 2023 Josua Parks (Vicarious)
* Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.common.util;
import com.google.inject.Injector;
import java.util.List;
import net.draycia.carbon.common.listeners.DeafenHandler;
import net.draycia.carbon.common.listeners.IgnoreHandler;
import net.draycia.carbon.common.listeners.ItemLinkHandler;
import net.draycia.carbon.common.listeners.MuteHandler;
import net.draycia.carbon.common.listeners.PingHandler;
public final class ListenerUtils {
private ListenerUtils() {
}
public static final List<Class<?>> LISTENER_CLASSES = List.of(DeafenHandler.class, IgnoreHandler.class,
ItemLinkHandler.class, MuteHandler.class, PingHandler.class);
public static void registerCommonListeners(final Injector injector) {
for (final var listenerClass : LISTENER_CLASSES) {
injector.getInstance(listenerClass);
}
}
}