use of org.spongepowered.mod.interfaces.IMixinNetPlayHandler in project SpongeForge by SpongePowered.
the class SpongeModNetworkManager method onCustomPacketRegistration.
@SubscribeEvent(priority = EventPriority.HIGHEST)
public void onCustomPacketRegistration(CustomPacketRegistrationEvent<?> event) {
Set<String> channels = ((IMixinNetPlayHandler) event.getHandler()).getRegisteredChannels();
;
final boolean isMainThread = Sponge.isServerAvailable() && Sponge.getServer().isMainThread();
try (final CauseStackManager.StackFrame frame = isMainThread ? Sponge.getCauseStackManager().pushCauseFrame() : null) {
if (isMainThread) {
if (event.getHandler() instanceof NetHandlerPlayServer) {
Sponge.getCauseStackManager().pushCause(((NetHandlerPlayServer) event.getHandler()).player);
}
Sponge.getCauseStackManager().addContext(NET_HANDLER, event.getHandler());
}
if (event.getOperation().equals("REGISTER")) {
channels.addAll(event.getRegistrations());
for (String channel : event.getRegistrations()) {
final Cause currentCause = isMainThread ? Sponge.getCauseStackManager().getCurrentCause() : Cause.of(EventContext.empty(), Sponge.getGame());
SpongeImpl.postEvent(SpongeEventFactory.createChannelRegistrationEventRegister(currentCause, channel));
}
} else if (event.getOperation().equals("UNREGISTER")) {
channels.removeAll(event.getRegistrations());
for (String channel : event.getRegistrations()) {
final Cause currentCause = isMainThread ? Sponge.getCauseStackManager().getCurrentCause() : Cause.of(EventContext.empty(), Sponge.getGame());
SpongeImpl.postEvent(SpongeEventFactory.createChannelRegistrationEventUnregister(currentCause, channel));
}
}
}
}
Aggregations