mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-03 22:15:45 +00:00
Improve event exception handling
This commit is contained in:
2 files changed
+31
-12
No files matched your search
@@ -22,6 +22,7 @@ package net.draycia.carbon.common.event;
|
|||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import com.google.inject.Singleton;
|
import com.google.inject.Singleton;
|
||||||
import com.seiama.event.EventConfig;
|
import com.seiama.event.EventConfig;
|
||||||
|
import com.seiama.event.EventSubscriber;
|
||||||
import com.seiama.event.EventSubscription;
|
import com.seiama.event.EventSubscription;
|
||||||
import com.seiama.event.bus.EventBus;
|
import com.seiama.event.bus.EventBus;
|
||||||
import com.seiama.event.bus.SimpleEventBus;
|
import com.seiama.event.bus.SimpleEventBus;
|
||||||
@@ -32,6 +33,7 @@ import net.draycia.carbon.api.event.CarbonEvent;
|
|||||||
import net.draycia.carbon.api.event.CarbonEventHandler;
|
import net.draycia.carbon.api.event.CarbonEventHandler;
|
||||||
import net.draycia.carbon.api.event.CarbonEventSubscriber;
|
import net.draycia.carbon.api.event.CarbonEventSubscriber;
|
||||||
import net.draycia.carbon.api.event.CarbonEventSubscription;
|
import net.draycia.carbon.api.event.CarbonEventSubscription;
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||||
|
|
||||||
@@ -44,16 +46,21 @@ import org.checkerframework.framework.qual.DefaultQualifier;
|
|||||||
@Singleton
|
@Singleton
|
||||||
public final class CarbonEventHandlerImpl implements CarbonEventHandler {
|
public final class CarbonEventHandlerImpl implements CarbonEventHandler {
|
||||||
|
|
||||||
@Inject
|
private final Logger logger;
|
||||||
private CarbonEventHandlerImpl() {
|
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private CarbonEventHandlerImpl(final Logger logger) {
|
||||||
|
this.logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
private final EventRegistry<CarbonEvent> eventRegistry = new SimpleEventRegistry<>(CarbonEvent.class);
|
private final EventRegistry<CarbonEvent> eventRegistry = new SimpleEventRegistry<>(CarbonEvent.class);
|
||||||
private final EventBus<CarbonEvent> eventBus = new SimpleEventBus<>(this.eventRegistry, this::onException);
|
private final EventBus<CarbonEvent> eventBus = new SimpleEventBus<>(this.eventRegistry, this::onException);
|
||||||
|
|
||||||
private <E> void onException(final EventSubscription<E> subscription, final E event, final Throwable throwable) {
|
private <E> void onException(final EventSubscription<E> subscription, final E event, final Throwable throwable) {
|
||||||
throwable.printStackTrace();
|
final Object subscriber = subscription.subscriber() instanceof SubscriberWrapper<?> wrapped
|
||||||
|
? wrapped.carbon
|
||||||
|
: subscription.subscriber();
|
||||||
|
this.logger.warn("Exception posting event '{}' to subscriber '{}'", event, subscriber, throwable);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -64,7 +71,7 @@ public final class CarbonEventHandlerImpl implements CarbonEventHandler {
|
|||||||
return new CarbonEventSubscriptionImpl<>(
|
return new CarbonEventSubscriptionImpl<>(
|
||||||
eventClass,
|
eventClass,
|
||||||
subscriber,
|
subscriber,
|
||||||
this.eventRegistry.subscribe(eventClass, subscriber::on)
|
this.eventRegistry.subscribe(eventClass, new SubscriberWrapper<>(subscriber, true))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,13 +87,7 @@ public final class CarbonEventHandlerImpl implements CarbonEventHandler {
|
|||||||
return new CarbonEventSubscriptionImpl<>(
|
return new CarbonEventSubscriptionImpl<>(
|
||||||
eventClass,
|
eventClass,
|
||||||
subscriber,
|
subscriber,
|
||||||
this.eventRegistry.subscribe(eventClass, eventConfig, event -> {
|
this.eventRegistry.subscribe(eventClass, eventConfig, new SubscriberWrapper<>(subscriber, acceptsCancelled))
|
||||||
// Our events implement seiama Cancellable; but API consumers won't be able to do that
|
|
||||||
if (!acceptsCancelled && event instanceof Cancellable cancellable && cancellable.cancelled()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
subscriber.on(event);
|
|
||||||
})
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,4 +96,20 @@ public final class CarbonEventHandlerImpl implements CarbonEventHandler {
|
|||||||
this.eventBus.post(event);
|
this.eventBus.post(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private record SubscriberWrapper<T extends CarbonEvent>(
|
||||||
|
CarbonEventSubscriber<T> carbon,
|
||||||
|
boolean acceptsCancelled
|
||||||
|
) implements EventSubscriber<T> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void on(final T event) throws Throwable {
|
||||||
|
// Our events implement seiama Cancellable; but API consumers won't be able to do that
|
||||||
|
if (!this.acceptsCancelled && event instanceof Cancellable cancellable && cancellable.cancelled()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.carbon.on(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -32,6 +32,7 @@ import net.minecraft.network.chat.ChatTypeDecoration;
|
|||||||
import net.minecraft.resources.RegistryDataLoader;
|
import net.minecraft.resources.RegistryDataLoader;
|
||||||
import net.minecraft.server.packs.resources.ResourceManager;
|
import net.minecraft.server.packs.resources.ResourceManager;
|
||||||
import org.spongepowered.asm.mixin.Mixin;
|
import org.spongepowered.asm.mixin.Mixin;
|
||||||
|
import org.spongepowered.asm.mixin.Unique;
|
||||||
import org.spongepowered.asm.mixin.injection.At;
|
import org.spongepowered.asm.mixin.injection.At;
|
||||||
import org.spongepowered.asm.mixin.injection.Inject;
|
import org.spongepowered.asm.mixin.injection.Inject;
|
||||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
||||||
@@ -50,7 +51,7 @@ abstract class RegistryDataLoaderMixin {
|
|||||||
),
|
),
|
||||||
locals = LocalCapture.CAPTURE_FAILEXCEPTION
|
locals = LocalCapture.CAPTURE_FAILEXCEPTION
|
||||||
)
|
)
|
||||||
@SuppressWarnings("unchecked,rawtypes")
|
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||||
private static void carbon$injectMessageTypes(
|
private static void carbon$injectMessageTypes(
|
||||||
final ResourceManager resourceManager,
|
final ResourceManager resourceManager,
|
||||||
final RegistryAccess registryAccess,
|
final RegistryAccess registryAccess,
|
||||||
@@ -67,6 +68,7 @@ abstract class RegistryDataLoaderMixin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Unique
|
||||||
private static ChatType carbonChatType() {
|
private static ChatType carbonChatType() {
|
||||||
return new ChatType(ChatTypeDecoration.withSender("%s"), ChatTypeDecoration.withSender("%s"));
|
return new ChatType(ChatTypeDecoration.withSender("%s"), ChatTypeDecoration.withSender("%s"));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user